mutability.js 6.9 KB

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