json.js 1.0 KB

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