circular.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. var assert = require('assert');
  2. var Traverse = require('traverse');
  3. var util = require('util');
  4. exports.circular = function () {
  5. var obj = { x : 3 };
  6. obj.y = obj;
  7. var foundY = false;
  8. Traverse(obj).forEach(function (x) {
  9. if (this.path.join('') == 'y') {
  10. assert.equal(
  11. util.inspect(this.circular.node),
  12. util.inspect(obj)
  13. );
  14. foundY = true;
  15. }
  16. });
  17. assert.ok(foundY);
  18. };
  19. exports.deepCirc = function () {
  20. var obj = { x : [ 1, 2, 3 ], y : [ 4, 5 ] };
  21. obj.y[2] = obj;
  22. var times = 0;
  23. Traverse(obj).forEach(function (x) {
  24. if (this.circular) {
  25. assert.deepEqual(this.circular.path, []);
  26. assert.deepEqual(this.path, [ 'y', 2 ]);
  27. times ++;
  28. }
  29. });
  30. assert.deepEqual(times, 1);
  31. };
  32. exports.doubleCirc = function () {
  33. var obj = { x : [ 1, 2, 3 ], y : [ 4, 5 ] };
  34. obj.y[2] = obj;
  35. obj.x.push(obj.y);
  36. var circs = [];
  37. Traverse(obj).forEach(function (x) {
  38. if (this.circular) {
  39. circs.push({ circ : this.circular, self : this, node : x });
  40. }
  41. });
  42. assert.deepEqual(circs[0].self.path, [ 'x', 3, 2 ]);
  43. assert.deepEqual(circs[0].circ.path, []);
  44. assert.deepEqual(circs[1].self.path, [ 'y', 2 ]);
  45. assert.deepEqual(circs[1].circ.path, []);
  46. assert.deepEqual(circs.length, 2);
  47. };
  48. exports.circDubForEach = function () {
  49. var obj = { x : [ 1, 2, 3 ], y : [ 4, 5 ] };
  50. obj.y[2] = obj;
  51. obj.x.push(obj.y);
  52. Traverse(obj).forEach(function (x) {
  53. if (this.circular) this.update('...');
  54. });
  55. assert.deepEqual(obj, { x : [ 1, 2, 3, [ 4, 5, '...' ] ], y : [ 4, 5, '...' ] });
  56. };
  57. exports.circDubMap = function () {
  58. var obj = { x : [ 1, 2, 3 ], y : [ 4, 5 ] };
  59. obj.y[2] = obj;
  60. obj.x.push(obj.y);
  61. var c = Traverse(obj).map(function (x) {
  62. if (this.circular) {
  63. this.update('...');
  64. }
  65. });
  66. assert.deepEqual(c, { x : [ 1, 2, 3, [ 4, 5, '...' ] ], y : [ 4, 5, '...' ] });
  67. };
  68. exports.circClone = function () {
  69. var obj = { x : [ 1, 2, 3 ], y : [ 4, 5 ] };
  70. obj.y[2] = obj;
  71. obj.x.push(obj.y);
  72. var clone = Traverse.clone(obj);
  73. assert.ok(obj !== clone);
  74. assert.ok(clone.y[2] === clone);
  75. assert.ok(clone.y[2] !== obj);
  76. assert.ok(clone.x[3][2] === clone);
  77. assert.ok(clone.x[3][2] !== obj);
  78. assert.deepEqual(clone.x.slice(0,3), [1,2,3]);
  79. assert.deepEqual(clone.y.slice(0,2), [4,5]);
  80. };
  81. exports.circMapScrub = function () {
  82. var obj = { a : 1, b : 2 };
  83. obj.c = obj;
  84. var scrubbed = Traverse(obj).map(function (node) {
  85. if (this.circular) this.remove();
  86. });
  87. assert.deepEqual(
  88. Object.keys(scrubbed).sort(),
  89. [ 'a', 'b' ]
  90. );
  91. assert.ok(Traverse.deepEqual(scrubbed, { a : 1, b : 2 }));
  92. assert.equal(obj.c, obj);
  93. };