This repository has been archived by the owner on Jan 26, 2022. It is now read-only.
/
DotNotation.ts
134 lines (109 loc) · 3.04 KB
/
DotNotation.ts
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
/**
* @file Dot Notation Utilities. Based Off https://github.com/sindresorhus/dot-prop
*/
import { isObj } from "./Functions";
export namespace dotprop {
function isValidPath(segments: string[]): boolean {
const disallowedKeys = ["__proto__", "prototype", "constructor"];
return !segments.some(s => disallowedKeys.includes(s));
}
function getPathSegments(path: string): string[] {
const segments = path.split("."),
parts = [];
for (let i = 0; i < segments.length; i++) {
let p = segments[i];
while (p[p.length - 1] === "\\" && segments[i + 1] !== undefined) {
p = p.slice(0, -1) + ".";
p += segments[++i];
}
parts.push(p);
}
return !isValidPath(parts) ? [] : parts;
}
/**
* Whether or not an object has a property.
* @param object The object
* @param path The path to the property.
*/
export function has(object: Dictionary, path: string): boolean {
if (!isObj(object)) return false;
const segments = getPathSegments(path);
if (segments.length === 0) return false;
for (const key of segments) {
if (isObj(object)) {
if (!(key in object)) return false;
object = object[key];
} else {
return false;
}
}
return true;
}
/**
* Get a value.
* @param obj The object.
* @param path The path to the value.
* @param defaultValue The default value.
*/
export function get<T>(
obj: Dictionary,
path: string,
defaultValue?: T
): T | undefined {
if (!isObj(obj)) {
return defaultValue === undefined ? (obj as T) : defaultValue;
}
const segments = getPathSegments(path);
if (segments.length === 0) return;
for (let i = 0; i < segments.length; i++) {
if (!Object.prototype.propertyIsEnumerable.call(obj, segments[i])) {
return defaultValue;
}
obj = obj[segments[i]];
if (obj === undefined || obj === null) {
if (i !== segments.length - 1) return defaultValue;
break;
}
}
return obj as T;
}
/**
* Set a value.
* @param object The object.
* @param path The path.
* @param value The value.
*/
export function set(
object: Dictionary,
path: string,
value: unknown
): Dictionary {
if (!isObj(object)) return object;
const root = object,
segments = getPathSegments(path);
for (let i = 0; i < segments.length; i++) {
const p = segments[i];
if (!isObj(object[p])) object[p] = {};
i === segments.length - 1 ? (object[p] = value) : (object = object[p]);
}
return root;
}
/**
* Delete a property.
* @param object The object.
* @param path The path.
*/
export function del(object: Dictionary, path: string): void {
if (!isObj(object)) return;
const segments = getPathSegments(path);
for (let i = 0; i < segments.length; i++) {
const p = segments[i];
if (i === segments.length - 1) {
delete object[p];
return;
}
object = object[p];
if (!isObj(object)) return;
}
}
}