-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
65 lines (54 loc) · 2.12 KB
/
index.ts
File metadata and controls
65 lines (54 loc) · 2.12 KB
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
interface GenericObject extends Object {
[key: string]: any;
}
const stringToArray = (needle: string, separator: string) =>
needle.split(separator).filter((value) => value.length);
const buildCore = (
target: GenericObject,
notation: string,
value: any,
separator = '.',
mergeValue = true
) => {
const parts = stringToArray(notation, separator);
const partItemLast = parts.pop();
let cloneTarget = target;
let next: string | undefined;
do {
next = parts.shift();
if (!next) continue;
if (!(cloneTarget[next] instanceof Object)) cloneTarget[next] = {};
cloneTarget = cloneTarget[next];
} while (next);
if (mergeValue && partItemLast && Object.prototype.hasOwnProperty.call(cloneTarget, partItemLast)) {
if (cloneTarget[partItemLast] instanceof Array) {
const cloneTargetItem = cloneTarget[partItemLast] as any[];
if (!cloneTargetItem.includes(value)) cloneTargetItem.push(value);
} else if (cloneTarget[partItemLast] instanceof Object && value instanceof Object) {
cloneTarget[partItemLast] = Object.keys(value).reduce(
(accumulator, current) => buildCore(accumulator, current, value[current]),
cloneTarget[partItemLast]
);
} else cloneTarget[partItemLast] = [cloneTarget[partItemLast], value];
} else if (!partItemLast) {
return null;
} else cloneTarget[partItemLast] = value;
return target;
};
export const slashNotationToObject = (slashNotation: string, value: any, targetObject: GenericObject = {}) =>
buildCore(targetObject, slashNotation, value, '/');
export const dotNotationToObject = (notation: string, value: any, targetObject: GenericObject = {}) =>
buildCore(targetObject, notation, value);
export const getValueByDotNotation = (objectTarget: GenericObject, notation: string, separator = '.') => {
const parts = stringToArray(notation, separator);
const partItemLast = parts.pop();
if (!partItemLast) return null;
let copyTarget = { ...objectTarget };
let next: string | undefined;
do {
next = parts.shift();
if (!next) continue;
copyTarget = copyTarget[next];
} while (next);
return copyTarget[partItemLast];
};