mutability.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. var assert = require('assert');
  2. var Traverse = require('traverse');
  3. exports.mutate = function () {
  4. var obj = { a : 1, b : 2, c : [ 3, 4 ] };
  5. var res = Traverse(obj).forEach(function (x) {
  6. if (typeof x === 'number' && x % 2 === 0) {
  7. this.update(x * 10);
  8. }
  9. });
  10. assert.deepEqual(obj, res);
  11. assert.deepEqual(obj, { a : 1, b : 20, c : [ 3, 40 ] });
  12. };
  13. exports.mutateT = function () {
  14. var obj = { a : 1, b : 2, c : [ 3, 4 ] };
  15. var res = Traverse.forEach(obj, function (x) {
  16. if (typeof x === 'number' && x % 2 === 0) {
  17. this.update(x * 10);
  18. }
  19. });
  20. assert.deepEqual(obj, res);
  21. assert.deepEqual(obj, { a : 1, b : 20, c : [ 3, 40 ] });
  22. };
  23. exports.map = function () {
  24. var obj = { a : 1, b : 2, c : [ 3, 4 ] };
  25. var res = Traverse(obj).map(function (x) {
  26. if (typeof x === 'number' && x % 2 === 0) {
  27. this.update(x * 10);
  28. }
  29. });
  30. assert.deepEqual(obj, { a : 1, b : 2, c : [ 3, 4 ] });
  31. assert.deepEqual(res, { a : 1, b : 20, c : [ 3, 40 ] });
  32. };
  33. exports.mapT = function () {
  34. var obj = { a : 1, b : 2, c : [ 3, 4 ] };
  35. var res = Traverse.map(obj, function (x) {
  36. if (typeof x === 'number' && x % 2 === 0) {
  37. this.update(x * 10);
  38. }
  39. });
  40. assert.deepEqual(obj, { a : 1, b : 2, c : [ 3, 4 ] });
  41. assert.deepEqual(res, { a : 1, b : 20, c : [ 3, 40 ] });
  42. };
  43. exports.clone = function () {
  44. var obj = { a : 1, b : 2, c : [ 3, 4 ] };
  45. var res = Traverse(obj).clone();
  46. assert.deepEqual(obj, res);
  47. assert.ok(obj !== res);
  48. obj.a ++;
  49. assert.deepEqual(res.a, 1);
  50. obj.c.push(5);
  51. assert.deepEqual(res.c, [ 3, 4 ]);
  52. };
  53. exports.cloneT = function () {
  54. var obj = { a : 1, b : 2, c : [ 3, 4 ] };
  55. var res = Traverse.clone(obj);
  56. assert.deepEqual(obj, res);
  57. assert.ok(obj !== res);
  58. obj.a ++;
  59. assert.deepEqual(res.a, 1);
  60. obj.c.push(5);
  61. assert.deepEqual(res.c, [ 3, 4 ]);
  62. };
  63. exports.reduce = function () {
  64. var obj = { a : 1, b : 2, c : [ 3, 4 ] };
  65. var res = Traverse(obj).reduce(function (acc, x) {
  66. if (this.isLeaf) acc.push(x);
  67. return acc;
  68. }, []);
  69. assert.deepEqual(obj, { a : 1, b : 2, c : [ 3, 4 ] });
  70. assert.deepEqual(res, [ 1, 2, 3, 4 ]);
  71. };
  72. exports.reduceInit = function () {
  73. var obj = { a : 1, b : 2, c : [ 3, 4 ] };
  74. var res = Traverse(obj).reduce(function (acc, x) {
  75. if (this.isRoot) assert.fail('got root');
  76. return acc;
  77. });
  78. assert.deepEqual(obj, { a : 1, b : 2, c : [ 3, 4 ] });
  79. assert.deepEqual(res, obj);
  80. };
  81. exports.remove = function () {
  82. var obj = { a : 1, b : 2, c : [ 3, 4 ] };
  83. Traverse(obj).forEach(function (x) {
  84. if (this.isLeaf && x % 2 == 0) this.remove();
  85. });
  86. assert.deepEqual(obj, { a : 1, c : [ 3 ] });
  87. };
  88. exports.removeMap = function () {
  89. var obj = { a : 1, b : 2, c : [ 3, 4 ] };
  90. var res = Traverse(obj).map(function (x) {
  91. if (this.isLeaf && x % 2 == 0) this.remove();
  92. });
  93. assert.deepEqual(obj, { a : 1, b : 2, c : [ 3, 4 ] });
  94. assert.deepEqual(res, { a : 1, c : [ 3 ] });
  95. };
  96. exports.delete = function () {
  97. var obj = { a : 1, b : 2, c : [ 3, 4 ] };
  98. Traverse(obj).forEach(function (x) {
  99. if (this.isLeaf && x % 2 == 0) this.delete();
  100. });
  101. assert.ok(!Traverse.deepEqual(
  102. obj, { a : 1, c : [ 3, undefined ] }
  103. ));
  104. assert.ok(Traverse.deepEqual(
  105. obj, { a : 1, c : [ 3 ] }
  106. ));
  107. assert.ok(!Traverse.deepEqual(
  108. obj, { a : 1, c : [ 3, null ] }
  109. ));
  110. };
  111. exports.deleteRedux = function () {
  112. var obj = { a : 1, b : 2, c : [ 3, 4, 5 ] };
  113. Traverse(obj).forEach(function (x) {
  114. if (this.isLeaf && x % 2 == 0) this.delete();
  115. });
  116. assert.ok(!Traverse.deepEqual(
  117. obj, { a : 1, c : [ 3, undefined, 5 ] }
  118. ));
  119. assert.ok(Traverse.deepEqual(
  120. obj, { a : 1, c : [ 3 ,, 5 ] }
  121. ));
  122. assert.ok(!Traverse.deepEqual(
  123. obj, { a : 1, c : [ 3, null, 5 ] }
  124. ));
  125. assert.ok(!Traverse.deepEqual(
  126. obj, { a : 1, c : [ 3, 5 ] }
  127. ));
  128. };
  129. exports.deleteMap = function () {
  130. var obj = { a : 1, b : 2, c : [ 3, 4 ] };
  131. var res = Traverse(obj).map(function (x) {
  132. if (this.isLeaf && x % 2 == 0) this.delete();
  133. });
  134. assert.ok(Traverse.deepEqual(
  135. obj,
  136. { a : 1, b : 2, c : [ 3, 4 ] }
  137. ));
  138. var xs = [ 3, 4 ];
  139. delete xs[1];
  140. assert.ok(Traverse.deepEqual(
  141. res, { a : 1, c : xs }
  142. ));
  143. assert.ok(Traverse.deepEqual(
  144. res, { a : 1, c : [ 3, ] }
  145. ));
  146. assert.ok(Traverse.deepEqual(
  147. res, { a : 1, c : [ 3 ] }
  148. ));
  149. };
  150. exports.deleteMapRedux = function () {
  151. var obj = { a : 1, b : 2, c : [ 3, 4, 5 ] };
  152. var res = Traverse(obj).map(function (x) {
  153. if (this.isLeaf && x % 2 == 0) this.delete();
  154. });
  155. assert.ok(Traverse.deepEqual(
  156. obj,
  157. { a : 1, b : 2, c : [ 3, 4, 5 ] }
  158. ));
  159. var xs = [ 3, 4, 5 ];
  160. delete xs[1];
  161. assert.ok(Traverse.deepEqual(
  162. res, { a : 1, c : xs }
  163. ));
  164. assert.ok(!Traverse.deepEqual(
  165. res, { a : 1, c : [ 3, 5 ] }
  166. ));
  167. assert.ok(Traverse.deepEqual(
  168. res, { a : 1, c : [ 3 ,, 5 ] }
  169. ));
  170. };