typetify 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +327 -0
- package/dist/async/index.d.mts +152 -0
- package/dist/async/index.d.ts +152 -0
- package/dist/async/index.js +49 -0
- package/dist/async/index.js.map +1 -0
- package/dist/async/index.mjs +4 -0
- package/dist/async/index.mjs.map +1 -0
- package/dist/chunk-2LJ6NZ6K.js +108 -0
- package/dist/chunk-2LJ6NZ6K.js.map +1 -0
- package/dist/chunk-44Y5JSGU.js +145 -0
- package/dist/chunk-44Y5JSGU.js.map +1 -0
- package/dist/chunk-4NXETABV.mjs +116 -0
- package/dist/chunk-4NXETABV.mjs.map +1 -0
- package/dist/chunk-6ZBTL74K.js +129 -0
- package/dist/chunk-6ZBTL74K.js.map +1 -0
- package/dist/chunk-CN3GYRJN.mjs +137 -0
- package/dist/chunk-CN3GYRJN.mjs.map +1 -0
- package/dist/chunk-CNTE6ZVH.js +359 -0
- package/dist/chunk-CNTE6ZVH.js.map +1 -0
- package/dist/chunk-DBENOSTA.js +156 -0
- package/dist/chunk-DBENOSTA.js.map +1 -0
- package/dist/chunk-DWIG5GF2.js +135 -0
- package/dist/chunk-DWIG5GF2.js.map +1 -0
- package/dist/chunk-EAUTTWTQ.mjs +231 -0
- package/dist/chunk-EAUTTWTQ.mjs.map +1 -0
- package/dist/chunk-FXWYPHA3.mjs +13 -0
- package/dist/chunk-FXWYPHA3.mjs.map +1 -0
- package/dist/chunk-GS3PP67B.js +200 -0
- package/dist/chunk-GS3PP67B.js.map +1 -0
- package/dist/chunk-J5LGTIGS.mjs +9 -0
- package/dist/chunk-J5LGTIGS.mjs.map +1 -0
- package/dist/chunk-JAOGY4JO.mjs +1007 -0
- package/dist/chunk-JAOGY4JO.mjs.map +1 -0
- package/dist/chunk-JB6UXRKD.mjs +97 -0
- package/dist/chunk-JB6UXRKD.mjs.map +1 -0
- package/dist/chunk-JQHUBZ4M.js +88 -0
- package/dist/chunk-JQHUBZ4M.js.map +1 -0
- package/dist/chunk-L3M7LGKL.mjs +128 -0
- package/dist/chunk-L3M7LGKL.mjs.map +1 -0
- package/dist/chunk-LT7JK7RJ.js +87 -0
- package/dist/chunk-LT7JK7RJ.js.map +1 -0
- package/dist/chunk-OEJK37LO.mjs +328 -0
- package/dist/chunk-OEJK37LO.mjs.map +1 -0
- package/dist/chunk-OPVES6W2.js +16 -0
- package/dist/chunk-OPVES6W2.js.map +1 -0
- package/dist/chunk-OWNUKWXV.js +291 -0
- package/dist/chunk-OWNUKWXV.js.map +1 -0
- package/dist/chunk-PQTXSQ4P.js +369 -0
- package/dist/chunk-PQTXSQ4P.js.map +1 -0
- package/dist/chunk-PZ5AY32C.js +11 -0
- package/dist/chunk-PZ5AY32C.js.map +1 -0
- package/dist/chunk-QFR7DVAJ.mjs +63 -0
- package/dist/chunk-QFR7DVAJ.mjs.map +1 -0
- package/dist/chunk-SGQNLTRK.js +73 -0
- package/dist/chunk-SGQNLTRK.js.map +1 -0
- package/dist/chunk-SIA5BSVY.js +1054 -0
- package/dist/chunk-SIA5BSVY.js.map +1 -0
- package/dist/chunk-SRDWUHDY.mjs +188 -0
- package/dist/chunk-SRDWUHDY.mjs.map +1 -0
- package/dist/chunk-TXU7NTT4.js +249 -0
- package/dist/chunk-TXU7NTT4.js.map +1 -0
- package/dist/chunk-TZEWREAC.mjs +277 -0
- package/dist/chunk-TZEWREAC.mjs.map +1 -0
- package/dist/chunk-V6CWFDIJ.mjs +123 -0
- package/dist/chunk-V6CWFDIJ.mjs.map +1 -0
- package/dist/chunk-YBJC5WMX.mjs +341 -0
- package/dist/chunk-YBJC5WMX.mjs.map +1 -0
- package/dist/chunk-YOPAXITF.mjs +75 -0
- package/dist/chunk-YOPAXITF.mjs.map +1 -0
- package/dist/chunk-ZE4FDBRI.mjs +79 -0
- package/dist/chunk-ZE4FDBRI.mjs.map +1 -0
- package/dist/collection/index.d.mts +291 -0
- package/dist/collection/index.d.ts +291 -0
- package/dist/collection/index.js +125 -0
- package/dist/collection/index.js.map +1 -0
- package/dist/collection/index.mjs +4 -0
- package/dist/collection/index.mjs.map +1 -0
- package/dist/core/index.d.mts +85 -0
- package/dist/core/index.d.ts +85 -0
- package/dist/core/index.js +41 -0
- package/dist/core/index.js.map +1 -0
- package/dist/core/index.mjs +4 -0
- package/dist/core/index.mjs.map +1 -0
- package/dist/decorator/index.d.mts +165 -0
- package/dist/decorator/index.d.ts +165 -0
- package/dist/decorator/index.js +57 -0
- package/dist/decorator/index.js.map +1 -0
- package/dist/decorator/index.mjs +4 -0
- package/dist/decorator/index.mjs.map +1 -0
- package/dist/dx/index.d.mts +125 -0
- package/dist/dx/index.d.ts +125 -0
- package/dist/dx/index.js +53 -0
- package/dist/dx/index.js.map +1 -0
- package/dist/dx/index.mjs +4 -0
- package/dist/dx/index.mjs.map +1 -0
- package/dist/flow/index.d.mts +165 -0
- package/dist/flow/index.d.ts +165 -0
- package/dist/flow/index.js +50 -0
- package/dist/flow/index.js.map +1 -0
- package/dist/flow/index.mjs +5 -0
- package/dist/flow/index.mjs.map +1 -0
- package/dist/fn/index.d.mts +77 -0
- package/dist/fn/index.d.ts +77 -0
- package/dist/fn/index.js +37 -0
- package/dist/fn/index.js.map +1 -0
- package/dist/fn/index.mjs +4 -0
- package/dist/fn/index.mjs.map +1 -0
- package/dist/guards/index.d.mts +165 -0
- package/dist/guards/index.d.ts +165 -0
- package/dist/guards/index.js +69 -0
- package/dist/guards/index.js.map +1 -0
- package/dist/guards/index.mjs +4 -0
- package/dist/guards/index.mjs.map +1 -0
- package/dist/index.d.mts +228 -0
- package/dist/index.d.ts +228 -0
- package/dist/index.js +775 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +51 -0
- package/dist/index.mjs.map +1 -0
- package/dist/input/index.d.mts +185 -0
- package/dist/input/index.d.ts +185 -0
- package/dist/input/index.js +74 -0
- package/dist/input/index.js.map +1 -0
- package/dist/input/index.mjs +5 -0
- package/dist/input/index.mjs.map +1 -0
- package/dist/iterator/index.d.mts +209 -0
- package/dist/iterator/index.d.ts +209 -0
- package/dist/iterator/index.js +522 -0
- package/dist/iterator/index.js.map +1 -0
- package/dist/iterator/index.mjs +502 -0
- package/dist/iterator/index.mjs.map +1 -0
- package/dist/logic/index.d.mts +531 -0
- package/dist/logic/index.d.ts +531 -0
- package/dist/logic/index.js +416 -0
- package/dist/logic/index.js.map +1 -0
- package/dist/logic/index.mjs +367 -0
- package/dist/logic/index.mjs.map +1 -0
- package/dist/math/index.d.mts +86 -0
- package/dist/math/index.d.ts +86 -0
- package/dist/math/index.js +45 -0
- package/dist/math/index.js.map +1 -0
- package/dist/math/index.mjs +4 -0
- package/dist/math/index.mjs.map +1 -0
- package/dist/narrowing/index.d.mts +429 -0
- package/dist/narrowing/index.d.ts +429 -0
- package/dist/narrowing/index.js +220 -0
- package/dist/narrowing/index.js.map +1 -0
- package/dist/narrowing/index.mjs +186 -0
- package/dist/narrowing/index.mjs.map +1 -0
- package/dist/object/index.d.mts +327 -0
- package/dist/object/index.d.ts +327 -0
- package/dist/object/index.js +113 -0
- package/dist/object/index.js.map +1 -0
- package/dist/object/index.mjs +4 -0
- package/dist/object/index.mjs.map +1 -0
- package/dist/result/index.d.mts +201 -0
- package/dist/result/index.d.ts +201 -0
- package/dist/result/index.js +86 -0
- package/dist/result/index.js.map +1 -0
- package/dist/result/index.mjs +5 -0
- package/dist/result/index.mjs.map +1 -0
- package/dist/schema/index.d.mts +216 -0
- package/dist/schema/index.d.ts +216 -0
- package/dist/schema/index.js +410 -0
- package/dist/schema/index.js.map +1 -0
- package/dist/schema/index.mjs +384 -0
- package/dist/schema/index.mjs.map +1 -0
- package/dist/string/index.d.mts +102 -0
- package/dist/string/index.d.ts +102 -0
- package/dist/string/index.js +49 -0
- package/dist/string/index.js.map +1 -0
- package/dist/string/index.mjs +4 -0
- package/dist/string/index.mjs.map +1 -0
- package/dist/typed/index.d.mts +1962 -0
- package/dist/typed/index.d.ts +1962 -0
- package/dist/typed/index.js +193 -0
- package/dist/typed/index.js.map +1 -0
- package/dist/typed/index.mjs +4 -0
- package/dist/typed/index.mjs.map +1 -0
- package/dist/types-Db0vauC3.d.mts +258 -0
- package/dist/types-Db0vauC3.d.ts +258 -0
- package/dist/types-VsDp2t8s.d.mts +30 -0
- package/dist/types-VsDp2t8s.d.ts +30 -0
- package/package.json +157 -0
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { __export } from './chunk-J5LGTIGS.mjs';
|
|
2
|
+
|
|
3
|
+
// src/fn/index.ts
|
|
4
|
+
var fn_exports = {};
|
|
5
|
+
__export(fn_exports, {
|
|
6
|
+
ary: () => ary,
|
|
7
|
+
curry: () => curry,
|
|
8
|
+
flip: () => flip,
|
|
9
|
+
memoize: () => memoize,
|
|
10
|
+
negate: () => negate,
|
|
11
|
+
partial: () => partial,
|
|
12
|
+
unary: () => unary
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
// src/fn/memoize.ts
|
|
16
|
+
function memoize(fn, resolver) {
|
|
17
|
+
const cache = /* @__PURE__ */ new Map();
|
|
18
|
+
const memoized = function(...args) {
|
|
19
|
+
const key = resolver ? resolver(...args) : args[0];
|
|
20
|
+
if (cache.has(key)) {
|
|
21
|
+
return cache.get(key);
|
|
22
|
+
}
|
|
23
|
+
const result = fn.apply(this, args);
|
|
24
|
+
cache.set(key, result);
|
|
25
|
+
return result;
|
|
26
|
+
};
|
|
27
|
+
return memoized;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// src/fn/negate.ts
|
|
31
|
+
function negate(fn) {
|
|
32
|
+
return function(...args) {
|
|
33
|
+
return !fn(...args);
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// src/fn/flip.ts
|
|
38
|
+
function flip(fn) {
|
|
39
|
+
return function(b, a) {
|
|
40
|
+
return fn(a, b);
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// src/fn/partial.ts
|
|
45
|
+
function partial(fn, ...partialArgs) {
|
|
46
|
+
return function(...args) {
|
|
47
|
+
return fn(...partialArgs, ...args);
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// src/fn/curry.ts
|
|
52
|
+
function curry(fn) {
|
|
53
|
+
const arity = fn.length;
|
|
54
|
+
function curried(...args) {
|
|
55
|
+
if (args.length >= arity) {
|
|
56
|
+
return fn(...args);
|
|
57
|
+
}
|
|
58
|
+
return (...moreArgs) => curried(...args, ...moreArgs);
|
|
59
|
+
}
|
|
60
|
+
return curried;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// src/fn/ary.ts
|
|
64
|
+
function ary(fn, n) {
|
|
65
|
+
return function(...args) {
|
|
66
|
+
return fn(...args.slice(0, n));
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// src/fn/unary.ts
|
|
71
|
+
function unary(fn) {
|
|
72
|
+
return function(arg) {
|
|
73
|
+
return fn(arg);
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export { ary, curry, flip, fn_exports, memoize, negate, partial, unary };
|
|
78
|
+
//# sourceMappingURL=chunk-ZE4FDBRI.mjs.map
|
|
79
|
+
//# sourceMappingURL=chunk-ZE4FDBRI.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/fn/index.ts","../src/fn/memoize.ts","../src/fn/negate.ts","../src/fn/flip.ts","../src/fn/partial.ts","../src/fn/curry.ts","../src/fn/ary.ts","../src/fn/unary.ts"],"names":[],"mappings":";;;AAAA,IAAA,UAAA,GAAA;AAAA,QAAA,CAAA,UAAA,EAAA;AAAA,EAAA,GAAA,EAAA,MAAA,GAAA;AAAA,EAAA,KAAA,EAAA,MAAA,KAAA;AAAA,EAAA,IAAA,EAAA,MAAA,IAAA;AAAA,EAAA,OAAA,EAAA,MAAA,OAAA;AAAA,EAAA,MAAA,EAAA,MAAA,MAAA;AAAA,EAAA,OAAA,EAAA,MAAA,OAAA;AAAA,EAAA,KAAA,EAAA,MAAA;AAAA,CAAA,CAAA;;;ACYO,SAAS,OAAA,CACd,IACA,QAAA,EACG;AACH,EAAA,MAAM,KAAA,uBAAY,GAAA,EAA4B;AAE9C,EAAA,MAAM,QAAA,GAAW,YAA4B,IAAA,EAAoC;AAC/E,IAAA,MAAM,MAAM,QAAA,GAAW,QAAA,CAAS,GAAG,IAAI,CAAA,GAAI,KAAK,CAAC,CAAA;AAEjD,IAAA,IAAI,KAAA,CAAM,GAAA,CAAI,GAAG,CAAA,EAAG;AAClB,MAAA,OAAO,KAAA,CAAM,IAAI,GAAG,CAAA;AAAA,IACtB;AAEA,IAAA,MAAM,MAAA,GAAS,EAAA,CAAG,KAAA,CAAM,IAAA,EAAM,IAAI,CAAA;AAClC,IAAA,KAAA,CAAM,GAAA,CAAI,KAAK,MAAM,CAAA;AACrB,IAAA,OAAO,MAAA;AAAA,EACT,CAAA;AAEA,EAAA,OAAO,QAAA;AACT;;;ACtBO,SAAS,OACd,EAAA,EACqC;AACrC,EAAA,OAAO,YAAa,IAAA,EAA8B;AAChD,IAAA,OAAO,CAAC,EAAA,CAAG,GAAG,IAAI,CAAA;AAAA,EACpB,CAAA;AACF;;;ACPO,SAAS,KACd,EAAA,EACmB;AACnB,EAAA,OAAO,SAAU,GAAM,CAAA,EAAS;AAC9B,IAAA,OAAO,EAAA,CAAG,GAAG,CAAC,CAAA;AAAA,EAChB,CAAA;AACF;;;ACNO,SAAS,OAAA,CACd,OACG,WAAA,EACgC;AACnC,EAAA,OAAO,YAAa,IAAA,EAA4B;AAC9C,IAAA,OAAO,EAAA,CAAG,GAAG,WAAA,EAAa,GAAG,IAAI,CAAA;AAAA,EACnC,CAAA;AACF;;;ACHO,SAAS,MACd,EAAA,EACyB;AACzB,EAAA,MAAM,QAAQ,EAAA,CAAG,MAAA;AAEjB,EAAA,SAAS,WAAW,IAAA,EAAkB;AACpC,IAAA,IAAI,IAAA,CAAK,UAAU,KAAA,EAAO;AACxB,MAAA,OAAO,EAAA,CAAG,GAAG,IAAI,CAAA;AAAA,IACnB;AACA,IAAA,OAAO,IAAI,QAAA,KAAoB,OAAA,CAAQ,GAAG,IAAA,EAAM,GAAG,QAAQ,CAAA;AAAA,EAC7D;AAEA,EAAA,OAAO,OAAA;AACT;;;AClBO,SAAS,GAAA,CACd,IACA,CAAA,EACmC;AACnC,EAAA,OAAO,YAAa,IAAA,EAA4B;AAC9C,IAAA,OAAO,GAAG,GAAG,IAAA,CAAK,KAAA,CAAM,CAAA,EAAG,CAAC,CAAC,CAAA;AAAA,EAC/B,CAAA;AACF;;;ACRO,SAAS,MACd,EAAA,EAC0C;AAC1C,EAAA,OAAO,SAAU,GAAA,EAAsC;AACrD,IAAA,OAAO,GAAG,GAAG,CAAA;AAAA,EACf,CAAA;AACF","file":"chunk-ZE4FDBRI.mjs","sourcesContent":["export { memoize } from './memoize'\nexport { negate } from './negate'\nexport { flip } from './flip'\nexport { partial } from './partial'\nexport { curry } from './curry'\nexport { ary } from './ary'\nexport { unary } from './unary'\n","/**\n * Creates a memoized version of a function.\n * Results are cached based on the first argument (by default).\n *\n * @example\n * const expensive = memoize((n: number) => {\n * console.log('computing...')\n * return n * 2\n * })\n * expensive(5) // logs 'computing...', returns 10\n * expensive(5) // returns 10 (cached)\n */\nexport function memoize<T extends (...args: any[]) => any>(\n fn: T,\n resolver?: (...args: Parameters<T>) => unknown\n): T {\n const cache = new Map<unknown, ReturnType<T>>()\n\n const memoized = function (this: unknown, ...args: Parameters<T>): ReturnType<T> {\n const key = resolver ? resolver(...args) : args[0]\n\n if (cache.has(key)) {\n return cache.get(key)!\n }\n\n const result = fn.apply(this, args)\n cache.set(key, result)\n return result\n }\n\n return memoized as T\n}\n","/**\n * Creates a function that negates the result of the predicate.\n *\n * @example\n * const isEven = (n: number) => n % 2 === 0\n * const isOdd = negate(isEven)\n * isOdd(3) // true\n * isOdd(4) // false\n */\nexport function negate<T extends (...args: any[]) => boolean>(\n fn: T\n): (...args: Parameters<T>) => boolean {\n return function (...args: Parameters<T>): boolean {\n return !fn(...args)\n }\n}\n","/**\n * Creates a function that invokes the original function with arguments reversed.\n *\n * @example\n * const divide = (a: number, b: number) => a / b\n * const flipped = flip(divide)\n * flipped(2, 10) // 5 (10 / 2)\n */\nexport function flip<A, B, R>(\n fn: (a: A, b: B) => R\n): (b: B, a: A) => R {\n return function (b: B, a: A): R {\n return fn(a, b)\n }\n}\n","/**\n * Creates a function with some arguments pre-filled.\n *\n * @example\n * const greet = (greeting: string, name: string) => `${greeting}, ${name}!`\n * const sayHello = partial(greet, 'Hello')\n * sayHello('World') // 'Hello, World!'\n */\nexport function partial<T extends (...args: any[]) => any, A extends any[]>(\n fn: T,\n ...partialArgs: A\n): (...args: any[]) => ReturnType<T> {\n return function (...args: any[]): ReturnType<T> {\n return fn(...partialArgs, ...args)\n }\n}\n","/**\n * Creates a curried version of a function.\n * The curried function can be called with fewer arguments,\n * returning a new function that takes the remaining arguments.\n *\n * @example\n * const add = (a: number, b: number, c: number) => a + b + c\n * const curriedAdd = curry(add)\n * curriedAdd(1)(2)(3) // 6\n * curriedAdd(1, 2)(3) // 6\n * curriedAdd(1)(2, 3) // 6\n */\nexport function curry<T extends (...args: any[]) => any>(\n fn: T\n): (...args: any[]) => any {\n const arity = fn.length\n\n function curried(...args: any[]): any {\n if (args.length >= arity) {\n return fn(...args)\n }\n return (...moreArgs: any[]) => curried(...args, ...moreArgs)\n }\n\n return curried\n}\n","/**\n * Creates a function that accepts up to n arguments, ignoring additional arguments.\n *\n * @example\n * const fn = ary(parseInt, 1)\n * ['1', '2', '3'].map(fn) // [1, 2, 3] instead of [1, NaN, NaN]\n */\nexport function ary<T extends (...args: any[]) => any>(\n fn: T,\n n: number\n): (...args: any[]) => ReturnType<T> {\n return function (...args: any[]): ReturnType<T> {\n return fn(...args.slice(0, n))\n }\n}\n","/**\n * Creates a function that accepts only one argument, ignoring additional arguments.\n *\n * @example\n * ['1', '2', '3'].map(unary(parseInt)) // [1, 2, 3]\n */\nexport function unary<T extends (...args: any[]) => any>(\n fn: T\n): (arg: Parameters<T>[0]) => ReturnType<T> {\n return function (arg: Parameters<T>[0]): ReturnType<T> {\n return fn(arg)\n }\n}\n"]}
|
|
@@ -0,0 +1,291 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns an array with unique values.
|
|
3
|
+
* Optionally accepts a key function to determine uniqueness.
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* unique([1, 2, 2, 3]) // [1, 2, 3]
|
|
7
|
+
* unique([{ id: 1 }, { id: 1 }, { id: 2 }], item => item.id) // [{ id: 1 }, { id: 2 }]
|
|
8
|
+
*/
|
|
9
|
+
declare function unique<T>(array: readonly T[]): T[];
|
|
10
|
+
declare function unique<T, K>(array: readonly T[], keyFn: (item: T) => K): T[];
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Groups array elements by a key function.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* const users = [
|
|
17
|
+
* { name: 'Alice', role: 'admin' },
|
|
18
|
+
* { name: 'Bob', role: 'user' },
|
|
19
|
+
* { name: 'Charlie', role: 'admin' },
|
|
20
|
+
* ]
|
|
21
|
+
* groupBy(users, user => user.role)
|
|
22
|
+
* // { admin: [{ name: 'Alice', ... }, { name: 'Charlie', ... }], user: [{ name: 'Bob', ... }] }
|
|
23
|
+
*/
|
|
24
|
+
declare function groupBy<T, K extends PropertyKey>(array: readonly T[], keyFn: (item: T) => K): Record<K, T[]>;
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Creates an object indexed by a key function.
|
|
28
|
+
* If multiple items have the same key, the last one wins.
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* const users = [
|
|
32
|
+
* { id: 1, name: 'Alice' },
|
|
33
|
+
* { id: 2, name: 'Bob' },
|
|
34
|
+
* ]
|
|
35
|
+
* indexBy(users, user => user.id)
|
|
36
|
+
* // { 1: { id: 1, name: 'Alice' }, 2: { id: 2, name: 'Bob' } }
|
|
37
|
+
*/
|
|
38
|
+
declare function indexBy<T, K extends PropertyKey>(array: readonly T[], keyFn: (item: T) => K): Record<K, T>;
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Splits an array into two arrays based on a predicate.
|
|
42
|
+
* First array contains items that pass, second contains items that fail.
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
* const [evens, odds] = partition([1, 2, 3, 4], n => n % 2 === 0)
|
|
46
|
+
* // evens: [2, 4], odds: [1, 3]
|
|
47
|
+
*/
|
|
48
|
+
declare function partition<T>(array: readonly T[], predicate: (item: T) => boolean): [T[], T[]];
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Splits an array into chunks of the specified size.
|
|
52
|
+
*
|
|
53
|
+
* @example
|
|
54
|
+
* chunk([1, 2, 3, 4, 5], 2)
|
|
55
|
+
* // [[1, 2], [3, 4], [5]]
|
|
56
|
+
*/
|
|
57
|
+
declare function chunk<T>(array: readonly T[], size: number): T[][];
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Removes null and undefined values from an array.
|
|
61
|
+
* Properly narrows the type.
|
|
62
|
+
*
|
|
63
|
+
* @example
|
|
64
|
+
* compact([1, null, 2, undefined, 3])
|
|
65
|
+
* // [1, 2, 3] with type number[]
|
|
66
|
+
*/
|
|
67
|
+
declare function compact<T>(array: readonly (T | null | undefined)[]): T[];
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Flattens an array one level deep.
|
|
71
|
+
*
|
|
72
|
+
* @example
|
|
73
|
+
* flatten([[1, 2], [3, 4], [5]])
|
|
74
|
+
* // [1, 2, 3, 4, 5]
|
|
75
|
+
*/
|
|
76
|
+
declare function flatten<T>(array: readonly (T | readonly T[])[]): T[];
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Sorts an array by a key function. Returns a new array.
|
|
80
|
+
*
|
|
81
|
+
* @example
|
|
82
|
+
* const users = [{ name: 'Bob' }, { name: 'Alice' }]
|
|
83
|
+
* sortBy(users, user => user.name)
|
|
84
|
+
* // [{ name: 'Alice' }, { name: 'Bob' }]
|
|
85
|
+
*/
|
|
86
|
+
declare function sortBy<T>(array: readonly T[], keyFn: (item: T) => string | number, order?: 'asc' | 'desc'): T[];
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Returns the first element of an array, or undefined if empty.
|
|
90
|
+
*
|
|
91
|
+
* @example
|
|
92
|
+
* first([1, 2, 3]) // 1
|
|
93
|
+
* first([]) // undefined
|
|
94
|
+
*/
|
|
95
|
+
declare function first<T>(array: readonly T[]): T | undefined;
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Returns the last element of an array, or undefined if empty.
|
|
99
|
+
*
|
|
100
|
+
* @example
|
|
101
|
+
* last([1, 2, 3]) // 3
|
|
102
|
+
* last([]) // undefined
|
|
103
|
+
*/
|
|
104
|
+
declare function last<T>(array: readonly T[]): T | undefined;
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Creates an array of numbers from start to end (exclusive).
|
|
108
|
+
*
|
|
109
|
+
* @example
|
|
110
|
+
* range(0, 5) // [0, 1, 2, 3, 4]
|
|
111
|
+
* range(1, 10, 2) // [1, 3, 5, 7, 9]
|
|
112
|
+
*/
|
|
113
|
+
declare function range(start: number, end: number, step?: number): number[];
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Returns a new array with elements randomly shuffled.
|
|
117
|
+
* Uses Fisher-Yates algorithm.
|
|
118
|
+
*
|
|
119
|
+
* @example
|
|
120
|
+
* shuffle([1, 2, 3, 4, 5])
|
|
121
|
+
* // [3, 1, 5, 2, 4] (random order)
|
|
122
|
+
*/
|
|
123
|
+
declare function shuffle<T>(array: readonly T[]): T[];
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Returns a random element from an array.
|
|
127
|
+
*
|
|
128
|
+
* @example
|
|
129
|
+
* sample([1, 2, 3, 4, 5]) // random element
|
|
130
|
+
* sample([]) // undefined
|
|
131
|
+
*/
|
|
132
|
+
declare function sample<T>(array: readonly T[]): T | undefined;
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Zips two arrays together into an array of tuples.
|
|
136
|
+
* Stops at the shorter array.
|
|
137
|
+
*
|
|
138
|
+
* @example
|
|
139
|
+
* zip([1, 2, 3], ['a', 'b', 'c'])
|
|
140
|
+
* // [[1, 'a'], [2, 'b'], [3, 'c']]
|
|
141
|
+
*/
|
|
142
|
+
declare function zip<A, B>(a: readonly A[], b: readonly B[]): [A, B][];
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Unzips an array of tuples into two arrays.
|
|
146
|
+
*
|
|
147
|
+
* @example
|
|
148
|
+
* unzip([[1, 'a'], [2, 'b'], [3, 'c']])
|
|
149
|
+
* // [[1, 2, 3], ['a', 'b', 'c']]
|
|
150
|
+
*/
|
|
151
|
+
declare function unzip<A, B>(array: readonly (readonly [A, B])[]): [A[], B[]];
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Returns elements in the first array that are not in the second.
|
|
155
|
+
*
|
|
156
|
+
* @example
|
|
157
|
+
* difference([1, 2, 3, 4], [2, 4])
|
|
158
|
+
* // [1, 3]
|
|
159
|
+
*/
|
|
160
|
+
declare function difference<T>(a: readonly T[], b: readonly T[]): T[];
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Returns elements that exist in both arrays.
|
|
164
|
+
*
|
|
165
|
+
* @example
|
|
166
|
+
* intersection([1, 2, 3], [2, 3, 4])
|
|
167
|
+
* // [2, 3]
|
|
168
|
+
*/
|
|
169
|
+
declare function intersection<T>(a: readonly T[], b: readonly T[]): T[];
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Returns the first n elements of an array.
|
|
173
|
+
*
|
|
174
|
+
* @example
|
|
175
|
+
* take([1, 2, 3, 4, 5], 3) // [1, 2, 3]
|
|
176
|
+
* take([1, 2], 5) // [1, 2]
|
|
177
|
+
*/
|
|
178
|
+
declare function take<T>(arr: readonly T[], n: number): T[];
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* Returns all but the first n elements of an array.
|
|
182
|
+
*
|
|
183
|
+
* @example
|
|
184
|
+
* drop([1, 2, 3, 4, 5], 2) // [3, 4, 5]
|
|
185
|
+
* drop([1, 2], 5) // []
|
|
186
|
+
*/
|
|
187
|
+
declare function drop<T>(arr: readonly T[], n: number): T[];
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* Returns elements from the beginning of an array while the predicate returns true.
|
|
191
|
+
*
|
|
192
|
+
* @example
|
|
193
|
+
* takeWhile([1, 2, 3, 4, 1], n => n < 3) // [1, 2]
|
|
194
|
+
* takeWhile([5, 1, 2], n => n < 3) // []
|
|
195
|
+
*/
|
|
196
|
+
declare function takeWhile<T>(arr: readonly T[], predicate: (item: T, index: number) => boolean): T[];
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* Drops elements from the beginning of an array while the predicate returns true.
|
|
200
|
+
*
|
|
201
|
+
* @example
|
|
202
|
+
* dropWhile([1, 2, 3, 4, 1], n => n < 3) // [3, 4, 1]
|
|
203
|
+
* dropWhile([5, 1, 2], n => n < 3) // [5, 1, 2]
|
|
204
|
+
*/
|
|
205
|
+
declare function dropWhile<T>(arr: readonly T[], predicate: (item: T, index: number) => boolean): T[];
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* Returns the last element in an array that satisfies the predicate.
|
|
209
|
+
*
|
|
210
|
+
* @example
|
|
211
|
+
* findLast([1, 2, 3, 4], n => n % 2 === 0) // 4
|
|
212
|
+
* findLast([1, 3, 5], n => n % 2 === 0) // undefined
|
|
213
|
+
*/
|
|
214
|
+
declare function findLast<T>(arr: readonly T[], predicate: (item: T, index: number) => boolean): T | undefined;
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* Creates an object counting occurrences of each key returned by the function.
|
|
218
|
+
*
|
|
219
|
+
* @example
|
|
220
|
+
* countBy([1, 2, 3, 4, 5], n => n % 2 === 0 ? 'even' : 'odd')
|
|
221
|
+
* // { odd: 3, even: 2 }
|
|
222
|
+
*
|
|
223
|
+
* countBy(['one', 'two', 'three'], s => s.length)
|
|
224
|
+
* // { 3: 2, 5: 1 }
|
|
225
|
+
*/
|
|
226
|
+
declare function countBy<T>(arr: readonly T[], fn: (item: T) => PropertyKey): Record<PropertyKey, number>;
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* Returns the element with the maximum value according to the function.
|
|
230
|
+
*
|
|
231
|
+
* @example
|
|
232
|
+
* maxBy([{ age: 20 }, { age: 30 }, { age: 25 }], u => u.age)
|
|
233
|
+
* // { age: 30 }
|
|
234
|
+
*
|
|
235
|
+
* maxBy([], u => u.age) // undefined
|
|
236
|
+
*/
|
|
237
|
+
declare function maxBy<T>(arr: readonly T[], fn: (item: T) => number): T | undefined;
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* Returns the element with the minimum value according to the function.
|
|
241
|
+
*
|
|
242
|
+
* @example
|
|
243
|
+
* minBy([{ age: 20 }, { age: 30 }, { age: 25 }], u => u.age)
|
|
244
|
+
* // { age: 20 }
|
|
245
|
+
*
|
|
246
|
+
* minBy([], u => u.age) // undefined
|
|
247
|
+
*/
|
|
248
|
+
declare function minBy<T>(arr: readonly T[], fn: (item: T) => number): T | undefined;
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* Sums the values returned by the function for each element.
|
|
252
|
+
*
|
|
253
|
+
* @example
|
|
254
|
+
* sumBy([{ price: 10 }, { price: 20 }, { price: 30 }], item => item.price)
|
|
255
|
+
* // 60
|
|
256
|
+
*
|
|
257
|
+
* sumBy([], item => item.price) // 0
|
|
258
|
+
*/
|
|
259
|
+
declare function sumBy<T>(arr: readonly T[], fn: (item: T) => number): number;
|
|
260
|
+
|
|
261
|
+
/**
|
|
262
|
+
* Returns the first element of an array, or undefined if empty.
|
|
263
|
+
* Alias for first().
|
|
264
|
+
*
|
|
265
|
+
* @example
|
|
266
|
+
* head([1, 2, 3]) // 1
|
|
267
|
+
* head([]) // undefined
|
|
268
|
+
*/
|
|
269
|
+
declare function head<T>(arr: readonly T[]): T | undefined;
|
|
270
|
+
|
|
271
|
+
/**
|
|
272
|
+
* Returns all elements except the first.
|
|
273
|
+
*
|
|
274
|
+
* @example
|
|
275
|
+
* tail([1, 2, 3]) // [2, 3]
|
|
276
|
+
* tail([1]) // []
|
|
277
|
+
* tail([]) // []
|
|
278
|
+
*/
|
|
279
|
+
declare function tail<T>(arr: readonly T[]): T[];
|
|
280
|
+
|
|
281
|
+
/**
|
|
282
|
+
* Returns all elements except the last.
|
|
283
|
+
*
|
|
284
|
+
* @example
|
|
285
|
+
* init([1, 2, 3]) // [1, 2]
|
|
286
|
+
* init([1]) // []
|
|
287
|
+
* init([]) // []
|
|
288
|
+
*/
|
|
289
|
+
declare function init<T>(arr: readonly T[]): T[];
|
|
290
|
+
|
|
291
|
+
export { chunk, compact, countBy, difference, drop, dropWhile, findLast, first, flatten, groupBy, head, indexBy, init, intersection, last, maxBy, minBy, partition, range, sample, shuffle, sortBy, sumBy, tail, take, takeWhile, unique, unzip, zip };
|
|
@@ -0,0 +1,291 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns an array with unique values.
|
|
3
|
+
* Optionally accepts a key function to determine uniqueness.
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* unique([1, 2, 2, 3]) // [1, 2, 3]
|
|
7
|
+
* unique([{ id: 1 }, { id: 1 }, { id: 2 }], item => item.id) // [{ id: 1 }, { id: 2 }]
|
|
8
|
+
*/
|
|
9
|
+
declare function unique<T>(array: readonly T[]): T[];
|
|
10
|
+
declare function unique<T, K>(array: readonly T[], keyFn: (item: T) => K): T[];
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Groups array elements by a key function.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* const users = [
|
|
17
|
+
* { name: 'Alice', role: 'admin' },
|
|
18
|
+
* { name: 'Bob', role: 'user' },
|
|
19
|
+
* { name: 'Charlie', role: 'admin' },
|
|
20
|
+
* ]
|
|
21
|
+
* groupBy(users, user => user.role)
|
|
22
|
+
* // { admin: [{ name: 'Alice', ... }, { name: 'Charlie', ... }], user: [{ name: 'Bob', ... }] }
|
|
23
|
+
*/
|
|
24
|
+
declare function groupBy<T, K extends PropertyKey>(array: readonly T[], keyFn: (item: T) => K): Record<K, T[]>;
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Creates an object indexed by a key function.
|
|
28
|
+
* If multiple items have the same key, the last one wins.
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* const users = [
|
|
32
|
+
* { id: 1, name: 'Alice' },
|
|
33
|
+
* { id: 2, name: 'Bob' },
|
|
34
|
+
* ]
|
|
35
|
+
* indexBy(users, user => user.id)
|
|
36
|
+
* // { 1: { id: 1, name: 'Alice' }, 2: { id: 2, name: 'Bob' } }
|
|
37
|
+
*/
|
|
38
|
+
declare function indexBy<T, K extends PropertyKey>(array: readonly T[], keyFn: (item: T) => K): Record<K, T>;
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Splits an array into two arrays based on a predicate.
|
|
42
|
+
* First array contains items that pass, second contains items that fail.
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
* const [evens, odds] = partition([1, 2, 3, 4], n => n % 2 === 0)
|
|
46
|
+
* // evens: [2, 4], odds: [1, 3]
|
|
47
|
+
*/
|
|
48
|
+
declare function partition<T>(array: readonly T[], predicate: (item: T) => boolean): [T[], T[]];
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Splits an array into chunks of the specified size.
|
|
52
|
+
*
|
|
53
|
+
* @example
|
|
54
|
+
* chunk([1, 2, 3, 4, 5], 2)
|
|
55
|
+
* // [[1, 2], [3, 4], [5]]
|
|
56
|
+
*/
|
|
57
|
+
declare function chunk<T>(array: readonly T[], size: number): T[][];
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Removes null and undefined values from an array.
|
|
61
|
+
* Properly narrows the type.
|
|
62
|
+
*
|
|
63
|
+
* @example
|
|
64
|
+
* compact([1, null, 2, undefined, 3])
|
|
65
|
+
* // [1, 2, 3] with type number[]
|
|
66
|
+
*/
|
|
67
|
+
declare function compact<T>(array: readonly (T | null | undefined)[]): T[];
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Flattens an array one level deep.
|
|
71
|
+
*
|
|
72
|
+
* @example
|
|
73
|
+
* flatten([[1, 2], [3, 4], [5]])
|
|
74
|
+
* // [1, 2, 3, 4, 5]
|
|
75
|
+
*/
|
|
76
|
+
declare function flatten<T>(array: readonly (T | readonly T[])[]): T[];
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Sorts an array by a key function. Returns a new array.
|
|
80
|
+
*
|
|
81
|
+
* @example
|
|
82
|
+
* const users = [{ name: 'Bob' }, { name: 'Alice' }]
|
|
83
|
+
* sortBy(users, user => user.name)
|
|
84
|
+
* // [{ name: 'Alice' }, { name: 'Bob' }]
|
|
85
|
+
*/
|
|
86
|
+
declare function sortBy<T>(array: readonly T[], keyFn: (item: T) => string | number, order?: 'asc' | 'desc'): T[];
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Returns the first element of an array, or undefined if empty.
|
|
90
|
+
*
|
|
91
|
+
* @example
|
|
92
|
+
* first([1, 2, 3]) // 1
|
|
93
|
+
* first([]) // undefined
|
|
94
|
+
*/
|
|
95
|
+
declare function first<T>(array: readonly T[]): T | undefined;
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Returns the last element of an array, or undefined if empty.
|
|
99
|
+
*
|
|
100
|
+
* @example
|
|
101
|
+
* last([1, 2, 3]) // 3
|
|
102
|
+
* last([]) // undefined
|
|
103
|
+
*/
|
|
104
|
+
declare function last<T>(array: readonly T[]): T | undefined;
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Creates an array of numbers from start to end (exclusive).
|
|
108
|
+
*
|
|
109
|
+
* @example
|
|
110
|
+
* range(0, 5) // [0, 1, 2, 3, 4]
|
|
111
|
+
* range(1, 10, 2) // [1, 3, 5, 7, 9]
|
|
112
|
+
*/
|
|
113
|
+
declare function range(start: number, end: number, step?: number): number[];
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Returns a new array with elements randomly shuffled.
|
|
117
|
+
* Uses Fisher-Yates algorithm.
|
|
118
|
+
*
|
|
119
|
+
* @example
|
|
120
|
+
* shuffle([1, 2, 3, 4, 5])
|
|
121
|
+
* // [3, 1, 5, 2, 4] (random order)
|
|
122
|
+
*/
|
|
123
|
+
declare function shuffle<T>(array: readonly T[]): T[];
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Returns a random element from an array.
|
|
127
|
+
*
|
|
128
|
+
* @example
|
|
129
|
+
* sample([1, 2, 3, 4, 5]) // random element
|
|
130
|
+
* sample([]) // undefined
|
|
131
|
+
*/
|
|
132
|
+
declare function sample<T>(array: readonly T[]): T | undefined;
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Zips two arrays together into an array of tuples.
|
|
136
|
+
* Stops at the shorter array.
|
|
137
|
+
*
|
|
138
|
+
* @example
|
|
139
|
+
* zip([1, 2, 3], ['a', 'b', 'c'])
|
|
140
|
+
* // [[1, 'a'], [2, 'b'], [3, 'c']]
|
|
141
|
+
*/
|
|
142
|
+
declare function zip<A, B>(a: readonly A[], b: readonly B[]): [A, B][];
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Unzips an array of tuples into two arrays.
|
|
146
|
+
*
|
|
147
|
+
* @example
|
|
148
|
+
* unzip([[1, 'a'], [2, 'b'], [3, 'c']])
|
|
149
|
+
* // [[1, 2, 3], ['a', 'b', 'c']]
|
|
150
|
+
*/
|
|
151
|
+
declare function unzip<A, B>(array: readonly (readonly [A, B])[]): [A[], B[]];
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Returns elements in the first array that are not in the second.
|
|
155
|
+
*
|
|
156
|
+
* @example
|
|
157
|
+
* difference([1, 2, 3, 4], [2, 4])
|
|
158
|
+
* // [1, 3]
|
|
159
|
+
*/
|
|
160
|
+
declare function difference<T>(a: readonly T[], b: readonly T[]): T[];
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Returns elements that exist in both arrays.
|
|
164
|
+
*
|
|
165
|
+
* @example
|
|
166
|
+
* intersection([1, 2, 3], [2, 3, 4])
|
|
167
|
+
* // [2, 3]
|
|
168
|
+
*/
|
|
169
|
+
declare function intersection<T>(a: readonly T[], b: readonly T[]): T[];
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Returns the first n elements of an array.
|
|
173
|
+
*
|
|
174
|
+
* @example
|
|
175
|
+
* take([1, 2, 3, 4, 5], 3) // [1, 2, 3]
|
|
176
|
+
* take([1, 2], 5) // [1, 2]
|
|
177
|
+
*/
|
|
178
|
+
declare function take<T>(arr: readonly T[], n: number): T[];
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* Returns all but the first n elements of an array.
|
|
182
|
+
*
|
|
183
|
+
* @example
|
|
184
|
+
* drop([1, 2, 3, 4, 5], 2) // [3, 4, 5]
|
|
185
|
+
* drop([1, 2], 5) // []
|
|
186
|
+
*/
|
|
187
|
+
declare function drop<T>(arr: readonly T[], n: number): T[];
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* Returns elements from the beginning of an array while the predicate returns true.
|
|
191
|
+
*
|
|
192
|
+
* @example
|
|
193
|
+
* takeWhile([1, 2, 3, 4, 1], n => n < 3) // [1, 2]
|
|
194
|
+
* takeWhile([5, 1, 2], n => n < 3) // []
|
|
195
|
+
*/
|
|
196
|
+
declare function takeWhile<T>(arr: readonly T[], predicate: (item: T, index: number) => boolean): T[];
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* Drops elements from the beginning of an array while the predicate returns true.
|
|
200
|
+
*
|
|
201
|
+
* @example
|
|
202
|
+
* dropWhile([1, 2, 3, 4, 1], n => n < 3) // [3, 4, 1]
|
|
203
|
+
* dropWhile([5, 1, 2], n => n < 3) // [5, 1, 2]
|
|
204
|
+
*/
|
|
205
|
+
declare function dropWhile<T>(arr: readonly T[], predicate: (item: T, index: number) => boolean): T[];
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* Returns the last element in an array that satisfies the predicate.
|
|
209
|
+
*
|
|
210
|
+
* @example
|
|
211
|
+
* findLast([1, 2, 3, 4], n => n % 2 === 0) // 4
|
|
212
|
+
* findLast([1, 3, 5], n => n % 2 === 0) // undefined
|
|
213
|
+
*/
|
|
214
|
+
declare function findLast<T>(arr: readonly T[], predicate: (item: T, index: number) => boolean): T | undefined;
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* Creates an object counting occurrences of each key returned by the function.
|
|
218
|
+
*
|
|
219
|
+
* @example
|
|
220
|
+
* countBy([1, 2, 3, 4, 5], n => n % 2 === 0 ? 'even' : 'odd')
|
|
221
|
+
* // { odd: 3, even: 2 }
|
|
222
|
+
*
|
|
223
|
+
* countBy(['one', 'two', 'three'], s => s.length)
|
|
224
|
+
* // { 3: 2, 5: 1 }
|
|
225
|
+
*/
|
|
226
|
+
declare function countBy<T>(arr: readonly T[], fn: (item: T) => PropertyKey): Record<PropertyKey, number>;
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* Returns the element with the maximum value according to the function.
|
|
230
|
+
*
|
|
231
|
+
* @example
|
|
232
|
+
* maxBy([{ age: 20 }, { age: 30 }, { age: 25 }], u => u.age)
|
|
233
|
+
* // { age: 30 }
|
|
234
|
+
*
|
|
235
|
+
* maxBy([], u => u.age) // undefined
|
|
236
|
+
*/
|
|
237
|
+
declare function maxBy<T>(arr: readonly T[], fn: (item: T) => number): T | undefined;
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* Returns the element with the minimum value according to the function.
|
|
241
|
+
*
|
|
242
|
+
* @example
|
|
243
|
+
* minBy([{ age: 20 }, { age: 30 }, { age: 25 }], u => u.age)
|
|
244
|
+
* // { age: 20 }
|
|
245
|
+
*
|
|
246
|
+
* minBy([], u => u.age) // undefined
|
|
247
|
+
*/
|
|
248
|
+
declare function minBy<T>(arr: readonly T[], fn: (item: T) => number): T | undefined;
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* Sums the values returned by the function for each element.
|
|
252
|
+
*
|
|
253
|
+
* @example
|
|
254
|
+
* sumBy([{ price: 10 }, { price: 20 }, { price: 30 }], item => item.price)
|
|
255
|
+
* // 60
|
|
256
|
+
*
|
|
257
|
+
* sumBy([], item => item.price) // 0
|
|
258
|
+
*/
|
|
259
|
+
declare function sumBy<T>(arr: readonly T[], fn: (item: T) => number): number;
|
|
260
|
+
|
|
261
|
+
/**
|
|
262
|
+
* Returns the first element of an array, or undefined if empty.
|
|
263
|
+
* Alias for first().
|
|
264
|
+
*
|
|
265
|
+
* @example
|
|
266
|
+
* head([1, 2, 3]) // 1
|
|
267
|
+
* head([]) // undefined
|
|
268
|
+
*/
|
|
269
|
+
declare function head<T>(arr: readonly T[]): T | undefined;
|
|
270
|
+
|
|
271
|
+
/**
|
|
272
|
+
* Returns all elements except the first.
|
|
273
|
+
*
|
|
274
|
+
* @example
|
|
275
|
+
* tail([1, 2, 3]) // [2, 3]
|
|
276
|
+
* tail([1]) // []
|
|
277
|
+
* tail([]) // []
|
|
278
|
+
*/
|
|
279
|
+
declare function tail<T>(arr: readonly T[]): T[];
|
|
280
|
+
|
|
281
|
+
/**
|
|
282
|
+
* Returns all elements except the last.
|
|
283
|
+
*
|
|
284
|
+
* @example
|
|
285
|
+
* init([1, 2, 3]) // [1, 2]
|
|
286
|
+
* init([1]) // []
|
|
287
|
+
* init([]) // []
|
|
288
|
+
*/
|
|
289
|
+
declare function init<T>(arr: readonly T[]): T[];
|
|
290
|
+
|
|
291
|
+
export { chunk, compact, countBy, difference, drop, dropWhile, findLast, first, flatten, groupBy, head, indexBy, init, intersection, last, maxBy, minBy, partition, range, sample, shuffle, sortBy, sumBy, tail, take, takeWhile, unique, unzip, zip };
|