Skip to content

Commit 02bc072

Browse files
committed
feat(set): Support for arguments as parameters instead of object.
1 parent a456614 commit 02bc072

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

__tests__/object-dot.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ describe('set', () => {
3030
odjectd.set({ object, path: 'a.b', value: 'foo' })
3131
expect(object).toEqual({ a: { exist: true, b: 'foo' } })
3232
})
33+
34+
test('with arguments instead of destructing object', () => {
35+
let object = { a: { exist: true } }
36+
odjectd.set(object, 'a.b', 'foo')
37+
expect(object.a.b).toBe('foo')
38+
})
3339
})
3440

3541
describe('get', () => {

lib/object-dot.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11

22
function set ({ object, path, value, separator = '.' }) {
3+
// support arguments as parameter instead of object.
4+
if (arguments.length > 1) [ object, path, value, separator = '.' ] = arguments
5+
36
let properties = (Array.isArray(path)) ? path : path.split(separator)
47
if (properties.length === 1) {
58
object[path] = value
@@ -13,11 +16,9 @@ function set ({ object, path, value, separator = '.' }) {
1316

1417
function get ({ object, path, value }) {
1518
let properties = path.split('.')
16-
1719
while (properties.length && object) {
1820
object = object[properties.shift()]
1921
}
20-
2122
return (value === undefined) ? object : value
2223
}
2324

0 commit comments

Comments
 (0)