es-toolkit 1.8.0 → 1.9.0-dev.170
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/CHANGELOG.md +9 -0
- package/dist/array/flatten.d.mts +19 -0
- package/dist/array/flatten.d.ts +19 -0
- package/dist/array/flatten.js +45 -0
- package/dist/array/flatten.js.map +1 -0
- package/dist/array/flatten.mjs +8 -0
- package/dist/array/head.d.mts +22 -0
- package/dist/array/head.d.ts +22 -0
- package/dist/array/head.js +33 -0
- package/dist/array/head.js.map +1 -0
- package/dist/array/head.mjs +8 -0
- package/dist/array/head.mjs.map +1 -0
- package/dist/array/index.d.mts +4 -0
- package/dist/array/index.d.ts +4 -0
- package/dist/array/index.js +61 -0
- package/dist/array/index.js.map +1 -1
- package/dist/array/index.mjs +59 -43
- package/dist/array/tail.d.mts +38 -0
- package/dist/array/tail.d.ts +38 -0
- package/dist/array/tail.js +41 -0
- package/dist/array/tail.js.map +1 -0
- package/dist/array/tail.mjs +8 -0
- package/dist/array/tail.mjs.map +1 -0
- package/dist/array/unzip.d.mts +18 -0
- package/dist/array/unzip.d.ts +18 -0
- package/dist/array/unzip.js +46 -0
- package/dist/array/unzip.js.map +1 -0
- package/dist/array/unzip.mjs +8 -0
- package/dist/array/unzip.mjs.map +1 -0
- package/dist/chunk-33MSBDC6.mjs +21 -0
- package/dist/chunk-33MSBDC6.mjs.map +1 -0
- package/dist/{chunk-FLTT2MGF.mjs → chunk-74WWMNZS.mjs} +1 -1
- package/dist/{chunk-FLTT2MGF.mjs.map → chunk-74WWMNZS.mjs.map} +1 -1
- package/dist/{chunk-UNLX2F2Y.mjs → chunk-DPH2KE6O.mjs} +1 -1
- package/dist/chunk-DPH2KE6O.mjs.map +1 -0
- package/dist/chunk-HM2JT2RX.mjs +9 -0
- package/dist/chunk-HM2JT2RX.mjs.map +1 -0
- package/dist/chunk-LOY6PATI.mjs +1 -0
- package/dist/chunk-LOY6PATI.mjs.map +1 -0
- package/dist/chunk-OF6NM4G7.mjs +17 -0
- package/dist/chunk-OF6NM4G7.mjs.map +1 -0
- package/dist/chunk-UYZKMBDO.mjs +22 -0
- package/dist/chunk-UYZKMBDO.mjs.map +1 -0
- package/dist/index.d.mts +4 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +61 -0
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +101 -85
- package/dist/math/index.js.map +1 -1
- package/dist/math/index.mjs +3 -3
- package/dist/math/range.d.mts +5 -6
- package/dist/math/range.d.ts +5 -6
- package/dist/math/range.js.map +1 -1
- package/dist/math/range.mjs +1 -1
- package/dist/object/index.js.map +1 -1
- package/dist/object/index.mjs +6 -6
- package/dist/object/invert.d.mts +0 -1
- package/dist/object/invert.d.ts +0 -1
- package/dist/object/invert.js.map +1 -1
- package/dist/object/invert.mjs +1 -1
- package/package.json +1 -1
- package/dist/chunk-OC7ZNKWX.mjs +0 -1
- package/dist/chunk-UNLX2F2Y.mjs.map +0 -1
- /package/dist/{chunk-OC7ZNKWX.mjs.map → array/flatten.mjs.map} +0 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/array/tail.ts"],"sourcesContent":["/**\n * Returns an empty array when the input is a single-element array.\n *\n * @param {readonly [T]} arr - The single-element array to process.\n * @returns {[]} An empty array.\n *\n * @example\n * const arr = [1];\n * const result = tail(arr);\n * // result will be []\n */\nexport function tail<T>(arr: readonly [T]): [];\n\n/**\n * Returns an empty array when the input is an empty array.\n *\n * @param {readonly []} arr - The empty array to process.\n * @returns {[]} An empty array.\n *\n * @example\n * const arr = [];\n * const result = tail(arr);\n * // result will be []\n */\nexport function tail(arr: readonly []): [];\n\n/**\n * Returns a new array with all elements except for the first when the input is a tuple array.\n *\n * @param {readonly [T, ...U[]]} arr - The tuple array to process.\n * @returns {U[]} A new array containing all elements of the input array except for the first one.\n *\n * @example\n * const arr = [1, 2, 3];\n * const result = tail(arr);\n * // result will be [2, 3]\n */\nexport function tail<T, U>(arr: readonly [T, ...U[]]): U[];\n\n/**\n * Returns a new array with all elements except for the first.\n *\n * This function takes an array and returns a new array containing all the elements\n * except for the first one. If the input array is empty or has only one element,\n * an empty array is returned.\n *\n * @param {readonly T[]} arr - The array to get the tail of.\n * @returns {T[]} A new array containing all elements of the input array except for the first one.\n *\n * @example\n * const arr1 = [1, 2, 3];\n * const result = tail(arr1);\n * // result will be [2, 3]\n *\n * const arr2 = [1];\n * const result2 = tail(arr2);\n * // result2 will be []\n *\n * const arr3 = [];\n * const result3 = tail(arr3);\n * // result3 will be []\n */\nexport function tail<T>(arr: readonly T[]): T[] {\n const len = arr.length;\n if (len <= 1) {\n return [];\n }\n const result = new Array(len - 1);\n for (let i = 1; i < len; i++) {\n result[i - 1] = arr[i];\n }\n return result;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AA8DO,SAAS,KAAQ,KAAwB;AAC9C,QAAM,MAAM,IAAI;AAChB,MAAI,OAAO,GAAG;AACZ,WAAO,CAAC;AAAA,EACV;AACA,QAAM,SAAS,IAAI,MAAM,MAAM,CAAC;AAChC,WAAS,IAAI,GAAG,IAAI,KAAK,KAAK;AAC5B,WAAO,IAAI,CAAC,IAAI,IAAI,CAAC;AAAA,EACvB;AACA,SAAO;AACT;","names":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Gathers elements in the same position in an internal array
|
|
3
|
+
* from a grouped array of elements and returns them as a new array.
|
|
4
|
+
*
|
|
5
|
+
* @param zipped - The nested array to unzip.
|
|
6
|
+
* @returns A new array of unzipped elements.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* const zipped = [['a', true, 1],['b', false, 2]];
|
|
10
|
+
* const result = unzip(zipped);
|
|
11
|
+
* // result will be [['a', 'b'], [true, false], [1, 2]]
|
|
12
|
+
*/
|
|
13
|
+
declare function unzip<T extends unknown[]>(zipped: Array<[...T]>): Unzip<T>;
|
|
14
|
+
type Unzip<K extends unknown[]> = {
|
|
15
|
+
[I in keyof K]: Array<K[I]>;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export { unzip };
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Gathers elements in the same position in an internal array
|
|
3
|
+
* from a grouped array of elements and returns them as a new array.
|
|
4
|
+
*
|
|
5
|
+
* @param zipped - The nested array to unzip.
|
|
6
|
+
* @returns A new array of unzipped elements.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* const zipped = [['a', true, 1],['b', false, 2]];
|
|
10
|
+
* const result = unzip(zipped);
|
|
11
|
+
* // result will be [['a', 'b'], [true, false], [1, 2]]
|
|
12
|
+
*/
|
|
13
|
+
declare function unzip<T extends unknown[]>(zipped: Array<[...T]>): Unzip<T>;
|
|
14
|
+
type Unzip<K extends unknown[]> = {
|
|
15
|
+
[I in keyof K]: Array<K[I]>;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export { unzip };
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/array/unzip.ts
|
|
21
|
+
var unzip_exports = {};
|
|
22
|
+
__export(unzip_exports, {
|
|
23
|
+
unzip: () => unzip
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(unzip_exports);
|
|
26
|
+
function unzip(zipped) {
|
|
27
|
+
let maxLen = 0;
|
|
28
|
+
for (let i = 0; i < zipped.length; i++) {
|
|
29
|
+
if (zipped[i].length > maxLen) {
|
|
30
|
+
maxLen = zipped[i].length;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
const result = new Array(maxLen);
|
|
34
|
+
for (let i = 0; i < maxLen; i++) {
|
|
35
|
+
result[i] = new Array(zipped.length);
|
|
36
|
+
for (let j = 0; j < zipped.length; j++) {
|
|
37
|
+
result[i][j] = zipped[j][i];
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
return result;
|
|
41
|
+
}
|
|
42
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
43
|
+
0 && (module.exports = {
|
|
44
|
+
unzip
|
|
45
|
+
});
|
|
46
|
+
//# sourceMappingURL=unzip.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/array/unzip.ts"],"sourcesContent":["/**\n * Gathers elements in the same position in an internal array \n * from a grouped array of elements and returns them as a new array.\n *\n * @param zipped - The nested array to unzip.\n * @returns A new array of unzipped elements.\n *\n * @example\n * const zipped = [['a', true, 1],['b', false, 2]];\n * const result = unzip(zipped);\n * // result will be [['a', 'b'], [true, false], [1, 2]]\n */\nexport function unzip<T extends unknown[]>(zipped: Array<[...T]>): Unzip<T> {\n // For performance reasons, use this implementation instead of\n // const maxLen = Math.max(...zipped.map(arr => arr.length));\n let maxLen = 0;\n\n for (let i = 0; i < zipped.length; i++) {\n if (zipped[i].length > maxLen) {\n maxLen = zipped[i].length;\n }\n }\n\n const result = new Array(maxLen) as Unzip<T>;\n\n for (let i = 0; i < maxLen; i++) {\n result[i] = new Array(zipped.length);\n for (let j = 0; j < zipped.length; j++) {\n result[i][j] = zipped[j][i];\n }\n }\n\n return result;\n}\n\ntype Unzip<K extends unknown[]> = { [I in keyof K]: Array<K[I]> };\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAYO,SAAS,MAA2B,QAAiC;AAG1E,MAAI,SAAS;AAEb,WAAS,IAAI,GAAG,IAAI,OAAO,QAAQ,KAAK;AACtC,QAAI,OAAO,CAAC,EAAE,SAAS,QAAQ;AAC7B,eAAS,OAAO,CAAC,EAAE;AAAA,IACrB;AAAA,EACF;AAEA,QAAM,SAAS,IAAI,MAAM,MAAM;AAE/B,WAAS,IAAI,GAAG,IAAI,QAAQ,KAAK;AAC/B,WAAO,CAAC,IAAI,IAAI,MAAM,OAAO,MAAM;AACnC,aAAS,IAAI,GAAG,IAAI,OAAO,QAAQ,KAAK;AACtC,aAAO,CAAC,EAAE,CAAC,IAAI,OAAO,CAAC,EAAE,CAAC;AAAA,IAC5B;AAAA,EACF;AAEA,SAAO;AACT;","names":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
// src/array/flatten.ts
|
|
2
|
+
function flatten(arr, depth = 1) {
|
|
3
|
+
const result = [];
|
|
4
|
+
const flooredDepth = Math.floor(depth);
|
|
5
|
+
const recursive = (arr2, currentDepth) => {
|
|
6
|
+
for (const item of arr2) {
|
|
7
|
+
if (Array.isArray(item) && currentDepth < flooredDepth) {
|
|
8
|
+
recursive(item, currentDepth + 1);
|
|
9
|
+
} else {
|
|
10
|
+
result.push(item);
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
};
|
|
14
|
+
recursive(arr, 0);
|
|
15
|
+
return result;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export {
|
|
19
|
+
flatten
|
|
20
|
+
};
|
|
21
|
+
//# sourceMappingURL=chunk-33MSBDC6.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/array/flatten.ts"],"sourcesContent":["/**\n * Flattens an array up to the specified depth.\n *\n * @template T - The type of elements within the array.\n * @template D - The depth to which the array should be flattened.\n * @param {T[]} arr - The array to flatten.\n * @param {D} depth - The depth level specifying how deep a nested array structure should be flattened. Defaults to 1.\n * @returns {Array<FlatArray<T[], D>>} A new array that has been flattened.\n *\n * @example\n * const arr = flatten([1, [2, 3], [4, [5, 6]]], 1);\n * // Returns: [1, 2, 3, 4, [5, 6]]\n *\n * const arr = flatten([1, [2, 3], [4, [5, 6]]], 2);\n * // Returns: [1, 2, 3, 4, 5, 6]\n */\nexport function flatten<T, D extends number = 1>(arr: readonly T[], depth = 1 as D): Array<FlatArray<T[], D>> {\n const result: Array<FlatArray<T[], D>> = [];\n const flooredDepth = Math.floor(depth);\n\n const recursive = (arr: readonly T[], currentDepth: number) => {\n for (const item of arr) {\n if (Array.isArray(item) && currentDepth < flooredDepth) {\n recursive(item, currentDepth + 1);\n } else {\n result.push(item as FlatArray<T[], D>);\n }\n }\n };\n\n recursive(arr, 0);\n return result;\n}\n"],"mappings":";AAgBO,SAAS,QAAiC,KAAmB,QAAQ,GAAkC;AAC5G,QAAM,SAAmC,CAAC;AAC1C,QAAM,eAAe,KAAK,MAAM,KAAK;AAErC,QAAM,YAAY,CAACA,MAAmB,iBAAyB;AAC7D,eAAW,QAAQA,MAAK;AACtB,UAAI,MAAM,QAAQ,IAAI,KAAK,eAAe,cAAc;AACtD,kBAAU,MAAM,eAAe,CAAC;AAAA,MAClC,OAAO;AACL,eAAO,KAAK,IAAyB;AAAA,MACvC;AAAA,IACF;AAAA,EACF;AAEA,YAAU,KAAK,CAAC;AAChB,SAAO;AACT;","names":["arr"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/object/invert.ts"],"sourcesContent":["/**\n * Inverts the keys and values of an object. The keys of the input object become the values of the output object and vice versa.\n *\n * This function takes an object and creates a new object by inverting its keys and values. If the input object has duplicate values,\n * the key of the last occurrence will be used as the value for the new key in the output object. It effectively creates a reverse mapping\n * of the input object's key-value pairs.\n *\n * @template K - Type of the keys in the input object (string, number, symbol)\n * @template V - Type of the values in the input object (string, number, symbol)\n * @param {Record<K, V>} obj - The input object whose keys and values are to be inverted\n * @returns {{ [key in V]: K }} - A new object with keys and values inverted\n *\n * @example\n * invert({ a: 1, b: 2, c: 3 }); // { 1: 'a', 2: 'b', 3: 'c' }\n * invert({ 1: 'a', 2: 'b', 3: 'c' }); // { a: '1', b: '2', c: '3' }\n * invert({ a: 1, 2: 'b', c: 3, 4: 'd' }); // { 1: 'a', b: '2', 3: 'c', d: '4' }\n * invert({ a: Symbol('sym1'), b: Symbol('sym2') }); // { [Symbol('sym1')]: 'a', [Symbol('sym2')]: 'b' }\n */\
|
|
1
|
+
{"version":3,"sources":["../src/object/invert.ts"],"sourcesContent":["/**\n * Inverts the keys and values of an object. The keys of the input object become the values of the output object and vice versa.\n *\n * This function takes an object and creates a new object by inverting its keys and values. If the input object has duplicate values,\n * the key of the last occurrence will be used as the value for the new key in the output object. It effectively creates a reverse mapping\n * of the input object's key-value pairs.\n *\n * @template K - Type of the keys in the input object (string, number, symbol)\n * @template V - Type of the values in the input object (string, number, symbol)\n * @param {Record<K, V>} obj - The input object whose keys and values are to be inverted\n * @returns {{ [key in V]: K }} - A new object with keys and values inverted\n *\n * @example\n * invert({ a: 1, b: 2, c: 3 }); // { 1: 'a', 2: 'b', 3: 'c' }\n * invert({ 1: 'a', 2: 'b', 3: 'c' }); // { a: '1', b: '2', c: '3' }\n * invert({ a: 1, 2: 'b', c: 3, 4: 'd' }); // { 1: 'a', b: '2', 3: 'c', d: '4' }\n * invert({ a: Symbol('sym1'), b: Symbol('sym2') }); // { [Symbol('sym1')]: 'a', [Symbol('sym2')]: 'b' }\n */\nexport function invert<K extends PropertyKey, V extends PropertyKey>(obj: Record<K, V>): { [key in V]: K } {\n const result = {} as { [key in V]: K };\n\n for (const key in obj) {\n const value = obj[key as K] as V;\n result[value] = key;\n }\n\n return result;\n}\n"],"mappings":";AAkBO,SAAS,OAAqD,KAAsC;AACzG,QAAM,SAAS,CAAC;AAEhB,aAAW,OAAO,KAAK;AACrB,UAAM,QAAQ,IAAI,GAAQ;AAC1B,WAAO,KAAK,IAAI;AAAA,EAClB;AAEA,SAAO;AACT;","names":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/math/range.ts"],"sourcesContent":["/**\n * Returns an array of numbers from `start` to `end`, incrementing by `step`.\n *\n * If `step` is not provided, it defaults to `1`.\n *\n * @param {number} start - The starting number of the range (inclusive).\n * @param {number} [end] - The end number of the range (exclusive).\n * @param {number} [step] - The step value for the range. (default: 1)\n * @returns {number[]} An array of numbers from `start` to `end` with the specified `step`.\n *\n * @example\n * // Returns [0, 1, 2, 3]\n * range(4);\n *\n * @example\n * // Returns [0, 5, 10, 15]\n * range(0, 20, 5);\n *\n * @example\n * // Returns [0, -1, -2, -3]\n * range(0, -4, -1);\n *\n * @example\n * // Throws an error: The step value must be a non-zero integer.\n * range(1, 4, 0);\n */\nexport function range(end: number): number[];\nexport function range(start: number, end: number): number[];\nexport function range(start: number, end: number, step: number): number[];\nexport function range(start: number, end?: number, step?: number): number[] {\n if (end == null) {\n end = start;\n start = 0;\n }\n\n if (step == null) {\n step = 1;\n }\n\n if (!Number.isInteger(step) || step === 0) {\n throw new Error(`The step value must be a non-zero integer.`);\n }\n\n const length = Math.max(Math.ceil((end - start) / step), 0);\n const result = new Array(length);\n\n for (let i = 0; i < length; i++) {\n result[i] = start + i * step;\n }\n\n return result;\n}\n"],"mappings":";AA6BO,SAAS,MAAM,OAAe,KAAc,MAAyB;AAC1E,MAAI,OAAO,MAAM;AACf,UAAM;AACN,YAAQ;AAAA,EACV;AAEA,MAAI,QAAQ,MAAM;AAChB,WAAO;AAAA,EACT;AAEA,MAAI,CAAC,OAAO,UAAU,IAAI,KAAK,SAAS,GAAG;AACzC,UAAM,IAAI,MAAM,4CAA4C;AAAA,EAC9D;AAEA,QAAM,SAAS,KAAK,IAAI,KAAK,MAAM,MAAM,SAAS,IAAI,GAAG,CAAC;AAC1D,QAAM,SAAS,IAAI,MAAM,MAAM;AAE/B,WAAS,IAAI,GAAG,IAAI,QAAQ,KAAK;AAC/B,WAAO,CAAC,IAAI,QAAQ,IAAI;AAAA,EAC1B;AAEA,SAAO;AACT;","names":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/array/head.ts"],"sourcesContent":["/**\n * Returns the first element of an array.\n *\n * This function takes an array and returns the first element of the array.\n * If the array is empty, the function returns `undefined`.\n *\n * @param {T[]} arr - The array from which to get the first element.\n * @returns {T | undefined} The first element of the array, or `undefined` if the array is empty.\n *\n * @example\n * const arr = [1, 2, 3];\n * const firstElement = head(arr);\n * // firstElement will be 1\n *\n * const emptyArr: number[] = [];\n * const noElement = head(emptyArr);\n * // noElement will be undefined\n */\nexport function head<T>(arr: readonly [T, ...T[]]): T;\nexport function head<T>(arr: readonly T[]): T | undefined;\nexport function head<T>(arr: readonly T[]): T | undefined {\n return arr[0];\n}\n"],"mappings":";AAoBO,SAAS,KAAQ,KAAkC;AACxD,SAAO,IAAI,CAAC;AACd;","names":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
//# sourceMappingURL=chunk-LOY6PATI.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
// src/array/tail.ts
|
|
2
|
+
function tail(arr) {
|
|
3
|
+
const len = arr.length;
|
|
4
|
+
if (len <= 1) {
|
|
5
|
+
return [];
|
|
6
|
+
}
|
|
7
|
+
const result = new Array(len - 1);
|
|
8
|
+
for (let i = 1; i < len; i++) {
|
|
9
|
+
result[i - 1] = arr[i];
|
|
10
|
+
}
|
|
11
|
+
return result;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export {
|
|
15
|
+
tail
|
|
16
|
+
};
|
|
17
|
+
//# sourceMappingURL=chunk-OF6NM4G7.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/array/tail.ts"],"sourcesContent":["/**\n * Returns an empty array when the input is a single-element array.\n *\n * @param {readonly [T]} arr - The single-element array to process.\n * @returns {[]} An empty array.\n *\n * @example\n * const arr = [1];\n * const result = tail(arr);\n * // result will be []\n */\nexport function tail<T>(arr: readonly [T]): [];\n\n/**\n * Returns an empty array when the input is an empty array.\n *\n * @param {readonly []} arr - The empty array to process.\n * @returns {[]} An empty array.\n *\n * @example\n * const arr = [];\n * const result = tail(arr);\n * // result will be []\n */\nexport function tail(arr: readonly []): [];\n\n/**\n * Returns a new array with all elements except for the first when the input is a tuple array.\n *\n * @param {readonly [T, ...U[]]} arr - The tuple array to process.\n * @returns {U[]} A new array containing all elements of the input array except for the first one.\n *\n * @example\n * const arr = [1, 2, 3];\n * const result = tail(arr);\n * // result will be [2, 3]\n */\nexport function tail<T, U>(arr: readonly [T, ...U[]]): U[];\n\n/**\n * Returns a new array with all elements except for the first.\n *\n * This function takes an array and returns a new array containing all the elements\n * except for the first one. If the input array is empty or has only one element,\n * an empty array is returned.\n *\n * @param {readonly T[]} arr - The array to get the tail of.\n * @returns {T[]} A new array containing all elements of the input array except for the first one.\n *\n * @example\n * const arr1 = [1, 2, 3];\n * const result = tail(arr1);\n * // result will be [2, 3]\n *\n * const arr2 = [1];\n * const result2 = tail(arr2);\n * // result2 will be []\n *\n * const arr3 = [];\n * const result3 = tail(arr3);\n * // result3 will be []\n */\nexport function tail<T>(arr: readonly T[]): T[] {\n const len = arr.length;\n if (len <= 1) {\n return [];\n }\n const result = new Array(len - 1);\n for (let i = 1; i < len; i++) {\n result[i - 1] = arr[i];\n }\n return result;\n}\n"],"mappings":";AA8DO,SAAS,KAAQ,KAAwB;AAC9C,QAAM,MAAM,IAAI;AAChB,MAAI,OAAO,GAAG;AACZ,WAAO,CAAC;AAAA,EACV;AACA,QAAM,SAAS,IAAI,MAAM,MAAM,CAAC;AAChC,WAAS,IAAI,GAAG,IAAI,KAAK,KAAK;AAC5B,WAAO,IAAI,CAAC,IAAI,IAAI,CAAC;AAAA,EACvB;AACA,SAAO;AACT;","names":[]}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
// src/array/unzip.ts
|
|
2
|
+
function unzip(zipped) {
|
|
3
|
+
let maxLen = 0;
|
|
4
|
+
for (let i = 0; i < zipped.length; i++) {
|
|
5
|
+
if (zipped[i].length > maxLen) {
|
|
6
|
+
maxLen = zipped[i].length;
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
const result = new Array(maxLen);
|
|
10
|
+
for (let i = 0; i < maxLen; i++) {
|
|
11
|
+
result[i] = new Array(zipped.length);
|
|
12
|
+
for (let j = 0; j < zipped.length; j++) {
|
|
13
|
+
result[i][j] = zipped[j][i];
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
return result;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export {
|
|
20
|
+
unzip
|
|
21
|
+
};
|
|
22
|
+
//# sourceMappingURL=chunk-UYZKMBDO.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/array/unzip.ts"],"sourcesContent":["/**\n * Gathers elements in the same position in an internal array \n * from a grouped array of elements and returns them as a new array.\n *\n * @param zipped - The nested array to unzip.\n * @returns A new array of unzipped elements.\n *\n * @example\n * const zipped = [['a', true, 1],['b', false, 2]];\n * const result = unzip(zipped);\n * // result will be [['a', 'b'], [true, false], [1, 2]]\n */\nexport function unzip<T extends unknown[]>(zipped: Array<[...T]>): Unzip<T> {\n // For performance reasons, use this implementation instead of\n // const maxLen = Math.max(...zipped.map(arr => arr.length));\n let maxLen = 0;\n\n for (let i = 0; i < zipped.length; i++) {\n if (zipped[i].length > maxLen) {\n maxLen = zipped[i].length;\n }\n }\n\n const result = new Array(maxLen) as Unzip<T>;\n\n for (let i = 0; i < maxLen; i++) {\n result[i] = new Array(zipped.length);\n for (let j = 0; j < zipped.length; j++) {\n result[i][j] = zipped[j][i];\n }\n }\n\n return result;\n}\n\ntype Unzip<K extends unknown[]> = { [I in keyof K]: Array<K[I]> };\n"],"mappings":";AAYO,SAAS,MAA2B,QAAiC;AAG1E,MAAI,SAAS;AAEb,WAAS,IAAI,GAAG,IAAI,OAAO,QAAQ,KAAK;AACtC,QAAI,OAAO,CAAC,EAAE,SAAS,QAAQ;AAC7B,eAAS,OAAO,CAAC,EAAE;AAAA,IACrB;AAAA,EACF;AAEA,QAAM,SAAS,IAAI,MAAM,MAAM;AAE/B,WAAS,IAAI,GAAG,IAAI,QAAQ,KAAK;AAC/B,WAAO,CAAC,IAAI,IAAI,MAAM,OAAO,MAAM;AACnC,aAAS,IAAI,GAAG,IAAI,OAAO,QAAQ,KAAK;AACtC,aAAO,CAAC,EAAE,CAAC,IAAI,OAAO,CAAC,EAAE,CAAC;AAAA,IAC5B;AAAA,EACF;AAEA,SAAO;AACT;","names":[]}
|
package/dist/index.d.mts
CHANGED
|
@@ -9,6 +9,7 @@ export { dropRight } from './array/dropRight.mjs';
|
|
|
9
9
|
export { dropRightWhile } from './array/dropRightWhile.mjs';
|
|
10
10
|
export { dropWhile } from './array/dropWhile.mjs';
|
|
11
11
|
export { fill } from './array/fill.mjs';
|
|
12
|
+
export { flatten } from './array/flatten.mjs';
|
|
12
13
|
export { forEachRight } from './array/forEachRight.mjs';
|
|
13
14
|
export { groupBy } from './array/groupBy.mjs';
|
|
14
15
|
export { intersection } from './array/intersection.mjs';
|
|
@@ -32,6 +33,7 @@ export { unionWith } from './array/unionWith.mjs';
|
|
|
32
33
|
export { uniq } from './array/uniq.mjs';
|
|
33
34
|
export { uniqBy } from './array/uniqBy.mjs';
|
|
34
35
|
export { uniqWith } from './array/uniqWith.mjs';
|
|
36
|
+
export { unzip } from './array/unzip.mjs';
|
|
35
37
|
export { unzipWith } from './array/unzipWith.mjs';
|
|
36
38
|
export { xor } from './array/xor.mjs';
|
|
37
39
|
export { xorBy } from './array/xorBy.mjs';
|
|
@@ -40,6 +42,8 @@ export { zip } from './array/zip.mjs';
|
|
|
40
42
|
export { zipObject } from './array/zipObject.mjs';
|
|
41
43
|
export { zipWith } from './array/zipWith.mjs';
|
|
42
44
|
export { without } from './array/without.mjs';
|
|
45
|
+
export { head } from './array/head.mjs';
|
|
46
|
+
export { tail } from './array/tail.mjs';
|
|
43
47
|
export { AbortError } from './error/AbortError.mjs';
|
|
44
48
|
export { debounce } from './function/debounce.mjs';
|
|
45
49
|
export { noop } from './function/noop.mjs';
|
package/dist/index.d.ts
CHANGED
|
@@ -9,6 +9,7 @@ export { dropRight } from './array/dropRight.js';
|
|
|
9
9
|
export { dropRightWhile } from './array/dropRightWhile.js';
|
|
10
10
|
export { dropWhile } from './array/dropWhile.js';
|
|
11
11
|
export { fill } from './array/fill.js';
|
|
12
|
+
export { flatten } from './array/flatten.js';
|
|
12
13
|
export { forEachRight } from './array/forEachRight.js';
|
|
13
14
|
export { groupBy } from './array/groupBy.js';
|
|
14
15
|
export { intersection } from './array/intersection.js';
|
|
@@ -32,6 +33,7 @@ export { unionWith } from './array/unionWith.js';
|
|
|
32
33
|
export { uniq } from './array/uniq.js';
|
|
33
34
|
export { uniqBy } from './array/uniqBy.js';
|
|
34
35
|
export { uniqWith } from './array/uniqWith.js';
|
|
36
|
+
export { unzip } from './array/unzip.js';
|
|
35
37
|
export { unzipWith } from './array/unzipWith.js';
|
|
36
38
|
export { xor } from './array/xor.js';
|
|
37
39
|
export { xorBy } from './array/xorBy.js';
|
|
@@ -40,6 +42,8 @@ export { zip } from './array/zip.js';
|
|
|
40
42
|
export { zipObject } from './array/zipObject.js';
|
|
41
43
|
export { zipWith } from './array/zipWith.js';
|
|
42
44
|
export { without } from './array/without.js';
|
|
45
|
+
export { head } from './array/head.js';
|
|
46
|
+
export { tail } from './array/tail.js';
|
|
43
47
|
export { AbortError } from './error/AbortError.js';
|
|
44
48
|
export { debounce } from './function/debounce.js';
|
|
45
49
|
export { noop } from './function/noop.js';
|
package/dist/index.js
CHANGED
|
@@ -49,8 +49,10 @@ __export(src_exports, {
|
|
|
49
49
|
dropRightWhile: () => dropRightWhile,
|
|
50
50
|
dropWhile: () => dropWhile,
|
|
51
51
|
fill: () => fill,
|
|
52
|
+
flatten: () => flatten,
|
|
52
53
|
forEachRight: () => forEachRight,
|
|
53
54
|
groupBy: () => groupBy,
|
|
55
|
+
head: () => head,
|
|
54
56
|
inRange: () => inRange,
|
|
55
57
|
intersection: () => intersection,
|
|
56
58
|
intersectionBy: () => intersectionBy,
|
|
@@ -81,6 +83,7 @@ __export(src_exports, {
|
|
|
81
83
|
sampleSize: () => sampleSize,
|
|
82
84
|
shuffle: () => shuffle,
|
|
83
85
|
sum: () => sum,
|
|
86
|
+
tail: () => tail,
|
|
84
87
|
take: () => take,
|
|
85
88
|
takeRight: () => takeRight,
|
|
86
89
|
takeRightWhile: () => takeRightWhile,
|
|
@@ -92,6 +95,7 @@ __export(src_exports, {
|
|
|
92
95
|
uniq: () => uniq,
|
|
93
96
|
uniqBy: () => uniqBy,
|
|
94
97
|
uniqWith: () => uniqWith,
|
|
98
|
+
unzip: () => unzip,
|
|
95
99
|
unzipWith: () => unzipWith,
|
|
96
100
|
without: () => without,
|
|
97
101
|
xor: () => xor,
|
|
@@ -199,6 +203,23 @@ function fill(arr, value, start = 0, end = arr.length) {
|
|
|
199
203
|
return arr;
|
|
200
204
|
}
|
|
201
205
|
|
|
206
|
+
// src/array/flatten.ts
|
|
207
|
+
function flatten(arr, depth = 1) {
|
|
208
|
+
const result = [];
|
|
209
|
+
const flooredDepth = Math.floor(depth);
|
|
210
|
+
const recursive = (arr2, currentDepth) => {
|
|
211
|
+
for (const item of arr2) {
|
|
212
|
+
if (Array.isArray(item) && currentDepth < flooredDepth) {
|
|
213
|
+
recursive(item, currentDepth + 1);
|
|
214
|
+
} else {
|
|
215
|
+
result.push(item);
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
};
|
|
219
|
+
recursive(arr, 0);
|
|
220
|
+
return result;
|
|
221
|
+
}
|
|
222
|
+
|
|
202
223
|
// src/array/forEachRight.ts
|
|
203
224
|
function forEachRight(arr, callback) {
|
|
204
225
|
for (let i = arr.length - 1; i >= 0; i--) {
|
|
@@ -457,6 +478,24 @@ function uniqBy(arr, mapper) {
|
|
|
457
478
|
return Array.from(map.values());
|
|
458
479
|
}
|
|
459
480
|
|
|
481
|
+
// src/array/unzip.ts
|
|
482
|
+
function unzip(zipped) {
|
|
483
|
+
let maxLen = 0;
|
|
484
|
+
for (let i = 0; i < zipped.length; i++) {
|
|
485
|
+
if (zipped[i].length > maxLen) {
|
|
486
|
+
maxLen = zipped[i].length;
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
const result = new Array(maxLen);
|
|
490
|
+
for (let i = 0; i < maxLen; i++) {
|
|
491
|
+
result[i] = new Array(zipped.length);
|
|
492
|
+
for (let j = 0; j < zipped.length; j++) {
|
|
493
|
+
result[i][j] = zipped[j][i];
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
|
+
return result;
|
|
497
|
+
}
|
|
498
|
+
|
|
460
499
|
// src/array/unzipWith.ts
|
|
461
500
|
function unzipWith(target, iteratee) {
|
|
462
501
|
const maxLength = Math.max(...target.map((innerArray) => innerArray.length));
|
|
@@ -532,6 +571,24 @@ function without(array, ...values) {
|
|
|
532
571
|
return array.filter((item) => !valuesSet.has(item));
|
|
533
572
|
}
|
|
534
573
|
|
|
574
|
+
// src/array/head.ts
|
|
575
|
+
function head(arr) {
|
|
576
|
+
return arr[0];
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
// src/array/tail.ts
|
|
580
|
+
function tail(arr) {
|
|
581
|
+
const len = arr.length;
|
|
582
|
+
if (len <= 1) {
|
|
583
|
+
return [];
|
|
584
|
+
}
|
|
585
|
+
const result = new Array(len - 1);
|
|
586
|
+
for (let i = 1; i < len; i++) {
|
|
587
|
+
result[i - 1] = arr[i];
|
|
588
|
+
}
|
|
589
|
+
return result;
|
|
590
|
+
}
|
|
591
|
+
|
|
535
592
|
// src/error/AbortError.ts
|
|
536
593
|
var AbortError = class extends Error {
|
|
537
594
|
constructor(message = "The operation was aborted") {
|
|
@@ -775,8 +832,10 @@ function delay(ms, { signal } = {}) {
|
|
|
775
832
|
dropRightWhile,
|
|
776
833
|
dropWhile,
|
|
777
834
|
fill,
|
|
835
|
+
flatten,
|
|
778
836
|
forEachRight,
|
|
779
837
|
groupBy,
|
|
838
|
+
head,
|
|
780
839
|
inRange,
|
|
781
840
|
intersection,
|
|
782
841
|
intersectionBy,
|
|
@@ -807,6 +866,7 @@ function delay(ms, { signal } = {}) {
|
|
|
807
866
|
sampleSize,
|
|
808
867
|
shuffle,
|
|
809
868
|
sum,
|
|
869
|
+
tail,
|
|
810
870
|
take,
|
|
811
871
|
takeRight,
|
|
812
872
|
takeRightWhile,
|
|
@@ -818,6 +878,7 @@ function delay(ms, { signal } = {}) {
|
|
|
818
878
|
uniq,
|
|
819
879
|
uniqBy,
|
|
820
880
|
uniqWith,
|
|
881
|
+
unzip,
|
|
821
882
|
unzipWith,
|
|
822
883
|
without,
|
|
823
884
|
xor,
|