-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
37 lines (34 loc) · 823 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
'use strict';
// https://github.com/jonschlinkert/assign-deep/commit/90bf1c551d05940898168d04066bbf15060f50cc
var isValidKey = function (key) {
return (
typeof key === 'string' &&
key !== '__proto__' &&
key !== 'constructor' &&
key !== 'prototype'
);
};
var setPath = function (obj, path, value, delimiter) {
var arr;
var key;
if (!obj || typeof obj !== 'object') {
obj = {};
}
if (typeof path === 'string') {
path = path.split(delimiter || '.');
}
if (Array.isArray(path) && path.length > 0) {
arr = path;
key = arr[0];
if (isValidKey(key)) {
if (arr.length > 1) {
arr.shift();
obj[key] = setPath(obj[key], arr, value, delimiter);
} else {
obj[key] = value;
}
}
}
return obj;
};
module.exports = exports = setPath;