|
1 | 1 | import { expectType } from "ts-expect";
|
2 | 2 | import { flatten } from "./index";
|
3 | 3 |
|
4 |
| -describe("flatten", function() { |
5 |
| - it("should flatten an array", function() { |
| 4 | +describe("flatten", () => { |
| 5 | + it("should flatten an array", () => { |
6 | 6 | const result = flatten([1, [2, [3, [4, [5]]], 6, [[7], 8], 9], 10]);
|
7 | 7 |
|
8 | 8 | expectType<number[]>(result);
|
9 | 9 |
|
10 | 10 | expect(result).toStrictEqual([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
|
11 | 11 | });
|
12 | 12 |
|
13 |
| - it("should work with array-like", function() { |
| 13 | + it("should work with array-like", () => { |
14 | 14 | const result = flatten("test");
|
15 | 15 |
|
16 | 16 | expectType<string[]>(result);
|
17 | 17 |
|
18 | 18 | expect(result).toStrictEqual(["t", "e", "s", "t"]);
|
19 | 19 | });
|
20 | 20 |
|
21 |
| - it("should work with arguments", function() { |
22 |
| - const result = flatten(arguments); |
| 21 | + it("should work with readonly array", () => { |
| 22 | + const input = [1, [2, [3, [4]]]] as const; |
| 23 | + const result = flatten(input); |
| 24 | + |
| 25 | + expectType<(1 | 2 | 3 | 4)[]>(result); |
| 26 | + |
| 27 | + expect(result).toStrictEqual([1, 2, 3, 4]); |
| 28 | + }); |
| 29 | + |
| 30 | + it("should work with arguments", () => { |
| 31 | + const input = (function() { |
| 32 | + return arguments; |
| 33 | + })(); |
| 34 | + const result = flatten(input); |
23 | 35 |
|
24 | 36 | expectType<any[]>(result);
|
25 | 37 |
|
26 | 38 | expect(result).toStrictEqual([]);
|
27 | 39 | });
|
| 40 | + |
| 41 | + it("should work with mixed types", () => { |
| 42 | + const fn = (x: string) => x; |
| 43 | + const input = [1, ["test", [fn, [true]]]]; |
| 44 | + const result = flatten(input); |
| 45 | + |
| 46 | + expectType<(number | string | boolean | typeof fn)[]>(result); |
| 47 | + |
| 48 | + expect(result).toStrictEqual([1, "test", fn, true]); |
| 49 | + }); |
| 50 | + |
| 51 | + it("should work with tuples", () => { |
| 52 | + const input: [number, [number, number], [number]] = [1, [1, 2], [3]]; |
| 53 | + const result = flatten(input); |
| 54 | + |
| 55 | + expectType<number[]>(result); |
| 56 | + |
| 57 | + expect(result).toStrictEqual([1, 1, 2, 3]); |
| 58 | + }); |
28 | 59 | });
|
0 commit comments