json.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. var test = require('tape');
  2. var traverse = require('../');
  3. test('json test', function (t) {
  4. var id = 54;
  5. var callbacks = {};
  6. var obj = { moo : function () {}, foo : [2,3,4, function () {}] };
  7. var scrubbed = traverse(obj).map(function (x) {
  8. if (typeof x === 'function') {
  9. callbacks[id] = { id : id, f : x, path : this.path };
  10. this.update('[Function]');
  11. id++;
  12. }
  13. });
  14. t.equal(
  15. scrubbed.moo, '[Function]',
  16. 'obj.moo replaced with "[Function]"'
  17. );
  18. t.equal(
  19. scrubbed.foo[3], '[Function]',
  20. 'obj.foo[3] replaced with "[Function]"'
  21. );
  22. t.same(scrubbed, {
  23. moo : '[Function]',
  24. foo : [ 2, 3, 4, "[Function]" ]
  25. }, 'Full JSON string matches');
  26. t.same(
  27. typeof obj.moo, 'function',
  28. 'Original obj.moo still a function'
  29. );
  30. t.same(
  31. typeof obj.foo[3], 'function',
  32. 'Original obj.foo[3] still a function'
  33. );
  34. t.same(callbacks, {
  35. 54: { id: 54, f : obj.moo, path: [ 'moo' ] },
  36. 55: { id: 55, f : obj.foo[3], path: [ 'foo', '3' ] },
  37. }, 'Check the generated callbacks list');
  38. t.end();
  39. });