@vielzeug/arsenal 1.1.1 → 2.0.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 +31 -75
- package/dist/_dev.cjs +1 -1
- package/dist/_dev.cjs.map +1 -1
- package/dist/_dev.js +2 -5
- package/dist/_dev.js.map +1 -1
- package/dist/array/chunk.cjs +1 -1
- package/dist/array/chunk.cjs.map +1 -1
- package/dist/array/chunk.d.ts +2 -1
- package/dist/array/chunk.d.ts.map +1 -1
- package/dist/array/chunk.js +5 -7
- package/dist/array/chunk.js.map +1 -1
- package/dist/array/compare.cjs +1 -1
- package/dist/array/compare.cjs.map +1 -1
- package/dist/array/compare.d.ts.map +1 -1
- package/dist/array/compare.js +13 -14
- package/dist/array/compare.js.map +1 -1
- package/dist/array/index.cjs +1 -0
- package/dist/array/index.js +29 -0
- package/dist/array/search.cjs +1 -1
- package/dist/array/search.cjs.map +1 -1
- package/dist/array/search.d.ts +8 -77
- package/dist/array/search.d.ts.map +1 -1
- package/dist/array/search.js +21 -38
- package/dist/array/search.js.map +1 -1
- package/dist/array/sort.cjs +1 -1
- package/dist/array/sort.cjs.map +1 -1
- package/dist/array/sort.d.ts +1 -1
- package/dist/array/sort.d.ts.map +1 -1
- package/dist/array/sort.js +9 -10
- package/dist/array/sort.js.map +1 -1
- package/dist/arsenal.cjs +1 -1
- package/dist/arsenal.cjs.map +1 -1
- package/dist/arsenal.iife.js +1 -1
- package/dist/arsenal.iife.js.map +1 -1
- package/dist/arsenal.js +1 -1
- package/dist/arsenal.js.map +1 -1
- package/dist/async/attempt.cjs +1 -1
- package/dist/async/attempt.cjs.map +1 -1
- package/dist/async/attempt.d.ts +3 -8
- package/dist/async/attempt.d.ts.map +1 -1
- package/dist/async/attempt.js +3 -10
- package/dist/async/attempt.js.map +1 -1
- package/dist/async/index.cjs +1 -0
- package/dist/async/index.d.ts +1 -1
- package/dist/async/index.d.ts.map +1 -1
- package/dist/async/index.js +9 -0
- package/dist/async/parallel.cjs +1 -1
- package/dist/async/parallel.cjs.map +1 -1
- package/dist/async/parallel.d.ts +1 -1
- package/dist/async/parallel.d.ts.map +1 -1
- package/dist/async/parallel.js +15 -16
- package/dist/async/parallel.js.map +1 -1
- package/dist/async/retry.cjs +1 -1
- package/dist/async/retry.cjs.map +1 -1
- package/dist/async/retry.d.ts +1 -1
- package/dist/async/retry.d.ts.map +1 -1
- package/dist/async/retry.js +14 -15
- package/dist/async/retry.js.map +1 -1
- package/dist/async/taskPool.cjs +2 -0
- package/dist/async/taskPool.cjs.map +1 -0
- package/dist/async/taskPool.d.ts +14 -0
- package/dist/async/taskPool.d.ts.map +1 -0
- package/dist/async/taskPool.js +51 -0
- package/dist/async/taskPool.js.map +1 -0
- package/dist/cache/cache.cjs +2 -0
- package/dist/cache/cache.cjs.map +1 -0
- package/dist/cache/cache.d.ts +17 -0
- package/dist/cache/cache.d.ts.map +1 -0
- package/dist/cache/cache.js +59 -0
- package/dist/cache/cache.js.map +1 -0
- package/dist/cache/index.cjs +1 -0
- package/dist/cache/index.d.ts +1 -1
- package/dist/cache/index.d.ts.map +1 -1
- package/dist/cache/index.js +3 -0
- package/dist/cache/memo.cjs +1 -1
- package/dist/cache/memo.cjs.map +1 -1
- package/dist/cache/memo.d.ts +2 -2
- package/dist/cache/memo.js +24 -23
- package/dist/cache/memo.js.map +1 -1
- package/dist/errors.cjs +1 -1
- package/dist/errors.cjs.map +1 -1
- package/dist/errors.d.ts +0 -7
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +2 -2
- package/dist/errors.js.map +1 -1
- package/dist/function/debounce.cjs +1 -1
- package/dist/function/debounce.cjs.map +1 -1
- package/dist/function/debounce.js +15 -17
- package/dist/function/debounce.js.map +1 -1
- package/dist/function/index.cjs +1 -0
- package/dist/function/index.d.ts +0 -2
- package/dist/function/index.d.ts.map +1 -1
- package/dist/function/index.js +8 -0
- package/dist/function/pipe.cjs +1 -1
- package/dist/function/pipe.cjs.map +1 -1
- package/dist/function/pipe.js +1 -1
- package/dist/function/pipe.js.map +1 -1
- package/dist/guards/index.cjs +1 -0
- package/dist/guards/index.d.ts +0 -3
- package/dist/guards/index.d.ts.map +1 -1
- package/dist/guards/index.js +17 -0
- package/dist/guards/isEqual.cjs.map +1 -1
- package/dist/guards/isEqual.js.map +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.d.ts +2 -91
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +30 -118
- package/dist/math/allocate.cjs +1 -1
- package/dist/math/allocate.cjs.map +1 -1
- package/dist/math/allocate.d.ts +1 -1
- package/dist/math/allocate.d.ts.map +1 -1
- package/dist/math/allocate.js +13 -14
- package/dist/math/allocate.js.map +1 -1
- package/dist/math/index.cjs +1 -0
- package/dist/math/index.d.ts +0 -3
- package/dist/math/index.d.ts.map +1 -1
- package/dist/math/index.js +17 -0
- package/dist/math/mod.cjs +1 -1
- package/dist/math/mod.cjs.map +1 -1
- package/dist/math/mod.d.ts +1 -1
- package/dist/math/mod.d.ts.map +1 -1
- package/dist/math/mod.js +4 -5
- package/dist/math/mod.js.map +1 -1
- package/dist/math/range.cjs +1 -1
- package/dist/math/range.cjs.map +1 -1
- package/dist/math/range.d.ts +1 -1
- package/dist/math/range.d.ts.map +1 -1
- package/dist/math/range.js +14 -15
- package/dist/math/range.js.map +1 -1
- package/dist/math/sum.cjs +1 -1
- package/dist/math/sum.cjs.map +1 -1
- package/dist/math/sum.d.ts +2 -2
- package/dist/math/sum.d.ts.map +1 -1
- package/dist/math/sum.js +6 -7
- package/dist/math/sum.js.map +1 -1
- package/dist/object/getPath.cjs +1 -1
- package/dist/object/getPath.cjs.map +1 -1
- package/dist/object/getPath.d.ts +12 -42
- package/dist/object/getPath.d.ts.map +1 -1
- package/dist/object/getPath.js +21 -22
- package/dist/object/getPath.js.map +1 -1
- package/dist/object/index.cjs +1 -0
- package/dist/object/index.d.ts +1 -1
- package/dist/object/index.d.ts.map +1 -1
- package/dist/object/index.js +15 -0
- package/dist/object/merge.cjs +1 -1
- package/dist/object/merge.cjs.map +1 -1
- package/dist/object/merge.d.ts +1 -1
- package/dist/object/merge.d.ts.map +1 -1
- package/dist/object/merge.js +2 -3
- package/dist/object/merge.js.map +1 -1
- package/dist/object/prune.cjs +1 -1
- package/dist/object/prune.cjs.map +1 -1
- package/dist/object/prune.d.ts.map +1 -1
- package/dist/object/prune.js +22 -23
- package/dist/object/prune.js.map +1 -1
- package/dist/object/tryParseJson.cjs +2 -0
- package/dist/object/tryParseJson.cjs.map +1 -0
- package/dist/object/tryParseJson.d.ts +9 -0
- package/dist/object/tryParseJson.d.ts.map +1 -0
- package/dist/object/tryParseJson.js +18 -0
- package/dist/object/tryParseJson.js.map +1 -0
- package/dist/random/_index.cjs +2 -0
- package/dist/random/_index.cjs.map +1 -0
- package/dist/random/_index.d.ts +3 -0
- package/dist/random/_index.d.ts.map +1 -0
- package/dist/random/_index.js +10 -0
- package/dist/random/_index.js.map +1 -0
- package/dist/random/draw.cjs +1 -1
- package/dist/random/draw.cjs.map +1 -1
- package/dist/random/draw.d.ts +3 -2
- package/dist/random/draw.d.ts.map +1 -1
- package/dist/random/draw.js +12 -12
- package/dist/random/draw.js.map +1 -1
- package/dist/random/index.cjs +1 -0
- package/dist/random/index.d.ts +1 -0
- package/dist/random/index.d.ts.map +1 -1
- package/dist/random/index.js +5 -0
- package/dist/random/random.cjs +1 -1
- package/dist/random/random.cjs.map +1 -1
- package/dist/random/random.d.ts +2 -1
- package/dist/random/random.d.ts.map +1 -1
- package/dist/random/random.js +5 -6
- package/dist/random/random.js.map +1 -1
- package/dist/random/shuffle.cjs +1 -1
- package/dist/random/shuffle.cjs.map +1 -1
- package/dist/random/shuffle.d.ts +2 -1
- package/dist/random/shuffle.d.ts.map +1 -1
- package/dist/random/shuffle.js +7 -7
- package/dist/random/shuffle.js.map +1 -1
- package/dist/random/source.d.ts +4 -0
- package/dist/random/source.d.ts.map +1 -0
- package/dist/string/index.cjs +1 -0
- package/dist/string/index.d.ts +0 -2
- package/dist/string/index.d.ts.map +1 -1
- package/dist/string/index.js +12 -0
- package/dist/string/similarity.cjs +1 -1
- package/dist/string/similarity.cjs.map +1 -1
- package/dist/string/similarity.d.ts.map +1 -1
- package/dist/string/similarity.js +16 -17
- package/dist/string/similarity.js.map +1 -1
- package/dist/string/truncate.cjs +1 -1
- package/dist/string/truncate.cjs.map +1 -1
- package/dist/string/truncate.d.ts +2 -1
- package/dist/string/truncate.d.ts.map +1 -1
- package/dist/string/truncate.js +10 -11
- package/dist/string/truncate.js.map +1 -1
- package/dist/types.d.ts +1 -1
- package/dist/types.d.ts.map +1 -1
- package/package.json +14 -19
- package/dist/_common/_secureRandomIndex.cjs +0 -2
- package/dist/_common/_secureRandomIndex.cjs.map +0 -1
- package/dist/_common/_secureRandomIndex.d.ts +0 -2
- package/dist/_common/_secureRandomIndex.d.ts.map +0 -1
- package/dist/_common/_secureRandomIndex.js +0 -9
- package/dist/_common/_secureRandomIndex.js.map +0 -1
- package/dist/async/queue.cjs +0 -2
- package/dist/async/queue.cjs.map +0 -1
- package/dist/async/queue.d.ts +0 -42
- package/dist/async/queue.d.ts.map +0 -1
- package/dist/async/queue.js +0 -72
- package/dist/async/queue.js.map +0 -1
- package/dist/cache/stash.cjs +0 -2
- package/dist/cache/stash.cjs.map +0 -1
- package/dist/cache/stash.d.ts +0 -111
- package/dist/cache/stash.d.ts.map +0 -1
- package/dist/cache/stash.js +0 -92
- package/dist/cache/stash.js.map +0 -1
- package/dist/function/constant.cjs +0 -2
- package/dist/function/constant.cjs.map +0 -1
- package/dist/function/constant.d.ts +0 -5
- package/dist/function/constant.d.ts.map +0 -1
- package/dist/function/constant.js +0 -8
- package/dist/function/constant.js.map +0 -1
- package/dist/function/identity.cjs +0 -2
- package/dist/function/identity.cjs.map +0 -1
- package/dist/function/identity.d.ts +0 -5
- package/dist/function/identity.d.ts.map +0 -1
- package/dist/function/identity.js +0 -8
- package/dist/function/identity.js.map +0 -1
- package/dist/guards/isArray.cjs +0 -2
- package/dist/guards/isArray.cjs.map +0 -1
- package/dist/guards/isArray.d.ts +0 -16
- package/dist/guards/isArray.d.ts.map +0 -1
- package/dist/guards/isArray.js +0 -8
- package/dist/guards/isArray.js.map +0 -1
- package/dist/guards/isBoolean.cjs +0 -2
- package/dist/guards/isBoolean.cjs.map +0 -1
- package/dist/guards/isBoolean.d.ts +0 -23
- package/dist/guards/isBoolean.d.ts.map +0 -1
- package/dist/guards/isBoolean.js +0 -8
- package/dist/guards/isBoolean.js.map +0 -1
- package/dist/guards/isString.cjs +0 -2
- package/dist/guards/isString.cjs.map +0 -1
- package/dist/guards/isString.d.ts +0 -15
- package/dist/guards/isString.d.ts.map +0 -1
- package/dist/guards/isString.js +0 -8
- package/dist/guards/isString.js.map +0 -1
- package/dist/math/abs.cjs +0 -2
- package/dist/math/abs.cjs.map +0 -1
- package/dist/math/abs.d.ts +0 -17
- package/dist/math/abs.d.ts.map +0 -1
- package/dist/math/abs.js +0 -8
- package/dist/math/abs.js.map +0 -1
- package/dist/math/max.cjs +0 -2
- package/dist/math/max.cjs.map +0 -1
- package/dist/math/max.d.ts +0 -21
- package/dist/math/max.d.ts.map +0 -1
- package/dist/math/max.js +0 -12
- package/dist/math/max.js.map +0 -1
- package/dist/math/min.cjs +0 -2
- package/dist/math/min.cjs.map +0 -1
- package/dist/math/min.d.ts +0 -21
- package/dist/math/min.d.ts.map +0 -1
- package/dist/math/min.js +0 -12
- package/dist/math/min.js.map +0 -1
- package/dist/object/parseJSON.cjs +0 -2
- package/dist/object/parseJSON.cjs.map +0 -1
- package/dist/object/parseJSON.d.ts +0 -32
- package/dist/object/parseJSON.d.ts.map +0 -1
- package/dist/object/parseJSON.js +0 -16
- package/dist/object/parseJSON.js.map +0 -1
- package/dist/string/endsWith.cjs +0 -2
- package/dist/string/endsWith.cjs.map +0 -1
- package/dist/string/endsWith.d.ts +0 -5
- package/dist/string/endsWith.d.ts.map +0 -1
- package/dist/string/endsWith.js +0 -8
- package/dist/string/endsWith.js.map +0 -1
- package/dist/string/startsWith.cjs +0 -2
- package/dist/string/startsWith.cjs.map +0 -1
- package/dist/string/startsWith.d.ts +0 -5
- package/dist/string/startsWith.d.ts.map +0 -1
- package/dist/string/startsWith.js +0 -8
- package/dist/string/startsWith.js.map +0 -1
package/dist/math/sum.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sum.cjs","names":[],"sources":["../../src/math/sum.ts"],"sourcesContent":["
|
|
1
|
+
{"version":3,"file":"sum.cjs","names":[],"sources":["../../src/math/sum.ts"],"sourcesContent":["/**\n * Sum numbers in an array or numbers mapped by a callback function.\n *\n * @example\n * ```ts\n * sum([1, 2, 3]) // 6\n * sum([{value: 1}, {value: 2}, {value: 3}], (item) => item.value) // 6\n * sum(['apple', 'banana', 'cherry']) // TypeError\n * ```\n *\n * @param array - The array to sum.\n * @param callback - An optional callback function to map the values.\n *\n * @returns The sum of the numbers in the array or the sum of the mapped values.\n *\n * @throws {TypeError} If (after an optional `callback` mapping) any item is not a finite number.\n */\nexport function sum<T>(array: T[], callback?: (item: T) => number): number {\n if (array.length === 0) return 0;\n\n return array.reduce<number>((acc, item) => {\n const val = callback ? callback(item) : (item as unknown as number);\n\n if (typeof val !== 'number' || Number.isNaN(val)) {\n throw new TypeError('sum: encountered a non-numeric value; provide a callback to map non-number items');\n }\n\n return acc + val;\n }, 0);\n}\n"],"mappings":"AAiBA,SAAgB,EAAO,EAAY,EAAwC,CAGzE,OAFI,EAAM,SAAW,EAAU,EAExB,EAAM,QAAgB,EAAK,IAAS,CACzC,IAAM,EAAM,EAAW,EAAS,CAAI,EAAK,EAEzC,GAAI,OAAO,GAAQ,UAAY,OAAO,MAAM,CAAG,EAC7C,MAAU,UAAU,kFAAkF,EAGxG,OAAO,EAAM,CACf,EAAG,CAAC,CACN"}
|
package/dist/math/sum.d.ts
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* ```ts
|
|
6
6
|
* sum([1, 2, 3]) // 6
|
|
7
7
|
* sum([{value: 1}, {value: 2}, {value: 3}], (item) => item.value) // 6
|
|
8
|
-
* sum(['apple', 'banana', 'cherry']) //
|
|
8
|
+
* sum(['apple', 'banana', 'cherry']) // TypeError
|
|
9
9
|
* ```
|
|
10
10
|
*
|
|
11
11
|
* @param array - The array to sum.
|
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
*
|
|
14
14
|
* @returns The sum of the numbers in the array or the sum of the mapped values.
|
|
15
15
|
*
|
|
16
|
-
* @throws {
|
|
16
|
+
* @throws {TypeError} If (after an optional `callback` mapping) any item is not a finite number.
|
|
17
17
|
*/
|
|
18
18
|
export declare function sum<T>(array: T[], callback?: (item: T) => number): number;
|
|
19
19
|
//# sourceMappingURL=sum.d.ts.map
|
package/dist/math/sum.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sum.d.ts","sourceRoot":"","sources":["../../src/math/sum.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"sum.d.ts","sourceRoot":"","sources":["../../src/math/sum.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,GAAG,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,EAAE,EAAE,QAAQ,CAAC,EAAE,CAAC,IAAI,EAAE,CAAC,KAAK,MAAM,GAAG,MAAM,CAYzE"}
|
package/dist/math/sum.js
CHANGED
|
@@ -1,13 +1,12 @@
|
|
|
1
|
-
import { ArsenalValidationError as e } from "../errors.js";
|
|
2
1
|
//#region src/math/sum.ts
|
|
3
|
-
function
|
|
4
|
-
return
|
|
5
|
-
let
|
|
6
|
-
if (typeof
|
|
7
|
-
return
|
|
2
|
+
function e(e, t) {
|
|
3
|
+
return e.length === 0 ? 0 : e.reduce((e, n) => {
|
|
4
|
+
let r = t ? t(n) : n;
|
|
5
|
+
if (typeof r != "number" || Number.isNaN(r)) throw TypeError("sum: encountered a non-numeric value; provide a callback to map non-number items");
|
|
6
|
+
return e + r;
|
|
8
7
|
}, 0);
|
|
9
8
|
}
|
|
10
9
|
//#endregion
|
|
11
|
-
export {
|
|
10
|
+
export { e as sum };
|
|
12
11
|
|
|
13
12
|
//# sourceMappingURL=sum.js.map
|
package/dist/math/sum.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sum.js","names":[],"sources":["../../src/math/sum.ts"],"sourcesContent":["
|
|
1
|
+
{"version":3,"file":"sum.js","names":[],"sources":["../../src/math/sum.ts"],"sourcesContent":["/**\n * Sum numbers in an array or numbers mapped by a callback function.\n *\n * @example\n * ```ts\n * sum([1, 2, 3]) // 6\n * sum([{value: 1}, {value: 2}, {value: 3}], (item) => item.value) // 6\n * sum(['apple', 'banana', 'cherry']) // TypeError\n * ```\n *\n * @param array - The array to sum.\n * @param callback - An optional callback function to map the values.\n *\n * @returns The sum of the numbers in the array or the sum of the mapped values.\n *\n * @throws {TypeError} If (after an optional `callback` mapping) any item is not a finite number.\n */\nexport function sum<T>(array: T[], callback?: (item: T) => number): number {\n if (array.length === 0) return 0;\n\n return array.reduce<number>((acc, item) => {\n const val = callback ? callback(item) : (item as unknown as number);\n\n if (typeof val !== 'number' || Number.isNaN(val)) {\n throw new TypeError('sum: encountered a non-numeric value; provide a callback to map non-number items');\n }\n\n return acc + val;\n }, 0);\n}\n"],"mappings":";AAiBA,SAAgB,EAAO,GAAY,GAAwC;CAGzE,OAFI,EAAM,WAAW,IAAU,IAExB,EAAM,QAAgB,GAAK,MAAS;EACzC,IAAM,IAAM,IAAW,EAAS,CAAI,IAAK;EAEzC,IAAI,OAAO,KAAQ,YAAY,OAAO,MAAM,CAAG,GAC7C,MAAU,UAAU,kFAAkF;EAGxG,OAAO,IAAM;CACf,GAAG,CAAC;AACN"}
|
package/dist/object/getPath.cjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
const e=require("../
|
|
1
|
+
const e=require("../_common/unsafePaths.cjs");function t(e){return e.replace(/\[(\d+)\]/g,`.$1`).replace(/^\.|\.$/g,``).split(`.`).filter(Boolean)}function n(n,r){let i=n;for(let n of t(r))if(e.UNSAFE_PATH_SEGMENTS.has(n)||typeof i!=`object`||!i||(i=i[n],i===void 0))return;return i}function r(e,t){return n(e,t)}function i(e,t,r){let i=n(e,t);return i===void 0?r:i}function a(e,t){let r=n(e,t);if(r===void 0)throw TypeError(`Path does not exist: '${t}'`);return r}exports.getPath=r,exports.getPathOr=i,exports.requirePath=a;
|
|
2
2
|
//# sourceMappingURL=getPath.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"getPath.cjs","names":[],"sources":["../../src/object/getPath.ts"],"sourcesContent":["import type { Obj } from '../types';\n\nimport { UNSAFE_PATH_SEGMENTS } from '../_common/unsafePaths';\
|
|
1
|
+
{"version":3,"file":"getPath.cjs","names":[],"sources":["../../src/object/getPath.ts"],"sourcesContent":["import type { Obj } from '../types';\n\nimport { UNSAFE_PATH_SEGMENTS } from '../_common/unsafePaths';\n\ntype PathValue<T, P extends string> = P extends `${infer Key}.${infer Rest}`\n ? Key extends keyof T\n ? PathValue<T[Key], Rest>\n : undefined\n : P extends keyof T\n ? T[P]\n : undefined;\n\nfunction pathSegments(path: string): string[] {\n return path\n .replace(/\\[(\\d+)\\]/g, '.$1')\n .replace(/^\\.|\\.$/g, '')\n .split('.')\n .filter(Boolean);\n}\n\nfunction readPath(item: Obj, path: string): unknown {\n let current: unknown = item;\n\n for (const segment of pathSegments(path)) {\n if (UNSAFE_PATH_SEGMENTS.has(segment) || current == null || typeof current !== 'object') return undefined;\n\n current = (current as Record<string, unknown>)[segment];\n\n if (current === undefined) return undefined;\n }\n\n return current;\n}\n\n/**\n * Retrieves a value at a dot-notation or numeric bracket-notation path.\n * Returns `undefined` when the path is missing or contains an unsafe segment.\n * Use `getPathOr` to supply a fallback or `requirePath` to throw on a missing path.\n */\nexport function getPath<T extends Obj, P extends string>(item: T, path: P): PathValue<T, P> | undefined {\n return readPath(item, path) as PathValue<T, P> | undefined;\n}\n\n/**\n * Retrieves a value at a path or returns `fallback` when the path is missing.\n */\nexport function getPathOr<T extends Obj, P extends string, F>(item: T, path: P, fallback: F): PathValue<T, P> | F {\n const value = readPath(item, path);\n\n return (value === undefined ? fallback : value) as PathValue<T, P> | F;\n}\n\n/**\n * Retrieves a value at a path or throws when the path is missing.\n */\nexport function requirePath<T extends Obj, P extends string>(item: T, path: P): Exclude<PathValue<T, P>, undefined> {\n const value = readPath(item, path);\n\n if (value === undefined) throw new TypeError(`Path does not exist: '${path}'`);\n\n return value as Exclude<PathValue<T, P>, undefined>;\n}\n"],"mappings":"8CAYA,SAAS,EAAa,EAAwB,CAC5C,OAAO,EACJ,QAAQ,aAAc,KAAK,CAAC,CAC5B,QAAQ,WAAY,EAAE,CAAC,CACvB,MAAM,GAAG,CAAC,CACV,OAAO,OAAO,CACnB,CAEA,SAAS,EAAS,EAAW,EAAuB,CAClD,IAAI,EAAmB,EAEvB,IAAK,IAAM,KAAW,EAAa,CAAI,EAKrC,GAJI,EAAA,qBAAqB,IAAI,CAAO,GAAwB,OAAO,GAAY,WAAtC,IAEzC,EAAW,EAAoC,GAE3C,IAAY,IAAA,IAAW,OAG7B,OAAO,CACT,CAOA,SAAgB,EAAyC,EAAS,EAAsC,CACtG,OAAO,EAAS,EAAM,CAAI,CAC5B,CAKA,SAAgB,EAA8C,EAAS,EAAS,EAAkC,CAChH,IAAM,EAAQ,EAAS,EAAM,CAAI,EAEjC,OAAQ,IAAU,IAAA,GAAY,EAAW,CAC3C,CAKA,SAAgB,EAA6C,EAAS,EAA8C,CAClH,IAAM,EAAQ,EAAS,EAAM,CAAI,EAEjC,GAAI,IAAU,IAAA,GAAW,MAAU,UAAU,yBAAyB,EAAK,EAAE,EAE7E,OAAO,CACT"}
|
package/dist/object/getPath.d.ts
CHANGED
|
@@ -1,48 +1,18 @@
|
|
|
1
1
|
import type { Obj } from '../types';
|
|
2
2
|
type PathValue<T, P extends string> = P extends `${infer Key}.${infer Rest}` ? Key extends keyof T ? PathValue<T[Key], Rest> : undefined : P extends keyof T ? T[P] : undefined;
|
|
3
|
-
export type GetPathOptions = {
|
|
4
|
-
/**
|
|
5
|
-
* When `false`, throws an `ArsenalValidationError` on bracket-notation segments instead of
|
|
6
|
-
* auto-converting them to dot notation. Default: `true`.
|
|
7
|
-
*/
|
|
8
|
-
bracketNotation?: boolean;
|
|
9
|
-
/**
|
|
10
|
-
* Returned when the path is missing or resolves to `undefined`.
|
|
11
|
-
* Mutually exclusive with `strict` — if both are set, `strict` takes precedence.
|
|
12
|
-
*/
|
|
13
|
-
fallback?: unknown;
|
|
14
|
-
/**
|
|
15
|
-
* When `true`, throws an `ArsenalValidationError` if any path segment is missing.
|
|
16
|
-
* Default: `false`.
|
|
17
|
-
*/
|
|
18
|
-
strict?: boolean;
|
|
19
|
-
};
|
|
20
3
|
/**
|
|
21
|
-
* Retrieves
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
* Unsafe path segments (`__proto__`, `constructor`, `prototype`) always return `options.fallback`.
|
|
25
|
-
*
|
|
26
|
-
* @example
|
|
27
|
-
* ```ts
|
|
28
|
-
* const obj = { a: { b: { c: 3 } }, d: [1, 2, 3] };
|
|
29
|
-
*
|
|
30
|
-
* getPath(obj, 'a.b.c'); // 3
|
|
31
|
-
* getPath(obj, 'a.b.x', { fallback: 'fallback' }); // 'fallback'
|
|
32
|
-
* getPath(obj, 'd[1]'); // 2 (bracket auto-converted)
|
|
33
|
-
* getPath(obj, 'e.f.g', { strict: true }); // throws ArsenalValidationError
|
|
34
|
-
* getPath(obj, 'a[0]', { bracketNotation: false }); // throws ArsenalValidationError
|
|
35
|
-
* ```
|
|
36
|
-
*
|
|
37
|
-
* @template T - The object type.
|
|
38
|
-
* @template P - The path string type.
|
|
39
|
-
* @param item - The object to query.
|
|
40
|
-
* @param path - The dot-separated path.
|
|
41
|
-
* @param [options] - Options: `fallback`, `strict`, `bracketNotation`.
|
|
42
|
-
* @returns The resolved value, or `options.fallback` if missing.
|
|
43
|
-
* @throws {ArsenalValidationError} When `bracketNotation: false` and bracket syntax is used.
|
|
44
|
-
* @throws {ArsenalValidationError} When `strict: true` and the path does not exist.
|
|
4
|
+
* Retrieves a value at a dot-notation or numeric bracket-notation path.
|
|
5
|
+
* Returns `undefined` when the path is missing or contains an unsafe segment.
|
|
6
|
+
* Use `getPathOr` to supply a fallback or `requirePath` to throw on a missing path.
|
|
45
7
|
*/
|
|
46
|
-
export declare function getPath<T extends Obj, P extends string>(item: T, path: P
|
|
8
|
+
export declare function getPath<T extends Obj, P extends string>(item: T, path: P): PathValue<T, P> | undefined;
|
|
9
|
+
/**
|
|
10
|
+
* Retrieves a value at a path or returns `fallback` when the path is missing.
|
|
11
|
+
*/
|
|
12
|
+
export declare function getPathOr<T extends Obj, P extends string, F>(item: T, path: P, fallback: F): PathValue<T, P> | F;
|
|
13
|
+
/**
|
|
14
|
+
* Retrieves a value at a path or throws when the path is missing.
|
|
15
|
+
*/
|
|
16
|
+
export declare function requirePath<T extends Obj, P extends string>(item: T, path: P): Exclude<PathValue<T, P>, undefined>;
|
|
47
17
|
export {};
|
|
48
18
|
//# sourceMappingURL=getPath.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"getPath.d.ts","sourceRoot":"","sources":["../../src/object/getPath.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,UAAU,CAAC;
|
|
1
|
+
{"version":3,"file":"getPath.d.ts","sourceRoot":"","sources":["../../src/object/getPath.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,UAAU,CAAC;AAIpC,KAAK,SAAS,CAAC,CAAC,EAAE,CAAC,SAAS,MAAM,IAAI,CAAC,SAAS,GAAG,MAAM,GAAG,IAAI,MAAM,IAAI,EAAE,GACxE,GAAG,SAAS,MAAM,CAAC,GACjB,SAAS,CAAC,CAAC,CAAC,GAAG,CAAC,EAAE,IAAI,CAAC,GACvB,SAAS,GACX,CAAC,SAAS,MAAM,CAAC,GACf,CAAC,CAAC,CAAC,CAAC,GACJ,SAAS,CAAC;AAwBhB;;;;GAIG;AACH,wBAAgB,OAAO,CAAC,CAAC,SAAS,GAAG,EAAE,CAAC,SAAS,MAAM,EAAE,IAAI,EAAE,CAAC,EAAE,IAAI,EAAE,CAAC,GAAG,SAAS,CAAC,CAAC,EAAE,CAAC,CAAC,GAAG,SAAS,CAEtG;AAED;;GAEG;AACH,wBAAgB,SAAS,CAAC,CAAC,SAAS,GAAG,EAAE,CAAC,SAAS,MAAM,EAAE,CAAC,EAAE,IAAI,EAAE,CAAC,EAAE,IAAI,EAAE,CAAC,EAAE,QAAQ,EAAE,CAAC,GAAG,SAAS,CAAC,CAAC,EAAE,CAAC,CAAC,GAAG,CAAC,CAIhH;AAED;;GAEG;AACH,wBAAgB,WAAW,CAAC,CAAC,SAAS,GAAG,EAAE,CAAC,SAAS,MAAM,EAAE,IAAI,EAAE,CAAC,EAAE,IAAI,EAAE,CAAC,GAAG,OAAO,CAAC,SAAS,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,SAAS,CAAC,CAMlH"}
|
package/dist/object/getPath.js
CHANGED
|
@@ -1,27 +1,26 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { UNSAFE_PATH_SEGMENTS as t } from "../_common/unsafePaths.js";
|
|
1
|
+
import { UNSAFE_PATH_SEGMENTS as e } from "../_common/unsafePaths.js";
|
|
3
2
|
//#region src/object/getPath.ts
|
|
4
|
-
function
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
}
|
|
22
|
-
return
|
|
3
|
+
function t(e) {
|
|
4
|
+
return e.replace(/\[(\d+)\]/g, ".$1").replace(/^\.|\.$/g, "").split(".").filter(Boolean);
|
|
5
|
+
}
|
|
6
|
+
function n(n, r) {
|
|
7
|
+
let i = n;
|
|
8
|
+
for (let n of t(r)) if (e.has(n) || typeof i != "object" || !i || (i = i[n], i === void 0)) return;
|
|
9
|
+
return i;
|
|
10
|
+
}
|
|
11
|
+
function r(e, t) {
|
|
12
|
+
return n(e, t);
|
|
13
|
+
}
|
|
14
|
+
function i(e, t, r) {
|
|
15
|
+
let i = n(e, t);
|
|
16
|
+
return i === void 0 ? r : i;
|
|
17
|
+
}
|
|
18
|
+
function a(e, t) {
|
|
19
|
+
let r = n(e, t);
|
|
20
|
+
if (r === void 0) throw TypeError(`Path does not exist: '${t}'`);
|
|
21
|
+
return r;
|
|
23
22
|
}
|
|
24
23
|
//#endregion
|
|
25
|
-
export {
|
|
24
|
+
export { r as getPath, i as getPathOr, a as requirePath };
|
|
26
25
|
|
|
27
26
|
//# sourceMappingURL=getPath.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"getPath.js","names":[],"sources":["../../src/object/getPath.ts"],"sourcesContent":["import type { Obj } from '../types';\n\nimport { UNSAFE_PATH_SEGMENTS } from '../_common/unsafePaths';\
|
|
1
|
+
{"version":3,"file":"getPath.js","names":[],"sources":["../../src/object/getPath.ts"],"sourcesContent":["import type { Obj } from '../types';\n\nimport { UNSAFE_PATH_SEGMENTS } from '../_common/unsafePaths';\n\ntype PathValue<T, P extends string> = P extends `${infer Key}.${infer Rest}`\n ? Key extends keyof T\n ? PathValue<T[Key], Rest>\n : undefined\n : P extends keyof T\n ? T[P]\n : undefined;\n\nfunction pathSegments(path: string): string[] {\n return path\n .replace(/\\[(\\d+)\\]/g, '.$1')\n .replace(/^\\.|\\.$/g, '')\n .split('.')\n .filter(Boolean);\n}\n\nfunction readPath(item: Obj, path: string): unknown {\n let current: unknown = item;\n\n for (const segment of pathSegments(path)) {\n if (UNSAFE_PATH_SEGMENTS.has(segment) || current == null || typeof current !== 'object') return undefined;\n\n current = (current as Record<string, unknown>)[segment];\n\n if (current === undefined) return undefined;\n }\n\n return current;\n}\n\n/**\n * Retrieves a value at a dot-notation or numeric bracket-notation path.\n * Returns `undefined` when the path is missing or contains an unsafe segment.\n * Use `getPathOr` to supply a fallback or `requirePath` to throw on a missing path.\n */\nexport function getPath<T extends Obj, P extends string>(item: T, path: P): PathValue<T, P> | undefined {\n return readPath(item, path) as PathValue<T, P> | undefined;\n}\n\n/**\n * Retrieves a value at a path or returns `fallback` when the path is missing.\n */\nexport function getPathOr<T extends Obj, P extends string, F>(item: T, path: P, fallback: F): PathValue<T, P> | F {\n const value = readPath(item, path);\n\n return (value === undefined ? fallback : value) as PathValue<T, P> | F;\n}\n\n/**\n * Retrieves a value at a path or throws when the path is missing.\n */\nexport function requirePath<T extends Obj, P extends string>(item: T, path: P): Exclude<PathValue<T, P>, undefined> {\n const value = readPath(item, path);\n\n if (value === undefined) throw new TypeError(`Path does not exist: '${path}'`);\n\n return value as Exclude<PathValue<T, P>, undefined>;\n}\n"],"mappings":";;AAYA,SAAS,EAAa,GAAwB;CAC5C,OAAO,EACJ,QAAQ,cAAc,KAAK,CAAC,CAC5B,QAAQ,YAAY,EAAE,CAAC,CACvB,MAAM,GAAG,CAAC,CACV,OAAO,OAAO;AACnB;AAEA,SAAS,EAAS,GAAW,GAAuB;CAClD,IAAI,IAAmB;CAEvB,KAAK,IAAM,KAAW,EAAa,CAAI,GAKrC,IAJI,EAAqB,IAAI,CAAO,KAAwB,OAAO,KAAY,aAAtC,MAEzC,IAAW,EAAoC,IAE3C,MAAY,KAAA,IAAW;CAG7B,OAAO;AACT;AAOA,SAAgB,EAAyC,GAAS,GAAsC;CACtG,OAAO,EAAS,GAAM,CAAI;AAC5B;AAKA,SAAgB,EAA8C,GAAS,GAAS,GAAkC;CAChH,IAAM,IAAQ,EAAS,GAAM,CAAI;CAEjC,OAAQ,MAAU,KAAA,IAAY,IAAW;AAC3C;AAKA,SAAgB,EAA6C,GAAS,GAA8C;CAClH,IAAM,IAAQ,EAAS,GAAM,CAAI;CAEjC,IAAI,MAAU,KAAA,GAAW,MAAU,UAAU,yBAAyB,EAAK,EAAE;CAE7E,OAAO;AACT"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});const e=require("./getPath.cjs"),t=require("./hash.cjs"),n=require("./omit.cjs"),r=require("./pick.cjs"),i=require("./defaults.cjs"),a=require("./diff.cjs"),o=require("./filterValues.cjs"),s=require("./flattenPaths.cjs"),c=require("./invert.cjs"),l=require("./mapKeys.cjs"),u=require("./mapValues.cjs"),d=require("./merge.cjs"),f=require("./prune.cjs"),p=require("./tryParseJson.cjs");exports.deepMerge=d.deepMerge,exports.defaults=i.defaults,exports.diff=a.diff,exports.diffArrays=a.diffArrays,exports.filterValues=o.filterValues,exports.flattenPaths=s.flattenPaths,exports.getPath=e.getPath,exports.getPathOr=e.getPathOr,exports.hash=t.hash,exports.invert=c.invert,exports.mapKeys=l.mapKeys,exports.mapValues=u.mapValues,exports.omit=n.omit,exports.pick=r.pick,exports.prune=f.prune,exports.requirePath=e.requirePath,exports.shallowMerge=d.shallowMerge,exports.tryParseJson=p.tryParseJson,exports.unflattenPaths=s.unflattenPaths;
|
package/dist/object/index.d.ts
CHANGED
|
@@ -9,7 +9,7 @@ export * from './mapKeys';
|
|
|
9
9
|
export * from './mapValues';
|
|
10
10
|
export * from './merge';
|
|
11
11
|
export * from './omit';
|
|
12
|
-
export * from './parseJSON';
|
|
13
12
|
export * from './pick';
|
|
14
13
|
export * from './prune';
|
|
14
|
+
export * from './tryParseJson';
|
|
15
15
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/object/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,cAAc,QAAQ,CAAC;AACvB,cAAc,gBAAgB,CAAC;AAC/B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,WAAW,CAAC;AAC1B,cAAc,QAAQ,CAAC;AACvB,cAAc,UAAU,CAAC;AACzB,cAAc,WAAW,CAAC;AAC1B,cAAc,aAAa,CAAC;AAC5B,cAAc,SAAS,CAAC;AACxB,cAAc,QAAQ,CAAC;AACvB,cAAc,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/object/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,cAAc,QAAQ,CAAC;AACvB,cAAc,gBAAgB,CAAC;AAC/B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,WAAW,CAAC;AAC1B,cAAc,QAAQ,CAAC;AACvB,cAAc,UAAU,CAAC;AACzB,cAAc,WAAW,CAAC;AAC1B,cAAc,aAAa,CAAC;AAC5B,cAAc,SAAS,CAAC;AACxB,cAAc,QAAQ,CAAC;AACvB,cAAc,QAAQ,CAAC;AACvB,cAAc,SAAS,CAAC;AACxB,cAAc,gBAAgB,CAAC"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { getPath as e, getPathOr as t, requirePath as n } from "./getPath.js";
|
|
2
|
+
import { hash as r } from "./hash.js";
|
|
3
|
+
import { omit as i } from "./omit.js";
|
|
4
|
+
import { pick as a } from "./pick.js";
|
|
5
|
+
import { defaults as o } from "./defaults.js";
|
|
6
|
+
import { diff as s, diffArrays as c } from "./diff.js";
|
|
7
|
+
import { filterValues as l } from "./filterValues.js";
|
|
8
|
+
import { flattenPaths as u, unflattenPaths as d } from "./flattenPaths.js";
|
|
9
|
+
import { invert as f } from "./invert.js";
|
|
10
|
+
import { mapKeys as p } from "./mapKeys.js";
|
|
11
|
+
import { mapValues as m } from "./mapValues.js";
|
|
12
|
+
import { deepMerge as h, shallowMerge as g } from "./merge.js";
|
|
13
|
+
import { prune as _ } from "./prune.js";
|
|
14
|
+
import { tryParseJson as v } from "./tryParseJson.js";
|
|
15
|
+
export { h as deepMerge, o as defaults, s as diff, c as diffArrays, l as filterValues, u as flattenPaths, e as getPath, t as getPathOr, r as hash, f as invert, p as mapKeys, m as mapValues, i as omit, a as pick, _ as prune, n as requirePath, g as shallowMerge, v as tryParseJson, d as unflattenPaths };
|
package/dist/object/merge.cjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
const e=require("../_common/unsafePaths.cjs"),t=require("../guards/isPlainObject.cjs");function n(r,i,a){if(!t.isPlainObject(i))return i;let o={...r};for(let r of Object.keys(i)){if(e.isUnsafeKey(r))continue;let s=i[r],c=o[r];o[r]=Array.isArray(s)&&Array.isArray(c)?a.arrayStrategy===`concat`?[...c,...s]:s:t.isPlainObject(s)&&t.isPlainObject(c)?n(c,s,a):s}return o}function r(
|
|
1
|
+
const e=require("../_common/unsafePaths.cjs"),t=require("../guards/isPlainObject.cjs");function n(r,i,a){if(!t.isPlainObject(i))return i;let o={...r};for(let r of Object.keys(i)){if(e.isUnsafeKey(r))continue;let s=i[r],c=o[r];o[r]=Array.isArray(s)&&Array.isArray(c)?a.arrayStrategy===`concat`?[...c,...s]:s:t.isPlainObject(s)&&t.isPlainObject(c)?n(c,s,a):s}return o}function r(e,t={}){return e.reduce((e,r)=>n(e,r,t),{})}function i(...t){if(t.length===0)return{};let n={};for(let r of t)for(let t of Object.keys(r))e.isUnsafeKey(t)||(n[t]=r[t]);return n}exports.deepMerge=r,exports.shallowMerge=i;
|
|
2
2
|
//# sourceMappingURL=merge.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"merge.cjs","names":[],"sources":["../../src/object/merge.ts"],"sourcesContent":["import type { Obj } from '../types';\n\nimport { isUnsafeKey } from '../_common/unsafePaths';\nimport { isPlainObject } from '../guards/isPlainObject';\n\nexport type DeepMergeOptions = {\n /** How to handle arrays when both source and target contain one. Default: `'replace'`. */\n arrayStrategy?: 'concat' | 'replace';\n};\n\ntype DeepMerge<T, U> = T extends Obj\n ? U extends Obj\n ? {\n [K in keyof T | keyof U]: K extends keyof T\n ? K extends keyof U\n ? DeepMerge<T[K], U[K]>\n : T[K]\n : K extends keyof U\n ? U[K]\n : never;\n }\n : U\n : U;\n\ntype Merge<T extends Obj[]> = T extends [infer First, ...infer Rest]\n ? First extends Obj\n ? Rest extends Obj[]\n ? DeepMerge<First, Merge<Rest>>\n : First\n : Obj\n : Obj;\n\nfunction mergeObjects<T extends Obj, U extends Obj>(target: T, source: U, options: DeepMergeOptions): DeepMerge<T, U> {\n if (!isPlainObject(source)) return source as DeepMerge<T, U>;\n\n const result = { ...target } as DeepMerge<T, U>;\n\n for (const key of Object.keys(source)) {\n // Guard against prototype pollution: skip dangerous keys\n if (isUnsafeKey(key)) {\n continue;\n }\n\n const sourceValue = source[key];\n const targetValue = result[key];\n\n (result as Record<string, unknown>)[key] =\n Array.isArray(sourceValue) && Array.isArray(targetValue)\n ? options.arrayStrategy === 'concat'\n ? [...targetValue, ...sourceValue]\n : sourceValue\n : isPlainObject(sourceValue) && isPlainObject(targetValue)\n ? mergeObjects(targetValue as Obj, sourceValue as Obj, options)\n : sourceValue;\n }\n\n return result;\n}\n\n/**\n * Deeply merges two or more objects. Arrays in source objects **replace** the target array by default.\n * Pass `{ arrayStrategy: 'concat' }` as the last argument to concatenate arrays instead.\n *\n * @example\n * ```ts\n * deepMerge({ a: 1, b: { x: 1 } }, { b: { y: 2 }, c: 3 });\n * // { a: 1, b: { x: 1, y: 2 }, c: 3 }\n *\n * deepMerge({ tags: ['a'] }, { tags: ['b'] });\n * // { tags: ['b'] } ← arrays replaced\n *\n * deepMerge({ tags: ['a'] }, { tags: ['b'] }, { arrayStrategy: 'concat' });\n * // { tags: ['a', 'b'] }\n * ```\n */\nexport function deepMerge<T extends Obj[]>(
|
|
1
|
+
{"version":3,"file":"merge.cjs","names":[],"sources":["../../src/object/merge.ts"],"sourcesContent":["import type { Obj } from '../types';\n\nimport { isUnsafeKey } from '../_common/unsafePaths';\nimport { isPlainObject } from '../guards/isPlainObject';\n\nexport type DeepMergeOptions = {\n /** How to handle arrays when both source and target contain one. Default: `'replace'`. */\n arrayStrategy?: 'concat' | 'replace';\n};\n\ntype DeepMerge<T, U> = T extends Obj\n ? U extends Obj\n ? {\n [K in keyof T | keyof U]: K extends keyof T\n ? K extends keyof U\n ? DeepMerge<T[K], U[K]>\n : T[K]\n : K extends keyof U\n ? U[K]\n : never;\n }\n : U\n : U;\n\ntype Merge<T extends Obj[]> = T extends [infer First, ...infer Rest]\n ? First extends Obj\n ? Rest extends Obj[]\n ? DeepMerge<First, Merge<Rest>>\n : First\n : Obj\n : Obj;\n\nfunction mergeObjects<T extends Obj, U extends Obj>(target: T, source: U, options: DeepMergeOptions): DeepMerge<T, U> {\n if (!isPlainObject(source)) return source as DeepMerge<T, U>;\n\n const result = { ...target } as DeepMerge<T, U>;\n\n for (const key of Object.keys(source)) {\n // Guard against prototype pollution: skip dangerous keys\n if (isUnsafeKey(key)) {\n continue;\n }\n\n const sourceValue = source[key];\n const targetValue = result[key];\n\n (result as Record<string, unknown>)[key] =\n Array.isArray(sourceValue) && Array.isArray(targetValue)\n ? options.arrayStrategy === 'concat'\n ? [...targetValue, ...sourceValue]\n : sourceValue\n : isPlainObject(sourceValue) && isPlainObject(targetValue)\n ? mergeObjects(targetValue as Obj, sourceValue as Obj, options)\n : sourceValue;\n }\n\n return result;\n}\n\n/**\n * Deeply merges two or more objects. Arrays in source objects **replace** the target array by default.\n * Pass `{ arrayStrategy: 'concat' }` as the last argument to concatenate arrays instead.\n *\n * @example\n * ```ts\n * deepMerge({ a: 1, b: { x: 1 } }, { b: { y: 2 }, c: 3 });\n * // { a: 1, b: { x: 1, y: 2 }, c: 3 }\n *\n * deepMerge({ tags: ['a'] }, { tags: ['b'] });\n * // { tags: ['b'] } ← arrays replaced\n *\n * deepMerge({ tags: ['a'] }, { tags: ['b'] }, { arrayStrategy: 'concat' });\n * // { tags: ['a', 'b'] }\n * ```\n */\nexport function deepMerge<T extends Obj[]>(items: readonly [...T], options: DeepMergeOptions = {}): Merge<T> {\n return items.reduce((acc: Obj, item) => mergeObjects(acc, item, options) as Obj, {} as Obj) as Merge<T>;\n}\n\n/**\n * Shallowly merges two or more objects. Later sources win for duplicate keys.\n * Dangerous keys (`__proto__`, `constructor`, `prototype`) are always skipped.\n *\n * @example\n * ```ts\n * shallowMerge({ a: 1, b: { x: 1 } }, { b: { y: 2 } });\n * // { a: 1, b: { y: 2 } } ← nested object replaced, not merged\n *\n * shallowMerge(defaults, overrides, extras);\n * ```\n */\nexport function shallowMerge<T extends Obj[]>(...items: [...T]): Merge<T> {\n if (items.length === 0) return {} as Merge<T>;\n\n const result: Obj = {};\n\n for (const item of items) {\n for (const key of Object.keys(item)) {\n if (isUnsafeKey(key)) continue;\n\n result[key] = item[key];\n }\n }\n\n return result as Merge<T>;\n}\n"],"mappings":"uFAgCA,SAAS,EAA2C,EAAW,EAAW,EAA4C,CACpH,GAAI,CAAC,EAAA,cAAc,CAAM,EAAG,OAAO,EAEnC,IAAM,EAAS,CAAE,GAAG,CAAO,EAE3B,IAAK,IAAM,KAAO,OAAO,KAAK,CAAM,EAAG,CAErC,GAAI,EAAA,YAAY,CAAG,EACjB,SAGF,IAAM,EAAc,EAAO,GACrB,EAAc,EAAO,GAE3B,EAAoC,GAClC,MAAM,QAAQ,CAAW,GAAK,MAAM,QAAQ,CAAW,EACnD,EAAQ,gBAAkB,SACxB,CAAC,GAAG,EAAa,GAAG,CAAW,EAC/B,EACF,EAAA,cAAc,CAAW,GAAK,EAAA,cAAc,CAAW,EACrD,EAAa,EAAoB,EAAoB,CAAO,EAC5D,CACV,CAEA,OAAO,CACT,CAkBA,SAAgB,EAA2B,EAAwB,EAA4B,CAAC,EAAa,CAC3G,OAAO,EAAM,QAAQ,EAAU,IAAS,EAAa,EAAK,EAAM,CAAO,EAAU,CAAC,CAAQ,CAC5F,CAcA,SAAgB,EAA8B,GAAG,EAAyB,CACxE,GAAI,EAAM,SAAW,EAAG,MAAO,CAAC,EAEhC,IAAM,EAAc,CAAC,EAErB,IAAK,IAAM,KAAQ,EACjB,IAAK,IAAM,KAAO,OAAO,KAAK,CAAI,EAC5B,EAAA,YAAY,CAAG,IAEnB,EAAO,GAAO,EAAK,IAIvB,OAAO,CACT"}
|
package/dist/object/merge.d.ts
CHANGED
|
@@ -23,7 +23,7 @@ type Merge<T extends Obj[]> = T extends [infer First, ...infer Rest] ? First ext
|
|
|
23
23
|
* // { tags: ['a', 'b'] }
|
|
24
24
|
* ```
|
|
25
25
|
*/
|
|
26
|
-
export declare function deepMerge<T extends Obj[]>(
|
|
26
|
+
export declare function deepMerge<T extends Obj[]>(items: readonly [...T], options?: DeepMergeOptions): Merge<T>;
|
|
27
27
|
/**
|
|
28
28
|
* Shallowly merges two or more objects. Later sources win for duplicate keys.
|
|
29
29
|
* Dangerous keys (`__proto__`, `constructor`, `prototype`) are always skipped.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"merge.d.ts","sourceRoot":"","sources":["../../src/object/merge.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,UAAU,CAAC;AAKpC,MAAM,MAAM,gBAAgB,GAAG;IAC7B,0FAA0F;IAC1F,aAAa,CAAC,EAAE,QAAQ,GAAG,SAAS,CAAC;CACtC,CAAC;AAEF,KAAK,SAAS,CAAC,CAAC,EAAE,CAAC,IAAI,CAAC,SAAS,GAAG,GAChC,CAAC,SAAS,GAAG,GACX;KACG,CAAC,IAAI,MAAM,CAAC,GAAG,MAAM,CAAC,GAAG,CAAC,SAAS,MAAM,CAAC,GACvC,CAAC,SAAS,MAAM,CAAC,GACf,SAAS,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,GACrB,CAAC,CAAC,CAAC,CAAC,GACN,CAAC,SAAS,MAAM,CAAC,GACf,CAAC,CAAC,CAAC,CAAC,GACJ,KAAK;CACZ,GACD,CAAC,GACH,CAAC,CAAC;AAEN,KAAK,KAAK,CAAC,CAAC,SAAS,GAAG,EAAE,IAAI,CAAC,SAAS,CAAC,MAAM,KAAK,EAAE,GAAG,MAAM,IAAI,CAAC,GAChE,KAAK,SAAS,GAAG,GACf,IAAI,SAAS,GAAG,EAAE,GAChB,SAAS,CAAC,KAAK,EAAE,KAAK,CAAC,IAAI,CAAC,CAAC,GAC7B,KAAK,GACP,GAAG,GACL,GAAG,CAAC;AA6BR;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,SAAS,CAAC,CAAC,SAAS,GAAG,EAAE,EAAE,
|
|
1
|
+
{"version":3,"file":"merge.d.ts","sourceRoot":"","sources":["../../src/object/merge.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,UAAU,CAAC;AAKpC,MAAM,MAAM,gBAAgB,GAAG;IAC7B,0FAA0F;IAC1F,aAAa,CAAC,EAAE,QAAQ,GAAG,SAAS,CAAC;CACtC,CAAC;AAEF,KAAK,SAAS,CAAC,CAAC,EAAE,CAAC,IAAI,CAAC,SAAS,GAAG,GAChC,CAAC,SAAS,GAAG,GACX;KACG,CAAC,IAAI,MAAM,CAAC,GAAG,MAAM,CAAC,GAAG,CAAC,SAAS,MAAM,CAAC,GACvC,CAAC,SAAS,MAAM,CAAC,GACf,SAAS,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,GACrB,CAAC,CAAC,CAAC,CAAC,GACN,CAAC,SAAS,MAAM,CAAC,GACf,CAAC,CAAC,CAAC,CAAC,GACJ,KAAK;CACZ,GACD,CAAC,GACH,CAAC,CAAC;AAEN,KAAK,KAAK,CAAC,CAAC,SAAS,GAAG,EAAE,IAAI,CAAC,SAAS,CAAC,MAAM,KAAK,EAAE,GAAG,MAAM,IAAI,CAAC,GAChE,KAAK,SAAS,GAAG,GACf,IAAI,SAAS,GAAG,EAAE,GAChB,SAAS,CAAC,KAAK,EAAE,KAAK,CAAC,IAAI,CAAC,CAAC,GAC7B,KAAK,GACP,GAAG,GACL,GAAG,CAAC;AA6BR;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,SAAS,CAAC,CAAC,SAAS,GAAG,EAAE,EAAE,KAAK,EAAE,SAAS,CAAC,GAAG,CAAC,CAAC,EAAE,OAAO,GAAE,gBAAqB,GAAG,KAAK,CAAC,CAAC,CAAC,CAE3G;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,YAAY,CAAC,CAAC,SAAS,GAAG,EAAE,EAAE,GAAG,KAAK,EAAE,CAAC,GAAG,CAAC,CAAC,GAAG,KAAK,CAAC,CAAC,CAAC,CAcxE"}
|
package/dist/object/merge.js
CHANGED
|
@@ -11,9 +11,8 @@ function n(r, i, a) {
|
|
|
11
11
|
}
|
|
12
12
|
return o;
|
|
13
13
|
}
|
|
14
|
-
function r(
|
|
15
|
-
|
|
16
|
-
return a.length === 0 ? {} : a.reduce((e, t) => n(e, t, i), {});
|
|
14
|
+
function r(e, t = {}) {
|
|
15
|
+
return e.reduce((e, r) => n(e, r, t), {});
|
|
17
16
|
}
|
|
18
17
|
function i(...t) {
|
|
19
18
|
if (t.length === 0) return {};
|
package/dist/object/merge.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"merge.js","names":[],"sources":["../../src/object/merge.ts"],"sourcesContent":["import type { Obj } from '../types';\n\nimport { isUnsafeKey } from '../_common/unsafePaths';\nimport { isPlainObject } from '../guards/isPlainObject';\n\nexport type DeepMergeOptions = {\n /** How to handle arrays when both source and target contain one. Default: `'replace'`. */\n arrayStrategy?: 'concat' | 'replace';\n};\n\ntype DeepMerge<T, U> = T extends Obj\n ? U extends Obj\n ? {\n [K in keyof T | keyof U]: K extends keyof T\n ? K extends keyof U\n ? DeepMerge<T[K], U[K]>\n : T[K]\n : K extends keyof U\n ? U[K]\n : never;\n }\n : U\n : U;\n\ntype Merge<T extends Obj[]> = T extends [infer First, ...infer Rest]\n ? First extends Obj\n ? Rest extends Obj[]\n ? DeepMerge<First, Merge<Rest>>\n : First\n : Obj\n : Obj;\n\nfunction mergeObjects<T extends Obj, U extends Obj>(target: T, source: U, options: DeepMergeOptions): DeepMerge<T, U> {\n if (!isPlainObject(source)) return source as DeepMerge<T, U>;\n\n const result = { ...target } as DeepMerge<T, U>;\n\n for (const key of Object.keys(source)) {\n // Guard against prototype pollution: skip dangerous keys\n if (isUnsafeKey(key)) {\n continue;\n }\n\n const sourceValue = source[key];\n const targetValue = result[key];\n\n (result as Record<string, unknown>)[key] =\n Array.isArray(sourceValue) && Array.isArray(targetValue)\n ? options.arrayStrategy === 'concat'\n ? [...targetValue, ...sourceValue]\n : sourceValue\n : isPlainObject(sourceValue) && isPlainObject(targetValue)\n ? mergeObjects(targetValue as Obj, sourceValue as Obj, options)\n : sourceValue;\n }\n\n return result;\n}\n\n/**\n * Deeply merges two or more objects. Arrays in source objects **replace** the target array by default.\n * Pass `{ arrayStrategy: 'concat' }` as the last argument to concatenate arrays instead.\n *\n * @example\n * ```ts\n * deepMerge({ a: 1, b: { x: 1 } }, { b: { y: 2 }, c: 3 });\n * // { a: 1, b: { x: 1, y: 2 }, c: 3 }\n *\n * deepMerge({ tags: ['a'] }, { tags: ['b'] });\n * // { tags: ['b'] } ← arrays replaced\n *\n * deepMerge({ tags: ['a'] }, { tags: ['b'] }, { arrayStrategy: 'concat' });\n * // { tags: ['a', 'b'] }\n * ```\n */\nexport function deepMerge<T extends Obj[]>(
|
|
1
|
+
{"version":3,"file":"merge.js","names":[],"sources":["../../src/object/merge.ts"],"sourcesContent":["import type { Obj } from '../types';\n\nimport { isUnsafeKey } from '../_common/unsafePaths';\nimport { isPlainObject } from '../guards/isPlainObject';\n\nexport type DeepMergeOptions = {\n /** How to handle arrays when both source and target contain one. Default: `'replace'`. */\n arrayStrategy?: 'concat' | 'replace';\n};\n\ntype DeepMerge<T, U> = T extends Obj\n ? U extends Obj\n ? {\n [K in keyof T | keyof U]: K extends keyof T\n ? K extends keyof U\n ? DeepMerge<T[K], U[K]>\n : T[K]\n : K extends keyof U\n ? U[K]\n : never;\n }\n : U\n : U;\n\ntype Merge<T extends Obj[]> = T extends [infer First, ...infer Rest]\n ? First extends Obj\n ? Rest extends Obj[]\n ? DeepMerge<First, Merge<Rest>>\n : First\n : Obj\n : Obj;\n\nfunction mergeObjects<T extends Obj, U extends Obj>(target: T, source: U, options: DeepMergeOptions): DeepMerge<T, U> {\n if (!isPlainObject(source)) return source as DeepMerge<T, U>;\n\n const result = { ...target } as DeepMerge<T, U>;\n\n for (const key of Object.keys(source)) {\n // Guard against prototype pollution: skip dangerous keys\n if (isUnsafeKey(key)) {\n continue;\n }\n\n const sourceValue = source[key];\n const targetValue = result[key];\n\n (result as Record<string, unknown>)[key] =\n Array.isArray(sourceValue) && Array.isArray(targetValue)\n ? options.arrayStrategy === 'concat'\n ? [...targetValue, ...sourceValue]\n : sourceValue\n : isPlainObject(sourceValue) && isPlainObject(targetValue)\n ? mergeObjects(targetValue as Obj, sourceValue as Obj, options)\n : sourceValue;\n }\n\n return result;\n}\n\n/**\n * Deeply merges two or more objects. Arrays in source objects **replace** the target array by default.\n * Pass `{ arrayStrategy: 'concat' }` as the last argument to concatenate arrays instead.\n *\n * @example\n * ```ts\n * deepMerge({ a: 1, b: { x: 1 } }, { b: { y: 2 }, c: 3 });\n * // { a: 1, b: { x: 1, y: 2 }, c: 3 }\n *\n * deepMerge({ tags: ['a'] }, { tags: ['b'] });\n * // { tags: ['b'] } ← arrays replaced\n *\n * deepMerge({ tags: ['a'] }, { tags: ['b'] }, { arrayStrategy: 'concat' });\n * // { tags: ['a', 'b'] }\n * ```\n */\nexport function deepMerge<T extends Obj[]>(items: readonly [...T], options: DeepMergeOptions = {}): Merge<T> {\n return items.reduce((acc: Obj, item) => mergeObjects(acc, item, options) as Obj, {} as Obj) as Merge<T>;\n}\n\n/**\n * Shallowly merges two or more objects. Later sources win for duplicate keys.\n * Dangerous keys (`__proto__`, `constructor`, `prototype`) are always skipped.\n *\n * @example\n * ```ts\n * shallowMerge({ a: 1, b: { x: 1 } }, { b: { y: 2 } });\n * // { a: 1, b: { y: 2 } } ← nested object replaced, not merged\n *\n * shallowMerge(defaults, overrides, extras);\n * ```\n */\nexport function shallowMerge<T extends Obj[]>(...items: [...T]): Merge<T> {\n if (items.length === 0) return {} as Merge<T>;\n\n const result: Obj = {};\n\n for (const item of items) {\n for (const key of Object.keys(item)) {\n if (isUnsafeKey(key)) continue;\n\n result[key] = item[key];\n }\n }\n\n return result as Merge<T>;\n}\n"],"mappings":";;;AAgCA,SAAS,EAA2C,GAAW,GAAW,GAA4C;CACpH,IAAI,CAAC,EAAc,CAAM,GAAG,OAAO;CAEnC,IAAM,IAAS,EAAE,GAAG,EAAO;CAE3B,KAAK,IAAM,KAAO,OAAO,KAAK,CAAM,GAAG;EAErC,IAAI,EAAY,CAAG,GACjB;EAGF,IAAM,IAAc,EAAO,IACrB,IAAc,EAAO;EAE3B,EAAoC,KAClC,MAAM,QAAQ,CAAW,KAAK,MAAM,QAAQ,CAAW,IACnD,EAAQ,kBAAkB,WACxB,CAAC,GAAG,GAAa,GAAG,CAAW,IAC/B,IACF,EAAc,CAAW,KAAK,EAAc,CAAW,IACrD,EAAa,GAAoB,GAAoB,CAAO,IAC5D;CACV;CAEA,OAAO;AACT;AAkBA,SAAgB,EAA2B,GAAwB,IAA4B,CAAC,GAAa;CAC3G,OAAO,EAAM,QAAQ,GAAU,MAAS,EAAa,GAAK,GAAM,CAAO,GAAU,CAAC,CAAQ;AAC5F;AAcA,SAAgB,EAA8B,GAAG,GAAyB;CACxE,IAAI,EAAM,WAAW,GAAG,OAAO,CAAC;CAEhC,IAAM,IAAc,CAAC;CAErB,KAAK,IAAM,KAAQ,GACjB,KAAK,IAAM,KAAO,OAAO,KAAK,CAAI,GAC5B,EAAY,CAAG,MAEnB,EAAO,KAAO,EAAK;CAIvB,OAAO;AACT"}
|
package/dist/object/prune.cjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
const e=require("../
|
|
1
|
+
const e=require("../_common/unsafePaths.cjs"),t=require("../array/filterMap.cjs"),n=require("../guards/isNil.cjs"),r=require("../guards/isPlainObject.cjs"),i=require("../guards/isEmpty.cjs");function a(o){if(!n.isNil(o)){if(typeof o==`string`){let e=o.trim();return e===``?void 0:e}if(Array.isArray(o)){let e=t.filterMap(o,e=>{let t=a(e);return i.isEmpty(t)?void 0:t});return e.length===0?void 0:e}if(r.isPlainObject(o)){let t={};for(let[n,r]of Object.entries(o)){if(e.isUnsafeKey(n))continue;let o=a(r);i.isEmpty(o)||(t[n]=o)}return Object.keys(t).length===0?void 0:t}return o}}exports.prune=a;
|
|
2
2
|
//# sourceMappingURL=prune.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"prune.cjs","names":[],"sources":["../../src/object/prune.ts"],"sourcesContent":["import { isUnsafeKey } from '../_common/unsafePaths';\nimport { filterMap } from '../array/filterMap';\nimport { isEmpty } from '../guards/isEmpty';\nimport { isNil } from '../guards/isNil';\nimport { isPlainObject } from '../guards/isPlainObject';\
|
|
1
|
+
{"version":3,"file":"prune.cjs","names":[],"sources":["../../src/object/prune.ts"],"sourcesContent":["import { isUnsafeKey } from '../_common/unsafePaths';\nimport { filterMap } from '../array/filterMap';\nimport { isEmpty } from '../guards/isEmpty';\nimport { isNil } from '../guards/isNil';\nimport { isPlainObject } from '../guards/isPlainObject';\n\n/**\n * Removes all nullable and empty values from strings, arrays, or objects.\n *\n * - For strings: trims whitespace and returns `undefined` if the result is empty.\n * - For arrays: recursively removes `null`, `undefined`, empty strings, and empty objects/arrays.\n * - For objects: recursively removes properties whose pruned value is empty. Dangerous keys\n * (`__proto__`, `constructor`, `prototype`) are always skipped.\n * - For any other type: returns the value unchanged.\n *\n * @example\n * ```ts\n * prune(' hello '); // 'hello'\n * prune(' '); // undefined\n * prune([1, null, '', 2, undefined, 3]); // [1, 2, 3]\n * prune({ a: 1, b: null, c: '', d: 2 }); // { a: 1, d: 2 }\n * prune({ a: { b: null, c: '' }, d: 1 }); // { d: 1 }\n * ```\n *\n * @param value - The value to prune.\n * @returns The pruned value, or `undefined` if the result would be empty.\n */\nexport function prune(value: string): string | undefined;\nexport function prune<T>(value: T[]): Array<NonNullable<T>> | undefined;\nexport function prune<T extends Record<string, unknown>>(value: T): Partial<T> | undefined;\nexport function prune<T>(value: T): T | undefined;\nexport function prune<T>(value: T): T | undefined {\n if (isNil(value)) return undefined;\n\n if (typeof value === 'string') {\n const trimmed = value.trim();\n\n return (trimmed === '' ? undefined : trimmed) as T | undefined;\n }\n\n if (Array.isArray(value)) {\n const cleaned = filterMap(value as unknown[], (item) => {\n const pruned = prune(item);\n\n return isEmpty(pruned) ? undefined : (pruned as unknown);\n });\n\n return (cleaned.length === 0 ? undefined : cleaned) as T | undefined;\n }\n\n if (isPlainObject(value)) {\n const cleaned: Record<string, unknown> = {};\n\n for (const [key, val] of Object.entries(value)) {\n if (isUnsafeKey(key)) continue;\n\n const cleanedValue = prune(val);\n\n if (!isEmpty(cleanedValue)) {\n cleaned[key] = cleanedValue;\n }\n }\n\n return (Object.keys(cleaned).length === 0 ? undefined : cleaned) as T | undefined;\n }\n\n return value;\n}\n"],"mappings":"+LA+BA,SAAgB,EAAS,EAAyB,CAC5C,MAAA,MAAM,CAAK,EAEf,IAAI,OAAO,GAAU,SAAU,CAC7B,IAAM,EAAU,EAAM,KAAK,EAE3B,OAAQ,IAAY,GAAK,IAAA,GAAY,CACvC,CAEA,GAAI,MAAM,QAAQ,CAAK,EAAG,CACxB,IAAM,EAAU,EAAA,UAAU,EAAqB,GAAS,CACtD,IAAM,EAAS,EAAM,CAAI,EAEzB,OAAO,EAAA,QAAQ,CAAM,EAAI,IAAA,GAAa,CACxC,CAAC,EAED,OAAQ,EAAQ,SAAW,EAAI,IAAA,GAAY,CAC7C,CAEA,GAAI,EAAA,cAAc,CAAK,EAAG,CACxB,IAAM,EAAmC,CAAC,EAE1C,IAAK,GAAM,CAAC,EAAK,KAAQ,OAAO,QAAQ,CAAK,EAAG,CAC9C,GAAI,EAAA,YAAY,CAAG,EAAG,SAEtB,IAAM,EAAe,EAAM,CAAG,EAEzB,EAAA,QAAQ,CAAY,IACvB,EAAQ,GAAO,EAEnB,CAEA,OAAQ,OAAO,KAAK,CAAO,CAAC,CAAC,SAAW,EAAI,IAAA,GAAY,CAC1D,CAEA,OAAO,CA5BP,CA6BF"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"prune.d.ts","sourceRoot":"","sources":["../../src/object/prune.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"prune.d.ts","sourceRoot":"","sources":["../../src/object/prune.ts"],"names":[],"mappings":"AAMA;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAgB,KAAK,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAAC;AACzD,wBAAgB,KAAK,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,EAAE,GAAG,KAAK,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC,GAAG,SAAS,CAAC;AACxE,wBAAgB,KAAK,CAAC,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,KAAK,EAAE,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,GAAG,SAAS,CAAC;AAC3F,wBAAgB,KAAK,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,GAAG,CAAC,GAAG,SAAS,CAAC"}
|
package/dist/object/prune.js
CHANGED
|
@@ -1,36 +1,35 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import { isEmpty as a } from "../guards/isEmpty.js";
|
|
1
|
+
import { isUnsafeKey as e } from "../_common/unsafePaths.js";
|
|
2
|
+
import { filterMap as t } from "../array/filterMap.js";
|
|
3
|
+
import { isNil as n } from "../guards/isNil.js";
|
|
4
|
+
import { isPlainObject as r } from "../guards/isPlainObject.js";
|
|
5
|
+
import { isEmpty as i } from "../guards/isEmpty.js";
|
|
7
6
|
//#region src/object/prune.ts
|
|
8
|
-
function o
|
|
9
|
-
if (!
|
|
10
|
-
if (
|
|
11
|
-
let e =
|
|
7
|
+
function a(o) {
|
|
8
|
+
if (!n(o)) {
|
|
9
|
+
if (typeof o == "string") {
|
|
10
|
+
let e = o.trim();
|
|
12
11
|
return e === "" ? void 0 : e;
|
|
13
12
|
}
|
|
14
|
-
if (Array.isArray(
|
|
15
|
-
let e =
|
|
16
|
-
let t =
|
|
17
|
-
return
|
|
13
|
+
if (Array.isArray(o)) {
|
|
14
|
+
let e = t(o, (e) => {
|
|
15
|
+
let t = a(e);
|
|
16
|
+
return i(t) ? void 0 : t;
|
|
18
17
|
});
|
|
19
18
|
return e.length === 0 ? void 0 : e;
|
|
20
19
|
}
|
|
21
|
-
if (
|
|
22
|
-
let
|
|
23
|
-
for (let [n, r] of Object.entries(
|
|
24
|
-
if (
|
|
25
|
-
let
|
|
26
|
-
|
|
20
|
+
if (r(o)) {
|
|
21
|
+
let t = {};
|
|
22
|
+
for (let [n, r] of Object.entries(o)) {
|
|
23
|
+
if (e(n)) continue;
|
|
24
|
+
let o = a(r);
|
|
25
|
+
i(o) || (t[n] = o);
|
|
27
26
|
}
|
|
28
|
-
return Object.keys(
|
|
27
|
+
return Object.keys(t).length === 0 ? void 0 : t;
|
|
29
28
|
}
|
|
30
|
-
return
|
|
29
|
+
return o;
|
|
31
30
|
}
|
|
32
31
|
}
|
|
33
32
|
//#endregion
|
|
34
|
-
export {
|
|
33
|
+
export { a as prune };
|
|
35
34
|
|
|
36
35
|
//# sourceMappingURL=prune.js.map
|
package/dist/object/prune.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"prune.js","names":[],"sources":["../../src/object/prune.ts"],"sourcesContent":["import { isUnsafeKey } from '../_common/unsafePaths';\nimport { filterMap } from '../array/filterMap';\nimport { isEmpty } from '../guards/isEmpty';\nimport { isNil } from '../guards/isNil';\nimport { isPlainObject } from '../guards/isPlainObject';\
|
|
1
|
+
{"version":3,"file":"prune.js","names":[],"sources":["../../src/object/prune.ts"],"sourcesContent":["import { isUnsafeKey } from '../_common/unsafePaths';\nimport { filterMap } from '../array/filterMap';\nimport { isEmpty } from '../guards/isEmpty';\nimport { isNil } from '../guards/isNil';\nimport { isPlainObject } from '../guards/isPlainObject';\n\n/**\n * Removes all nullable and empty values from strings, arrays, or objects.\n *\n * - For strings: trims whitespace and returns `undefined` if the result is empty.\n * - For arrays: recursively removes `null`, `undefined`, empty strings, and empty objects/arrays.\n * - For objects: recursively removes properties whose pruned value is empty. Dangerous keys\n * (`__proto__`, `constructor`, `prototype`) are always skipped.\n * - For any other type: returns the value unchanged.\n *\n * @example\n * ```ts\n * prune(' hello '); // 'hello'\n * prune(' '); // undefined\n * prune([1, null, '', 2, undefined, 3]); // [1, 2, 3]\n * prune({ a: 1, b: null, c: '', d: 2 }); // { a: 1, d: 2 }\n * prune({ a: { b: null, c: '' }, d: 1 }); // { d: 1 }\n * ```\n *\n * @param value - The value to prune.\n * @returns The pruned value, or `undefined` if the result would be empty.\n */\nexport function prune(value: string): string | undefined;\nexport function prune<T>(value: T[]): Array<NonNullable<T>> | undefined;\nexport function prune<T extends Record<string, unknown>>(value: T): Partial<T> | undefined;\nexport function prune<T>(value: T): T | undefined;\nexport function prune<T>(value: T): T | undefined {\n if (isNil(value)) return undefined;\n\n if (typeof value === 'string') {\n const trimmed = value.trim();\n\n return (trimmed === '' ? undefined : trimmed) as T | undefined;\n }\n\n if (Array.isArray(value)) {\n const cleaned = filterMap(value as unknown[], (item) => {\n const pruned = prune(item);\n\n return isEmpty(pruned) ? undefined : (pruned as unknown);\n });\n\n return (cleaned.length === 0 ? undefined : cleaned) as T | undefined;\n }\n\n if (isPlainObject(value)) {\n const cleaned: Record<string, unknown> = {};\n\n for (const [key, val] of Object.entries(value)) {\n if (isUnsafeKey(key)) continue;\n\n const cleanedValue = prune(val);\n\n if (!isEmpty(cleanedValue)) {\n cleaned[key] = cleanedValue;\n }\n }\n\n return (Object.keys(cleaned).length === 0 ? undefined : cleaned) as T | undefined;\n }\n\n return value;\n}\n"],"mappings":";;;;;;AA+BA,SAAgB,EAAS,GAAyB;CAC5C,OAAM,CAAK,GAEf;MAAI,OAAO,KAAU,UAAU;GAC7B,IAAM,IAAU,EAAM,KAAK;GAE3B,OAAQ,MAAY,KAAK,KAAA,IAAY;EACvC;EAEA,IAAI,MAAM,QAAQ,CAAK,GAAG;GACxB,IAAM,IAAU,EAAU,IAAqB,MAAS;IACtD,IAAM,IAAS,EAAM,CAAI;IAEzB,OAAO,EAAQ,CAAM,IAAI,KAAA,IAAa;GACxC,CAAC;GAED,OAAQ,EAAQ,WAAW,IAAI,KAAA,IAAY;EAC7C;EAEA,IAAI,EAAc,CAAK,GAAG;GACxB,IAAM,IAAmC,CAAC;GAE1C,KAAK,IAAM,CAAC,GAAK,MAAQ,OAAO,QAAQ,CAAK,GAAG;IAC9C,IAAI,EAAY,CAAG,GAAG;IAEtB,IAAM,IAAe,EAAM,CAAG;IAE9B,AAAK,EAAQ,CAAY,MACvB,EAAQ,KAAO;GAEnB;GAEA,OAAQ,OAAO,KAAK,CAAO,CAAC,CAAC,WAAW,IAAI,KAAA,IAAY;EAC1D;EAEA,OAAO;CA5BP;AA6BF"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tryParseJson.cjs","names":[],"sources":["../../src/object/tryParseJson.ts"],"sourcesContent":["export type JsonParseResult = { ok: true; value: unknown } | { error: SyntaxError; ok: false };\n\nexport function tryParseJson(text: string): JsonParseResult {\n try {\n return { ok: true, value: JSON.parse(text) as unknown };\n } catch (error) {\n return { error: error as SyntaxError, ok: false };\n }\n}\n"],"mappings":"AAEA,SAAgB,EAAa,EAA+B,CAC1D,GAAI,CACF,MAAO,CAAE,GAAI,GAAM,MAAO,KAAK,MAAM,CAAI,CAAa,CACxD,OAAS,EAAO,CACd,MAAO,CAAS,QAAsB,GAAI,EAAM,CAClD,CACF"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tryParseJson.d.ts","sourceRoot":"","sources":["../../src/object/tryParseJson.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,eAAe,GAAG;IAAE,EAAE,EAAE,IAAI,CAAC;IAAC,KAAK,EAAE,OAAO,CAAA;CAAE,GAAG;IAAE,KAAK,EAAE,WAAW,CAAC;IAAC,EAAE,EAAE,KAAK,CAAA;CAAE,CAAC;AAE/F,wBAAgB,YAAY,CAAC,IAAI,EAAE,MAAM,GAAG,eAAe,CAM1D"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
//#region src/object/tryParseJson.ts
|
|
2
|
+
function e(e) {
|
|
3
|
+
try {
|
|
4
|
+
return {
|
|
5
|
+
ok: !0,
|
|
6
|
+
value: JSON.parse(e)
|
|
7
|
+
};
|
|
8
|
+
} catch (e) {
|
|
9
|
+
return {
|
|
10
|
+
error: e,
|
|
11
|
+
ok: !1
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
//#endregion
|
|
16
|
+
export { e as tryParseJson };
|
|
17
|
+
|
|
18
|
+
//# sourceMappingURL=tryParseJson.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tryParseJson.js","names":[],"sources":["../../src/object/tryParseJson.ts"],"sourcesContent":["export type JsonParseResult = { ok: true; value: unknown } | { error: SyntaxError; ok: false };\n\nexport function tryParseJson(text: string): JsonParseResult {\n try {\n return { ok: true, value: JSON.parse(text) as unknown };\n } catch (error) {\n return { error: error as SyntaxError, ok: false };\n }\n}\n"],"mappings":";AAEA,SAAgB,EAAa,GAA+B;CAC1D,IAAI;EACF,OAAO;GAAE,IAAI;GAAM,OAAO,KAAK,MAAM,CAAI;EAAa;CACxD,SAAS,GAAO;EACd,OAAO;GAAS;GAAsB,IAAI;EAAM;CAClD;AACF"}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
function e(e,t){let n=t?.next()??crypto.getRandomValues(new Uint32Array(1))[0]/4294967296;if(!Number.isFinite(n)||n<0||n>=1)throw RangeError(`random source must return a finite number in [0, 1)`);return Math.floor(n*e)}exports.randomIndex=e;
|
|
2
|
+
//# sourceMappingURL=_index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"_index.cjs","names":[],"sources":["../../src/random/_index.ts"],"sourcesContent":["import type { RandomSource } from './source';\n\nexport function randomIndex(exclusiveMax: number, source?: RandomSource): number {\n const value = source?.next() ?? crypto.getRandomValues(new Uint32Array(1))[0]! / 0x100000000;\n\n if (!Number.isFinite(value) || value < 0 || value >= 1) {\n throw new RangeError('random source must return a finite number in [0, 1)');\n }\n\n return Math.floor(value * exclusiveMax);\n}\n"],"mappings":"AAEA,SAAgB,EAAY,EAAsB,EAA+B,CAC/E,IAAM,EAAQ,GAAQ,KAAK,GAAK,OAAO,gBAAgB,IAAI,YAAY,CAAC,CAAC,CAAC,CAAC,GAAM,WAEjF,GAAI,CAAC,OAAO,SAAS,CAAK,GAAK,EAAQ,GAAK,GAAS,EACnD,MAAU,WAAW,qDAAqD,EAG5E,OAAO,KAAK,MAAM,EAAQ,CAAY,CACxC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"_index.d.ts","sourceRoot":"","sources":["../../src/random/_index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,UAAU,CAAC;AAE7C,wBAAgB,WAAW,CAAC,YAAY,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,YAAY,GAAG,MAAM,CAQ/E"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
//#region src/random/_index.ts
|
|
2
|
+
function e(e, t) {
|
|
3
|
+
let n = t?.next() ?? crypto.getRandomValues(/* @__PURE__ */ new Uint32Array(1))[0] / 4294967296;
|
|
4
|
+
if (!Number.isFinite(n) || n < 0 || n >= 1) throw RangeError("random source must return a finite number in [0, 1)");
|
|
5
|
+
return Math.floor(n * e);
|
|
6
|
+
}
|
|
7
|
+
//#endregion
|
|
8
|
+
export { e as randomIndex };
|
|
9
|
+
|
|
10
|
+
//# sourceMappingURL=_index.js.map
|