json.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. var assert = require('assert');
  2. var Traverse = require('traverse');
  3. exports['json test'] = function () {
  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. assert.equal(
  15. scrubbed.moo, '[Function]',
  16. 'obj.moo replaced with "[Function]"'
  17. );
  18. assert.equal(
  19. scrubbed.foo[3], '[Function]',
  20. 'obj.foo[3] replaced with "[Function]"'
  21. );
  22. assert.deepEqual(scrubbed, {
  23. moo : '[Function]',
  24. foo : [ 2, 3, 4, "[Function]" ]
  25. }, 'Full JSON string matches');
  26. assert.deepEqual(
  27. typeof obj.moo, 'function',
  28. 'Original obj.moo still a function'
  29. );
  30. assert.deepEqual(
  31. typeof obj.foo[3], 'function',
  32. 'Original obj.foo[3] still a function'
  33. );
  34. assert.deepEqual(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. };