diff --git a/index.js b/index.js index b7d0d07..038a1ad 100644 --- a/index.js +++ b/index.js @@ -122,16 +122,18 @@ function walk (root, cb, immutable) { state.node = x; if (stopHere) keepGoing = false; }, - 'delete' : function () { + 'delete' : function (stopHere) { delete state.parent.node[state.key]; + if (stopHere) keepGoing = false; }, - remove : function () { + remove : function (stopHere) { if (Array_isArray(state.parent.node)) { state.parent.node.splice(state.key, 1); } else { delete state.parent.node[state.key]; } + if (stopHere) keepGoing = false; }, keys : null, before : function (f) { modifiers.before = f }, diff --git a/test/mutability.js b/test/mutability.js index d2f4ce4..d536239 100644 --- a/test/mutability.js +++ b/test/mutability.js @@ -97,6 +97,30 @@ exports.remove = function () { assert.deepEqual(obj, { a : 1, c : [ 3 ] }); }; +exports.removeNoStop = function() { + var obj = { a : 1, b : 2, c : { d: 3, e: 4 } }; + + var keysWithoutStop = []; + Traverse(obj).forEach(function (x) { + keysWithoutStop.push(this.key) + if (this.key == 'c') this.remove(); + }); + + assert.deepEqual(keysWithoutStop, [undefined, 'a', 'b', 'c', 'd', 'e']) +} + +exports.removeStop = function() { + var obj = { a : 1, b : 2, c : { d: 3, e: 4 } }; + + var keys = []; + Traverse(obj).forEach(function (x) { + keys.push(this.key) + if (this.key == 'c') this.remove(true); + }); + + assert.deepEqual(keys, [undefined, 'a', 'b', 'c']) +} + exports.removeMap = function () { var obj = { a : 1, b : 2, c : [ 3, 4 ] }; var res = Traverse(obj).map(function (x) { @@ -126,6 +150,30 @@ exports.delete = function () { )); }; +exports.deleteNoStop = function() { + var obj = { a : 1, b : 2, c : { d: 3, e: 4 } }; + + var keys = []; + Traverse(obj).forEach(function (x) { + keys.push(this.key) + if (this.key == 'c') this.delete(); + }); + + assert.deepEqual(keys, [undefined, 'a', 'b', 'c', 'd', 'e']) +} + +exports.deleteStop = function() { + var obj = { a : 1, b : 2, c : { d: 3, e: 4 } }; + + var keys = []; + Traverse(obj).forEach(function (x) { + keys.push(this.key) + if (this.key == 'c') this.delete(true); + }); + + assert.deepEqual(keys, [undefined, 'a', 'b', 'c']) +} + exports.deleteRedux = function () { var obj = { a : 1, b : 2, c : [ 3, 4, 5 ] }; Traverse(obj).forEach(function (x) {