@vielzeug/toolkit 1.0.12 → 1.0.14
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/dist/array/{sortBy.cjs → arrange.cjs} +2 -2
- package/dist/array/arrange.cjs.map +1 -0
- package/dist/array/arrange.js +6 -0
- package/dist/array/arrange.js.map +1 -0
- package/dist/array/chunk.cjs +1 -1
- package/dist/array/chunk.cjs.map +1 -1
- package/dist/array/chunk.js +15 -12
- package/dist/array/chunk.js.map +1 -1
- package/dist/array/group.cjs +1 -1
- package/dist/array/group.cjs.map +1 -1
- package/dist/array/group.js +9 -9
- package/dist/array/group.js.map +1 -1
- package/dist/array/list.cjs +1 -1
- package/dist/array/list.cjs.map +1 -1
- package/dist/array/list.js +87 -50
- package/dist/array/list.js.map +1 -1
- package/dist/array/pick.cjs.map +1 -1
- package/dist/array/pick.js.map +1 -1
- package/dist/array/remoteList.cjs +2 -0
- package/dist/array/remoteList.cjs.map +1 -0
- package/dist/array/remoteList.js +123 -0
- package/dist/array/remoteList.js.map +1 -0
- package/dist/date/expires.cjs.map +1 -1
- package/dist/date/expires.js.map +1 -1
- package/dist/date/interval.cjs.map +1 -1
- package/dist/date/interval.js.map +1 -1
- package/dist/date/timeDiff.cjs +1 -1
- package/dist/date/timeDiff.cjs.map +1 -1
- package/dist/date/timeDiff.js +10 -12
- package/dist/date/timeDiff.js.map +1 -1
- package/dist/function/curry.cjs +1 -1
- package/dist/function/curry.cjs.map +1 -1
- package/dist/function/curry.js +3 -3
- package/dist/function/curry.js.map +1 -1
- package/dist/function/debounce.cjs +1 -1
- package/dist/function/debounce.cjs.map +1 -1
- package/dist/function/debounce.js +22 -5
- package/dist/function/debounce.js.map +1 -1
- package/dist/function/memo.cjs +1 -1
- package/dist/function/memo.cjs.map +1 -1
- package/dist/function/memo.js +14 -13
- package/dist/function/memo.js.map +1 -1
- package/dist/function/parallel.cjs +2 -0
- package/dist/function/parallel.cjs.map +1 -0
- package/dist/function/parallel.js +28 -0
- package/dist/function/parallel.js.map +1 -0
- package/dist/function/proxy.cjs.map +1 -1
- package/dist/function/proxy.js.map +1 -1
- package/dist/function/prune.cjs +2 -0
- package/dist/function/prune.cjs.map +1 -0
- package/dist/function/prune.js +30 -0
- package/dist/function/prune.js.map +1 -0
- package/dist/function/retry.cjs +1 -1
- package/dist/function/retry.cjs.map +1 -1
- package/dist/function/retry.js +13 -13
- package/dist/function/retry.js.map +1 -1
- package/dist/function/sleep.cjs +1 -1
- package/dist/function/sleep.cjs.map +1 -1
- package/dist/function/sleep.js +8 -3
- package/dist/function/sleep.js.map +1 -1
- package/dist/function/throttle.cjs +1 -1
- package/dist/function/throttle.cjs.map +1 -1
- package/dist/function/throttle.js +33 -6
- package/dist/function/throttle.js.map +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.d.ts +455 -95
- package/dist/index.js +218 -194
- package/dist/index.js.map +1 -1
- package/dist/math/abs.cjs +2 -0
- package/dist/math/abs.cjs.map +1 -0
- package/dist/math/abs.js +7 -0
- package/dist/math/abs.js.map +1 -0
- package/dist/math/add.cjs +2 -0
- package/dist/math/add.cjs.map +1 -0
- package/dist/math/add.js +9 -0
- package/dist/math/add.js.map +1 -0
- package/dist/math/allocate.cjs +2 -0
- package/dist/math/allocate.cjs.map +1 -0
- package/dist/math/allocate.js +29 -0
- package/dist/math/allocate.js.map +1 -0
- package/dist/math/average.cjs +1 -1
- package/dist/math/average.cjs.map +1 -1
- package/dist/math/average.js +18 -12
- package/dist/math/average.js.map +1 -1
- package/dist/math/distribute.cjs +2 -0
- package/dist/math/distribute.cjs.map +1 -0
- package/dist/math/distribute.js +18 -0
- package/dist/math/distribute.js.map +1 -0
- package/dist/math/divide.cjs +2 -0
- package/dist/math/divide.cjs.map +1 -0
- package/dist/math/divide.js +11 -0
- package/dist/math/divide.js.map +1 -0
- package/dist/math/median.cjs.map +1 -1
- package/dist/math/median.js.map +1 -1
- package/dist/math/multiply.cjs +2 -0
- package/dist/math/multiply.cjs.map +1 -0
- package/dist/math/multiply.js +9 -0
- package/dist/math/multiply.js.map +1 -0
- package/dist/math/range.cjs +1 -1
- package/dist/math/range.cjs.map +1 -1
- package/dist/math/range.js +14 -6
- package/dist/math/range.js.map +1 -1
- package/dist/math/subtract.cjs +2 -0
- package/dist/math/subtract.cjs.map +1 -0
- package/dist/math/subtract.js +9 -0
- package/dist/math/subtract.js.map +1 -0
- package/dist/math/sum.cjs +1 -1
- package/dist/math/sum.cjs.map +1 -1
- package/dist/math/sum.js +11 -18
- package/dist/math/sum.js.map +1 -1
- package/dist/money/currency.cjs +2 -0
- package/dist/money/currency.cjs.map +1 -0
- package/dist/money/currency.js +49 -0
- package/dist/money/currency.js.map +1 -0
- package/dist/money/exchange.cjs +2 -0
- package/dist/money/exchange.cjs.map +1 -0
- package/dist/money/exchange.js +13 -0
- package/dist/money/exchange.js.map +1 -0
- package/dist/object/clone.cjs +1 -1
- package/dist/object/clone.cjs.map +1 -1
- package/dist/object/clone.js +13 -3
- package/dist/object/clone.js.map +1 -1
- package/dist/object/merge.cjs +1 -1
- package/dist/object/merge.cjs.map +1 -1
- package/dist/object/merge.js +11 -15
- package/dist/object/merge.js.map +1 -1
- package/dist/object/parseJSON.cjs.map +1 -1
- package/dist/object/parseJSON.js.map +1 -1
- package/dist/object/path.cjs.map +1 -1
- package/dist/object/path.js.map +1 -1
- package/dist/object/seek.cjs +1 -1
- package/dist/object/seek.cjs.map +1 -1
- package/dist/object/seek.js +8 -6
- package/dist/object/seek.js.map +1 -1
- package/dist/random/uuid.cjs +1 -1
- package/dist/random/uuid.cjs.map +1 -1
- package/dist/random/uuid.js +6 -3
- package/dist/random/uuid.js.map +1 -1
- package/dist/string/camelCase.cjs +1 -1
- package/dist/string/camelCase.cjs.map +1 -1
- package/dist/string/camelCase.js +3 -3
- package/dist/string/camelCase.js.map +1 -1
- package/dist/string/similarity.cjs +1 -1
- package/dist/string/similarity.cjs.map +1 -1
- package/dist/string/similarity.js +24 -21
- 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.js +14 -5
- package/dist/string/truncate.js.map +1 -1
- package/dist/typed/is.cjs +1 -1
- package/dist/typed/is.cjs.map +1 -1
- package/dist/typed/is.js +30 -31
- package/dist/typed/is.js.map +1 -1
- package/dist/typed/isEmpty.cjs +1 -1
- package/dist/typed/isEmpty.cjs.map +1 -1
- package/dist/typed/isEmpty.js +6 -6
- package/dist/typed/isEmpty.js.map +1 -1
- package/dist/typed/isEqual.cjs +1 -1
- package/dist/typed/isEqual.cjs.map +1 -1
- package/dist/typed/isEqual.js +22 -17
- package/dist/typed/isEqual.js.map +1 -1
- package/package.json +1 -1
- package/dist/array/sortBy.cjs.map +0 -1
- package/dist/array/sortBy.js +0 -6
- package/dist/array/sortBy.js.map +0 -1
package/dist/math/average.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"average.js","sources":["../../src/math/average.ts"],"sourcesContent":["import { sum } from './sum';\n\n/**\n * Calculates the average of an array of numbers or dates.\n *\n * @example\n * ```ts\n * const arr = [1, 2, 3, 4, 5];\n * average(arr); // 3\n * average(arr, (num) => num * 2); // 6\n * average(arr, (num) => new Date(Date.now() + 1000 * 60 * 60 * 24 * num); // Date object representing 3 days from now\n * ```\n *\n * @param array - The array of numbers or dates to average.\n * @param callback - (optional) A callback function to map the values.\n * @returns The average of the numbers or dates in the array.\n */\n\nexport function average<T>(array: T[], callback?: (item: T) => number | Date): number | Date | undefined {\n if (array.length === 0) return undefined;\n
|
|
1
|
+
{"version":3,"file":"average.js","sources":["../../src/math/average.ts"],"sourcesContent":["import { sum } from './sum';\n\n/**\n * Calculates the average of an array of numbers or dates.\n *\n * @example\n * ```ts\n * const arr = [1, 2, 3, 4, 5];\n * average(arr); // 3\n * average(arr, (num) => num * 2); // 6\n * average(arr, (num) => new Date(Date.now() + 1000 * 60 * 60 * 24 * num); // Date object representing 3 days from now\n * ```\n *\n * @param array - The array of numbers or dates to average.\n * @param callback - (optional) A callback function to map the values.\n * @returns The average of the numbers or dates in the array.\n */\n\nexport function average<T>(array: T[], callback?: (item: T) => number | Date): number | Date | undefined {\n if (array.length === 0) return undefined;\n\n // Check if we are dealing with Dates (either directly or via callback)\n const firstItem = callback ? callback(array[0]) : array[0];\n const isDate = firstItem instanceof Date;\n\n if (isDate) {\n const totalTimestamp = array.reduce<number>((acc, item) => {\n const val = callback ? callback(item) : item;\n if (!(val instanceof Date)) {\n throw new TypeError('average expected all items to be Date objects');\n }\n return acc + val.getTime();\n }, 0);\n return new Date(totalTimestamp / array.length);\n }\n\n // Handle numbers\n try {\n const total = sum(array, callback as (item: T) => number);\n if (typeof total === 'number') {\n return total / array.length;\n }\n } catch (err) {\n if (err instanceof TypeError) return undefined;\n throw err;\n }\n\n return undefined;\n}\n"],"names":["average","array","callback","totalTimestamp","acc","item","val","total","sum","err"],"mappings":";AAkBO,SAASA,EAAWC,GAAYC,GAAkE;AACvG,MAAID,EAAM,WAAW,EAAG;AAMxB,OAHkBC,IAAWA,EAASD,EAAM,CAAC,CAAC,IAAIA,EAAM,CAAC,cACrB,MAExB;AACV,UAAME,IAAiBF,EAAM,OAAe,CAACG,GAAKC,MAAS;AACzD,YAAMC,IAAMJ,IAAWA,EAASG,CAAI,IAAIA;AACxC,UAAI,EAAEC,aAAe;AACnB,cAAM,IAAI,UAAU,+CAA+C;AAErE,aAAOF,IAAME,EAAI,QAAA;AAAA,IACnB,GAAG,CAAC;AACJ,WAAO,IAAI,KAAKH,IAAiBF,EAAM,MAAM;AAAA,EAC/C;AAGA,MAAI;AACF,UAAMM,IAAQC,EAAIP,GAAOC,CAA+B;AACxD,QAAI,OAAOK,KAAU;AACnB,aAAOA,IAAQN,EAAM;AAAA,EAEzB,SAASQ,GAAK;AACZ,QAAIA,aAAe,UAAW;AAC9B,UAAMA;AAAA,EACR;AAGF;"}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});function c(r,e){if(e<1)throw new Error("Parts must be at least 1");if(typeof r=="bigint"){const t=r/BigInt(e),b=r%BigInt(e),o=[];for(let n=0;n<e;n++)o.push(n<Number(b)?t+1n:t);return o}const i=Math.floor(r/e),u=r-i*e,s=[];for(let t=0;t<e;t++)s.push(t<u?i+1:i);return s}exports.distribute=c;
|
|
2
|
+
//# sourceMappingURL=distribute.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"distribute.cjs","sources":["../../src/math/distribute.ts"],"sourcesContent":["/**\n * Distributes an amount evenly among N parties.\n * Handles rounding to ensure the sum equals the original amount exactly.\n * Useful for splitting bills, costs, or payments equally.\n *\n * @example\n * ```ts\n * // Split $100 among 3 people\n * distribute(100, 3);\n * // [34, 33, 33] - sum is exactly 100\n *\n * // Split with bigint (e.g., cents)\n * distribute(10000n, 3);\n * // [3334n, 3333n, 3333n] - sum is exactly 10000n\n * ```\n *\n * @param amount - Total amount to distribute\n * @param parts - Number of parts to divide into\n * @returns Array of distributed amounts (a sum equals original amount)\n * @throws {Error} If parts are less than 1\n */\nexport function distribute(amount: number, parts: number): number[];\nexport function distribute(amount: bigint, parts: number): bigint[];\nexport function distribute(amount: number | bigint, parts: number): (number | bigint)[] {\n if (parts < 1) {\n throw new Error('Parts must be at least 1');\n }\n\n if (typeof amount === 'bigint') {\n const baseShare = amount / BigInt(parts);\n const remainder = amount % BigInt(parts);\n const results: bigint[] = [];\n\n for (let i = 0; i < parts; i++) {\n // Distribute the remainder to first items\n results.push(i < Number(remainder) ? baseShare + 1n : baseShare);\n }\n\n return results;\n }\n\n // Handle number type\n const baseShare = Math.floor(amount / parts);\n const remainder = amount - baseShare * parts;\n const results: number[] = [];\n\n for (let i = 0; i < parts; i++) {\n // Distribute the remainder to first items\n results.push(i < remainder ? baseShare + 1 : baseShare);\n }\n\n return results;\n}\n"],"names":["distribute","amount","parts","baseShare","remainder","results","i"],"mappings":"gFAuBO,SAASA,EAAWC,EAAyBC,EAAoC,CACtF,GAAIA,EAAQ,EACV,MAAM,IAAI,MAAM,0BAA0B,EAG5C,GAAI,OAAOD,GAAW,SAAU,CAC9B,MAAME,EAAYF,EAAS,OAAOC,CAAK,EACjCE,EAAYH,EAAS,OAAOC,CAAK,EACjCG,EAAoB,CAAA,EAE1B,QAASC,EAAI,EAAGA,EAAIJ,EAAOI,IAEzBD,EAAQ,KAAKC,EAAI,OAAOF,CAAS,EAAID,EAAY,GAAKA,CAAS,EAGjE,OAAOE,CACT,CAGA,MAAMF,EAAY,KAAK,MAAMF,EAASC,CAAK,EACrCE,EAAYH,EAASE,EAAYD,EACjCG,EAAoB,CAAA,EAE1B,QAASC,EAAI,EAAGA,EAAIJ,EAAOI,IAEzBD,EAAQ,KAAKC,EAAIF,EAAYD,EAAY,EAAIA,CAAS,EAGxD,OAAOE,CACT"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
function b(t, e) {
|
|
2
|
+
if (e < 1)
|
|
3
|
+
throw new Error("Parts must be at least 1");
|
|
4
|
+
if (typeof t == "bigint") {
|
|
5
|
+
const r = t / BigInt(e), f = t % BigInt(e), o = [];
|
|
6
|
+
for (let n = 0; n < e; n++)
|
|
7
|
+
o.push(n < Number(f) ? r + 1n : r);
|
|
8
|
+
return o;
|
|
9
|
+
}
|
|
10
|
+
const i = Math.floor(t / e), c = t - i * e, s = [];
|
|
11
|
+
for (let r = 0; r < e; r++)
|
|
12
|
+
s.push(r < c ? i + 1 : i);
|
|
13
|
+
return s;
|
|
14
|
+
}
|
|
15
|
+
export {
|
|
16
|
+
b as distribute
|
|
17
|
+
};
|
|
18
|
+
//# sourceMappingURL=distribute.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"distribute.js","sources":["../../src/math/distribute.ts"],"sourcesContent":["/**\n * Distributes an amount evenly among N parties.\n * Handles rounding to ensure the sum equals the original amount exactly.\n * Useful for splitting bills, costs, or payments equally.\n *\n * @example\n * ```ts\n * // Split $100 among 3 people\n * distribute(100, 3);\n * // [34, 33, 33] - sum is exactly 100\n *\n * // Split with bigint (e.g., cents)\n * distribute(10000n, 3);\n * // [3334n, 3333n, 3333n] - sum is exactly 10000n\n * ```\n *\n * @param amount - Total amount to distribute\n * @param parts - Number of parts to divide into\n * @returns Array of distributed amounts (a sum equals original amount)\n * @throws {Error} If parts are less than 1\n */\nexport function distribute(amount: number, parts: number): number[];\nexport function distribute(amount: bigint, parts: number): bigint[];\nexport function distribute(amount: number | bigint, parts: number): (number | bigint)[] {\n if (parts < 1) {\n throw new Error('Parts must be at least 1');\n }\n\n if (typeof amount === 'bigint') {\n const baseShare = amount / BigInt(parts);\n const remainder = amount % BigInt(parts);\n const results: bigint[] = [];\n\n for (let i = 0; i < parts; i++) {\n // Distribute the remainder to first items\n results.push(i < Number(remainder) ? baseShare + 1n : baseShare);\n }\n\n return results;\n }\n\n // Handle number type\n const baseShare = Math.floor(amount / parts);\n const remainder = amount - baseShare * parts;\n const results: number[] = [];\n\n for (let i = 0; i < parts; i++) {\n // Distribute the remainder to first items\n results.push(i < remainder ? baseShare + 1 : baseShare);\n }\n\n return results;\n}\n"],"names":["distribute","amount","parts","baseShare","remainder","results","i"],"mappings":"AAuBO,SAASA,EAAWC,GAAyBC,GAAoC;AACtF,MAAIA,IAAQ;AACV,UAAM,IAAI,MAAM,0BAA0B;AAG5C,MAAI,OAAOD,KAAW,UAAU;AAC9B,UAAME,IAAYF,IAAS,OAAOC,CAAK,GACjCE,IAAYH,IAAS,OAAOC,CAAK,GACjCG,IAAoB,CAAA;AAE1B,aAASC,IAAI,GAAGA,IAAIJ,GAAOI;AAEzBD,MAAAA,EAAQ,KAAKC,IAAI,OAAOF,CAAS,IAAID,IAAY,KAAKA,CAAS;AAGjE,WAAOE;AAAAA,EACT;AAGA,QAAMF,IAAY,KAAK,MAAMF,IAASC,CAAK,GACrCE,IAAYH,IAASE,IAAYD,GACjCG,IAAoB,CAAA;AAE1B,WAASC,IAAI,GAAGA,IAAIJ,GAAOI;AAEzB,IAAAD,EAAQ,KAAKC,IAAIF,IAAYD,IAAY,IAAIA,CAAS;AAGxD,SAAOE;AACT;"}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});function r(t,e){if(e===0||e===0n)throw new Error("Division by zero");if(typeof t=="bigint"&&typeof e=="bigint"||typeof t=="number"&&typeof e=="number")return t/e;throw new TypeError("Both arguments must be of the same type (number or bigint)")}exports.divide=r;
|
|
2
|
+
//# sourceMappingURL=divide.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"divide.cjs","sources":["../../src/math/divide.ts"],"sourcesContent":["/**\n * Divides a number by a divisor with precision handling for financial calculations.\n * Supports both regular numbers and bigint for exact precision.\n *\n * @example\n * ```ts\n * divide(20, 5); // 4\n * divide(0.6, 3); // 0.2 (precision-safe)\n * divide(500n, 5n); // 100n\n * ```\n *\n * @param a - Number to divide (dividend)\n * @param b - Divisor\n * @returns Quotient of a divided by b\n * @throws {Error} If divisor is zero\n */\nexport function divide(a: number, b: number): number;\nexport function divide(a: bigint, b: bigint): bigint;\nexport function divide(a: number | bigint, b: number | bigint): number | bigint {\n if (b === 0 || b === 0n) {\n throw new Error('Division by zero');\n }\n\n if (typeof a === 'bigint' && typeof b === 'bigint') {\n return a / b;\n }\n if (typeof a === 'number' && typeof b === 'number') {\n return a / b;\n }\n throw new TypeError('Both arguments must be of the same type (number or bigint)');\n}\n"],"names":["divide","a","b"],"mappings":"gFAkBO,SAASA,EAAOC,EAAoBC,EAAqC,CAC9E,GAAIA,IAAM,GAAKA,IAAM,GACnB,MAAM,IAAI,MAAM,kBAAkB,EAMpC,GAHI,OAAOD,GAAM,UAAY,OAAOC,GAAM,UAGtC,OAAOD,GAAM,UAAY,OAAOC,GAAM,SACxC,OAAOD,EAAIC,EAEb,MAAM,IAAI,UAAU,4DAA4D,CAClF"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
function t(r, e) {
|
|
2
|
+
if (e === 0 || e === 0n)
|
|
3
|
+
throw new Error("Division by zero");
|
|
4
|
+
if (typeof r == "bigint" && typeof e == "bigint" || typeof r == "number" && typeof e == "number")
|
|
5
|
+
return r / e;
|
|
6
|
+
throw new TypeError("Both arguments must be of the same type (number or bigint)");
|
|
7
|
+
}
|
|
8
|
+
export {
|
|
9
|
+
t as divide
|
|
10
|
+
};
|
|
11
|
+
//# sourceMappingURL=divide.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"divide.js","sources":["../../src/math/divide.ts"],"sourcesContent":["/**\n * Divides a number by a divisor with precision handling for financial calculations.\n * Supports both regular numbers and bigint for exact precision.\n *\n * @example\n * ```ts\n * divide(20, 5); // 4\n * divide(0.6, 3); // 0.2 (precision-safe)\n * divide(500n, 5n); // 100n\n * ```\n *\n * @param a - Number to divide (dividend)\n * @param b - Divisor\n * @returns Quotient of a divided by b\n * @throws {Error} If divisor is zero\n */\nexport function divide(a: number, b: number): number;\nexport function divide(a: bigint, b: bigint): bigint;\nexport function divide(a: number | bigint, b: number | bigint): number | bigint {\n if (b === 0 || b === 0n) {\n throw new Error('Division by zero');\n }\n\n if (typeof a === 'bigint' && typeof b === 'bigint') {\n return a / b;\n }\n if (typeof a === 'number' && typeof b === 'number') {\n return a / b;\n }\n throw new TypeError('Both arguments must be of the same type (number or bigint)');\n}\n"],"names":["divide","a","b"],"mappings":"AAkBO,SAASA,EAAOC,GAAoBC,GAAqC;AAC9E,MAAIA,MAAM,KAAKA,MAAM;AACnB,UAAM,IAAI,MAAM,kBAAkB;AAMpC,MAHI,OAAOD,KAAM,YAAY,OAAOC,KAAM,YAGtC,OAAOD,KAAM,YAAY,OAAOC,KAAM;AACxC,WAAOD,IAAIC;AAEb,QAAM,IAAI,UAAU,4DAA4D;AAClF;"}
|
package/dist/math/median.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"median.cjs","sources":["../../src/math/median.ts"],"sourcesContent":["/**\n * Returns the median of an array of numbers.\n *\n * @example\n * ```ts\n * median([1, 2, 3, 4, 100]); // 3\n * median([1, 2, 3, 4, 5], (n) => n * 2); // 6\n * median([]); // undefined\n * median([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); // 5.5\n * median([new Date(2020-01-01), new Date(2020-01-02), new Date(2020-01-31)]) // 2020-01-02\n * ```\n *\n * @param arr - The array of numbers.\n * @param callback - (optional) A callback function to map the numbers.\n * @returns The median of the numbers.\n */\nexport function median<T>(arr: T[], callback?: (item: T) => number | Date): number | Date | undefined {\n if (arr.length === 0) return undefined;\n\n const values = callback ? arr.map(callback) : (arr as unknown as (number | Date)[]);\n const allDates = values.every((v) => v instanceof Date);\n const numericValues = values.map((v) => (v instanceof Date ? v.getTime() : (v as number)));\n\n numericValues.sort((a, b) => a - b);\n\n const mid = Math.floor(numericValues.length / 2);\n const medianValue =\n numericValues.length % 2 === 0 ? (numericValues[mid - 1] + numericValues[mid]) / 2 : numericValues[mid];\n\n return allDates ? new Date(medianValue) : medianValue;\n}\n"],"names":["median","arr","callback","values","allDates","v","numericValues","a","b","mid","medianValue"],"mappings":"gFAgBO,SAASA,EAAUC,EAAUC,EAAkE,CACpG,GAAID,EAAI,SAAW,EAAG,OAEtB,MAAME,EAASD,EAAWD,EAAI,IAAIC,CAAQ,EAAKD,EACzCG,EAAWD,EAAO,MAAOE,GAAMA,aAAa,IAAI,EAChDC,EAAgBH,EAAO,IAAKE,GAAOA,aAAa,KAAOA,EAAE,QAAA,EAAaA,CAAa,EAEzFC,EAAc,KAAK,CAACC,EAAGC,IAAMD,EAAIC,CAAC,EAElC,MAAMC,EAAM,KAAK,MAAMH,EAAc,OAAS,CAAC,EACzCI,EACJJ,EAAc,OAAS,IAAM,GAAKA,EAAcG,EAAM,CAAC,EAAIH,EAAcG,CAAG,GAAK,EAAIH,EAAcG,CAAG,EAExG,
|
|
1
|
+
{"version":3,"file":"median.cjs","sources":["../../src/math/median.ts"],"sourcesContent":["/**\n * Returns the median of an array of numbers.\n *\n * @example\n * ```ts\n * median([1, 2, 3, 4, 100]); // 3\n * median([1, 2, 3, 4, 5], (n) => n * 2); // 6\n * median([]); // undefined\n * median([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); // 5.5\n * median([new Date(2020-01-01), new Date(2020-01-02), new Date(2020-01-31)]) // 2020-01-02\n * ```\n *\n * @param arr - The array of numbers.\n * @param callback - (optional) A callback function to map the numbers.\n * @returns The median of the numbers.\n */\nexport function median<T>(arr: T[], callback?: (item: T) => number | Date): number | Date | undefined {\n if (arr.length === 0) return undefined;\n\n const values = callback ? arr.map(callback) : (arr as unknown as (number | Date)[]);\n const allDates = values.every((v) => v instanceof Date);\n const numericValues = values.map((v) => (v instanceof Date ? v.getTime() : (v as number)));\n\n numericValues.sort((a, b) => a - b);\n\n const mid = Math.floor(numericValues.length / 2);\n const medianValue =\n numericValues.length % 2 === 0 ? (numericValues[mid - 1] + numericValues[mid]) / 2 : numericValues[mid];\n\n return (allDates ? new Date(medianValue) : medianValue) as unknown as number | Date;\n}\n"],"names":["median","arr","callback","values","allDates","v","numericValues","a","b","mid","medianValue"],"mappings":"gFAgBO,SAASA,EAAUC,EAAUC,EAAkE,CACpG,GAAID,EAAI,SAAW,EAAG,OAEtB,MAAME,EAASD,EAAWD,EAAI,IAAIC,CAAQ,EAAKD,EACzCG,EAAWD,EAAO,MAAOE,GAAMA,aAAa,IAAI,EAChDC,EAAgBH,EAAO,IAAKE,GAAOA,aAAa,KAAOA,EAAE,QAAA,EAAaA,CAAa,EAEzFC,EAAc,KAAK,CAACC,EAAGC,IAAMD,EAAIC,CAAC,EAElC,MAAMC,EAAM,KAAK,MAAMH,EAAc,OAAS,CAAC,EACzCI,EACJJ,EAAc,OAAS,IAAM,GAAKA,EAAcG,EAAM,CAAC,EAAIH,EAAcG,CAAG,GAAK,EAAIH,EAAcG,CAAG,EAExG,OAAQL,EAAW,IAAI,KAAKM,CAAW,EAAIA,CAC7C"}
|
package/dist/math/median.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"median.js","sources":["../../src/math/median.ts"],"sourcesContent":["/**\n * Returns the median of an array of numbers.\n *\n * @example\n * ```ts\n * median([1, 2, 3, 4, 100]); // 3\n * median([1, 2, 3, 4, 5], (n) => n * 2); // 6\n * median([]); // undefined\n * median([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); // 5.5\n * median([new Date(2020-01-01), new Date(2020-01-02), new Date(2020-01-31)]) // 2020-01-02\n * ```\n *\n * @param arr - The array of numbers.\n * @param callback - (optional) A callback function to map the numbers.\n * @returns The median of the numbers.\n */\nexport function median<T>(arr: T[], callback?: (item: T) => number | Date): number | Date | undefined {\n if (arr.length === 0) return undefined;\n\n const values = callback ? arr.map(callback) : (arr as unknown as (number | Date)[]);\n const allDates = values.every((v) => v instanceof Date);\n const numericValues = values.map((v) => (v instanceof Date ? v.getTime() : (v as number)));\n\n numericValues.sort((a, b) => a - b);\n\n const mid = Math.floor(numericValues.length / 2);\n const medianValue =\n numericValues.length % 2 === 0 ? (numericValues[mid - 1] + numericValues[mid]) / 2 : numericValues[mid];\n\n return allDates ? new Date(medianValue) : medianValue;\n}\n"],"names":["median","arr","callback","values","allDates","v","numericValues","a","b","mid","medianValue"],"mappings":"AAgBO,SAASA,EAAUC,GAAUC,GAAkE;AACpG,MAAID,EAAI,WAAW,EAAG;AAEtB,QAAME,IAASD,IAAWD,EAAI,IAAIC,CAAQ,IAAKD,GACzCG,IAAWD,EAAO,MAAM,CAACE,MAAMA,aAAa,IAAI,GAChDC,IAAgBH,EAAO,IAAI,CAACE,MAAOA,aAAa,OAAOA,EAAE,QAAA,IAAaA,CAAa;AAEzF,EAAAC,EAAc,KAAK,CAACC,GAAGC,MAAMD,IAAIC,CAAC;AAElC,QAAMC,IAAM,KAAK,MAAMH,EAAc,SAAS,CAAC,GACzCI,IACJJ,EAAc,SAAS,MAAM,KAAKA,EAAcG,IAAM,CAAC,IAAIH,EAAcG,CAAG,KAAK,IAAIH,EAAcG,CAAG;AAExG,
|
|
1
|
+
{"version":3,"file":"median.js","sources":["../../src/math/median.ts"],"sourcesContent":["/**\n * Returns the median of an array of numbers.\n *\n * @example\n * ```ts\n * median([1, 2, 3, 4, 100]); // 3\n * median([1, 2, 3, 4, 5], (n) => n * 2); // 6\n * median([]); // undefined\n * median([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); // 5.5\n * median([new Date(2020-01-01), new Date(2020-01-02), new Date(2020-01-31)]) // 2020-01-02\n * ```\n *\n * @param arr - The array of numbers.\n * @param callback - (optional) A callback function to map the numbers.\n * @returns The median of the numbers.\n */\nexport function median<T>(arr: T[], callback?: (item: T) => number | Date): number | Date | undefined {\n if (arr.length === 0) return undefined;\n\n const values = callback ? arr.map(callback) : (arr as unknown as (number | Date)[]);\n const allDates = values.every((v) => v instanceof Date);\n const numericValues = values.map((v) => (v instanceof Date ? v.getTime() : (v as number)));\n\n numericValues.sort((a, b) => a - b);\n\n const mid = Math.floor(numericValues.length / 2);\n const medianValue =\n numericValues.length % 2 === 0 ? (numericValues[mid - 1] + numericValues[mid]) / 2 : numericValues[mid];\n\n return (allDates ? new Date(medianValue) : medianValue) as unknown as number | Date;\n}\n"],"names":["median","arr","callback","values","allDates","v","numericValues","a","b","mid","medianValue"],"mappings":"AAgBO,SAASA,EAAUC,GAAUC,GAAkE;AACpG,MAAID,EAAI,WAAW,EAAG;AAEtB,QAAME,IAASD,IAAWD,EAAI,IAAIC,CAAQ,IAAKD,GACzCG,IAAWD,EAAO,MAAM,CAACE,MAAMA,aAAa,IAAI,GAChDC,IAAgBH,EAAO,IAAI,CAACE,MAAOA,aAAa,OAAOA,EAAE,QAAA,IAAaA,CAAa;AAEzF,EAAAC,EAAc,KAAK,CAACC,GAAGC,MAAMD,IAAIC,CAAC;AAElC,QAAMC,IAAM,KAAK,MAAMH,EAAc,SAAS,CAAC,GACzCI,IACJJ,EAAc,SAAS,MAAM,KAAKA,EAAcG,IAAM,CAAC,IAAIH,EAAcG,CAAG,KAAK,IAAIH,EAAcG,CAAG;AAExG,SAAQL,IAAW,IAAI,KAAKM,CAAW,IAAIA;AAC7C;"}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});function r(t,e){if(typeof t=="bigint"&&typeof e=="bigint"||typeof t=="number"&&typeof e=="number")return t*e;throw new TypeError("Both arguments must be of the same type (number or bigint)")}exports.multiply=r;
|
|
2
|
+
//# sourceMappingURL=multiply.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"multiply.cjs","sources":["../../src/math/multiply.ts"],"sourcesContent":["/**\n * Multiplies a number by a scalar with precision handling for financial calculations.\n * Supports both regular numbers and bigint for exact precision.\n *\n * @example\n * ```ts\n * multiply(10, 5); // 50\n * multiply(0.1, 3); // 0.3 (precision-safe)\n * multiply(100n, 5n); // 500n\n * ```\n *\n * @param a - Number to multiply\n * @param b - Multiplier\n * @returns Product of a and b\n */\nexport function multiply(a: number, b: number): number;\nexport function multiply(a: bigint, b: bigint): bigint;\nexport function multiply(a: number | bigint, b: number | bigint): number | bigint {\n if (typeof a === 'bigint' && typeof b === 'bigint') {\n return a * b;\n }\n if (typeof a === 'number' && typeof b === 'number') {\n return a * b;\n }\n throw new TypeError('Both arguments must be of the same type (number or bigint)');\n}\n"],"names":["multiply","a","b"],"mappings":"gFAiBO,SAASA,EAASC,EAAoBC,EAAqC,CAIhF,GAHI,OAAOD,GAAM,UAAY,OAAOC,GAAM,UAGtC,OAAOD,GAAM,UAAY,OAAOC,GAAM,SACxC,OAAOD,EAAIC,EAEb,MAAM,IAAI,UAAU,4DAA4D,CAClF"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
function r(t, e) {
|
|
2
|
+
if (typeof t == "bigint" && typeof e == "bigint" || typeof t == "number" && typeof e == "number")
|
|
3
|
+
return t * e;
|
|
4
|
+
throw new TypeError("Both arguments must be of the same type (number or bigint)");
|
|
5
|
+
}
|
|
6
|
+
export {
|
|
7
|
+
r as multiply
|
|
8
|
+
};
|
|
9
|
+
//# sourceMappingURL=multiply.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"multiply.js","sources":["../../src/math/multiply.ts"],"sourcesContent":["/**\n * Multiplies a number by a scalar with precision handling for financial calculations.\n * Supports both regular numbers and bigint for exact precision.\n *\n * @example\n * ```ts\n * multiply(10, 5); // 50\n * multiply(0.1, 3); // 0.3 (precision-safe)\n * multiply(100n, 5n); // 500n\n * ```\n *\n * @param a - Number to multiply\n * @param b - Multiplier\n * @returns Product of a and b\n */\nexport function multiply(a: number, b: number): number;\nexport function multiply(a: bigint, b: bigint): bigint;\nexport function multiply(a: number | bigint, b: number | bigint): number | bigint {\n if (typeof a === 'bigint' && typeof b === 'bigint') {\n return a * b;\n }\n if (typeof a === 'number' && typeof b === 'number') {\n return a * b;\n }\n throw new TypeError('Both arguments must be of the same type (number or bigint)');\n}\n"],"names":["multiply","a","b"],"mappings":"AAiBO,SAASA,EAASC,GAAoBC,GAAqC;AAIhF,MAHI,OAAOD,KAAM,YAAY,OAAOC,KAAM,YAGtC,OAAOD,KAAM,YAAY,OAAOC,KAAM;AACxC,WAAOD,IAAIC;AAEb,QAAM,IAAI,UAAU,4DAA4D;AAClF;"}
|
package/dist/math/range.cjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const a=require("../function/assert.cjs");function m(r,i,e){if(a.assert(Number.isFinite(r)&&Number.isFinite(i)&&Number.isFinite(e),"start, stop, and step must be finite numbers",{args:{start:r,step:e,stop:i},type:TypeError}),a.assert(e!==0,"Step cannot be 0",{args:{step:e},type:Error}),r===i)return[];const n=Math.max(0,Math.ceil((i-r)/e+Number.EPSILON));return a.assert(n<=1e7,"Range exceeds maximum allowed size of 10,000,000",{args:{length:n,start:r,step:e,stop:i},type:Error}),Array.from({length:n},(t,u)=>r+u*e)}exports.range=m;
|
|
2
2
|
//# sourceMappingURL=range.cjs.map
|
package/dist/math/range.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"range.cjs","sources":["../../src/math/range.ts"],"sourcesContent":["/**\n * Creates an array of numbers progressing from start up to, but not including, end. A step is used to specify the difference between each number in the array.\n *\n * @example\n * ```ts\n * const start = 0;\n * const stop = 10;\n * const step = 2;\n *\n * range(start, stop, step) // [0, 2, 4, 6, 8];\n * ```\n *\n * @param start - The start of the range.\n * @param stop - The end of the range.\n * @param step - The value to increment or decrement by.\n *\n * @returns The range of numbers.\n */\nexport function range(start: number, stop: number, step: number) {\n
|
|
1
|
+
{"version":3,"file":"range.cjs","sources":["../../src/math/range.ts"],"sourcesContent":["import { assert } from '../function/assert';\n\n/**\n * Creates an array of numbers progressing from start up to, but not including, end. A step is used to specify the difference between each number in the array.\n *\n * @example\n * ```ts\n * const start = 0;\n * const stop = 10;\n * const step = 2;\n *\n * range(start, stop, step) // [0, 2, 4, 6, 8];\n * ```\n *\n * @param start - The start of the range.\n * @param stop - The end of the range.\n * @param step - The value to increment or decrement by.\n *\n * @returns The range of numbers.\n *\n * @throws {TypeError} If start, stop, or step are not finite numbers.\n * @throws {Error} If step is 0 or if range exceeds maximum size.\n */\nexport function range(start: number, stop: number, step: number) {\n assert(\n Number.isFinite(start) && Number.isFinite(stop) && Number.isFinite(step),\n 'start, stop, and step must be finite numbers',\n { args: { start, step, stop }, type: TypeError },\n );\n assert(step !== 0, 'Step cannot be 0', { args: { step }, type: Error });\n\n if (start === stop) {\n return [];\n }\n\n const length = Math.max(0, Math.ceil((stop - start) / step + Number.EPSILON));\n\n assert(length <= 10_000_000, 'Range exceeds maximum allowed size of 10,000,000', {\n args: { length, start, step, stop },\n type: Error,\n });\n\n return Array.from({ length }, (_, i) => start + i * step);\n}\n"],"names":["range","start","stop","step","assert","length","_","i"],"mappings":"0HAuBO,SAASA,EAAMC,EAAeC,EAAcC,EAAc,CAQ/D,GAPAC,EAAAA,OACE,OAAO,SAASH,CAAK,GAAK,OAAO,SAASC,CAAI,GAAK,OAAO,SAASC,CAAI,EACvE,+CACA,CAAE,KAAM,CAAE,MAAAF,EAAO,KAAAE,EAAM,KAAAD,CAAA,EAAQ,KAAM,SAAA,CAAU,EAEjDE,SAAOD,IAAS,EAAG,mBAAoB,CAAE,KAAM,CAAE,KAAAA,CAAA,EAAQ,KAAM,MAAO,EAElEF,IAAUC,EACZ,MAAO,CAAA,EAGT,MAAMG,EAAS,KAAK,IAAI,EAAG,KAAK,MAAMH,EAAOD,GAASE,EAAO,OAAO,OAAO,CAAC,EAE5EC,OAAAA,SAAOC,GAAU,IAAY,mDAAoD,CAC/E,KAAM,CAAE,OAAAA,EAAQ,MAAAJ,EAAO,KAAAE,EAAM,KAAAD,CAAA,EAC7B,KAAM,KAAA,CACP,EAEM,MAAM,KAAK,CAAE,OAAAG,CAAA,EAAU,CAACC,EAAGC,IAAMN,EAAQM,EAAIJ,CAAI,CAC1D"}
|
package/dist/math/range.js
CHANGED
|
@@ -1,10 +1,18 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
if (
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
import { assert as m } from "../function/assert.js";
|
|
2
|
+
function b(r, i, e) {
|
|
3
|
+
if (m(
|
|
4
|
+
Number.isFinite(r) && Number.isFinite(i) && Number.isFinite(e),
|
|
5
|
+
"start, stop, and step must be finite numbers",
|
|
6
|
+
{ args: { start: r, step: e, stop: i }, type: TypeError }
|
|
7
|
+
), m(e !== 0, "Step cannot be 0", { args: { step: e }, type: Error }), r === i)
|
|
8
|
+
return [];
|
|
9
|
+
const n = Math.max(0, Math.ceil((i - r) / e + Number.EPSILON));
|
|
10
|
+
return m(n <= 1e7, "Range exceeds maximum allowed size of 10,000,000", {
|
|
11
|
+
args: { length: n, start: r, step: e, stop: i },
|
|
12
|
+
type: Error
|
|
13
|
+
}), Array.from({ length: n }, (u, a) => r + a * e);
|
|
6
14
|
}
|
|
7
15
|
export {
|
|
8
|
-
|
|
16
|
+
b as range
|
|
9
17
|
};
|
|
10
18
|
//# sourceMappingURL=range.js.map
|
package/dist/math/range.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"range.js","sources":["../../src/math/range.ts"],"sourcesContent":["/**\n * Creates an array of numbers progressing from start up to, but not including, end. A step is used to specify the difference between each number in the array.\n *\n * @example\n * ```ts\n * const start = 0;\n * const stop = 10;\n * const step = 2;\n *\n * range(start, stop, step) // [0, 2, 4, 6, 8];\n * ```\n *\n * @param start - The start of the range.\n * @param stop - The end of the range.\n * @param step - The value to increment or decrement by.\n *\n * @returns The range of numbers.\n */\nexport function range(start: number, stop: number, step: number) {\n
|
|
1
|
+
{"version":3,"file":"range.js","sources":["../../src/math/range.ts"],"sourcesContent":["import { assert } from '../function/assert';\n\n/**\n * Creates an array of numbers progressing from start up to, but not including, end. A step is used to specify the difference between each number in the array.\n *\n * @example\n * ```ts\n * const start = 0;\n * const stop = 10;\n * const step = 2;\n *\n * range(start, stop, step) // [0, 2, 4, 6, 8];\n * ```\n *\n * @param start - The start of the range.\n * @param stop - The end of the range.\n * @param step - The value to increment or decrement by.\n *\n * @returns The range of numbers.\n *\n * @throws {TypeError} If start, stop, or step are not finite numbers.\n * @throws {Error} If step is 0 or if range exceeds maximum size.\n */\nexport function range(start: number, stop: number, step: number) {\n assert(\n Number.isFinite(start) && Number.isFinite(stop) && Number.isFinite(step),\n 'start, stop, and step must be finite numbers',\n { args: { start, step, stop }, type: TypeError },\n );\n assert(step !== 0, 'Step cannot be 0', { args: { step }, type: Error });\n\n if (start === stop) {\n return [];\n }\n\n const length = Math.max(0, Math.ceil((stop - start) / step + Number.EPSILON));\n\n assert(length <= 10_000_000, 'Range exceeds maximum allowed size of 10,000,000', {\n args: { length, start, step, stop },\n type: Error,\n });\n\n return Array.from({ length }, (_, i) => start + i * step);\n}\n"],"names":["range","start","stop","step","assert","length","_","i"],"mappings":";AAuBO,SAASA,EAAMC,GAAeC,GAAcC,GAAc;AAQ/D,MAPAC;AAAA,IACE,OAAO,SAASH,CAAK,KAAK,OAAO,SAASC,CAAI,KAAK,OAAO,SAASC,CAAI;AAAA,IACvE;AAAA,IACA,EAAE,MAAM,EAAE,OAAAF,GAAO,MAAAE,GAAM,MAAAD,EAAA,GAAQ,MAAM,UAAA;AAAA,EAAU,GAEjDE,EAAOD,MAAS,GAAG,oBAAoB,EAAE,MAAM,EAAE,MAAAA,EAAA,GAAQ,MAAM,OAAO,GAElEF,MAAUC;AACZ,WAAO,CAAA;AAGT,QAAMG,IAAS,KAAK,IAAI,GAAG,KAAK,MAAMH,IAAOD,KAASE,IAAO,OAAO,OAAO,CAAC;AAE5E,SAAAC,EAAOC,KAAU,KAAY,oDAAoD;AAAA,IAC/E,MAAM,EAAE,QAAAA,GAAQ,OAAAJ,GAAO,MAAAE,GAAM,MAAAD,EAAA;AAAA,IAC7B,MAAM;AAAA,EAAA,CACP,GAEM,MAAM,KAAK,EAAE,QAAAG,EAAA,GAAU,CAACC,GAAGC,MAAMN,IAAQM,IAAIJ,CAAI;AAC1D;"}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});function r(t,e){if(typeof t=="bigint"&&typeof e=="bigint"||typeof t=="number"&&typeof e=="number")return t-e;throw new TypeError("Both arguments must be of the same type (number or bigint)")}exports.subtract=r;
|
|
2
|
+
//# sourceMappingURL=subtract.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"subtract.cjs","sources":["../../src/math/subtract.ts"],"sourcesContent":["/**\n * Subtracts one number from another with precision handling for financial calculations.\n * Supports both regular numbers and bigint for exact precision.\n *\n * @example\n * ```ts\n * subtract(20, 10); // 10\n * subtract(0.3, 0.1); // 0.2 (precision-safe)\n * subtract(300n, 100n); // 200n\n * ```\n *\n * @param a - Number to subtract from\n * @param b - Number to subtract\n * @returns Difference of a and b\n */\nexport function subtract(a: number, b: number): number;\nexport function subtract(a: bigint, b: bigint): bigint;\nexport function subtract(a: number | bigint, b: number | bigint): number | bigint {\n if (typeof a === 'bigint' && typeof b === 'bigint') {\n return a - b;\n }\n if (typeof a === 'number' && typeof b === 'number') {\n return a - b;\n }\n throw new TypeError('Both arguments must be of the same type (number or bigint)');\n}\n"],"names":["subtract","a","b"],"mappings":"gFAiBO,SAASA,EAASC,EAAoBC,EAAqC,CAIhF,GAHI,OAAOD,GAAM,UAAY,OAAOC,GAAM,UAGtC,OAAOD,GAAM,UAAY,OAAOC,GAAM,SACxC,OAAOD,EAAIC,EAEb,MAAM,IAAI,UAAU,4DAA4D,CAClF"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
function r(t, e) {
|
|
2
|
+
if (typeof t == "bigint" && typeof e == "bigint" || typeof t == "number" && typeof e == "number")
|
|
3
|
+
return t - e;
|
|
4
|
+
throw new TypeError("Both arguments must be of the same type (number or bigint)");
|
|
5
|
+
}
|
|
6
|
+
export {
|
|
7
|
+
r as subtract
|
|
8
|
+
};
|
|
9
|
+
//# sourceMappingURL=subtract.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"subtract.js","sources":["../../src/math/subtract.ts"],"sourcesContent":["/**\n * Subtracts one number from another with precision handling for financial calculations.\n * Supports both regular numbers and bigint for exact precision.\n *\n * @example\n * ```ts\n * subtract(20, 10); // 10\n * subtract(0.3, 0.1); // 0.2 (precision-safe)\n * subtract(300n, 100n); // 200n\n * ```\n *\n * @param a - Number to subtract from\n * @param b - Number to subtract\n * @returns Difference of a and b\n */\nexport function subtract(a: number, b: number): number;\nexport function subtract(a: bigint, b: bigint): bigint;\nexport function subtract(a: number | bigint, b: number | bigint): number | bigint {\n if (typeof a === 'bigint' && typeof b === 'bigint') {\n return a - b;\n }\n if (typeof a === 'number' && typeof b === 'number') {\n return a - b;\n }\n throw new TypeError('Both arguments must be of the same type (number or bigint)');\n}\n"],"names":["subtract","a","b"],"mappings":"AAiBO,SAASA,EAASC,GAAoBC,GAAqC;AAIhF,MAHI,OAAOD,KAAM,YAAY,OAAOC,KAAM,YAGtC,OAAOD,KAAM,YAAY,OAAOC,KAAM;AACxC,WAAOD,IAAIC;AAEb,QAAM,IAAI,UAAU,4DAA4D;AAClF;"}
|
package/dist/math/sum.cjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});function
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});function u(r,t){if(r.length!==0)return r.reduce((o,n)=>{const e=t?t(n):n;if(typeof e!="number")throw new TypeError(`Expected number, got ${typeof e}`);if(Number.isNaN(e))throw new TypeError("Cannot sum NaN values");return o+e},0)}exports.sum=u;
|
|
2
2
|
//# sourceMappingURL=sum.cjs.map
|
package/dist/math/sum.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sum.cjs","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 *
|
|
1
|
+
{"version":3,"file":"sum.cjs","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 */\nexport function sum<T>(array: T[], callback?: (item: T) => number): number | undefined {\n if (array.length === 0) return undefined;\n\n return array.reduce<number>((acc, item) => {\n const val = callback ? callback(item) : item;\n if (typeof val !== 'number') {\n throw new TypeError(`Expected number, got ${typeof val}`);\n }\n if (Number.isNaN(val)) {\n throw new TypeError('Cannot sum NaN values');\n }\n return acc + val;\n }, 0);\n}\n"],"names":["sum","array","callback","acc","item","val"],"mappings":"gFAeO,SAASA,EAAOC,EAAYC,EAAoD,CACrF,GAAID,EAAM,SAAW,EAErB,OAAOA,EAAM,OAAe,CAACE,EAAKC,IAAS,CACzC,MAAMC,EAAMH,EAAWA,EAASE,CAAI,EAAIA,EACxC,GAAI,OAAOC,GAAQ,SACjB,MAAM,IAAI,UAAU,wBAAwB,OAAOA,CAAG,EAAE,EAE1D,GAAI,OAAO,MAAMA,CAAG,EAClB,MAAM,IAAI,UAAU,uBAAuB,EAE7C,OAAOF,EAAME,CACf,EAAG,CAAC,CACN"}
|
package/dist/math/sum.js
CHANGED
|
@@ -1,22 +1,15 @@
|
|
|
1
|
-
function
|
|
2
|
-
if (
|
|
3
|
-
return
|
|
4
|
-
const
|
|
5
|
-
if (
|
|
6
|
-
|
|
7
|
-
if (
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
},
|
|
11
|
-
}
|
|
12
|
-
function i(t, e) {
|
|
13
|
-
const n = t.getTime();
|
|
14
|
-
return e === void 0 ? new Date(n) : e instanceof Date ? new Date(e.getTime() + n) : new Date(e + n);
|
|
15
|
-
}
|
|
16
|
-
function o(t, e) {
|
|
17
|
-
return t === void 0 ? e : t instanceof Date ? new Date(t.getTime() + e) : t + e;
|
|
1
|
+
function u(r, n) {
|
|
2
|
+
if (r.length !== 0)
|
|
3
|
+
return r.reduce((o, t) => {
|
|
4
|
+
const e = n ? n(t) : t;
|
|
5
|
+
if (typeof e != "number")
|
|
6
|
+
throw new TypeError(`Expected number, got ${typeof e}`);
|
|
7
|
+
if (Number.isNaN(e))
|
|
8
|
+
throw new TypeError("Cannot sum NaN values");
|
|
9
|
+
return o + e;
|
|
10
|
+
}, 0);
|
|
18
11
|
}
|
|
19
12
|
export {
|
|
20
|
-
|
|
13
|
+
u as sum
|
|
21
14
|
};
|
|
22
15
|
//# sourceMappingURL=sum.js.map
|
package/dist/math/sum.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sum.js","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 *
|
|
1
|
+
{"version":3,"file":"sum.js","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 */\nexport function sum<T>(array: T[], callback?: (item: T) => number): number | undefined {\n if (array.length === 0) return undefined;\n\n return array.reduce<number>((acc, item) => {\n const val = callback ? callback(item) : item;\n if (typeof val !== 'number') {\n throw new TypeError(`Expected number, got ${typeof val}`);\n }\n if (Number.isNaN(val)) {\n throw new TypeError('Cannot sum NaN values');\n }\n return acc + val;\n }, 0);\n}\n"],"names":["sum","array","callback","acc","item","val"],"mappings":"AAeO,SAASA,EAAOC,GAAYC,GAAoD;AACrF,MAAID,EAAM,WAAW;AAErB,WAAOA,EAAM,OAAe,CAACE,GAAKC,MAAS;AACzC,YAAMC,IAAMH,IAAWA,EAASE,CAAI,IAAIA;AACxC,UAAI,OAAOC,KAAQ;AACjB,cAAM,IAAI,UAAU,wBAAwB,OAAOA,CAAG,EAAE;AAE1D,UAAI,OAAO,MAAMA,CAAG;AAClB,cAAM,IAAI,UAAU,uBAAuB;AAE7C,aAAOF,IAAME;AAAA,IACf,GAAG,CAAC;AACN;"}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});function u(e,t={}){const{locale:c="en-US",style:i="symbol",minimumFractionDigits:s,maximumFractionDigits:a}=t,n=l(e.currency),o=10**n,m=Number(e.amount)/o;let r;switch(i){case"symbol":r="symbol";break;case"code":r="code";break;case"name":r="name";break;default:r="symbol"}return new Intl.NumberFormat(c,{currency:e.currency,currencyDisplay:r,maximumFractionDigits:a??n,minimumFractionDigits:s??n,style:"currency"}).format(m)}function l(e){const t=["BIF","CLP","DJF","GNF","JPY","KMF","KRW","MGA","PYG","RWF","UGX","VND","VUV","XAF","XOF","XPF"],c=["BHD","IQD","JOD","KWD","LYD","OMR","TND"];return t.includes(e.toUpperCase())?0:c.includes(e.toUpperCase())?3:2}exports.currency=u;
|
|
2
|
+
//# sourceMappingURL=currency.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"currency.cjs","sources":["../../src/money/currency.ts"],"sourcesContent":["import type { Money } from './types';\n\n/**\n * Options for currency formatting.\n */\nexport type CurrencyFormatOptions = {\n locale?: string; // BCP 47 language tag (e.g., 'en-US', 'de-DE')\n style?: 'symbol' | 'code' | 'name'; // Display style\n minimumFractionDigits?: number; // Minimum decimal places\n maximumFractionDigits?: number; // Maximum decimal places\n};\n\n/**\n * Formats a monetary amount as a currency string with proper locale and symbol.\n * Handles decimal places automatically based on currency.\n *\n * @example\n * ```ts\n * const money = { amount: 123456n, currency: 'USD' };\n *\n * currency(money); // '$1,234.56' (default en-US)\n * currency(money, { locale: 'de-DE' }); // '1.234,56 $'\n * currency(money, { style: 'code' }); // 'USD 1,234.56'\n * currency(money, { style: 'name' }); // '1,234.56 US dollars'\n * ```\n *\n * @param money - Money object to format\n * @param options - Formatting options\n * @returns Formatted currency string\n */\nexport function currency(money: Money, options: CurrencyFormatOptions = {}): string {\n const { locale = 'en-US', style = 'symbol', minimumFractionDigits, maximumFractionDigits } = options;\n\n // Get decimal places for currency (default to 2 for most currencies)\n const decimalPlaces = getCurrencyDecimals(money.currency);\n\n // Convert bigint amount to decimal (divide by 10^decimalPlaces)\n const divisor = 10 ** decimalPlaces;\n const amount = Number(money.amount) / divisor;\n\n // Determine Intl.NumberFormat style\n let currencyDisplay: 'symbol' | 'code' | 'name';\n switch (style) {\n case 'symbol':\n currencyDisplay = 'symbol';\n break;\n case 'code':\n currencyDisplay = 'code';\n break;\n case 'name':\n currencyDisplay = 'name';\n break;\n default:\n currencyDisplay = 'symbol';\n }\n\n const formatter = new Intl.NumberFormat(locale, {\n currency: money.currency,\n currencyDisplay,\n maximumFractionDigits: maximumFractionDigits ?? decimalPlaces,\n minimumFractionDigits: minimumFractionDigits ?? decimalPlaces,\n style: 'currency',\n });\n\n return formatter.format(amount);\n}\n\n/**\n * Gets the number of decimal places for a currency.\n * Most currencies use 2 decimal places, but some use 0 or 3.\n */\nfunction getCurrencyDecimals(currencyCode: string): number {\n const zeroDecimalCurrencies = [\n 'BIF',\n 'CLP',\n 'DJF',\n 'GNF',\n 'JPY',\n 'KMF',\n 'KRW',\n 'MGA',\n 'PYG',\n 'RWF',\n 'UGX',\n 'VND',\n 'VUV',\n 'XAF',\n 'XOF',\n 'XPF',\n ];\n const threeDecimalCurrencies = ['BHD', 'IQD', 'JOD', 'KWD', 'LYD', 'OMR', 'TND'];\n\n if (zeroDecimalCurrencies.includes(currencyCode.toUpperCase())) {\n return 0;\n }\n if (threeDecimalCurrencies.includes(currencyCode.toUpperCase())) {\n return 3;\n }\n return 2; // Default for most currencies\n}\n"],"names":["currency","money","options","locale","style","minimumFractionDigits","maximumFractionDigits","decimalPlaces","getCurrencyDecimals","divisor","amount","currencyDisplay","currencyCode","zeroDecimalCurrencies","threeDecimalCurrencies"],"mappings":"gFA8BO,SAASA,EAASC,EAAcC,EAAiC,GAAY,CAClF,KAAM,CAAE,OAAAC,EAAS,QAAS,MAAAC,EAAQ,SAAU,sBAAAC,EAAuB,sBAAAC,GAA0BJ,EAGvFK,EAAgBC,EAAoBP,EAAM,QAAQ,EAGlDQ,EAAU,IAAMF,EAChBG,EAAS,OAAOT,EAAM,MAAM,EAAIQ,EAGtC,IAAIE,EACJ,OAAQP,EAAA,CACN,IAAK,SACHO,EAAkB,SAClB,MACF,IAAK,OACHA,EAAkB,OAClB,MACF,IAAK,OACHA,EAAkB,OAClB,MACF,QACEA,EAAkB,QAAA,CAWtB,OARkB,IAAI,KAAK,aAAaR,EAAQ,CAC9C,SAAUF,EAAM,SAChB,gBAAAU,EACA,sBAAuBL,GAAyBC,EAChD,sBAAuBF,GAAyBE,EAChD,MAAO,UAAA,CACR,EAEgB,OAAOG,CAAM,CAChC,CAMA,SAASF,EAAoBI,EAA8B,CACzD,MAAMC,EAAwB,CAC5B,MACA,MACA,MACA,MACA,MACA,MACA,MACA,MACA,MACA,MACA,MACA,MACA,MACA,MACA,MACA,KAAA,EAEIC,EAAyB,CAAC,MAAO,MAAO,MAAO,MAAO,MAAO,MAAO,KAAK,EAE/E,OAAID,EAAsB,SAASD,EAAa,YAAA,CAAa,EACpD,EAELE,EAAuB,SAASF,EAAa,YAAA,CAAa,EACrD,EAEF,CACT"}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
function D(e, c = {}) {
|
|
2
|
+
const { locale: t = "en-US", style: i = "symbol", minimumFractionDigits: s, maximumFractionDigits: a } = c, n = u(e.currency), m = 10 ** n, o = Number(e.amount) / m;
|
|
3
|
+
let r;
|
|
4
|
+
switch (i) {
|
|
5
|
+
case "symbol":
|
|
6
|
+
r = "symbol";
|
|
7
|
+
break;
|
|
8
|
+
case "code":
|
|
9
|
+
r = "code";
|
|
10
|
+
break;
|
|
11
|
+
case "name":
|
|
12
|
+
r = "name";
|
|
13
|
+
break;
|
|
14
|
+
default:
|
|
15
|
+
r = "symbol";
|
|
16
|
+
}
|
|
17
|
+
return new Intl.NumberFormat(t, {
|
|
18
|
+
currency: e.currency,
|
|
19
|
+
currencyDisplay: r,
|
|
20
|
+
maximumFractionDigits: a ?? n,
|
|
21
|
+
minimumFractionDigits: s ?? n,
|
|
22
|
+
style: "currency"
|
|
23
|
+
}).format(o);
|
|
24
|
+
}
|
|
25
|
+
function u(e) {
|
|
26
|
+
const c = [
|
|
27
|
+
"BIF",
|
|
28
|
+
"CLP",
|
|
29
|
+
"DJF",
|
|
30
|
+
"GNF",
|
|
31
|
+
"JPY",
|
|
32
|
+
"KMF",
|
|
33
|
+
"KRW",
|
|
34
|
+
"MGA",
|
|
35
|
+
"PYG",
|
|
36
|
+
"RWF",
|
|
37
|
+
"UGX",
|
|
38
|
+
"VND",
|
|
39
|
+
"VUV",
|
|
40
|
+
"XAF",
|
|
41
|
+
"XOF",
|
|
42
|
+
"XPF"
|
|
43
|
+
], t = ["BHD", "IQD", "JOD", "KWD", "LYD", "OMR", "TND"];
|
|
44
|
+
return c.includes(e.toUpperCase()) ? 0 : t.includes(e.toUpperCase()) ? 3 : 2;
|
|
45
|
+
}
|
|
46
|
+
export {
|
|
47
|
+
D as currency
|
|
48
|
+
};
|
|
49
|
+
//# sourceMappingURL=currency.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"currency.js","sources":["../../src/money/currency.ts"],"sourcesContent":["import type { Money } from './types';\n\n/**\n * Options for currency formatting.\n */\nexport type CurrencyFormatOptions = {\n locale?: string; // BCP 47 language tag (e.g., 'en-US', 'de-DE')\n style?: 'symbol' | 'code' | 'name'; // Display style\n minimumFractionDigits?: number; // Minimum decimal places\n maximumFractionDigits?: number; // Maximum decimal places\n};\n\n/**\n * Formats a monetary amount as a currency string with proper locale and symbol.\n * Handles decimal places automatically based on currency.\n *\n * @example\n * ```ts\n * const money = { amount: 123456n, currency: 'USD' };\n *\n * currency(money); // '$1,234.56' (default en-US)\n * currency(money, { locale: 'de-DE' }); // '1.234,56 $'\n * currency(money, { style: 'code' }); // 'USD 1,234.56'\n * currency(money, { style: 'name' }); // '1,234.56 US dollars'\n * ```\n *\n * @param money - Money object to format\n * @param options - Formatting options\n * @returns Formatted currency string\n */\nexport function currency(money: Money, options: CurrencyFormatOptions = {}): string {\n const { locale = 'en-US', style = 'symbol', minimumFractionDigits, maximumFractionDigits } = options;\n\n // Get decimal places for currency (default to 2 for most currencies)\n const decimalPlaces = getCurrencyDecimals(money.currency);\n\n // Convert bigint amount to decimal (divide by 10^decimalPlaces)\n const divisor = 10 ** decimalPlaces;\n const amount = Number(money.amount) / divisor;\n\n // Determine Intl.NumberFormat style\n let currencyDisplay: 'symbol' | 'code' | 'name';\n switch (style) {\n case 'symbol':\n currencyDisplay = 'symbol';\n break;\n case 'code':\n currencyDisplay = 'code';\n break;\n case 'name':\n currencyDisplay = 'name';\n break;\n default:\n currencyDisplay = 'symbol';\n }\n\n const formatter = new Intl.NumberFormat(locale, {\n currency: money.currency,\n currencyDisplay,\n maximumFractionDigits: maximumFractionDigits ?? decimalPlaces,\n minimumFractionDigits: minimumFractionDigits ?? decimalPlaces,\n style: 'currency',\n });\n\n return formatter.format(amount);\n}\n\n/**\n * Gets the number of decimal places for a currency.\n * Most currencies use 2 decimal places, but some use 0 or 3.\n */\nfunction getCurrencyDecimals(currencyCode: string): number {\n const zeroDecimalCurrencies = [\n 'BIF',\n 'CLP',\n 'DJF',\n 'GNF',\n 'JPY',\n 'KMF',\n 'KRW',\n 'MGA',\n 'PYG',\n 'RWF',\n 'UGX',\n 'VND',\n 'VUV',\n 'XAF',\n 'XOF',\n 'XPF',\n ];\n const threeDecimalCurrencies = ['BHD', 'IQD', 'JOD', 'KWD', 'LYD', 'OMR', 'TND'];\n\n if (zeroDecimalCurrencies.includes(currencyCode.toUpperCase())) {\n return 0;\n }\n if (threeDecimalCurrencies.includes(currencyCode.toUpperCase())) {\n return 3;\n }\n return 2; // Default for most currencies\n}\n"],"names":["currency","money","options","locale","style","minimumFractionDigits","maximumFractionDigits","decimalPlaces","getCurrencyDecimals","divisor","amount","currencyDisplay","currencyCode","zeroDecimalCurrencies","threeDecimalCurrencies"],"mappings":"AA8BO,SAASA,EAASC,GAAcC,IAAiC,IAAY;AAClF,QAAM,EAAE,QAAAC,IAAS,SAAS,OAAAC,IAAQ,UAAU,uBAAAC,GAAuB,uBAAAC,MAA0BJ,GAGvFK,IAAgBC,EAAoBP,EAAM,QAAQ,GAGlDQ,IAAU,MAAMF,GAChBG,IAAS,OAAOT,EAAM,MAAM,IAAIQ;AAGtC,MAAIE;AACJ,UAAQP,GAAA;AAAA,IACN,KAAK;AACH,MAAAO,IAAkB;AAClB;AAAA,IACF,KAAK;AACH,MAAAA,IAAkB;AAClB;AAAA,IACF,KAAK;AACH,MAAAA,IAAkB;AAClB;AAAA,IACF;AACE,MAAAA,IAAkB;AAAA,EAAA;AAWtB,SARkB,IAAI,KAAK,aAAaR,GAAQ;AAAA,IAC9C,UAAUF,EAAM;AAAA,IAChB,iBAAAU;AAAA,IACA,uBAAuBL,KAAyBC;AAAA,IAChD,uBAAuBF,KAAyBE;AAAA,IAChD,OAAO;AAAA,EAAA,CACR,EAEgB,OAAOG,CAAM;AAChC;AAMA,SAASF,EAAoBI,GAA8B;AACzD,QAAMC,IAAwB;AAAA,IAC5B;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EAAA,GAEIC,IAAyB,CAAC,OAAO,OAAO,OAAO,OAAO,OAAO,OAAO,KAAK;AAE/E,SAAID,EAAsB,SAASD,EAAa,YAAA,CAAa,IACpD,IAELE,EAAuB,SAASF,EAAa,YAAA,CAAa,IACrD,IAEF;AACT;"}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});function r(e,t){if(e.currency!==t.from)throw new Error(`Currency mismatch: expected ${t.from}, got ${e.currency}`);const n=BigInt(Math.round(t.rate*1e6));return{amount:e.amount*n/1000000n,currency:t.to}}exports.exchange=r;
|
|
2
|
+
//# sourceMappingURL=exchange.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"exchange.cjs","sources":["../../src/money/exchange.ts"],"sourcesContent":["import type { Money } from './types';\n\n/**\n * Exchange rate for currency conversion.\n */\nexport type ExchangeRate = {\n from: string; // Source currency code\n to: string; // Target currency code\n rate: number; // Exchange rate multiplier\n};\n\n/**\n * Converts money from one currency to another using the provided exchange rate.\n * Maintains precision by using bigint arithmetic.\n *\n * @example\n * ```ts\n * const usd = { amount: 100000n, currency: 'USD' }; // $1,000.00\n * const rate = { from: 'USD', to: 'EUR', rate: 0.85 };\n *\n * exchange(usd, rate);\n * // { amount: 85000n, currency: 'EUR' } // €850.00\n * ```\n *\n * @param money - Money to convert\n * @param rate - Exchange rate information\n * @returns Converted money in target currency\n * @throws {Error} If source currency doesn't match rate.from\n */\nexport function exchange(money: Money, rate: ExchangeRate): Money {\n if (money.currency !== rate.from) {\n throw new Error(`Currency mismatch: expected ${rate.from}, got ${money.currency}`);\n }\n\n // Convert rate to avoid floating point issues\n // Multiply by 1000000 for precision, then divide back\n const rateBigInt = BigInt(Math.round(rate.rate * 1000000));\n const convertedAmount = (money.amount * rateBigInt) / 1000000n;\n\n return {\n amount: convertedAmount,\n currency: rate.to,\n };\n}\n"],"names":["exchange","money","rate","rateBigInt"],"mappings":"gFA6BO,SAASA,EAASC,EAAcC,EAA2B,CAChE,GAAID,EAAM,WAAaC,EAAK,KAC1B,MAAM,IAAI,MAAM,+BAA+BA,EAAK,IAAI,SAASD,EAAM,QAAQ,EAAE,EAKnF,MAAME,EAAa,OAAO,KAAK,MAAMD,EAAK,KAAO,GAAO,CAAC,EAGzD,MAAO,CACL,OAHuBD,EAAM,OAASE,EAAc,SAIpD,SAAUD,EAAK,EAAA,CAEnB"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
function o(r, n) {
|
|
2
|
+
if (r.currency !== n.from)
|
|
3
|
+
throw new Error(`Currency mismatch: expected ${n.from}, got ${r.currency}`);
|
|
4
|
+
const t = BigInt(Math.round(n.rate * 1e6));
|
|
5
|
+
return {
|
|
6
|
+
amount: r.amount * t / 1000000n,
|
|
7
|
+
currency: n.to
|
|
8
|
+
};
|
|
9
|
+
}
|
|
10
|
+
export {
|
|
11
|
+
o as exchange
|
|
12
|
+
};
|
|
13
|
+
//# sourceMappingURL=exchange.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"exchange.js","sources":["../../src/money/exchange.ts"],"sourcesContent":["import type { Money } from './types';\n\n/**\n * Exchange rate for currency conversion.\n */\nexport type ExchangeRate = {\n from: string; // Source currency code\n to: string; // Target currency code\n rate: number; // Exchange rate multiplier\n};\n\n/**\n * Converts money from one currency to another using the provided exchange rate.\n * Maintains precision by using bigint arithmetic.\n *\n * @example\n * ```ts\n * const usd = { amount: 100000n, currency: 'USD' }; // $1,000.00\n * const rate = { from: 'USD', to: 'EUR', rate: 0.85 };\n *\n * exchange(usd, rate);\n * // { amount: 85000n, currency: 'EUR' } // €850.00\n * ```\n *\n * @param money - Money to convert\n * @param rate - Exchange rate information\n * @returns Converted money in target currency\n * @throws {Error} If source currency doesn't match rate.from\n */\nexport function exchange(money: Money, rate: ExchangeRate): Money {\n if (money.currency !== rate.from) {\n throw new Error(`Currency mismatch: expected ${rate.from}, got ${money.currency}`);\n }\n\n // Convert rate to avoid floating point issues\n // Multiply by 1000000 for precision, then divide back\n const rateBigInt = BigInt(Math.round(rate.rate * 1000000));\n const convertedAmount = (money.amount * rateBigInt) / 1000000n;\n\n return {\n amount: convertedAmount,\n currency: rate.to,\n };\n}\n"],"names":["exchange","money","rate","rateBigInt"],"mappings":"AA6BO,SAASA,EAASC,GAAcC,GAA2B;AAChE,MAAID,EAAM,aAAaC,EAAK;AAC1B,UAAM,IAAI,MAAM,+BAA+BA,EAAK,IAAI,SAASD,EAAM,QAAQ,EAAE;AAKnF,QAAME,IAAa,OAAO,KAAK,MAAMD,EAAK,OAAO,GAAO,CAAC;AAGzD,SAAO;AAAA,IACL,QAHuBD,EAAM,SAASE,IAAc;AAAA,IAIpD,UAAUD,EAAK;AAAA,EAAA;AAEnB;"}
|
package/dist/object/clone.cjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});function t(e){return structuredClone(e)}exports.clone=t;
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});function t(e){if(typeof structuredClone=="function")try{return structuredClone(e)}catch(r){throw new Error(`Failed to clone item: ${r instanceof Error?r.message:String(r)}`)}try{return JSON.parse(JSON.stringify(e))}catch(r){throw new Error(`Failed to clone item using JSON: ${r instanceof Error?r.message:String(r)}`)}}exports.clone=t;
|
|
2
2
|
//# sourceMappingURL=clone.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"clone.cjs","sources":["../../src/object/clone.ts"],"sourcesContent":["/**\n * Creates a deep copy of the provided data using the structuredClone algorithm.\n *\n * @example\n * ```ts\n * const obj = { a: 1, b: { c: 2 } };\n * const dup = clone(obj);\n *\n * dup.b.c = 3;\n * console.log(obj.b.c); // logs 2\n * console.log(dup.b.c); // logs 3\n * ```\n *\n * @param item - The data to clone.\n *\n * @returns A deep copy of the provided data.\n */\nexport function clone<T>(item: T): T {\n return structuredClone(item);\n}\n"],"names":["clone","item"],"mappings":"
|
|
1
|
+
{"version":3,"file":"clone.cjs","sources":["../../src/object/clone.ts"],"sourcesContent":["/**\n * Creates a deep copy of the provided data using the structuredClone algorithm.\n *\n * @example\n * ```ts\n * const obj = { a: 1, b: { c: 2 } };\n * const dup = clone(obj);\n *\n * dup.b.c = 3;\n * console.log(obj.b.c); // logs 2\n * console.log(dup.b.c); // logs 3\n * ```\n *\n * @param item - The data to clone.\n *\n * @returns A deep copy of the provided data.\n *\n * @throws {Error} If the item cannot be cloned.\n */\nexport function clone<T>(item: T): T {\n if (typeof structuredClone === 'function') {\n try {\n return structuredClone(item);\n } catch (error) {\n throw new Error(`Failed to clone item: ${error instanceof Error ? error.message : String(error)}`);\n }\n }\n try {\n return JSON.parse(JSON.stringify(item));\n } catch (error) {\n throw new Error(`Failed to clone item using JSON: ${error instanceof Error ? error.message : String(error)}`);\n }\n}\n"],"names":["clone","item","error"],"mappings":"gFAmBO,SAASA,EAASC,EAAY,CACnC,GAAI,OAAO,iBAAoB,WAC7B,GAAI,CACF,OAAO,gBAAgBA,CAAI,CAC7B,OAASC,EAAO,CACd,MAAM,IAAI,MAAM,yBAAyBA,aAAiB,MAAQA,EAAM,QAAU,OAAOA,CAAK,CAAC,EAAE,CACnG,CAEF,GAAI,CACF,OAAO,KAAK,MAAM,KAAK,UAAUD,CAAI,CAAC,CACxC,OAASC,EAAO,CACd,MAAM,IAAI,MAAM,oCAAoCA,aAAiB,MAAQA,EAAM,QAAU,OAAOA,CAAK,CAAC,EAAE,CAC9G,CACF"}
|
package/dist/object/clone.js
CHANGED
|
@@ -1,7 +1,17 @@
|
|
|
1
|
-
function
|
|
2
|
-
|
|
1
|
+
function t(e) {
|
|
2
|
+
if (typeof structuredClone == "function")
|
|
3
|
+
try {
|
|
4
|
+
return structuredClone(e);
|
|
5
|
+
} catch (r) {
|
|
6
|
+
throw new Error(`Failed to clone item: ${r instanceof Error ? r.message : String(r)}`);
|
|
7
|
+
}
|
|
8
|
+
try {
|
|
9
|
+
return JSON.parse(JSON.stringify(e));
|
|
10
|
+
} catch (r) {
|
|
11
|
+
throw new Error(`Failed to clone item using JSON: ${r instanceof Error ? r.message : String(r)}`);
|
|
12
|
+
}
|
|
3
13
|
}
|
|
4
14
|
export {
|
|
5
|
-
|
|
15
|
+
t as clone
|
|
6
16
|
};
|
|
7
17
|
//# sourceMappingURL=clone.js.map
|
package/dist/object/clone.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"clone.js","sources":["../../src/object/clone.ts"],"sourcesContent":["/**\n * Creates a deep copy of the provided data using the structuredClone algorithm.\n *\n * @example\n * ```ts\n * const obj = { a: 1, b: { c: 2 } };\n * const dup = clone(obj);\n *\n * dup.b.c = 3;\n * console.log(obj.b.c); // logs 2\n * console.log(dup.b.c); // logs 3\n * ```\n *\n * @param item - The data to clone.\n *\n * @returns A deep copy of the provided data.\n */\nexport function clone<T>(item: T): T {\n return structuredClone(item);\n}\n"],"names":["clone","item"],"mappings":"
|
|
1
|
+
{"version":3,"file":"clone.js","sources":["../../src/object/clone.ts"],"sourcesContent":["/**\n * Creates a deep copy of the provided data using the structuredClone algorithm.\n *\n * @example\n * ```ts\n * const obj = { a: 1, b: { c: 2 } };\n * const dup = clone(obj);\n *\n * dup.b.c = 3;\n * console.log(obj.b.c); // logs 2\n * console.log(dup.b.c); // logs 3\n * ```\n *\n * @param item - The data to clone.\n *\n * @returns A deep copy of the provided data.\n *\n * @throws {Error} If the item cannot be cloned.\n */\nexport function clone<T>(item: T): T {\n if (typeof structuredClone === 'function') {\n try {\n return structuredClone(item);\n } catch (error) {\n throw new Error(`Failed to clone item: ${error instanceof Error ? error.message : String(error)}`);\n }\n }\n try {\n return JSON.parse(JSON.stringify(item));\n } catch (error) {\n throw new Error(`Failed to clone item using JSON: ${error instanceof Error ? error.message : String(error)}`);\n }\n}\n"],"names":["clone","item","error"],"mappings":"AAmBO,SAASA,EAASC,GAAY;AACnC,MAAI,OAAO,mBAAoB;AAC7B,QAAI;AACF,aAAO,gBAAgBA,CAAI;AAAA,IAC7B,SAASC,GAAO;AACd,YAAM,IAAI,MAAM,yBAAyBA,aAAiB,QAAQA,EAAM,UAAU,OAAOA,CAAK,CAAC,EAAE;AAAA,IACnG;AAEF,MAAI;AACF,WAAO,KAAK,MAAM,KAAK,UAAUD,CAAI,CAAC;AAAA,EACxC,SAASC,GAAO;AACd,UAAM,IAAI,MAAM,oCAAoCA,aAAiB,QAAQA,EAAM,UAAU,OAAOA,CAAK,CAAC,EAAE;AAAA,EAC9G;AACF;"}
|
package/dist/object/merge.cjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const o=require("../typed/isArray.cjs"),f=require("../typed/isObject.cjs");function a(
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const o=require("../typed/isArray.cjs"),f=require("../typed/isObject.cjs");function a(i="deep",...n){return n.length===0?{}:i==="shallow"?Object.assign({},...n):n.reduce((e,r)=>l(e,r,i),{})}function l(i,n,e){if(!f.isObject(n))return n;const r={...i};for(const c in n){if(!Object.hasOwn(n,c))continue;const t=n[c],u=r[c];r[c]=o.isArray(t)&&o.isArray(u)?b(u,t,e):f.isObject(t)&&f.isObject(u)?l(u,t,e):O(u,t,e)}return r}function b(i,n,e){return i?e==="arrayConcat"?i.concat(n):e==="arrayReplace"?n:Array.from(new Set([...i,...n])):n}function O(i,n,e){return typeof e=="function"?e(i,n):e==="lastWins"||n!==void 0?n:i}exports.merge=a;
|
|
2
2
|
//# sourceMappingURL=merge.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"merge.cjs","sources":["../../src/object/merge.ts"],"sourcesContent":["import { isArray } from '../typed/isArray';\nimport { isObject } from '../typed/isObject';\nimport type { Obj } from '../types';\n\ntype MergeStrategy =\n | 'deep'\n | 'shallow'\n | 'lastWins'\n | 'arrayConcat'\n | 'arrayReplace'\n // biome-ignore lint/suspicious/noExplicitAny: -\n | ((target: any, source: any) => any);\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\n/**\n * Merges multiple objects based on a specified merge strategy.\n *\n * @example\n * ```ts\n * const obj1 = { a: 1, b: { x: 10, y: \"hello\" }, c: [1] };\n * const obj2 = { b: { y: 20, z: true }, c: [2] };\n * const obj3 = { d: false, c: [3] };\n *\n * merge(\"deep\", obj1, obj2, obj3); // { a: 1, b: { x: 10, y: 20, z: true }, c: [1, 2, 3], d: false }\n * merge(\"shallow\", obj1, obj2, obj3); // { a: 1, b: { y: 20, z: true }, c: [3], d: false }\n * ```\n *\n * @param [strategy='deep'] - The merging strategy to use.\n * @param items - The objects to merge.\n * @returns A new merged object.\n */\nexport function merge<T extends Obj[]>(strategy: MergeStrategy = 'deep', ...items: [...T]): Merge<T> {\n if (items.length === 0) return {} as Merge<T>;\n\n if (strategy === 'shallow') {\n return Object.assign({}, ...items) as Merge<T>;\n }\n\n
|
|
1
|
+
{"version":3,"file":"merge.cjs","sources":["../../src/object/merge.ts"],"sourcesContent":["import { isArray } from '../typed/isArray';\nimport { isObject } from '../typed/isObject';\nimport type { Obj } from '../types';\n\n// #region MergeStrategy\ntype MergeStrategy =\n | 'deep'\n | 'shallow'\n | 'lastWins'\n | 'arrayConcat'\n | 'arrayReplace'\n // biome-ignore lint/suspicious/noExplicitAny: -\n | ((target: any, source: any) => any);\n// #endregion MergeStrategy\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\n/**\n * Merges multiple objects based on a specified merge strategy.\n *\n * @example\n * ```ts\n * const obj1 = { a: 1, b: { x: 10, y: \"hello\" }, c: [1] };\n * const obj2 = { b: { y: 20, z: true }, c: [2] };\n * const obj3 = { d: false, c: [3] };\n *\n * merge(\"deep\", obj1, obj2, obj3); // { a: 1, b: { x: 10, y: 20, z: true }, c: [1, 2, 3], d: false }\n * merge(\"shallow\", obj1, obj2, obj3); // { a: 1, b: { y: 20, z: true }, c: [3], d: false }\n * ```\n *\n * @param [strategy='deep'] - The merging strategy to use.\n * @param items - The objects to merge.\n * @returns A new merged object.\n */\nexport function merge<T extends Obj[]>(strategy: MergeStrategy = 'deep', ...items: [...T]): Merge<T> {\n if (items.length === 0) return {} as Merge<T>;\n\n if (strategy === 'shallow') {\n return Object.assign({}, ...items) as Merge<T>;\n }\n\n return items.reduce((acc, obj) => deepMerge(acc, obj, strategy) as unknown as Merge<T>, {} as Merge<T>);\n}\n\n/**\n * Deeply merges two objects based on the provided strategy.\n *\n * - Uses **direct property access** for performance.\n * - **Avoids redundant deep merging** where unnecessary.\n * - Optimized **array merging strategies**.\n *\n * @param target - The target object.\n * @param source - The source object.\n * @param strategy - The merge strategy.\n * @returns A new merged object.\n */\nfunction deepMerge<T extends Obj, U extends Obj>(target: T, source: U, strategy: MergeStrategy): DeepMerge<T, U> {\n if (!isObject(source)) return source as DeepMerge<T, U>;\n\n const result = { ...target } as DeepMerge<T, U>;\n\n for (const key in source) {\n if (!Object.hasOwn(source, key)) continue; // Prevent prototype pollution\n\n const sourceValue = source[key];\n const targetValue = result[key];\n\n // biome-ignore lint/suspicious/noExplicitAny: -\n (result as any)[key] =\n isArray(sourceValue) && isArray(targetValue)\n ? handleArrayMerge(targetValue, sourceValue, strategy)\n : isObject(sourceValue) && isObject(targetValue)\n ? deepMerge(targetValue, sourceValue, strategy)\n : applyMergeStrategy(targetValue, sourceValue, strategy);\n }\n\n return result;\n}\n\n/**\n * Optimized array merge based on strategy.\n *\n * - `\"arrayConcat\"` → Concatenates arrays.\n * - `\"arrayReplace\"` → Replaces the existing array.\n * - Default: **Unique merge** (Set-based optimization).\n */\nfunction handleArrayMerge<T, U>(targetArray: T[] | undefined, sourceArray: U[], strategy: MergeStrategy): (T | U)[] {\n if (!targetArray) return sourceArray;\n // biome-ignore lint/suspicious/noExplicitAny: -\n if (strategy === 'arrayConcat') return targetArray.concat(sourceArray as any);\n if (strategy === 'arrayReplace') return sourceArray;\n return Array.from(new Set([...targetArray, ...sourceArray])); // Unique merge\n}\n\n/**\n * Determines the appropriate value to assign based on the merge strategy.\n *\n * - `\"lastWins\"` → Overwrites with the latest value.\n * - Custom functions → Allows user-defined behavior.\n */\nfunction applyMergeStrategy<T, U>(target: T, source: U, strategy: MergeStrategy): T | U {\n if (typeof strategy === 'function') return strategy(target, source);\n return strategy === 'lastWins' || source !== undefined ? source : target;\n}\n"],"names":["merge","strategy","items","acc","obj","deepMerge","target","source","isObject","result","key","sourceValue","targetValue","isArray","handleArrayMerge","applyMergeStrategy","targetArray","sourceArray"],"mappings":"2JAsDO,SAASA,EAAuBC,EAA0B,UAAWC,EAAyB,CACnG,OAAIA,EAAM,SAAW,EAAU,CAAA,EAE3BD,IAAa,UACR,OAAO,OAAO,CAAA,EAAI,GAAGC,CAAK,EAG5BA,EAAM,OAAO,CAACC,EAAKC,IAAQC,EAAUF,EAAKC,EAAKH,CAAQ,EAA0B,EAAc,CACxG,CAcA,SAASI,EAAwCC,EAAWC,EAAWN,EAA0C,CAC/G,GAAI,CAACO,EAAAA,SAASD,CAAM,EAAG,OAAOA,EAE9B,MAAME,EAAS,CAAE,GAAGH,CAAA,EAEpB,UAAWI,KAAOH,EAAQ,CACxB,GAAI,CAAC,OAAO,OAAOA,EAAQG,CAAG,EAAG,SAEjC,MAAMC,EAAcJ,EAAOG,CAAG,EACxBE,EAAcH,EAAOC,CAAG,EAG7BD,EAAeC,CAAG,EACjBG,EAAAA,QAAQF,CAAW,GAAKE,EAAAA,QAAQD,CAAW,EACvCE,EAAiBF,EAAaD,EAAaV,CAAQ,EACnDO,EAAAA,SAASG,CAAW,GAAKH,EAAAA,SAASI,CAAW,EAC3CP,EAAUO,EAAaD,EAAaV,CAAQ,EAC5Cc,EAAmBH,EAAaD,EAAaV,CAAQ,CAC/D,CAEA,OAAOQ,CACT,CASA,SAASK,EAAuBE,EAA8BC,EAAkBhB,EAAoC,CAClH,OAAKe,EAEDf,IAAa,cAAsBe,EAAY,OAAOC,CAAkB,EACxEhB,IAAa,eAAuBgB,EACjC,MAAM,KAAK,IAAI,IAAI,CAAC,GAAGD,EAAa,GAAGC,CAAW,CAAC,CAAC,EAJlCA,CAK3B,CAQA,SAASF,EAAyBT,EAAWC,EAAWN,EAAgC,CACtF,OAAI,OAAOA,GAAa,WAAmBA,EAASK,EAAQC,CAAM,EAC3DN,IAAa,YAAcM,IAAW,OAAYA,EAASD,CACpE"}
|
package/dist/object/merge.js
CHANGED
|
@@ -1,27 +1,23 @@
|
|
|
1
1
|
import { isArray as c } from "../typed/isArray.js";
|
|
2
2
|
import { isObject as u } from "../typed/isObject.js";
|
|
3
|
-
function m(
|
|
4
|
-
return n.length === 0 ? {} :
|
|
5
|
-
// @ts-expect-error
|
|
6
|
-
(e, i) => l(e, i, r),
|
|
7
|
-
{}
|
|
8
|
-
);
|
|
3
|
+
function m(i = "deep", ...n) {
|
|
4
|
+
return n.length === 0 ? {} : i === "shallow" ? Object.assign({}, ...n) : n.reduce((e, r) => l(e, r, i), {});
|
|
9
5
|
}
|
|
10
|
-
function l(
|
|
6
|
+
function l(i, n, e) {
|
|
11
7
|
if (!u(n)) return n;
|
|
12
|
-
const
|
|
8
|
+
const r = { ...i };
|
|
13
9
|
for (const o in n) {
|
|
14
10
|
if (!Object.hasOwn(n, o)) continue;
|
|
15
|
-
const
|
|
16
|
-
|
|
11
|
+
const f = n[o], t = r[o];
|
|
12
|
+
r[o] = c(f) && c(t) ? p(t, f, e) : u(f) && u(t) ? l(t, f, e) : a(t, f, e);
|
|
17
13
|
}
|
|
18
|
-
return
|
|
14
|
+
return r;
|
|
19
15
|
}
|
|
20
|
-
function p(
|
|
21
|
-
return
|
|
16
|
+
function p(i, n, e) {
|
|
17
|
+
return i ? e === "arrayConcat" ? i.concat(n) : e === "arrayReplace" ? n : Array.from(/* @__PURE__ */ new Set([...i, ...n])) : n;
|
|
22
18
|
}
|
|
23
|
-
function a(
|
|
24
|
-
return typeof e == "function" ? e(
|
|
19
|
+
function a(i, n, e) {
|
|
20
|
+
return typeof e == "function" ? e(i, n) : e === "lastWins" || n !== void 0 ? n : i;
|
|
25
21
|
}
|
|
26
22
|
export {
|
|
27
23
|
m as merge
|
package/dist/object/merge.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"merge.js","sources":["../../src/object/merge.ts"],"sourcesContent":["import { isArray } from '../typed/isArray';\nimport { isObject } from '../typed/isObject';\nimport type { Obj } from '../types';\n\ntype MergeStrategy =\n | 'deep'\n | 'shallow'\n | 'lastWins'\n | 'arrayConcat'\n | 'arrayReplace'\n // biome-ignore lint/suspicious/noExplicitAny: -\n | ((target: any, source: any) => any);\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\n/**\n * Merges multiple objects based on a specified merge strategy.\n *\n * @example\n * ```ts\n * const obj1 = { a: 1, b: { x: 10, y: \"hello\" }, c: [1] };\n * const obj2 = { b: { y: 20, z: true }, c: [2] };\n * const obj3 = { d: false, c: [3] };\n *\n * merge(\"deep\", obj1, obj2, obj3); // { a: 1, b: { x: 10, y: 20, z: true }, c: [1, 2, 3], d: false }\n * merge(\"shallow\", obj1, obj2, obj3); // { a: 1, b: { y: 20, z: true }, c: [3], d: false }\n * ```\n *\n * @param [strategy='deep'] - The merging strategy to use.\n * @param items - The objects to merge.\n * @returns A new merged object.\n */\nexport function merge<T extends Obj[]>(strategy: MergeStrategy = 'deep', ...items: [...T]): Merge<T> {\n if (items.length === 0) return {} as Merge<T>;\n\n if (strategy === 'shallow') {\n return Object.assign({}, ...items) as Merge<T>;\n }\n\n
|
|
1
|
+
{"version":3,"file":"merge.js","sources":["../../src/object/merge.ts"],"sourcesContent":["import { isArray } from '../typed/isArray';\nimport { isObject } from '../typed/isObject';\nimport type { Obj } from '../types';\n\n// #region MergeStrategy\ntype MergeStrategy =\n | 'deep'\n | 'shallow'\n | 'lastWins'\n | 'arrayConcat'\n | 'arrayReplace'\n // biome-ignore lint/suspicious/noExplicitAny: -\n | ((target: any, source: any) => any);\n// #endregion MergeStrategy\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\n/**\n * Merges multiple objects based on a specified merge strategy.\n *\n * @example\n * ```ts\n * const obj1 = { a: 1, b: { x: 10, y: \"hello\" }, c: [1] };\n * const obj2 = { b: { y: 20, z: true }, c: [2] };\n * const obj3 = { d: false, c: [3] };\n *\n * merge(\"deep\", obj1, obj2, obj3); // { a: 1, b: { x: 10, y: 20, z: true }, c: [1, 2, 3], d: false }\n * merge(\"shallow\", obj1, obj2, obj3); // { a: 1, b: { y: 20, z: true }, c: [3], d: false }\n * ```\n *\n * @param [strategy='deep'] - The merging strategy to use.\n * @param items - The objects to merge.\n * @returns A new merged object.\n */\nexport function merge<T extends Obj[]>(strategy: MergeStrategy = 'deep', ...items: [...T]): Merge<T> {\n if (items.length === 0) return {} as Merge<T>;\n\n if (strategy === 'shallow') {\n return Object.assign({}, ...items) as Merge<T>;\n }\n\n return items.reduce((acc, obj) => deepMerge(acc, obj, strategy) as unknown as Merge<T>, {} as Merge<T>);\n}\n\n/**\n * Deeply merges two objects based on the provided strategy.\n *\n * - Uses **direct property access** for performance.\n * - **Avoids redundant deep merging** where unnecessary.\n * - Optimized **array merging strategies**.\n *\n * @param target - The target object.\n * @param source - The source object.\n * @param strategy - The merge strategy.\n * @returns A new merged object.\n */\nfunction deepMerge<T extends Obj, U extends Obj>(target: T, source: U, strategy: MergeStrategy): DeepMerge<T, U> {\n if (!isObject(source)) return source as DeepMerge<T, U>;\n\n const result = { ...target } as DeepMerge<T, U>;\n\n for (const key in source) {\n if (!Object.hasOwn(source, key)) continue; // Prevent prototype pollution\n\n const sourceValue = source[key];\n const targetValue = result[key];\n\n // biome-ignore lint/suspicious/noExplicitAny: -\n (result as any)[key] =\n isArray(sourceValue) && isArray(targetValue)\n ? handleArrayMerge(targetValue, sourceValue, strategy)\n : isObject(sourceValue) && isObject(targetValue)\n ? deepMerge(targetValue, sourceValue, strategy)\n : applyMergeStrategy(targetValue, sourceValue, strategy);\n }\n\n return result;\n}\n\n/**\n * Optimized array merge based on strategy.\n *\n * - `\"arrayConcat\"` → Concatenates arrays.\n * - `\"arrayReplace\"` → Replaces the existing array.\n * - Default: **Unique merge** (Set-based optimization).\n */\nfunction handleArrayMerge<T, U>(targetArray: T[] | undefined, sourceArray: U[], strategy: MergeStrategy): (T | U)[] {\n if (!targetArray) return sourceArray;\n // biome-ignore lint/suspicious/noExplicitAny: -\n if (strategy === 'arrayConcat') return targetArray.concat(sourceArray as any);\n if (strategy === 'arrayReplace') return sourceArray;\n return Array.from(new Set([...targetArray, ...sourceArray])); // Unique merge\n}\n\n/**\n * Determines the appropriate value to assign based on the merge strategy.\n *\n * - `\"lastWins\"` → Overwrites with the latest value.\n * - Custom functions → Allows user-defined behavior.\n */\nfunction applyMergeStrategy<T, U>(target: T, source: U, strategy: MergeStrategy): T | U {\n if (typeof strategy === 'function') return strategy(target, source);\n return strategy === 'lastWins' || source !== undefined ? source : target;\n}\n"],"names":["merge","strategy","items","acc","obj","deepMerge","target","source","isObject","result","key","sourceValue","targetValue","isArray","handleArrayMerge","applyMergeStrategy","targetArray","sourceArray"],"mappings":";;AAsDO,SAASA,EAAuBC,IAA0B,WAAWC,GAAyB;AACnG,SAAIA,EAAM,WAAW,IAAU,CAAA,IAE3BD,MAAa,YACR,OAAO,OAAO,CAAA,GAAI,GAAGC,CAAK,IAG5BA,EAAM,OAAO,CAACC,GAAKC,MAAQC,EAAUF,GAAKC,GAAKH,CAAQ,GAA0B,EAAc;AACxG;AAcA,SAASI,EAAwCC,GAAWC,GAAWN,GAA0C;AAC/G,MAAI,CAACO,EAASD,CAAM,EAAG,QAAOA;AAE9B,QAAME,IAAS,EAAE,GAAGH,EAAA;AAEpB,aAAWI,KAAOH,GAAQ;AACxB,QAAI,CAAC,OAAO,OAAOA,GAAQG,CAAG,EAAG;AAEjC,UAAMC,IAAcJ,EAAOG,CAAG,GACxBE,IAAcH,EAAOC,CAAG;AAG7B,IAAAD,EAAeC,CAAG,IACjBG,EAAQF,CAAW,KAAKE,EAAQD,CAAW,IACvCE,EAAiBF,GAAaD,GAAaV,CAAQ,IACnDO,EAASG,CAAW,KAAKH,EAASI,CAAW,IAC3CP,EAAUO,GAAaD,GAAaV,CAAQ,IAC5Cc,EAAmBH,GAAaD,GAAaV,CAAQ;AAAA,EAC/D;AAEA,SAAOQ;AACT;AASA,SAASK,EAAuBE,GAA8BC,GAAkBhB,GAAoC;AAClH,SAAKe,IAEDf,MAAa,gBAAsBe,EAAY,OAAOC,CAAkB,IACxEhB,MAAa,iBAAuBgB,IACjC,MAAM,KAAK,oBAAI,IAAI,CAAC,GAAGD,GAAa,GAAGC,CAAW,CAAC,CAAC,IAJlCA;AAK3B;AAQA,SAASF,EAAyBT,GAAWC,GAAWN,GAAgC;AACtF,SAAI,OAAOA,KAAa,aAAmBA,EAASK,GAAQC,CAAM,IAC3DN,MAAa,cAAcM,MAAW,SAAYA,IAASD;AACpE;"}
|