forked from jonschlinkert/set-value
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
164 lines (129 loc) · 3.98 KB
/
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*!
* set-value <https://github.com/jonschlinkert/set-value>
*
* Copyright (c) Jon Schlinkert (https://github.com/jonschlinkert).
* Released under the MIT License.
*/
'use strict';
const { deleteProperty } = Reflect;
const isPrimitive = require('is-primitive');
const isPlainObject = require('is-plain-object');
const isObject = value => {
return (typeof value === 'object' && value !== null) || typeof value === 'function';
};
const isUnsafeKey = key => {
return key === '__proto__' || key === 'constructor' || key === 'prototype';
};
const validateKey = key => {
if (!isPrimitive(key)) {
throw new TypeError('Object keys must be strings or symbols');
}
if (isUnsafeKey(key)) {
throw new Error(`Cannot set unsafe key: "${key}"`);
}
};
const toStringKey = input => {
return Array.isArray(input) ? input.flat().map(String).join(',') : input;
};
const createMemoKey = (input, options) => {
if (typeof input !== 'string' || !options) return input;
let key = input + ';';
if (options.arrays !== undefined) key += `arrays=${options.arrays};`;
if (options.separator !== undefined) key += `separator=${options.separator};`;
if (options.split !== undefined) key += `split=${options.split};`;
if (options.merge !== undefined) key += `merge=${options.merge};`;
if (options.insert !== undefined) key += `insert=${options.insert};`;
if (options.preservePaths !== undefined) key += `preservePaths=${options.preservePaths};`;
return key;
};
const memoize = (input, options, fn) => {
const key = toStringKey(options ? createMemoKey(input, options) : input);
validateKey(key);
const value = setValue.cache.get(key) || fn();
setValue.cache.set(key, value);
return value;
};
const splitString = (input, options = {}) => {
const sep = options.separator || '.';
const preserve = sep === '/' ? false : options.preservePaths;
if (typeof input === 'string' && preserve !== false && /\//.test(input)) {
return [input];
}
const parts = [];
let part = '';
const push = part => {
let number;
if (part.trim() !== '' && Number.isInteger((number = Number(part)))) {
parts.push(number);
} else {
parts.push(part);
}
};
for (let i = 0; i < input.length; i++) {
const value = input[i];
if (value === '\\') {
part += input[++i];
continue;
}
if (value === sep) {
push(part);
part = '';
continue;
}
part += value;
}
if (part) {
push(part);
}
return parts;
};
const split = (input, options) => {
if (options && typeof options.split === 'function') return options.split(input);
if (typeof input === 'symbol') return [input];
if (Array.isArray(input)) return input;
return memoize(input, options, () => splitString(input, options));
};
const assignProp = (obj, prop, value, options) => {
validateKey(prop);
// Delete property when "value" is undefined
if (value === undefined) {
deleteProperty(obj, prop);
} else if (options && options.merge && isPlainObject(obj[prop]) && isPlainObject(value)) {
const merge = options.merge === 'function' ? options.merge : Object.assign;
obj[prop] = merge(obj[prop], value);
} else if (options && options.insert && Array.isArray(obj)) {
obj.splice(prop, 0, value)
} else {
obj[prop] = value;
}
return obj;
};
const setValue = (target, path, value, options) => {
if (!path || !isObject(target)) return target;
const keys = split(path, options);
let obj = target;
for (let i = 0; i < keys.length; i++) {
const key = keys[i];
const next = keys[i + 1];
validateKey(key);
if (next === undefined) {
assignProp(obj, key, value, options);
break;
}
if (typeof next === 'number' && !Array.isArray(obj[key])) {
obj = obj[key] = [];
continue;
}
if (!isObject(obj[key])) {
obj[key] = {};
}
obj = obj[key];
}
return target;
};
setValue.split = split;
setValue.cache = new Map();
setValue.clear = () => {
setValue.cache = new Map();
};
module.exports = setValue;