diff --git a/test/mutability.js b/test/mutability.js new file mode 100644 index 0000000..95b1f7a --- /dev/null +++ b/test/mutability.js @@ -0,0 +1,68 @@ +var assert = require('assert'); +var Traverse = require('traverse'); + +exports.mutate = function () { + var obj = { a : 1, b : 2, c : [ 3, 4 ] }; + var res = Traverse(obj).forEach(function (x) { + if (typeof x === 'number' && x % 2 === 0) { + this.update(x * 10); + } + }); + assert.eql(obj, res); + assert.eql(obj, { a : 1, b : 20, c : [ 3, 40 ] }); +}; + +exports.mutateT = function () { + var obj = { a : 1, b : 2, c : [ 3, 4 ] }; + var res = Traverse.forEach(obj, function (x) { + if (typeof x === 'number' && x % 2 === 0) { + this.update(x * 10); + } + }); + assert.eql(obj, res); + assert.eql(obj, { a : 1, b : 20, c : [ 3, 40 ] }); +}; + +exports.map = function () { + var obj = { a : 1, b : 2, c : [ 3, 4 ] }; + var res = Traverse(obj).map(function (x) { + if (typeof x === 'number' && x % 2 === 0) { + this.update(x * 10); + } + }); + assert.eql(obj, { a : 1, b : 2, c : [ 3, 4 ] }); + assert.eql(res, { a : 1, b : 20, c : [ 3, 40 ] }); +}; + +exports.mapT = function () { + var obj = { a : 1, b : 2, c : [ 3, 4 ] }; + var res = Traverse.map(obj, function (x) { + if (typeof x === 'number' && x % 2 === 0) { + this.update(x * 10); + } + }); + assert.eql(obj, { a : 1, b : 2, c : [ 3, 4 ] }); + assert.eql(res, { a : 1, b : 20, c : [ 3, 40 ] }); +}; + +exports.clone = function () { + var obj = { a : 1, b : 2, c : [ 3, 4 ] }; + var res = Traverse(obj).clone(); + assert.eql(obj, res); + assert.ok(obj !== res); + obj.a ++; + assert.eql(res.a, 1); + obj.c.push(5); + assert.eql(res.c, [ 3, 4 ]); +}; + +exports.cloneT = function () { + var obj = { a : 1, b : 2, c : [ 3, 4 ] }; + var res = Traverse.clone(obj); + assert.eql(obj, res); + assert.ok(obj !== res); + obj.a ++; + assert.eql(res.a, 1); + obj.c.push(5); + assert.eql(res.c, [ 3, 4 ]); +};