Skip to content

Commit

Permalink
setPath and setPathIn now treat non-enumerable properties encount…
Browse files Browse the repository at this point in the history
…ered in a path as non-existent properties
  • Loading branch information
ascartabelli committed Apr 28, 2016
1 parent 4d918ba commit 30ae88f
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 10 deletions.
11 changes: 7 additions & 4 deletions dist/lamb.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* @overview lamb - A lightweight, and docile, JavaScript library to help embracing functional programming.
* @author Andrea Scartabelli <andrea.scartabelli@gmail.com>
* @version 0.23.0
* @version 0.24.0-alpha.1
* @module lamb
* @license MIT
* @preserve
Expand All @@ -18,7 +18,7 @@
* @category Core
* @type String
*/
lamb._version = "0.23.0";
lamb._version = "0.24.0-alpha.1";

// alias used as a placeholder argument for partial application
var _ = lamb;
Expand Down Expand Up @@ -299,7 +299,7 @@
}

function _isEnumerable (obj, key) {
return key in Object(obj) && isIn(enumerables(obj), key);
return key in Object(obj) && ~enumerables(obj).indexOf(key);
}

function _setIndex (arrayLike, index, value, updater) {
Expand All @@ -322,7 +322,10 @@
target = getIndex(obj, headAsInt);
setter = partial(_setIndex, obj, headAsInt);
} else {
target = (obj || {})[parts[0]];
if (_isEnumerable(obj, parts[0])) {
target = obj[parts[0]];
}

setter = partial(setIn, obj, parts[0]);
}

Expand Down
4 changes: 2 additions & 2 deletions dist/lamb.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/lamb.min.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"coveralls": "gulp coverage && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage"
},
"tonicExample": "var _ = require('lamb');",
"version": "0.23.0",
"version": "0.24.0-alpha.1",
"devDependencies": {
"coveralls": "^2.11.8",
"gulp": "^3.9.1",
Expand Down
7 changes: 5 additions & 2 deletions src/accessors.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ function _getNaturalIndex (index, len) {
}

function _isEnumerable (obj, key) {
return key in Object(obj) && isIn(enumerables(obj), key);
return key in Object(obj) && ~enumerables(obj).indexOf(key);
}

function _setIndex (arrayLike, index, value, updater) {
Expand All @@ -27,7 +27,10 @@ function _setPathIn (obj, parts, value) {
target = getIndex(obj, headAsInt);
setter = partial(_setIndex, obj, headAsInt);
} else {
target = (obj || {})[parts[0]];
if (_isEnumerable(obj, parts[0])) {
target = obj[parts[0]];
}

setter = partial(setIn, obj, parts[0]);
}

Expand Down
15 changes: 15 additions & 0 deletions test/spec/accessorsSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,11 @@ describe("lamb.accessors", function () {

describe("setPath / setPathIn", function () {
var obj = {a: 2, b: {a: {g: 10, h: 11}, b: [4, 5], c: "foo"}, "c.d" : {"e.f": 6}};

Object.defineProperty(obj.b, "w", {
value: {x: 22, y: {z: 33}}
});

var objCopy = JSON.parse(JSON.stringify(obj));

afterEach(function () {
Expand Down Expand Up @@ -506,6 +511,16 @@ describe("lamb.accessors", function () {
expect(lamb.setPathIn(obj, "z.a.b", 99)).toEqual(r3);
});

it("should treat non-enumerable properties encountered in a path as non-existent properties", function () {
var r1 = {a: 2, b: {a: {g: 10, h: 11}, b: [4, 5], c: "foo", w: {z: 99}}, "c.d" : {"e.f": 6}};
var r2 = {a: 2, b: {a: {g: 10, h: 11}, b: [4, 5], c: "foo", w: {y: {z: 99}}}, "c.d" : {"e.f": 6}};

expect(lamb.setPathIn(obj, "b.w.z", 99)).toEqual(r1);
expect(lamb.setPath("b.w.z", 99)(obj)).toEqual(r1);
expect(lamb.setPathIn(obj, "b.w.y.z", 99)).toEqual(r2);
expect(lamb.setPath("b.w.y.z", 99)(obj)).toEqual(r2);
});

it("should replace indexes when an array is found and the key is a string containing an integer", function () {
var r = {a: 2, b: {a: {g: 10, h: 11}, b: [4, 99], c: "foo"}, "c.d" : {"e.f": 6}};

Expand Down

0 comments on commit 30ae88f

Please sign in to comment.