@vltpkg/dot-prop 0.0.0-0.1730239248325
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +14 -0
- package/README.md +1 -0
- package/dist/esm/index.d.ts +23 -0
- package/dist/esm/index.d.ts.map +1 -0
- package/dist/esm/index.js +235 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/package.json +3 -0
- package/package.json +59 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)
|
|
4
|
+
Copyright (c) vlt technology, Inc.
|
|
5
|
+
|
|
6
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
7
|
+
|
|
8
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
9
|
+
|
|
10
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
11
|
+
|
|
12
|
+
---
|
|
13
|
+
|
|
14
|
+
'@vltpkg/dot-prop' is a fork of 'dot-prop', extended and distributed under the terms of the MIT license above.
|
package/README.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# `@vltpkg/dot-prop`
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export type ArrayArg = unknown[];
|
|
2
|
+
export type ObjectArg = Record<PropertyKey, unknown>;
|
|
3
|
+
export type Arg = ArrayArg | ObjectArg;
|
|
4
|
+
export declare const Characters: {
|
|
5
|
+
readonly Escape: "\\";
|
|
6
|
+
readonly Dot: ".";
|
|
7
|
+
readonly Empty: "";
|
|
8
|
+
readonly LeftBracket: "[";
|
|
9
|
+
readonly RightBracket: "]";
|
|
10
|
+
};
|
|
11
|
+
export type Character = (typeof Characters)[keyof typeof Characters] | (string & {});
|
|
12
|
+
export declare const Parts: {
|
|
13
|
+
readonly Start: "start";
|
|
14
|
+
readonly Index: "index";
|
|
15
|
+
readonly IndexEnd: "indexEnd";
|
|
16
|
+
readonly Property: "property";
|
|
17
|
+
};
|
|
18
|
+
export type Part = (typeof Parts)[keyof typeof Parts];
|
|
19
|
+
export declare const get: (ogObject: Arg, path: string, defaultValue?: unknown) => unknown;
|
|
20
|
+
export declare const set: <T extends Arg>(object: T, path: string, value: unknown) => T;
|
|
21
|
+
export declare const del: (object: Arg, path: string) => boolean;
|
|
22
|
+
export declare const has: (object: Arg, path: string) => boolean;
|
|
23
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,QAAQ,GAAG,OAAO,EAAE,CAAA;AAChC,MAAM,MAAM,SAAS,GAAG,MAAM,CAAC,WAAW,EAAE,OAAO,CAAC,CAAA;AACpD,MAAM,MAAM,GAAG,GAAG,QAAQ,GAAG,SAAS,CAAA;AAYtC,eAAO,MAAM,UAAU;;;;;;CAMb,CAAA;AAEV,MAAM,MAAM,SAAS,GACjB,CAAC,OAAO,UAAU,CAAC,CAAC,MAAM,OAAO,UAAU,CAAC,GAC5C,CAAC,MAAM,GAAG,EAAE,CAAC,CAAA;AAEjB,eAAO,MAAM,KAAK;;;;;CAKR,CAAA;AAEV,MAAM,MAAM,IAAI,GAAG,CAAC,OAAO,KAAK,CAAC,CAAC,MAAM,OAAO,KAAK,CAAC,CAAA;AAkLrD,eAAO,MAAM,GAAG,aACJ,GAAG,QACP,MAAM,iBACG,OAAO,KACrB,OA6BF,CAAA;AAED,eAAO,MAAM,GAAG,GAAI,CAAC,SAAS,GAAG,UACvB,CAAC,QACH,MAAM,SACL,OAAO,KACb,CAuBF,CAAA;AAED,eAAO,MAAM,GAAG,WAAY,GAAG,QAAQ,MAAM,KAAG,OAuB/C,CAAA;AAED,eAAO,MAAM,GAAG,WAAY,GAAG,QAAQ,MAAM,KAAG,OAoB/C,CAAA"}
|
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
const DISALLOWED_KEYS = new Set([
|
|
2
|
+
'__proto__',
|
|
3
|
+
'prototype',
|
|
4
|
+
'constructor',
|
|
5
|
+
]);
|
|
6
|
+
const DIGITS = new Set('0123456789');
|
|
7
|
+
const ARRAY_PUSH = Symbol('ARRAY_PUSH');
|
|
8
|
+
export const Characters = {
|
|
9
|
+
Escape: '\\',
|
|
10
|
+
Dot: '.',
|
|
11
|
+
Empty: '',
|
|
12
|
+
LeftBracket: '[',
|
|
13
|
+
RightBracket: ']',
|
|
14
|
+
};
|
|
15
|
+
export const Parts = {
|
|
16
|
+
Start: 'start',
|
|
17
|
+
Index: 'index',
|
|
18
|
+
IndexEnd: 'indexEnd',
|
|
19
|
+
Property: 'property',
|
|
20
|
+
};
|
|
21
|
+
const checkInvalidCharacter = (part, current, msg) => {
|
|
22
|
+
if (current === part) {
|
|
23
|
+
if (msg === undefined) {
|
|
24
|
+
switch (current) {
|
|
25
|
+
case Parts.Index:
|
|
26
|
+
msg = 'character in an index';
|
|
27
|
+
break;
|
|
28
|
+
case Parts.IndexEnd:
|
|
29
|
+
msg = 'character after an index';
|
|
30
|
+
break;
|
|
31
|
+
/* c8 ignore next 3 */
|
|
32
|
+
default:
|
|
33
|
+
msg = '';
|
|
34
|
+
break;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
throw new Error(`Invalid ${msg}`.trim());
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
const getPathSegments = (path, allowEmptyIndex = false) => {
|
|
41
|
+
const segments = [];
|
|
42
|
+
let currentSegment = Characters.Empty;
|
|
43
|
+
let currentPart = Parts.Start;
|
|
44
|
+
let isIgnoring = false;
|
|
45
|
+
for (const character of path.split('')) {
|
|
46
|
+
switch (character) {
|
|
47
|
+
case Characters.Escape: {
|
|
48
|
+
checkInvalidCharacter(Parts.Index, currentPart);
|
|
49
|
+
checkInvalidCharacter(Parts.IndexEnd, currentPart);
|
|
50
|
+
if (isIgnoring)
|
|
51
|
+
currentSegment += character;
|
|
52
|
+
currentPart = Parts.Property;
|
|
53
|
+
isIgnoring = !isIgnoring;
|
|
54
|
+
break;
|
|
55
|
+
}
|
|
56
|
+
case Characters.Dot: {
|
|
57
|
+
checkInvalidCharacter(Parts.Index, currentPart);
|
|
58
|
+
if (currentPart === Parts.IndexEnd) {
|
|
59
|
+
currentPart = Parts.Property;
|
|
60
|
+
break;
|
|
61
|
+
}
|
|
62
|
+
if (isIgnoring) {
|
|
63
|
+
isIgnoring = false;
|
|
64
|
+
currentSegment += character;
|
|
65
|
+
break;
|
|
66
|
+
}
|
|
67
|
+
if (DISALLOWED_KEYS.has(currentSegment))
|
|
68
|
+
return [];
|
|
69
|
+
segments.push(currentSegment);
|
|
70
|
+
currentSegment = Characters.Empty;
|
|
71
|
+
currentPart = Parts.Property;
|
|
72
|
+
break;
|
|
73
|
+
}
|
|
74
|
+
case Characters.LeftBracket: {
|
|
75
|
+
checkInvalidCharacter(Parts.Index, currentPart);
|
|
76
|
+
if (currentPart === Parts.IndexEnd) {
|
|
77
|
+
currentPart = Parts.Index;
|
|
78
|
+
break;
|
|
79
|
+
}
|
|
80
|
+
if (isIgnoring) {
|
|
81
|
+
isIgnoring = false;
|
|
82
|
+
currentSegment += character;
|
|
83
|
+
break;
|
|
84
|
+
}
|
|
85
|
+
if (currentPart === Parts.Property) {
|
|
86
|
+
if (DISALLOWED_KEYS.has(currentSegment))
|
|
87
|
+
return [];
|
|
88
|
+
segments.push(currentSegment);
|
|
89
|
+
currentSegment = Characters.Empty;
|
|
90
|
+
}
|
|
91
|
+
currentPart = Parts.Index;
|
|
92
|
+
break;
|
|
93
|
+
}
|
|
94
|
+
case Characters.RightBracket: {
|
|
95
|
+
if (currentPart === Parts.Index) {
|
|
96
|
+
if (allowEmptyIndex)
|
|
97
|
+
checkInvalidCharacter(Characters.Empty, currentSegment, 'empty index');
|
|
98
|
+
segments.push(currentSegment === Characters.Empty ?
|
|
99
|
+
ARRAY_PUSH
|
|
100
|
+
: Number.parseInt(currentSegment, 10));
|
|
101
|
+
currentPart = Parts.IndexEnd;
|
|
102
|
+
currentSegment = Characters.Empty;
|
|
103
|
+
break;
|
|
104
|
+
}
|
|
105
|
+
// Falls through
|
|
106
|
+
}
|
|
107
|
+
default: {
|
|
108
|
+
if (!DIGITS.has(character))
|
|
109
|
+
checkInvalidCharacter(Parts.Index, currentPart);
|
|
110
|
+
checkInvalidCharacter(Parts.IndexEnd, currentPart);
|
|
111
|
+
if (currentPart === Parts.Start)
|
|
112
|
+
currentPart = Parts.Property;
|
|
113
|
+
if (isIgnoring) {
|
|
114
|
+
isIgnoring = false;
|
|
115
|
+
currentSegment += Characters.Escape;
|
|
116
|
+
}
|
|
117
|
+
currentSegment += character;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
if (isIgnoring)
|
|
122
|
+
currentSegment += Characters.Escape;
|
|
123
|
+
checkInvalidCharacter(Parts.Index, currentPart, 'index was not closed');
|
|
124
|
+
if (currentPart === Parts.Property) {
|
|
125
|
+
if (DISALLOWED_KEYS.has(currentSegment))
|
|
126
|
+
return [];
|
|
127
|
+
segments.push(currentSegment);
|
|
128
|
+
}
|
|
129
|
+
else if (currentPart === Parts.Start) {
|
|
130
|
+
segments.push(Characters.Empty);
|
|
131
|
+
}
|
|
132
|
+
return segments;
|
|
133
|
+
};
|
|
134
|
+
const isObject = (value) => value !== null && typeof value === 'object';
|
|
135
|
+
const isLast = (arr, i) => i === arr.length - 1;
|
|
136
|
+
const isStringIndex = (object, key) => {
|
|
137
|
+
if (typeof key !== 'symbol' &&
|
|
138
|
+
typeof key !== 'number' &&
|
|
139
|
+
Array.isArray(object)) {
|
|
140
|
+
const index = Number.parseInt(key, 10);
|
|
141
|
+
return (Number.isInteger(index) &&
|
|
142
|
+
object[index] === object[key]);
|
|
143
|
+
}
|
|
144
|
+
return false;
|
|
145
|
+
};
|
|
146
|
+
const assertNotStringIndex = (object, key) => {
|
|
147
|
+
if (isStringIndex(object, key)) {
|
|
148
|
+
throw new Error('Cannot use string index');
|
|
149
|
+
}
|
|
150
|
+
};
|
|
151
|
+
export const get = (ogObject, path, defaultValue) => {
|
|
152
|
+
let object = ogObject;
|
|
153
|
+
const pathArray = getPathSegments(path, true);
|
|
154
|
+
if (!pathArray.length) {
|
|
155
|
+
return defaultValue;
|
|
156
|
+
}
|
|
157
|
+
for (const [index, key] of pathArray.entries()) {
|
|
158
|
+
if (isStringIndex(object, key)) {
|
|
159
|
+
object = isLast(pathArray, index) ? undefined : null;
|
|
160
|
+
}
|
|
161
|
+
else {
|
|
162
|
+
object = object[key];
|
|
163
|
+
}
|
|
164
|
+
if ((object === undefined || object === null) &&
|
|
165
|
+
!isLast(pathArray, index)) {
|
|
166
|
+
// `object` is either `undefined` or `null` so we want to stop the loop,
|
|
167
|
+
// and if this is not the last bit of the path, and if it didn't return
|
|
168
|
+
// `undefined` it would return `null` if `object` is `null` but we want
|
|
169
|
+
// `get({foo: null}, 'foo.bar')` to equal `undefined`, or the supplied
|
|
170
|
+
// value, not `null`
|
|
171
|
+
return defaultValue;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
return object === undefined ? defaultValue : object;
|
|
175
|
+
};
|
|
176
|
+
export const set = (object, path, value) => {
|
|
177
|
+
const root = object;
|
|
178
|
+
const pathArray = getPathSegments(path);
|
|
179
|
+
for (const [index, key] of pathArray.entries()) {
|
|
180
|
+
assertNotStringIndex(object, key);
|
|
181
|
+
if (isLast(pathArray, index)) {
|
|
182
|
+
if (key === ARRAY_PUSH) {
|
|
183
|
+
;
|
|
184
|
+
object.push(value);
|
|
185
|
+
}
|
|
186
|
+
else {
|
|
187
|
+
;
|
|
188
|
+
object[key] = value;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
else if (!isObject(object[key])) {
|
|
192
|
+
const next = pathArray[index + 1];
|
|
193
|
+
object[key] =
|
|
194
|
+
typeof next === 'number' || next === ARRAY_PUSH ? [] : {};
|
|
195
|
+
}
|
|
196
|
+
object = object[key];
|
|
197
|
+
}
|
|
198
|
+
return root;
|
|
199
|
+
};
|
|
200
|
+
export const del = (object, path) => {
|
|
201
|
+
const pathArray = getPathSegments(path);
|
|
202
|
+
for (const [index, key] of pathArray.entries()) {
|
|
203
|
+
assertNotStringIndex(object, key);
|
|
204
|
+
if (isLast(pathArray, index)) {
|
|
205
|
+
if (Array.isArray(object)) {
|
|
206
|
+
object.splice(key, 1);
|
|
207
|
+
}
|
|
208
|
+
else {
|
|
209
|
+
delete object[key];
|
|
210
|
+
}
|
|
211
|
+
return true;
|
|
212
|
+
}
|
|
213
|
+
object = object[key];
|
|
214
|
+
if (!isObject(object)) {
|
|
215
|
+
return false;
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
return false;
|
|
219
|
+
};
|
|
220
|
+
export const has = (object, path) => {
|
|
221
|
+
const pathArray = getPathSegments(path);
|
|
222
|
+
if (!pathArray.length) {
|
|
223
|
+
return false;
|
|
224
|
+
}
|
|
225
|
+
for (const key of pathArray) {
|
|
226
|
+
if (!isObject(object) ||
|
|
227
|
+
!(key in object) ||
|
|
228
|
+
isStringIndex(object, key)) {
|
|
229
|
+
return false;
|
|
230
|
+
}
|
|
231
|
+
object = object[key];
|
|
232
|
+
}
|
|
233
|
+
return true;
|
|
234
|
+
};
|
|
235
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAIA,MAAM,eAAe,GAAG,IAAI,GAAG,CAAC;IAC9B,WAAW;IACX,WAAW;IACX,aAAa;CACd,CAAC,CAAA;AAEF,MAAM,MAAM,GAAG,IAAI,GAAG,CAAC,YAAY,CAAC,CAAA;AAEpC,MAAM,UAAU,GAAG,MAAM,CAAC,YAAY,CAAC,CAAA;AAEvC,MAAM,CAAC,MAAM,UAAU,GAAG;IACxB,MAAM,EAAE,IAAI;IACZ,GAAG,EAAE,GAAG;IACR,KAAK,EAAE,EAAE;IACT,WAAW,EAAE,GAAG;IAChB,YAAY,EAAE,GAAG;CACT,CAAA;AAMV,MAAM,CAAC,MAAM,KAAK,GAAG;IACnB,KAAK,EAAE,OAAO;IACd,KAAK,EAAE,OAAO;IACd,QAAQ,EAAE,UAAU;IACpB,QAAQ,EAAE,UAAU;CACZ,CAAA;AAIV,MAAM,qBAAqB,GAAG,CAC5B,IAAsB,EACtB,OAAyB,EACzB,GAAY,EACZ,EAAE;IACF,IAAI,OAAO,KAAK,IAAI,EAAE,CAAC;QACrB,IAAI,GAAG,KAAK,SAAS,EAAE,CAAC;YACtB,QAAQ,OAAO,EAAE,CAAC;gBAChB,KAAK,KAAK,CAAC,KAAK;oBACd,GAAG,GAAG,uBAAuB,CAAA;oBAC7B,MAAK;gBACP,KAAK,KAAK,CAAC,QAAQ;oBACjB,GAAG,GAAG,0BAA0B,CAAA;oBAChC,MAAK;gBACP,sBAAsB;gBACtB;oBACE,GAAG,GAAG,EAAE,CAAA;oBACR,MAAK;YACT,CAAC;QACH,CAAC;QACD,MAAM,IAAI,KAAK,CAAC,WAAW,GAAG,EAAE,CAAC,IAAI,EAAE,CAAC,CAAA;IAC1C,C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AG,CAAC,EAC1B,CAAC;YACD,OAAO,KAAK,CAAA;QACd,CAAC;QAED,MAAM,GAAG,MAAM,CAAC,GAAG,CAAQ,CAAA;IAC7B,CAAC;IAED,OAAO,IAAI,CAAA;AACb,CAAC,CAAA","sourcesContent":["export type ArrayArg = unknown[]\nexport type ObjectArg = Record<PropertyKey, unknown>\nexport type Arg = ArrayArg | ObjectArg\n\nconst DISALLOWED_KEYS = new Set([\n '__proto__',\n 'prototype',\n 'constructor',\n])\n\nconst DIGITS = new Set('0123456789')\n\nconst ARRAY_PUSH = Symbol('ARRAY_PUSH')\n\nexport const Characters = {\n Escape: '\\\\',\n Dot: '.',\n Empty: '',\n LeftBracket: '[',\n RightBracket: ']',\n} as const\n\nexport type Character =\n | (typeof Characters)[keyof typeof Characters]\n | (string & {})\n\nexport const Parts = {\n Start: 'start',\n Index: 'index',\n IndexEnd: 'indexEnd',\n Property: 'property',\n} as const\n\nexport type Part = (typeof Parts)[keyof typeof Parts]\n\nconst checkInvalidCharacter = (\n part: Part | Character,\n current: Part | Character,\n msg?: string,\n) => {\n if (current === part) {\n if (msg === undefined) {\n switch (current) {\n case Parts.Index:\n msg = 'character in an index'\n break\n case Parts.IndexEnd:\n msg = 'character after an index'\n break\n /* c8 ignore next 3 */\n default:\n msg = ''\n break\n }\n }\n throw new Error(`Invalid ${msg}`.trim())\n }\n}\n\nconst getPathSegments = (path: string, allowEmptyIndex = false) => {\n const segments = []\n\n let currentSegment: Character = Characters.Empty\n let currentPart: Part = Parts.Start\n let isIgnoring = false\n\n for (const character of path.split('') as Character[]) {\n switch (character) {\n case Characters.Escape: {\n checkInvalidCharacter(Parts.Index, currentPart)\n checkInvalidCharacter(Parts.IndexEnd, currentPart)\n\n if (isIgnoring) currentSegment += character\n\n currentPart = Parts.Property\n isIgnoring = !isIgnoring\n break\n }\n\n case Characters.Dot: {\n checkInvalidCharacter(Parts.Index, currentPart)\n\n if (currentPart === Parts.IndexEnd) {\n currentPart = Parts.Property\n break\n }\n\n if (isIgnoring) {\n isIgnoring = false\n currentSegment += character\n break\n }\n\n if (DISALLOWED_KEYS.has(currentSegment)) return []\n\n segments.push(currentSegment)\n currentSegment = Characters.Empty\n currentPart = Parts.Property\n break\n }\n\n case Characters.LeftBracket: {\n checkInvalidCharacter(Parts.Index, currentPart)\n\n if (currentPart === Parts.IndexEnd) {\n currentPart = Parts.Index\n break\n }\n\n if (isIgnoring) {\n isIgnoring = false\n currentSegment += character\n break\n }\n\n if (currentPart === Parts.Property) {\n if (DISALLOWED_KEYS.has(currentSegment)) return []\n\n segments.push(currentSegment)\n currentSegment = Characters.Empty\n }\n\n currentPart = Parts.Index\n break\n }\n\n case Characters.RightBracket: {\n if (currentPart === Parts.Index) {\n if (allowEmptyIndex)\n checkInvalidCharacter(\n Characters.Empty,\n currentSegment,\n 'empty index',\n )\n segments.push(\n currentSegment === Characters.Empty ?\n ARRAY_PUSH\n : Number.parseInt(currentSegment, 10),\n )\n currentPart = Parts.IndexEnd\n currentSegment = Characters.Empty\n break\n }\n\n // Falls through\n }\n\n default: {\n if (!DIGITS.has(character))\n checkInvalidCharacter(Parts.Index, currentPart)\n checkInvalidCharacter(Parts.IndexEnd, currentPart)\n\n if (currentPart === Parts.Start) currentPart = Parts.Property\n\n if (isIgnoring) {\n isIgnoring = false\n currentSegment += Characters.Escape\n }\n\n currentSegment += character\n }\n }\n }\n\n if (isIgnoring) currentSegment += Characters.Escape\n\n checkInvalidCharacter(\n Parts.Index,\n currentPart,\n 'index was not closed',\n )\n\n if (currentPart === Parts.Property) {\n if (DISALLOWED_KEYS.has(currentSegment)) return []\n segments.push(currentSegment)\n } else if (currentPart === Parts.Start) {\n segments.push(Characters.Empty)\n }\n\n return segments\n}\n\nconst isObject = (value: unknown): value is ObjectArg =>\n value !== null && typeof value === 'object'\n\nconst isLast = (arr: ArrayArg, i: number) => i === arr.length - 1\n\nconst isStringIndex = (\n object: unknown,\n key: PropertyKey,\n): key is string => {\n if (\n typeof key !== 'symbol' &&\n typeof key !== 'number' &&\n Array.isArray(object)\n ) {\n const index = Number.parseInt(key, 10)\n return (\n Number.isInteger(index) &&\n object[index] === object[key as unknown as number]\n )\n }\n return false\n}\n\nconst assertNotStringIndex = (object: unknown, key: PropertyKey) => {\n if (isStringIndex(object, key)) {\n throw new Error('Cannot use string index')\n }\n}\n\nexport const get = (\n ogObject: Arg,\n path: string,\n defaultValue?: unknown,\n): unknown => {\n let object: unknown = ogObject\n\n const pathArray = getPathSegments(path, true)\n if (!pathArray.length) {\n return defaultValue\n }\n\n for (const [index, key] of pathArray.entries()) {\n if (isStringIndex(object, key)) {\n object = isLast(pathArray, index) ? undefined : null\n } else {\n object = (object as ObjectArg)[key]\n }\n\n if (\n (object === undefined || object === null) &&\n !isLast(pathArray, index)\n ) {\n // `object` is either `undefined` or `null` so we want to stop the loop,\n // and if this is not the last bit of the path, and if it didn't return\n // `undefined` it would return `null` if `object` is `null` but we want\n // `get({foo: null}, 'foo.bar')` to equal `undefined`, or the supplied\n // value, not `null`\n return defaultValue\n }\n }\n\n return object === undefined ? defaultValue : object\n}\n\nexport const set = <T extends Arg>(\n object: T,\n path: string,\n value: unknown,\n): T => {\n const root = object\n const pathArray = getPathSegments(path)\n\n for (const [index, key] of pathArray.entries()) {\n assertNotStringIndex(object, key)\n\n if (isLast(pathArray, index)) {\n if (key === ARRAY_PUSH) {\n ;(object as ArrayArg).push(value)\n } else {\n ;(object as ObjectArg)[key as number] = value\n }\n } else if (!isObject((object as ObjectArg)[key])) {\n const next = pathArray[index + 1]\n ;(object as ObjectArg)[key as number] =\n typeof next === 'number' || next === ARRAY_PUSH ? [] : {}\n }\n\n object = (object as ObjectArg)[key as number] as T\n }\n\n return root\n}\n\nexport const del = (object: Arg, path: string): boolean => {\n const pathArray = getPathSegments(path)\n\n for (const [index, key] of pathArray.entries()) {\n assertNotStringIndex(object, key)\n\n if (isLast(pathArray, index)) {\n if (Array.isArray(object)) {\n object.splice(key as number, 1)\n } else {\n delete object[key as number]\n }\n return true\n }\n\n object = object[key as number] as Arg\n\n if (!isObject(object)) {\n return false\n }\n }\n\n return false\n}\n\nexport const has = (object: Arg, path: string): boolean => {\n const pathArray = getPathSegments(path)\n\n if (!pathArray.length) {\n return false\n }\n\n for (const key of pathArray) {\n if (\n !isObject(object) ||\n !(key in object) ||\n isStringIndex(object, key)\n ) {\n return false\n }\n\n object = object[key] as Arg\n }\n\n return true\n}\n"]}
|
package/package.json
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@vltpkg/dot-prop",
|
|
3
|
+
"description": "Get and manipulate an object using strings",
|
|
4
|
+
"version": "0.0.0-0.1730239248325",
|
|
5
|
+
"tshy": {
|
|
6
|
+
"selfLink": false,
|
|
7
|
+
"dialects": [
|
|
8
|
+
"esm"
|
|
9
|
+
],
|
|
10
|
+
"exports": {
|
|
11
|
+
"./package.json": "./package.json",
|
|
12
|
+
".": "./src/index.ts"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"dependencies": {},
|
|
16
|
+
"devDependencies": {
|
|
17
|
+
"@eslint/js": "^9.8.0",
|
|
18
|
+
"@types/eslint__js": "^8.42.3",
|
|
19
|
+
"@types/node": "^22.4.1",
|
|
20
|
+
"eslint": "^9.8.0",
|
|
21
|
+
"prettier": "^3.3.2",
|
|
22
|
+
"tap": "^21.0.1",
|
|
23
|
+
"tshy": "^3.0.2",
|
|
24
|
+
"typescript": "^5.5.4",
|
|
25
|
+
"typescript-eslint": "^8.0.1"
|
|
26
|
+
},
|
|
27
|
+
"license": "MIT",
|
|
28
|
+
"engines": {
|
|
29
|
+
"node": "20 || >=22"
|
|
30
|
+
},
|
|
31
|
+
"tap": {
|
|
32
|
+
"extends": "../../tap-config.yaml"
|
|
33
|
+
},
|
|
34
|
+
"prettier": "../../.prettierrc.js",
|
|
35
|
+
"module": "./dist/esm/index.js",
|
|
36
|
+
"type": "module",
|
|
37
|
+
"exports": {
|
|
38
|
+
"./package.json": "./package.json",
|
|
39
|
+
".": {
|
|
40
|
+
"import": {
|
|
41
|
+
"types": "./dist/esm/index.d.ts",
|
|
42
|
+
"default": "./dist/esm/index.js"
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
},
|
|
46
|
+
"files": [
|
|
47
|
+
"dist"
|
|
48
|
+
],
|
|
49
|
+
"scripts": {
|
|
50
|
+
"format": "prettier --write . --log-level warn --ignore-path ../../.prettierignore --cache",
|
|
51
|
+
"format:check": "prettier --check . --ignore-path ../../.prettierignore --cache",
|
|
52
|
+
"lint": "eslint . --fix",
|
|
53
|
+
"lint:check": "eslint .",
|
|
54
|
+
"presnap": "tshy",
|
|
55
|
+
"snap": "tap",
|
|
56
|
+
"pretest": "tshy",
|
|
57
|
+
"test": "tap"
|
|
58
|
+
}
|
|
59
|
+
}
|