Skip to content

Commit c738015

Browse files
committed
fix Path.set: only add when key not found in prototype chain
1 parent 15bee32 commit c738015

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

src/parse/path.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ exports.set = function (obj, path, val) {
291291
}
292292
}
293293
key = path[i]
294-
if (obj.hasOwnProperty(key)) {
294+
if (key in obj) {
295295
obj[key] = val
296296
} else {
297297
add(obj, key, val)

test/unit/specs/parse/path_spec.js

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -97,16 +97,25 @@ describe('Path Parser', function () {
9797
expect(obj.a.b.c).toBe(12345)
9898
})
9999

100-
it('set invalid', function () {
101-
var res = Path.set({}, 'ab[c]d', 123)
102-
expect(res).toBe(false)
103-
})
104-
105-
it('force set', function () {
100+
it('set non-existent', function () {
106101
var target = {}
107-
var res = Path.set(target, 'a.b.c', 123, true)
102+
var res = Path.set(target, 'a.b.c', 123)
108103
expect(res).toBe(true)
109104
expect(target.a.b.c).toBe(123)
110105
})
111106

107+
it('set on prototype chain', function () {
108+
var parent = { a: {} }
109+
var target = Object.create(parent)
110+
var res = Path.set(target, 'a.b.c', 123)
111+
expect(res).toBe(true)
112+
expect(target.hasOwnProperty('a')).toBe(false)
113+
expect(parent.a.b.c).toBe(123)
114+
})
115+
116+
it('set invalid', function () {
117+
var res = Path.set({}, 'ab[c]d', 123)
118+
expect(res).toBe(false)
119+
})
120+
112121
})

0 commit comments

Comments
 (0)