From c8df4971fb75bdf562f47027009353a546987872 Mon Sep 17 00:00:00 2001 From: Manuel Rueda Date: Sun, 21 Sep 2014 16:22:12 -0300 Subject: [PATCH 1/5] omitPath new function / grunt lint fixs --- Gruntfile.js | 8 ++--- docs/underscore.object.selectors.js.md | 25 +++++++++++++++ index.html | 21 +++++++++++- test/object.selectors.js | 15 +++++++++ underscore.object.selectors.js | 44 ++++++++++++++++++++++++++ 5 files changed, 108 insertions(+), 5 deletions(-) diff --git a/Gruntfile.js b/Gruntfile.js index 760ff7e..2f64994 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -70,10 +70,10 @@ module.exports = function(grunt) { 'index.html': ['docs/*.md', 'CHANGELOG.md'] }, options: { - scripts: [ - 'test/vendor/underscore.js', - 'dist/underscore-contrib.js' - ] + scripts: [ + 'test/vendor/underscore.js', + 'dist/underscore-contrib.js' + ] } } }, diff --git a/docs/underscore.object.selectors.js.md b/docs/underscore.object.selectors.js.md index 7343bfd..db067bd 100644 --- a/docs/underscore.object.selectors.js.md +++ b/docs/underscore.object.selectors.js.md @@ -182,3 +182,28 @@ var philosopherCities = { _.selectKeys(philosopherCities, ["Plato", "Plotinus"]); // => { Plato: "Athens", Plotinus: "Rome" } ``` + +#### omitPath + +**Signature:** `_.omitPath(obj:Object, ks:String|Array); + +Returns a copy of `obj` excluding the value represented by the `ks` path. +Path may be given as an array or as a dot-separated string. +If the path contains an array, the value of the path will be removed from all the array elements. + +```javascript +var test = { + foo: true, + bar: false, + baz: 42, + dada: { + carlos: { + pepe: 9 + }, + pedro: 'pedro' + } +}; + +_.omitPath(test, 'dada.carlos.pepe'); +// => {foo: true, bar: false, baz: 42, dada: {carlos: {}, pedro: 'pedro'}} +``` diff --git a/index.html b/index.html index 87a570f..dc9e6b2 100644 --- a/index.html +++ b/index.html @@ -10,7 +10,7 @@ -
+

Underscore-contrib (0.3.0)

The brass buckles on Underscore's utility belt - a contributors' library for Underscore.

@@ -1615,6 +1615,25 @@

selectKeys

_.selectKeys(philosopherCities, ["Plato", "Plotinus"]); // => { Plato: "Athens", Plotinus: "Rome" } +

omitPath

+

Signature: `_.omitPath(obj:Object, ks:String|Array);

+

Returns a copy of obj excluding the value represented by the ks path. +Path may be given as an array or as a dot-separated string. +If the path contains an array, the value of the path will be removed from all the array elements.

+
var test = {
+    foo: true, 
+    bar: false, 
+    baz: 42, 
+    dada: {
+        carlos: { 
+            pepe: 9 
+        }, 
+        pedro: 'pedro'
+    }
+};
+
+_.omitPath(test, 'dada.carlos.pepe');
+// => {foo: true, bar: false, baz: 42, dada: {carlos: {}, pedro: 'pedro'}}

util.existential

Functions which deal with whether a value "exists."

diff --git a/test/object.selectors.js b/test/object.selectors.js index 2c57ab0..57de640 100644 --- a/test/object.selectors.js +++ b/test/object.selectors.js @@ -76,4 +76,19 @@ $(document).ready(function() { deepEqual(_.omitWhen(a, _.isEmpty), {baz: "something", quux: ['a']}, "should return an object with kvs that return a falsey value for the given predicate"); }); + + test("omitPath", function(){ + var a = {foo: true, bar: false, baz: 42, dada: {carlos: { pepe: 9 }, pedro: 'pedro', list: [{file: '..', more: {other: { a: 1, b: 2}}, name: 'aa'}, {file: '..', name: 'bb'}]}}; + + deepEqual(_.omitPath(a, 'dada.carlos.pepe'), {foo: true, bar: false, baz: 42, dada: {carlos: {}, pedro: 'pedro', list: [{file: '..', more: {other: { a: 1, b: 2}} , name: 'aa'}, {file: '..', name: 'bb'}]}}, "should return an object without the value that represent the path"); + deepEqual(_.omitPath(a, 'dada.carlos'), {foo: true, bar: false, baz: 42, dada: {pedro: 'pedro', list: [{file: '..', more: {other: { a: 1, b: 2}} , name: 'aa'}, {file: '..', name: 'bb'}]}}, "should return an object without the value that represent the path"); + deepEqual(_.omitPath(a, ''), {foo: true, bar: false, baz: 42, dada: {carlos: { pepe: 9 }, pedro: 'pedro', list: [{file: '..', more: {other: { a: 1, b: 2}} , name: 'aa'}, {file: '..', name: 'bb'}]}}, "should return the whole object because the path is empty"); + + deepEqual(_.omitPath(a, 'dada.list.file'), {foo: true, bar: false, baz: 42, dada: {carlos: { pepe: 9 }, pedro: 'pedro', list: [{name: 'aa', more: {other: { a: 1, b: 2}}}, {name: 'bb'}]}}, "should return an object without the value in each object of the list"); + deepEqual(_.omitPath(a, 'dada.list.name'), {foo: true, bar: false, baz: 42, dada: {carlos: { pepe: 9 }, pedro: 'pedro', list: [{file: '..', more: {other: { a: 1, b: 2}}}, {file: '..'}]}}, "should return an object without the value in each object of the list"); + + deepEqual(_.omitPath(a, 'dada.list'), {foo: true, bar: false, baz: 42, dada: {carlos: { pepe: 9 }, pedro: 'pedro'}}, "should return an object without the list"); + + deepEqual(_.omitPath(a, 'dada.list.more.other.a'), {foo: true, bar: false, baz: 42, dada: {carlos: { pepe: 9 }, pedro: 'pedro', list: [{file: '..', more: {other: { b: 2}} , name: 'aa'}, {file: '..', name: 'bb'}]}}, "should return an object without the value inside the values of the list"); + }); }); diff --git a/underscore.object.selectors.js b/underscore.object.selectors.js index 17308ee..4fcd2fc 100644 --- a/underscore.object.selectors.js +++ b/underscore.object.selectors.js @@ -101,6 +101,50 @@ omitWhen: function(obj, pred) { return _.pickWhen(obj, function(e) { return !pred(e); }); + }, + + // Returns an object excluding the value represented by the path + omitPath: function(obj, ks){ + if (typeof ks == "string") ks = ks.split("."); + + // If we have reached an undefined property + // then stop executing and return undefined + if (obj === undefined) return void 0; + + // If the path array has no more elements, we've reached + // the intended property and return its value + if (ks.length === 0) return obj; + + // If we still have elements in the path array and the current + // value is null, stop executing and return undefined + if (obj === null) return void 0; + + var copy = {}; + + var deepFunc = function (obj, path){ + if (!path) + path = []; + _.each(obj, function(value, key) { + if (_.isObject(value)){ + if (_.difference(ks, _.union(path, key)).length !== 0){ + if (_.isArray(value)){ + _.getPath(copy, path)[key] = []; + }else{ + _.getPath(copy, path)[key] = {}; + } + path.push(key); + deepFunc(value, path); + path.pop(key); + } + }else{ + if (_.difference(ks, _.union(path, key)).length !== 0) + _.getPath(copy, path)[key] = value; + } + }); + }; + deepFunc(obj); + + return copy; } }); From 2ac384a9fada2b16c067931490ae50a00dc6b3a0 Mon Sep 17 00:00:00 2001 From: Julian Gonggrijp Date: Sat, 29 Aug 2020 17:31:42 +0200 Subject: [PATCH 2/5] Fix markdown (#177 review comment) --- docs/underscore.object.selectors.js.md | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/docs/underscore.object.selectors.js.md b/docs/underscore.object.selectors.js.md index dd2ff16..10ad7b2 100644 --- a/docs/underscore.object.selectors.js.md +++ b/docs/underscore.object.selectors.js.md @@ -223,7 +223,7 @@ _.selectKeys(philosopherCities, ["Plato", "Plotinus"]); #### omitPath -**Signature:** `_.omitPath(obj:Object, ks:String|Array); +**Signature:** `_.omitPath(obj:Object, ks:String|Array)` Returns a copy of `obj` excluding the value represented by the `ks` path. Path may be given as an array or as a dot-separated string. @@ -231,13 +231,13 @@ If the path contains an array, the value of the path will be removed from all th ```javascript var test = { - foo: true, - bar: false, - baz: 42, + foo: true, + bar: false, + baz: 42, dada: { - carlos: { - pepe: 9 - }, + carlos: { + pepe: 9 + }, pedro: 'pedro' } }; From 8ee77a338a069521642aa17e8f918bed32919df7 Mon Sep 17 00:00:00 2001 From: Julian Gonggrijp Date: Mon, 31 Aug 2020 14:04:40 +0200 Subject: [PATCH 3/5] Move _.omitPath to object.builders per comment by @joshuacc in #177 Also reimplements the function as suggested by @joshuacc. More tests pass in this way, but still not all. This is somewhat inconsistent; arguably, _.omitWhen, _.pickWhen and _.selectKeys belong in object.builders, too. --- docs/underscore.object.builders.js.md | 27 +++++++++++++++- docs/underscore.object.selectors.js.md | 25 --------------- test/object.builders.js | 18 +++++++++-- test/object.selectors.js | 15 --------- underscore.object.builders.js | 10 ++++++ underscore.object.selectors.js | 44 -------------------------- 6 files changed, 52 insertions(+), 87 deletions(-) diff --git a/docs/underscore.object.builders.js.md b/docs/underscore.object.builders.js.md index 74e27c2..d687310 100644 --- a/docs/underscore.object.builders.js.md +++ b/docs/underscore.object.builders.js.md @@ -26,7 +26,7 @@ _.frequencies(citations); Returns a new object resulting from merging the passed objects. Objects are processed in order, so each will override properties of the same -name occurring in earlier arguments. +name occurring in earlier arguments. Returns `null` if called without arguments. @@ -148,3 +148,28 @@ obj === imperialObj; ``` -------------------------------------------------------------------------------- + +#### omitPath + +**Signature:** `_.omitPath(obj:Object, ks:String|Array)` + +Returns a copy of `obj` excluding the value represented by the `ks` path. +Path may be given as an array or as a dot-separated string. +If the path contains an array, the value of the path will be removed from all the array elements. + +```javascript +var test = { + foo: true, + bar: false, + baz: 42, + dada: { + carlos: { + pepe: 9 + }, + pedro: 'pedro' + } +}; + +_.omitPath(test, 'dada.carlos.pepe'); +// => {foo: true, bar: false, baz: 42, dada: {carlos: {}, pedro: 'pedro'}} +``` diff --git a/docs/underscore.object.selectors.js.md b/docs/underscore.object.selectors.js.md index 10ad7b2..2f35b36 100644 --- a/docs/underscore.object.selectors.js.md +++ b/docs/underscore.object.selectors.js.md @@ -220,28 +220,3 @@ var philosopherCities = { _.selectKeys(philosopherCities, ["Plato", "Plotinus"]); // => { Plato: "Athens", Plotinus: "Rome" } ``` - -#### omitPath - -**Signature:** `_.omitPath(obj:Object, ks:String|Array)` - -Returns a copy of `obj` excluding the value represented by the `ks` path. -Path may be given as an array or as a dot-separated string. -If the path contains an array, the value of the path will be removed from all the array elements. - -```javascript -var test = { - foo: true, - bar: false, - baz: 42, - dada: { - carlos: { - pepe: 9 - }, - pedro: 'pedro' - } -}; - -_.omitPath(test, 'dada.carlos.pepe'); -// => {foo: true, bar: false, baz: 42, dada: {carlos: {}, pedro: 'pedro'}} -``` diff --git a/test/object.builders.js b/test/object.builders.js index 061cda9..31da99d 100644 --- a/test/object.builders.js +++ b/test/object.builders.js @@ -52,7 +52,7 @@ $(document).ready(function() { assert.deepEqual(_.setPath(obj, 9, ['a', 'b', 'c']), {a: {b: {c: 9, d: 108}}}, ''); assert.deepEqual(_.setPath(ary, 9, [1, 1, 0]), ['a', ['b', [9, 'd'], 'e']], ''); - assert.deepEqual(_.setPath(nest, 9, [1, 'b', 1]), [1, {a: 2, b: [3,9], c: 5}, 6], ''); + assert.deepEqual(_.setPath(nest, 9, [1, 'b', 1]), [1, {a: 2, b: [3,9], c: 5}, 6], ''); assert.deepEqual(_.setPath(obj, 9, 'a'), {a: 9}, ''); assert.deepEqual(_.setPath(ary, 9, 1), ['a', 9], ''); @@ -69,7 +69,7 @@ $(document).ready(function() { assert.deepEqual(_.updatePath(obj, _.always(9), ['a', 'b', 'c']), {a: {b: {c: 9, d: 108}}}, ''); assert.deepEqual(_.updatePath(ary, _.always(9), [1, 1, 0]), ['a', ['b', [9, 'd'], 'e']], ''); - assert.deepEqual(_.updatePath(nest, _.always(9), [1, 'b', 1]), [1, {a: 2, b: [3,9], c: 5}, 6], ''); + assert.deepEqual(_.updatePath(nest, _.always(9), [1, 'b', 1]), [1, {a: 2, b: [3,9], c: 5}, 6], ''); assert.deepEqual(_.updatePath(obj, _.always(9), 'a'), {a: 9}, ''); assert.deepEqual(_.updatePath(ary, _.always(9), 1), ['a', 9], ''); @@ -79,4 +79,18 @@ $(document).ready(function() { assert.deepEqual(nest, [1, {a: 2, b: [3,4], c: 5}, 6], 'should not modify the original nested structure'); }); + QUnit.test("omitPath", function(assert){ + var a = {foo: true, bar: false, baz: 42, dada: {carlos: { pepe: 9 }, pedro: 'pedro', list: [{file: '..', more: {other: { a: 1, b: 2}}, name: 'aa'}, {file: '..', name: 'bb'}]}}; + + assert.deepEqual(_.omitPath(a, 'dada.carlos.pepe'), {foo: true, bar: false, baz: 42, dada: {carlos: {}, pedro: 'pedro', list: [{file: '..', more: {other: { a: 1, b: 2}} , name: 'aa'}, {file: '..', name: 'bb'}]}}, "should return an object without the value that represent the path"); + assert.deepEqual(_.omitPath(a, 'dada.carlos'), {foo: true, bar: false, baz: 42, dada: {pedro: 'pedro', list: [{file: '..', more: {other: { a: 1, b: 2}} , name: 'aa'}, {file: '..', name: 'bb'}]}}, "should return an object without the value that represent the path"); + assert.deepEqual(_.omitPath(a, ''), {foo: true, bar: false, baz: 42, dada: {carlos: { pepe: 9 }, pedro: 'pedro', list: [{file: '..', more: {other: { a: 1, b: 2}} , name: 'aa'}, {file: '..', name: 'bb'}]}}, "should return the whole object because the path is empty"); + + assert.deepEqual(_.omitPath(a, 'dada.list.file'), {foo: true, bar: false, baz: 42, dada: {carlos: { pepe: 9 }, pedro: 'pedro', list: [{name: 'aa', more: {other: { a: 1, b: 2}}}, {name: 'bb'}]}}, "should return an object without the value in each object of the list"); + assert.deepEqual(_.omitPath(a, 'dada.list.name'), {foo: true, bar: false, baz: 42, dada: {carlos: { pepe: 9 }, pedro: 'pedro', list: [{file: '..', more: {other: { a: 1, b: 2}}}, {file: '..'}]}}, "should return an object without the value in each object of the list"); + + assert.deepEqual(_.omitPath(a, 'dada.list'), {foo: true, bar: false, baz: 42, dada: {carlos: { pepe: 9 }, pedro: 'pedro'}}, "should return an object without the list"); + + assert.deepEqual(_.omitPath(a, 'dada.list.more.other.a'), {foo: true, bar: false, baz: 42, dada: {carlos: { pepe: 9 }, pedro: 'pedro', list: [{file: '..', more: {other: { b: 2}} , name: 'aa'}, {file: '..', name: 'bb'}]}}, "should return an object without the value inside the values of the list"); + }); }); diff --git a/test/object.selectors.js b/test/object.selectors.js index 93644da..8cd0c1b 100644 --- a/test/object.selectors.js +++ b/test/object.selectors.js @@ -107,19 +107,4 @@ $(document).ready(function() { assert.deepEqual(_.omitWhen(a, _.isEmpty), {baz: "something", quux: ['a']}, "should return an object with kvs that return a falsey value for the given predicate"); }); - - QUnit.test("omitPath", function(assert){ - var a = {foo: true, bar: false, baz: 42, dada: {carlos: { pepe: 9 }, pedro: 'pedro', list: [{file: '..', more: {other: { a: 1, b: 2}}, name: 'aa'}, {file: '..', name: 'bb'}]}}; - - assert.deepEqual(_.omitPath(a, 'dada.carlos.pepe'), {foo: true, bar: false, baz: 42, dada: {carlos: {}, pedro: 'pedro', list: [{file: '..', more: {other: { a: 1, b: 2}} , name: 'aa'}, {file: '..', name: 'bb'}]}}, "should return an object without the value that represent the path"); - assert.deepEqual(_.omitPath(a, 'dada.carlos'), {foo: true, bar: false, baz: 42, dada: {pedro: 'pedro', list: [{file: '..', more: {other: { a: 1, b: 2}} , name: 'aa'}, {file: '..', name: 'bb'}]}}, "should return an object without the value that represent the path"); - assert.deepEqual(_.omitPath(a, ''), {foo: true, bar: false, baz: 42, dada: {carlos: { pepe: 9 }, pedro: 'pedro', list: [{file: '..', more: {other: { a: 1, b: 2}} , name: 'aa'}, {file: '..', name: 'bb'}]}}, "should return the whole object because the path is empty"); - - assert.deepEqual(_.omitPath(a, 'dada.list.file'), {foo: true, bar: false, baz: 42, dada: {carlos: { pepe: 9 }, pedro: 'pedro', list: [{name: 'aa', more: {other: { a: 1, b: 2}}}, {name: 'bb'}]}}, "should return an object without the value in each object of the list"); - assert.deepEqual(_.omitPath(a, 'dada.list.name'), {foo: true, bar: false, baz: 42, dada: {carlos: { pepe: 9 }, pedro: 'pedro', list: [{file: '..', more: {other: { a: 1, b: 2}}}, {file: '..'}]}}, "should return an object without the value in each object of the list"); - - assert.deepEqual(_.omitPath(a, 'dada.list'), {foo: true, bar: false, baz: 42, dada: {carlos: { pepe: 9 }, pedro: 'pedro'}}, "should return an object without the list"); - - assert.deepEqual(_.omitPath(a, 'dada.list.more.other.a'), {foo: true, bar: false, baz: 42, dada: {carlos: { pepe: 9 }, pedro: 'pedro', list: [{file: '..', more: {other: { b: 2}} , name: 'aa'}, {file: '..', name: 'bb'}]}}, "should return an object without the value inside the values of the list"); - }); }); diff --git a/underscore.object.builders.js b/underscore.object.builders.js index 613f9c3..cfa1a00 100644 --- a/underscore.object.builders.js +++ b/underscore.object.builders.js @@ -106,6 +106,16 @@ return ret; }, + // Returns an object excluding the value represented by the path + omitPath: function(obj, ks, copy){ + if (!obj) return copy; + if (typeof ks == "string") ks = ks.split("."); + if (!copy) copy = obj = _.snapshot(obj); + if (ks.length > 1) return _.omitPath(obj[ks[0]], _.tail(ks), copy); + delete obj[ks[0]]; + return copy; + }, + // Sets the value at any depth in a nested object based on the // path described by the keys given. setPath: function(obj, value, ks, defaultValue) { diff --git a/underscore.object.selectors.js b/underscore.object.selectors.js index f0ab2b1..cd548e6 100644 --- a/underscore.object.selectors.js +++ b/underscore.object.selectors.js @@ -121,50 +121,6 @@ omitWhen: function(obj, pred) { return _.pickWhen(obj, function(e) { return !pred(e); }); - }, - - // Returns an object excluding the value represented by the path - omitPath: function(obj, ks){ - if (typeof ks == "string") ks = ks.split("."); - - // If we have reached an undefined property - // then stop executing and return undefined - if (obj === undefined) return void 0; - - // If the path array has no more elements, we've reached - // the intended property and return its value - if (ks.length === 0) return obj; - - // If we still have elements in the path array and the current - // value is null, stop executing and return undefined - if (obj === null) return void 0; - - var copy = {}; - - var deepFunc = function (obj, path){ - if (!path) - path = []; - _.each(obj, function(value, key) { - if (_.isObject(value)){ - if (_.difference(ks, _.union(path, key)).length !== 0){ - if (_.isArray(value)){ - _.getPath(copy, path)[key] = []; - }else{ - _.getPath(copy, path)[key] = {}; - } - path.push(key); - deepFunc(value, path); - path.pop(key); - } - }else{ - if (_.difference(ks, _.union(path, key)).length !== 0) - _.getPath(copy, path)[key] = value; - } - }); - }; - deepFunc(obj); - - return copy; } }); From ccd828306f65122314abce9f8054152d136b15da Mon Sep 17 00:00:00 2001 From: Julian Gonggrijp Date: Mon, 31 Aug 2020 14:18:13 +0200 Subject: [PATCH 4/5] Make _.omitPath tests easier to read by using indentation (#177) --- test/object.builders.js | 120 ++++++++++++++++++++++++++++++++++++---- 1 file changed, 108 insertions(+), 12 deletions(-) diff --git a/test/object.builders.js b/test/object.builders.js index 31da99d..398f9e7 100644 --- a/test/object.builders.js +++ b/test/object.builders.js @@ -80,17 +80,113 @@ $(document).ready(function() { }); QUnit.test("omitPath", function(assert){ - var a = {foo: true, bar: false, baz: 42, dada: {carlos: { pepe: 9 }, pedro: 'pedro', list: [{file: '..', more: {other: { a: 1, b: 2}}, name: 'aa'}, {file: '..', name: 'bb'}]}}; - - assert.deepEqual(_.omitPath(a, 'dada.carlos.pepe'), {foo: true, bar: false, baz: 42, dada: {carlos: {}, pedro: 'pedro', list: [{file: '..', more: {other: { a: 1, b: 2}} , name: 'aa'}, {file: '..', name: 'bb'}]}}, "should return an object without the value that represent the path"); - assert.deepEqual(_.omitPath(a, 'dada.carlos'), {foo: true, bar: false, baz: 42, dada: {pedro: 'pedro', list: [{file: '..', more: {other: { a: 1, b: 2}} , name: 'aa'}, {file: '..', name: 'bb'}]}}, "should return an object without the value that represent the path"); - assert.deepEqual(_.omitPath(a, ''), {foo: true, bar: false, baz: 42, dada: {carlos: { pepe: 9 }, pedro: 'pedro', list: [{file: '..', more: {other: { a: 1, b: 2}} , name: 'aa'}, {file: '..', name: 'bb'}]}}, "should return the whole object because the path is empty"); - - assert.deepEqual(_.omitPath(a, 'dada.list.file'), {foo: true, bar: false, baz: 42, dada: {carlos: { pepe: 9 }, pedro: 'pedro', list: [{name: 'aa', more: {other: { a: 1, b: 2}}}, {name: 'bb'}]}}, "should return an object without the value in each object of the list"); - assert.deepEqual(_.omitPath(a, 'dada.list.name'), {foo: true, bar: false, baz: 42, dada: {carlos: { pepe: 9 }, pedro: 'pedro', list: [{file: '..', more: {other: { a: 1, b: 2}}}, {file: '..'}]}}, "should return an object without the value in each object of the list"); - - assert.deepEqual(_.omitPath(a, 'dada.list'), {foo: true, bar: false, baz: 42, dada: {carlos: { pepe: 9 }, pedro: 'pedro'}}, "should return an object without the list"); - - assert.deepEqual(_.omitPath(a, 'dada.list.more.other.a'), {foo: true, bar: false, baz: 42, dada: {carlos: { pepe: 9 }, pedro: 'pedro', list: [{file: '..', more: {other: { b: 2}} , name: 'aa'}, {file: '..', name: 'bb'}]}}, "should return an object without the value inside the values of the list"); + var a = { + foo: true, + bar: false, + baz: 42, + dada: { + carlos: { pepe: 9 }, + pedro: 'pedro', + list: [ + {file: '..', more: {other: { a: 1, b: 2}}, name: 'aa'}, + {file: '..', name: 'bb'} + ] + } + }; + + assert.deepEqual(_.omitPath(a, 'dada.carlos.pepe'), { + foo: true, + bar: false, + baz: 42, + dada: { + carlos: {}, + pedro: 'pedro', + list: [ + {file: '..', more: {other: { a: 1, b: 2}} , name: 'aa'}, + {file: '..', name: 'bb'} + ] + } + }, "should return an object without the value that represent the path"); + + assert.deepEqual(_.omitPath(a, 'dada.carlos'), { + foo: true, + bar: false, + baz: 42, + dada: { + pedro: 'pedro', + list: [ + {file: '..', more: {other: { a: 1, b: 2}} , name: 'aa'}, + {file: '..', name: 'bb'} + ] + } + }, "should return an object without the value that represent the path"); + + assert.deepEqual(_.omitPath(a, ''), { + foo: true, + bar: false, + baz: 42, + dada: { + carlos: { pepe: 9 }, + pedro: 'pedro', + list: [ + {file: '..', more: {other: { a: 1, b: 2}} , name: 'aa'}, + {file: '..', name: 'bb'} + ] + } + }, "should return the whole object because the path is empty"); + + assert.deepEqual(_.omitPath(a, 'dada.list.file'), { + foo: true, + bar: false, + baz: 42, + dada: { + carlos: { pepe: 9 }, + pedro: 'pedro', + list: [ + {name: 'aa', more: {other: { a: 1, b: 2}}}, + {name: 'bb'} + ] + } + }, "should return an object without the value in each object of the list"); + + assert.deepEqual(_.omitPath(a, 'dada.list.name'), { + foo: true, + bar: false, + baz: 42, + dada: { + carlos: { pepe: 9 }, + pedro: 'pedro', + list: [ + {file: '..', more: {other: { a: 1, b: 2}}}, + {file: '..'} + ] + } + }, "should return an object without the value in each object of the list"); + + assert.deepEqual(_.omitPath(a, 'dada.list'), { + foo: true, + bar: false, + baz: 42, + dada: { + carlos: { pepe: 9 }, + pedro: 'pedro' + } + }, "should return an object without the list"); + + assert.deepEqual(_.omitPath(a, 'dada.list.more.other.a'), { + foo: true, + bar: false, + baz: 42, + dada: { + carlos: { pepe: 9 }, + pedro: 'pedro', + list: [ + {file: '..', more: {other: { b: 2}} , name: 'aa'}, + {file: '..', name: 'bb'} + ] + } + }, + "should return an object without the value inside the values of the list" + ); }); }); From f00f6e48277babd1644262d08559d9f87bfbbf9e Mon Sep 17 00:00:00 2001 From: Julian Gonggrijp Date: Mon, 31 Aug 2020 14:24:09 +0200 Subject: [PATCH 5/5] Make _.omitPath consistent with path semantics in core Underscore (#177) Now all tests pass. --- docs/underscore.object.builders.js.md | 1 - test/object.builders.js | 12 ++++++------ 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/docs/underscore.object.builders.js.md b/docs/underscore.object.builders.js.md index d687310..2314be6 100644 --- a/docs/underscore.object.builders.js.md +++ b/docs/underscore.object.builders.js.md @@ -155,7 +155,6 @@ obj === imperialObj; Returns a copy of `obj` excluding the value represented by the `ks` path. Path may be given as an array or as a dot-separated string. -If the path contains an array, the value of the path will be removed from all the array elements. ```javascript var test = { diff --git a/test/object.builders.js b/test/object.builders.js index 398f9e7..0436d83 100644 --- a/test/object.builders.js +++ b/test/object.builders.js @@ -135,7 +135,7 @@ $(document).ready(function() { } }, "should return the whole object because the path is empty"); - assert.deepEqual(_.omitPath(a, 'dada.list.file'), { + assert.deepEqual(_.omitPath(a, 'dada.list.0.file'), { foo: true, bar: false, baz: 42, @@ -144,12 +144,12 @@ $(document).ready(function() { pedro: 'pedro', list: [ {name: 'aa', more: {other: { a: 1, b: 2}}}, - {name: 'bb'} + {file: '..', name: 'bb'} ] } - }, "should return an object without the value in each object of the list"); + }, "should return an object without the value in an element of the list"); - assert.deepEqual(_.omitPath(a, 'dada.list.name'), { + assert.deepEqual(_.omitPath(a, 'dada.list.1.name'), { foo: true, bar: false, baz: 42, @@ -157,7 +157,7 @@ $(document).ready(function() { carlos: { pepe: 9 }, pedro: 'pedro', list: [ - {file: '..', more: {other: { a: 1, b: 2}}}, + {name: 'aa', file: '..', more: {other: { a: 1, b: 2}}}, {file: '..'} ] } @@ -173,7 +173,7 @@ $(document).ready(function() { } }, "should return an object without the list"); - assert.deepEqual(_.omitPath(a, 'dada.list.more.other.a'), { + assert.deepEqual(_.omitPath(a, 'dada.list.0.more.other.a'), { foo: true, bar: false, baz: 42,