source: node_modules/traverse/test/mutability.js

main
Last change on this file was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago

Initial commit

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