@valkyriestudios/utils 12.28.0 → 12.30.0

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/README.md CHANGED
@@ -775,6 +775,8 @@ const myObj2 = {
775
775
  ],
776
776
  };
777
777
  deepGet(myObj2, 'b[0].price'); // 2
778
+
779
+ deepGet(myObj2, 'b.price'); // [2, 4]
778
780
  ```
779
781
 
780
782
  ### equal(a:any, b:any)
package/deep/get.d.ts CHANGED
@@ -18,9 +18,9 @@ type DeepGetResult<T extends ObjectType | ArrayType, P extends string> = P exten
18
18
  * Output:
19
19
  * 2
20
20
  *
21
- * @param val - Object/Array to get the value from
22
- * @param path - Path string to deeply get the value at
23
- * @param get_parent - If passed as true retrieves the parent of where the value lives
21
+ * @param {Record<string, unknown>|unknown[]} obj - Object/Array to get the value from
22
+ * @param {string} path - Path string to deeply get the value at
23
+ * @param {boolean} get_parent - If passed as true retrieves the parent of where the value lives
24
24
  * @throws {TypeError}
25
25
  */
26
26
  declare function deepGet<T extends ObjectType | ArrayType, P extends string>(obj: T, path: P, get_parent?: boolean): DeepGetResult<T, P> | undefined;
package/deep/get.js CHANGED
@@ -5,60 +5,87 @@ exports.default = deepGet;
5
5
  function deepGet(obj, path, get_parent = false) {
6
6
  if (Object.prototype.toString.call(obj) !== '[object Object]' &&
7
7
  !Array.isArray(obj))
8
- throw new TypeError('Deepget is only supported for objects');
9
- if (typeof path !== 'string')
10
- throw new TypeError('No path was given');
11
- const path_s = path.trim();
12
- const path_len = path_s.length;
13
- if (!path_len)
14
- throw new TypeError('No path was given');
15
- const parts = [];
16
- let cursor_part = '';
17
- let in_bracket = false;
18
- for (let i = 0; i < path_len; i++) {
19
- const char = path_s[i];
20
- if (char === '[' || char === ']') {
21
- in_bracket = !in_bracket;
22
- if (cursor_part) {
23
- parts.push(cursor_part);
24
- cursor_part = '';
25
- }
26
- }
27
- else if (char === '.' && !in_bracket) {
28
- if (cursor_part) {
29
- parts.push(cursor_part);
30
- cursor_part = '';
31
- }
32
- }
33
- else {
34
- cursor_part += char;
8
+ throw new TypeError('deepGet: Requires object or array');
9
+ if (typeof path !== 'string' ||
10
+ !path.length)
11
+ throw new TypeError('deepGet: Invalid path provided');
12
+ const nodes = [];
13
+ let node = obj;
14
+ let key = '';
15
+ for (let i = 0; i < path.length; i++) {
16
+ const char = path[i];
17
+ switch (char) {
18
+ case '[':
19
+ case ']':
20
+ case '.':
21
+ if (!key)
22
+ break;
23
+ if (Array.isArray(node)) {
24
+ if (!isNaN(Number(key))) {
25
+ const ix = parseInt(key, 10);
26
+ if (ix < 0 || ix > node.length - 1)
27
+ return undefined;
28
+ node = node[ix];
29
+ nodes.push(node);
30
+ }
31
+ else {
32
+ const extracted = [];
33
+ for (let y = 0; y < node.length; y++) {
34
+ const el = deepGet(node[y], key);
35
+ if (el !== undefined)
36
+ extracted.push(el);
37
+ }
38
+ node = extracted.length ? extracted.flat() : undefined;
39
+ nodes.push(node);
40
+ }
41
+ }
42
+ else if (typeof node === 'object' && node !== null) {
43
+ node = node[key];
44
+ nodes.push(node);
45
+ if (node === undefined)
46
+ return undefined;
47
+ }
48
+ else {
49
+ return undefined;
50
+ }
51
+ key = '';
52
+ break;
53
+ default:
54
+ key += char;
55
+ break;
35
56
  }
36
57
  }
37
- if (cursor_part)
38
- parts.push(cursor_part);
39
- let len = parts.length;
40
- if (!len || (len === 1 && get_parent))
41
- return obj;
42
- if (get_parent) {
43
- parts.pop();
44
- len -= 1;
45
- }
46
- let cursor = obj;
47
- for (let i = 0; i < len; i++) {
48
- if (Array.isArray(cursor)) {
49
- const ix = parseInt(parts[i], 10);
50
- if (ix < 0 || ix > cursor.length - 1)
51
- return undefined;
52
- cursor = cursor[ix];
58
+ if (key) {
59
+ if (Array.isArray(node)) {
60
+ if (!isNaN(Number(key))) {
61
+ const ix = parseInt(key, 10);
62
+ if (ix < 0 || ix > node.length - 1)
63
+ return undefined;
64
+ node = node[ix];
65
+ nodes.push(node);
66
+ }
67
+ else {
68
+ const extracted = [];
69
+ for (let i = 0; i < node.length; i++) {
70
+ const el = node[i];
71
+ if (el?.[key] !== undefined)
72
+ extracted.push(el?.[key]);
73
+ }
74
+ node = extracted.length ? extracted : undefined;
75
+ nodes.push(node);
76
+ }
53
77
  }
54
- else if (Object.prototype.toString.call(cursor) === '[object Object]') {
55
- cursor = cursor[parts[i]];
56
- if (cursor === undefined)
78
+ else if (typeof node === 'object' && node !== null) {
79
+ node = node[key];
80
+ nodes.push(node);
81
+ if (node === undefined)
57
82
  return undefined;
58
83
  }
59
84
  else {
60
85
  return undefined;
61
86
  }
62
87
  }
63
- return cursor;
88
+ if (get_parent)
89
+ nodes.pop();
90
+ return nodes.length ? nodes.pop() : obj;
64
91
  }
package/package.json CHANGED
@@ -1 +1,406 @@
1
- { "name": "@valkyriestudios/utils", "version": "12.28.0", "description": "A collection of single-function utilities for common tasks", "author": { "name": "Peter Vermeulen", "url": "https://www.linkedin.com/in/petervermeulen1/" }, "keywords": [ "utility", "library", "javascript", "js", "node", "bun" ], "license": "MIT", "repository": { "type": "git", "url": "git+https://github.com/ValkyrieStudios/utils.git" }, "bugs": { "url": "https://github.com/ValkyrieStudios/utils/issues" }, "homepage": "https://github.com/ValkyrieStudios/utils#readme", "types": "index.d.ts" }
1
+ {
2
+ "name": "@valkyriestudios/utils",
3
+ "version": "12.30.0",
4
+ "description": "A collection of single-function utilities for common tasks",
5
+ "author": {
6
+ "name": "Peter Vermeulen",
7
+ "url": "https://www.linkedin.com/in/petervermeulen1/"
8
+ },
9
+ "keywords": [
10
+ "utility",
11
+ "library",
12
+ "javascript",
13
+ "js",
14
+ "node",
15
+ "bun"
16
+ ],
17
+ "license": "MIT",
18
+ "scripts": {
19
+ "build": "npm run lint && npm run test && tsc -p ./tsconfig.build.json && tsc -p ./tsconfig.types.json && tsc -p ./tsconfig.types_all.json",
20
+ "test": "node --icu-data-dir=node_modules/full-icu --require esbuild-register --test ./test/lib/**/*.ts ./test/lib/*.ts",
21
+ "test:only": "node --icu-data-dir=node_modules/full-icu --require esbuild-register --test --test-only ./test/lib/**/*.ts ./test/lib/*.ts",
22
+ "test:coverage": "nyc npm run test",
23
+ "lint": "npm run lint:src && npm run lint:test",
24
+ "lint:src": "./node_modules/.bin/eslint ./lib",
25
+ "lint:test": "./node_modules/.bin/eslint ./test",
26
+ "codecov": "codecov",
27
+ "benchmark": "node --require esbuild-register ./test/benchmark.ts"
28
+ },
29
+ "repository": {
30
+ "type": "git",
31
+ "url": "git+https://github.com/ValkyrieStudios/utils.git"
32
+ },
33
+ "bugs": {
34
+ "url": "https://github.com/ValkyrieStudios/utils/issues"
35
+ },
36
+ "homepage": "https://github.com/ValkyrieStudios/utils#readme",
37
+ "types": "index.d.ts",
38
+ "sideEffects": false,
39
+ "exports": {
40
+ ".": {
41
+ "import": "./index.js",
42
+ "types": "./index.d.ts"
43
+ },
44
+ "./package.json": "./package.json",
45
+ "./equal": {
46
+ "import": "./equal.js",
47
+ "types": "./equal.d.ts"
48
+ },
49
+ "./is": {
50
+ "import": "./is.js",
51
+ "types": "./is.d.ts"
52
+ },
53
+ "./array": {
54
+ "import": "./array/index.js",
55
+ "types": "./array/index.d.ts"
56
+ },
57
+ "./array/dedupe": {
58
+ "import": "./array/dedupe.js",
59
+ "types": "./array/dedupe.d.ts"
60
+ },
61
+ "./array/groupBy": {
62
+ "import": "./array/groupBy.js",
63
+ "types": "./array/groupBy.d.ts"
64
+ },
65
+ "./array/is": {
66
+ "import": "./array/is.js",
67
+ "types": "./array/is.d.ts"
68
+ },
69
+ "./array/isNotEmpty": {
70
+ "import": "./array/isNotEmpty.js",
71
+ "types": "./array/isNotEmpty.d.ts"
72
+ },
73
+ "./array/join": {
74
+ "import": "./array/join.js",
75
+ "types": "./array/join.d.ts"
76
+ },
77
+ "./array/mapFn": {
78
+ "import": "./array/mapFn.js",
79
+ "types": "./array/mapFn.d.ts"
80
+ },
81
+ "./array/mapKey": {
82
+ "import": "./array/mapKey.js",
83
+ "types": "./array/mapKey.d.ts"
84
+ },
85
+ "./array/mapPrimitive": {
86
+ "import": "./array/mapPrimitive.js",
87
+ "types": "./array/mapPrimitive.d.ts"
88
+ },
89
+ "./array/shuffle": {
90
+ "import": "./array/shuffle.js",
91
+ "types": "./array/shuffle.d.ts"
92
+ },
93
+ "./array/sort": {
94
+ "import": "./array/sort.js",
95
+ "types": "./array/sort.d.ts"
96
+ },
97
+ "./array/split": {
98
+ "import": "./array/split.js",
99
+ "types": "./array/split.d.ts"
100
+ },
101
+ "./boolean": {
102
+ "import": "./boolean/index.js",
103
+ "types": "./boolean/index.d.ts"
104
+ },
105
+ "./boolean/is": {
106
+ "import": "./boolean/is.js",
107
+ "types": "./boolean/is.d.ts"
108
+ },
109
+ "./caching": {
110
+ "import": "./caching/index.js",
111
+ "types": "./caching/index.d.ts"
112
+ },
113
+ "./caching/LRU": {
114
+ "import": "./caching/LRU.js",
115
+ "types": "./caching/LRU.d.ts"
116
+ },
117
+ "./caching/memoize": {
118
+ "import": "./caching/memoize.js",
119
+ "types": "./caching/memoize.d.ts"
120
+ },
121
+ "./date": {
122
+ "import": "./date/index.js",
123
+ "types": "./date/index.d.ts"
124
+ },
125
+ "./date/addUTC": {
126
+ "import": "./date/addUTC.js",
127
+ "types": "./date/addUTC.d.ts"
128
+ },
129
+ "./date/convertToDate": {
130
+ "import": "./date/convertToDate.js",
131
+ "types": "./date/convertToDate.d.ts"
132
+ },
133
+ "./date/diff": {
134
+ "import": "./date/diff.js",
135
+ "types": "./date/diff.d.ts"
136
+ },
137
+ "./date/endOfUTC": {
138
+ "import": "./date/endOfUTC.js",
139
+ "types": "./date/endOfUTC.d.ts"
140
+ },
141
+ "./date/format": {
142
+ "import": "./date/format.js",
143
+ "types": "./date/format.d.ts"
144
+ },
145
+ "./date/is": {
146
+ "import": "./date/is.js",
147
+ "types": "./date/is.d.ts"
148
+ },
149
+ "./date/isFormat": {
150
+ "import": "./date/isFormat.js",
151
+ "types": "./date/isFormat.d.ts"
152
+ },
153
+ "./date/isLeap": {
154
+ "import": "./date/isLeap.js",
155
+ "types": "./date/isLeap.d.ts"
156
+ },
157
+ "./date/nowUnix": {
158
+ "import": "./date/nowUnix.js",
159
+ "types": "./date/nowUnix.d.ts"
160
+ },
161
+ "./date/nowUnixMs": {
162
+ "import": "./date/nowUnixMs.js",
163
+ "types": "./date/nowUnixMs.d.ts"
164
+ },
165
+ "./date/setTimeUTC": {
166
+ "import": "./date/setTimeUTC.js",
167
+ "types": "./date/setTimeUTC.d.ts"
168
+ },
169
+ "./date/startOfUTC": {
170
+ "import": "./date/startOfUTC.js",
171
+ "types": "./date/startOfUTC.d.ts"
172
+ },
173
+ "./date/toUnix": {
174
+ "import": "./date/toUnix.js",
175
+ "types": "./date/toUnix.d.ts"
176
+ },
177
+ "./date/toUTC": {
178
+ "import": "./date/toUTC.js",
179
+ "types": "./date/toUTC.d.ts"
180
+ },
181
+ "./deep": {
182
+ "import": "./deep/index.js",
183
+ "types": "./deep/index.d.ts"
184
+ },
185
+ "./deep/freeze": {
186
+ "import": "./deep/freeze.js",
187
+ "types": "./deep/freeze.d.ts"
188
+ },
189
+ "./deep/get": {
190
+ "import": "./deep/get.js",
191
+ "types": "./deep/get.d.ts"
192
+ },
193
+ "./deep/seal": {
194
+ "import": "./deep/seal.js",
195
+ "types": "./deep/seal.d.ts"
196
+ },
197
+ "./deep/set": {
198
+ "import": "./deep/set.js",
199
+ "types": "./deep/set.d.ts"
200
+ },
201
+ "./formdata": {
202
+ "import": "./formdata/index.js",
203
+ "types": "./formdata/index.d.ts"
204
+ },
205
+ "./formdata/is": {
206
+ "import": "./formdata/is.js",
207
+ "types": "./formdata/is.d.ts"
208
+ },
209
+ "./formdata/toObject": {
210
+ "import": "./formdata/toObject.js",
211
+ "types": "./formdata/toObject.d.ts"
212
+ },
213
+ "./function": {
214
+ "import": "./function/index.js",
215
+ "types": "./function/index.d.ts"
216
+ },
217
+ "./function/debounce": {
218
+ "import": "./function/debounce.js",
219
+ "types": "./function/debounce.d.ts"
220
+ },
221
+ "./function/is": {
222
+ "import": "./function/is.js",
223
+ "types": "./function/is.d.ts"
224
+ },
225
+ "./function/isAsync": {
226
+ "import": "./function/isAsync.js",
227
+ "types": "./function/isAsync.d.ts"
228
+ },
229
+ "./function/noop": {
230
+ "import": "./function/noop.js",
231
+ "types": "./function/noop.d.ts"
232
+ },
233
+ "./function/noopresolve": {
234
+ "import": "./function/noopresolve.js",
235
+ "types": "./function/noopresolve.d.ts"
236
+ },
237
+ "./function/noopreturn": {
238
+ "import": "./function/noopreturn.js",
239
+ "types": "./function/noopreturn.d.ts"
240
+ },
241
+ "./function/sleep": {
242
+ "import": "./function/sleep.js",
243
+ "types": "./function/sleep.d.ts"
244
+ },
245
+ "./hash": {
246
+ "import": "./hash/index.js",
247
+ "types": "./hash/index.d.ts"
248
+ },
249
+ "./hash/fnv1A": {
250
+ "import": "./hash/fnv1A.js",
251
+ "types": "./hash/fnv1A.d.ts"
252
+ },
253
+ "./hash/guid": {
254
+ "import": "./hash/guid.js",
255
+ "types": "./hash/guid.d.ts"
256
+ },
257
+ "./number": {
258
+ "import": "./number/index.js",
259
+ "types": "./number/index.d.ts"
260
+ },
261
+ "./number/is": {
262
+ "import": "./number/is.js",
263
+ "types": "./number/is.d.ts"
264
+ },
265
+ "./number/isAbove": {
266
+ "import": "./number/isAbove.js",
267
+ "types": "./number/isAbove.d.ts"
268
+ },
269
+ "./number/isAboveOrEqual": {
270
+ "import": "./number/isAboveOrEqual.js",
271
+ "types": "./number/isAboveOrEqual.d.ts"
272
+ },
273
+ "./number/isBelow": {
274
+ "import": "./number/isBelow.js",
275
+ "types": "./number/isBelow.d.ts"
276
+ },
277
+ "./number/isBelowOrEqual": {
278
+ "import": "./number/isBelowOrEqual.js",
279
+ "types": "./number/isBelowOrEqual.d.ts"
280
+ },
281
+ "./number/isBetween": {
282
+ "import": "./number/isBetween.js",
283
+ "types": "./number/isBetween.d.ts"
284
+ },
285
+ "./number/isInteger": {
286
+ "import": "./number/isInteger.js",
287
+ "types": "./number/isInteger.d.ts"
288
+ },
289
+ "./number/isIntegerAbove": {
290
+ "import": "./number/isIntegerAbove.js",
291
+ "types": "./number/isIntegerAbove.d.ts"
292
+ },
293
+ "./number/isIntegerAboveOrEqual": {
294
+ "import": "./number/isIntegerAboveOrEqual.js",
295
+ "types": "./number/isIntegerAboveOrEqual.d.ts"
296
+ },
297
+ "./number/isIntegerBelow": {
298
+ "import": "./number/isIntegerBelow.js",
299
+ "types": "./number/isIntegerBelow.d.ts"
300
+ },
301
+ "./number/isIntegerBelowOrEqual": {
302
+ "import": "./number/isIntegerBelowOrEqual.js",
303
+ "types": "./number/isIntegerBelowOrEqual.d.ts"
304
+ },
305
+ "./number/isIntegerBetween": {
306
+ "import": "./number/isIntegerBetween.js",
307
+ "types": "./number/isIntegerBetween.d.ts"
308
+ },
309
+ "./number/isNumericalNaN": {
310
+ "import": "./number/isNumericalNaN.js",
311
+ "types": "./number/isNumericalNaN.d.ts"
312
+ },
313
+ "./number/randomBetween": {
314
+ "import": "./number/randomBetween.js",
315
+ "types": "./number/randomBetween.d.ts"
316
+ },
317
+ "./number/randomIntBetween": {
318
+ "import": "./number/randomIntBetween.js",
319
+ "types": "./number/randomIntBetween.d.ts"
320
+ },
321
+ "./number/round": {
322
+ "import": "./number/round.js",
323
+ "types": "./number/round.d.ts"
324
+ },
325
+ "./number/toPercentage": {
326
+ "import": "./number/toPercentage.js",
327
+ "types": "./number/toPercentage.d.ts"
328
+ },
329
+ "./object": {
330
+ "import": "./object/index.js",
331
+ "types": "./object/index.d.ts"
332
+ },
333
+ "./object/define": {
334
+ "import": "./object/define.js",
335
+ "types": "./object/define.d.ts"
336
+ },
337
+ "./object/is": {
338
+ "import": "./object/is.js",
339
+ "types": "./object/is.d.ts"
340
+ },
341
+ "./object/isNotEmpty": {
342
+ "import": "./object/isNotEmpty.js",
343
+ "types": "./object/isNotEmpty.d.ts"
344
+ },
345
+ "./object/merge": {
346
+ "import": "./object/merge.js",
347
+ "types": "./object/merge.d.ts"
348
+ },
349
+ "./object/omit": {
350
+ "import": "./object/omit.js",
351
+ "types": "./object/omit.d.ts"
352
+ },
353
+ "./object/pick": {
354
+ "import": "./object/pick.js",
355
+ "types": "./object/pick.d.ts"
356
+ },
357
+ "./regexp": {
358
+ "import": "./regexp/index.js",
359
+ "types": "./regexp/index.d.ts"
360
+ },
361
+ "./regexp/is": {
362
+ "import": "./regexp/is.js",
363
+ "types": "./regexp/is.d.ts"
364
+ },
365
+ "./regexp/sanitize": {
366
+ "import": "./regexp/sanitize.js",
367
+ "types": "./regexp/sanitize.d.ts"
368
+ },
369
+ "./string": {
370
+ "import": "./string/index.js",
371
+ "types": "./string/index.d.ts"
372
+ },
373
+ "./string/humanizeBytes": {
374
+ "import": "./string/humanizeBytes.js",
375
+ "types": "./string/humanizeBytes.d.ts"
376
+ },
377
+ "./string/humanizeNumber": {
378
+ "import": "./string/humanizeNumber.js",
379
+ "types": "./string/humanizeNumber.d.ts"
380
+ },
381
+ "./string/is": {
382
+ "import": "./string/is.js",
383
+ "types": "./string/is.d.ts"
384
+ },
385
+ "./string/isBetween": {
386
+ "import": "./string/isBetween.js",
387
+ "types": "./string/isBetween.d.ts"
388
+ },
389
+ "./string/isNotEmpty": {
390
+ "import": "./string/isNotEmpty.js",
391
+ "types": "./string/isNotEmpty.d.ts"
392
+ },
393
+ "./string/shorten": {
394
+ "import": "./string/shorten.js",
395
+ "types": "./string/shorten.d.ts"
396
+ }
397
+ },
398
+ "devDependencies": {
399
+ "@types/node": "^22.13.1",
400
+ "esbuild-register": "^3.6.0",
401
+ "eslint": "^9.19.0",
402
+ "nyc": "^17.1.0",
403
+ "typescript": "^5.7.3",
404
+ "typescript-eslint": "^8.23.0"
405
+ }
406
+ }
@@ -3,13 +3,12 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.humanizeNumber = humanizeNumber;
4
4
  exports.default = humanizeNumber;
5
5
  const round_1 = require("../number/round");
6
- const isIntegerAboveOrEqual_1 = require("../number/isIntegerAboveOrEqual");
7
6
  const DEFAULT_UNITS = ['', 'k', 'm', 'b', 't', 'q'];
8
7
  function humanizeNumber(val, options = {}) {
9
8
  const DELIM = typeof options?.delim === 'string' ? options.delim : ',';
10
9
  const SEPARATOR = typeof options?.separator === 'string' && options.separator.trim().length ? options.separator : '.';
11
- const PRECISION = (0, isIntegerAboveOrEqual_1.isIntegerAboveOrEqual)(options?.precision, 0) ? options.precision : 2;
12
- const DIVIDER = (0, isIntegerAboveOrEqual_1.isIntegerAboveOrEqual)(options?.divider, 2) ? options.divider : 1000;
10
+ const PRECISION = Number.isInteger(options?.precision) && options.precision >= 0 ? options.precision : 2;
11
+ const DIVIDER = Number.isInteger(options?.divider) && options.divider >= 2 ? options.divider : 1000;
13
12
  const REAL = options?.real === true;
14
13
  const UNITS = Array.isArray(options?.units) && options.units.length
15
14
  ? options.units