Skip to content

Commit c8d2b30

Browse files
committed
refactor(eff): reorganize utility types and functions into logical sections
1 parent 5441e72 commit c8d2b30

File tree

1 file changed

+29
-19
lines changed

1 file changed

+29
-19
lines changed

packages/utilities/eff/src/index.ts

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
/* eslint-disable local/prefer-eqeq-nullish-comparison */
1212
/* eslint-disable prefer-rest-params */
1313

14-
// #region Primitive
14+
// #region Helper
1515

1616
/**
1717
* 1-byte version `undefined`, produces fewer bytes than `undefined` or `void 0` in output files.
@@ -23,6 +23,23 @@ export type _ = undefined; // eslint-disable-line local/no-shadow-underscore
2323
*/
2424
export const _ = undefined; // eslint-disable-line local/no-shadow-underscore
2525

26+
export type Pretty<T> =
27+
& {
28+
[P in keyof T]: T[P];
29+
}
30+
& {};
31+
32+
/**
33+
* An extension of Extract for type predicates which falls back to the base
34+
* in order to narrow the `unknown` case.
35+
*
36+
* @example
37+
* function isMyType<T>(data: T | MyType): data is NarrowedTo<T, MyType> { ... }
38+
*/
39+
export type NarrowedTo<T, Base> = Extract<T, Base> extends never ? Base
40+
: 0 extends 1 & NoInfer<T> ? Base
41+
: Extract<T, Base>;
42+
2643
// #endregion
2744

2845
// #region Function
@@ -1007,28 +1024,21 @@ export function isTruthy<T>(data: T): data is Exclude<T, "" | 0 | false | null |
10071024

10081025
// #endregion
10091026

1010-
// #region Helper
1027+
// #region Object & Array
10111028

1012-
export type Pretty<T> =
1013-
& {
1014-
[P in keyof T]: T[P];
1015-
}
1016-
& {};
1029+
export function chunk<T>(array: T[], size: number): T[][] {
1030+
const chunks: T[][] = [];
10171031

1018-
/**
1019-
* An extension of Extract for type predicates which falls back to the base
1020-
* in order to narrow the `unknown` case.
1021-
*
1022-
* @example
1023-
* function isMyType<T>(data: T | MyType): data is NarrowedTo<T, MyType> { ... }
1024-
*/
1025-
export type NarrowedTo<T, Base> = Extract<T, Base> extends never ? Base
1026-
: 0 extends 1 & NoInfer<T> ? Base
1027-
: Extract<T, Base>;
1032+
if (size <= 0) {
1033+
return chunks;
1034+
}
10281035

1029-
// #endregion
1036+
for (let i = 0, j = array.length; i < j; i += size) {
1037+
chunks.push(array.slice(i, i + size));
1038+
}
10301039

1031-
// #region Array
1040+
return chunks;
1041+
}
10321042

10331043
/**
10341044
* Creates a new array from two supplied arrays by calling the supplied function

0 commit comments

Comments
 (0)