-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d2c6087
commit 7dadd6b
Showing
4 changed files
with
177 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/** | ||
* Infer deep flatted array. | ||
* | ||
* @typeParam T - input any array | ||
* @returns deep flatted array | ||
* | ||
* @example | ||
* ```ts | ||
* FlattenDeep<[]> // [] | ||
* FlattenDeep<[[1, [2, [3, [4]], 5]]> // [1, 2, 3, 4, 5] | ||
* ``` | ||
* | ||
* @beta | ||
*/ | ||
type FlattenDeep<T extends readonly unknown[]> = T extends readonly [ | ||
infer A, | ||
...infer Rest | ||
] | ||
? A extends readonly unknown[] | ||
? [...FlattenDeep<A>, ...FlattenDeep<Rest>] | ||
: [A, ...FlattenDeep<Rest>] | ||
: [...T] | ||
/** | ||
* Recursively flattens array. | ||
* | ||
* @param val - The `array` to flatten | ||
* @returns The result of `val.flat(Infinity)` | ||
* | ||
* @example | ||
* ```ts | ||
* flattenDeep([]) // [] | ||
* flattenDeep([1, [2, [3, [4]], 5]]) // [1, 2, 3, 4, 5] | ||
* ``` | ||
* | ||
* @beta | ||
*/ | ||
const flattenDeep = <T extends readonly unknown[]>(val: T): FlattenDeep<T> => | ||
val.flat(Infinity) as FlattenDeep<T> | ||
|
||
export { flattenDeep } | ||
export type { FlattenDeep } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { assertEquals } from '../deps.ts' | ||
import { FlattenDeep as Flat, flattenDeep } from '../src/flattenDeep.ts' | ||
import { assertEqual } from './asserts.ts' | ||
Deno.test('flattenDeep', () => { | ||
const table: [unknown[], unknown[]][] = [ | ||
[[], []], | ||
[[null], [null]], | ||
[ | ||
[null, undefined, 1, 'hello'], | ||
[null, undefined, 1, 'hello'] | ||
], | ||
[[[]], []], | ||
[[[1]], [1]], | ||
[[[1, 2, 3]], [1, 2, 3]], | ||
[[[1, 2, 3, undefined, null]], [1, 2, 3, undefined, null]], | ||
[ | ||
[[1, 2, 3, undefined, null], 4, 5, 6], | ||
[1, 2, 3, undefined, null, 4, 5, 6] | ||
], | ||
[ | ||
[ | ||
[1, 2, 3, undefined, null], | ||
[4, 5, 6] | ||
], | ||
[1, 2, 3, undefined, null, 4, 5, 6] | ||
], | ||
[ | ||
[[1, 2, 3, undefined, ['hello', 4], null]], | ||
[1, 2, 3, undefined, 'hello', 4, null] | ||
], | ||
[ | ||
[ | ||
'', | ||
[1, 2, 3, undefined, ['hello', 4], null], | ||
[5, 6, ['world']], | ||
[[7, 8], 9] | ||
], | ||
['', 1, 2, 3, undefined, 'hello', 4, null, 5, 6, 'world', 7, 8, 9] | ||
] | ||
] | ||
table.forEach(([val, expected]) => { | ||
assertEquals( | ||
flattenDeep(val), | ||
expected, | ||
`flattenDeep(${val}) -> ${expected}` | ||
) | ||
}) | ||
}) | ||
|
||
Deno.test('types', () => { | ||
assertEqual<[], Flat<[]>>() | ||
assertEqual<never[], Flat<never[]>>() | ||
// TODO: Success this case | ||
// assertEqual<never[], Flat<never[][]>>() | ||
assertEqual<[''], Flat<['']>>() | ||
assertEqual<[number], Flat<[number]>>() | ||
assertEqual<[number, string], Flat<[number, string]>>() | ||
assertEqual<number[], Flat<number[]>>() | ||
assertEqual<(number | string)[], Flat<(number | string)[]>>() | ||
assertEqual<readonly [''], Flat<readonly ['']>>() | ||
assertEqual<readonly ['', 'hello'], Flat<readonly ['', 'hello']>>() | ||
assertEqual<readonly ['', ''], Flat<readonly ['', readonly ['']]>>() | ||
assertEqual< | ||
readonly ['', '', 'hello'], | ||
Flat<readonly ['', readonly ['', readonly ['hello']]]> | ||
>() | ||
}) |