mutability.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. var test = require('tape');
  2. var traverse = require('../');
  3. var deepEqual = require('./lib/deep_equal');
  4. test('mutate', function (t) {
  5. var obj = { a : 1, b : 2, c : [ 3, 4 ] };
  6. var res = traverse(obj).forEach(function (x) {
  7. if (typeof x === 'number' && x % 2 === 0) {
  8. this.update(x * 10);
  9. }
  10. });
  11. t.same(obj, res);
  12. t.same(obj, { a : 1, b : 20, c : [ 3, 40 ] });
  13. t.end();
  14. });
  15. test('mutateT', function (t) {
  16. var obj = { a : 1, b : 2, c : [ 3, 4 ] };
  17. var res = traverse.forEach(obj, function (x) {
  18. if (typeof x === 'number' && x % 2 === 0) {
  19. this.update(x * 10);
  20. }
  21. });
  22. t.same(obj, res);
  23. t.same(obj, { a : 1, b : 20, c : [ 3, 40 ] });
  24. t.end();
  25. });
  26. test('map', function (t) {
  27. var obj = { a : 1, b : 2, c : [ 3, 4 ] };
  28. var res = traverse(obj).map(function (x) {
  29. if (typeof x === 'number' && x % 2 === 0) {
  30. this.update(x * 10);
  31. }
  32. });
  33. t.same(obj, { a : 1, b : 2, c : [ 3, 4 ] });
  34. t.same(res, { a : 1, b : 20, c : [ 3, 40 ] });
  35. t.end();
  36. });
  37. test('mapT', function (t) {
  38. var obj = { a : 1, b : 2, c : [ 3, 4 ] };
  39. var res = traverse.map(obj, function (x) {
  40. if (typeof x === 'number' && x % 2 === 0) {
  41. this.update(x * 10);
  42. }
  43. });
  44. t.same(obj, { a : 1, b : 2, c : [ 3, 4 ] });
  45. t.same(res, { a : 1, b : 20, c : [ 3, 40 ] });
  46. t.end();
  47. });
  48. test('clone', function (t) {
  49. var obj = { a : 1, b : 2, c : [ 3, 4 ] };
  50. var res = traverse(obj).clone();
  51. t.same(obj, res);
  52. t.ok(obj !== res);
  53. obj.a ++;
  54. t.same(res.a, 1);
  55. obj.c.push(5);
  56. t.same(res.c, [ 3, 4 ]);
  57. t.end();
  58. });
  59. test('cloneT', function (t) {
  60. var obj = { a : 1, b : 2, c : [ 3, 4 ] };
  61. var res = traverse.clone(obj);
  62. t.same(obj, res);
  63. t.ok(obj !== res);
  64. obj.a ++;
  65. t.same(res.a, 1);
  66. obj.c.push(5);
  67. t.same(res.c, [ 3, 4 ]);
  68. t.end();
  69. });
  70. test('reduce', function (t) {
  71. var obj = { a : 1, b : 2, c : [ 3, 4 ] };
  72. var res = traverse(obj).reduce(function (acc, x) {
  73. if (this.isLeaf) acc.push(x);
  74. return acc;
  75. }, []);
  76. t.same(obj, { a : 1, b : 2, c : [ 3, 4 ] });
  77. t.same(res, [ 1, 2, 3, 4 ]);
  78. t.end();
  79. });
  80. test('reduceInit', function (t) {
  81. var obj = { a : 1, b : 2, c : [ 3, 4 ] };
  82. var res = traverse(obj).reduce(function (acc, x) {
  83. if (this.isRoot) assert.fail('got root');
  84. return acc;
  85. });
  86. t.same(obj, { a : 1, b : 2, c : [ 3, 4 ] });
  87. t.same(res, obj);
  88. t.end();
  89. });
  90. test('remove', function (t) {
  91. var obj = { a : 1, b : 2, c : [ 3, 4 ] };
  92. traverse(obj).forEach(function (x) {
  93. if (this.isLeaf && x % 2 == 0) this.remove();
  94. });
  95. t.same(obj, { a : 1, c : [ 3 ] });
  96. t.end();
  97. });
  98. exports.removeNoStop = function() {
  99. var obj = { a : 1, b : 2, c : { d: 3, e: 4 }, f: 5 };
  100. var keys = [];
  101. traverse(obj).forEach(function (x) {
  102. keys.push(this.key)
  103. if (this.key == 'c') this.remove();
  104. });
  105. t.same(keys, [undefined, 'a', 'b', 'c', 'd', 'e', 'f'])
  106. t.end();
  107. }
  108. exports.removeStop = function() {
  109. var obj = { a : 1, b : 2, c : { d: 3, e: 4 }, f: 5 };
  110. var keys = [];
  111. traverse(obj).forEach(function (x) {
  112. keys.push(this.key)
  113. if (this.key == 'c') this.remove(true);
  114. });
  115. t.same(keys, [undefined, 'a', 'b', 'c', 'f'])
  116. t.end();
  117. }
  118. test('removeMap', function (t) {
  119. var obj = { a : 1, b : 2, c : [ 3, 4 ] };
  120. var res = traverse(obj).map(function (x) {
  121. if (this.isLeaf && x % 2 == 0) this.remove();
  122. });
  123. t.same(obj, { a : 1, b : 2, c : [ 3, 4 ] });
  124. t.same(res, { a : 1, c : [ 3 ] });
  125. t.end();
  126. });
  127. test('delete', function (t) {
  128. var obj = { a : 1, b : 2, c : [ 3, 4 ] };
  129. traverse(obj).forEach(function (x) {
  130. if (this.isLeaf && x % 2 == 0) this.delete();
  131. });
  132. t.ok(!deepEqual(
  133. obj, { a : 1, c : [ 3, undefined ] }
  134. ));
  135. t.ok(deepEqual(
  136. obj, { a : 1, c : [ 3 ] }
  137. ));
  138. t.ok(!deepEqual(
  139. obj, { a : 1, c : [ 3, null ] }
  140. ));
  141. t.end();
  142. });
  143. test('deleteNoStop', function (t) {
  144. var obj = { a : 1, b : 2, c : { d: 3, e: 4 } };
  145. var keys = [];
  146. traverse(obj).forEach(function (x) {
  147. keys.push(this.key)
  148. if (this.key == 'c') this.delete();
  149. });
  150. t.same(keys, [undefined, 'a', 'b', 'c', 'd', 'e'])
  151. t.end();
  152. });
  153. test('deleteStop', function (t) {
  154. var obj = { a : 1, b : 2, c : { d: 3, e: 4 } };
  155. var keys = [];
  156. traverse(obj).forEach(function (x) {
  157. keys.push(this.key)
  158. if (this.key == 'c') this.delete(true);
  159. });
  160. t.same(keys, [undefined, 'a', 'b', 'c'])
  161. t.end();
  162. });
  163. test('deleteRedux', function (t) {
  164. var obj = { a : 1, b : 2, c : [ 3, 4, 5 ] };
  165. traverse(obj).forEach(function (x) {
  166. if (this.isLeaf && x % 2 == 0) this.delete();
  167. });
  168. t.ok(!deepEqual(
  169. obj, { a : 1, c : [ 3, undefined, 5 ] }
  170. ));
  171. t.ok(deepEqual(
  172. obj, { a : 1, c : [ 3 ,, 5 ] }
  173. ));
  174. t.ok(!deepEqual(
  175. obj, { a : 1, c : [ 3, null, 5 ] }
  176. ));
  177. t.ok(!deepEqual(
  178. obj, { a : 1, c : [ 3, 5 ] }
  179. ));
  180. t.end();
  181. });
  182. test('deleteMap', function (t) {
  183. var obj = { a : 1, b : 2, c : [ 3, 4 ] };
  184. var res = traverse(obj).map(function (x) {
  185. if (this.isLeaf && x % 2 == 0) this.delete();
  186. });
  187. t.ok(deepEqual(
  188. obj,
  189. { a : 1, b : 2, c : [ 3, 4 ] }
  190. ));
  191. var xs = [ 3, 4 ];
  192. delete xs[1];
  193. t.ok(deepEqual(
  194. res, { a : 1, c : xs }
  195. ));
  196. t.ok(deepEqual(
  197. res, { a : 1, c : [ 3, ] }
  198. ));
  199. t.ok(deepEqual(
  200. res, { a : 1, c : [ 3 ] }
  201. ));
  202. t.end();
  203. });
  204. test('deleteMapRedux', function (t) {
  205. var obj = { a : 1, b : 2, c : [ 3, 4, 5 ] };
  206. var res = traverse(obj).map(function (x) {
  207. if (this.isLeaf && x % 2 == 0) this.delete();
  208. });
  209. t.ok(deepEqual(
  210. obj,
  211. { a : 1, b : 2, c : [ 3, 4, 5 ] }
  212. ));
  213. var xs = [ 3, 4, 5 ];
  214. delete xs[1];
  215. t.ok(deepEqual(
  216. res, { a : 1, c : xs }
  217. ));
  218. t.ok(!deepEqual(
  219. res, { a : 1, c : [ 3, 5 ] }
  220. ));
  221. t.ok(deepEqual(
  222. res, { a : 1, c : [ 3 ,, 5 ] }
  223. ));
  224. t.end();
  225. });
  226. test('objectToString', function (t) {
  227. var obj = { a : 1, b : 2, c : [ 3, 4 ] };
  228. var res = traverse(obj).forEach(function (x) {
  229. if (typeof x === 'object' && !this.isRoot) {
  230. this.update(JSON.stringify(x));
  231. }
  232. });
  233. t.same(obj, res);
  234. t.same(obj, { a : 1, b : 2, c : "[3,4]" });
  235. t.end();
  236. });
  237. test('stringToObject', function (t) {
  238. var obj = { a : 1, b : 2, c : "[3,4]" };
  239. var res = traverse(obj).forEach(function (x) {
  240. if (typeof x === 'string') {
  241. this.update(JSON.parse(x));
  242. }
  243. else if (typeof x === 'number' && x % 2 === 0) {
  244. this.update(x * 10);
  245. }
  246. });
  247. t.deepEqual(obj, res);
  248. t.deepEqual(obj, { a : 1, b : 20, c : [ 3, 40 ] });
  249. t.end();
  250. });