Skip to content

Commit 49ba212

Browse files
committed
fix: array join
1 parent 85b3f03 commit 49ba212

File tree

2 files changed

+31
-12
lines changed

2 files changed

+31
-12
lines changed

scripts/array/join.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,14 @@ import { type ShiftTuple } from "./types";
66
type ComputeResult<
77
GenericArray extends readonly string[],
88
GenericSeparator extends string,
9-
> = GenericArray extends AnyTuple
10-
? IsEqual<GenericArray["length"], 1> extends true
11-
? GenericArray[0]
12-
: `${GenericArray[0]}${GenericSeparator}${ComputeResult<Adaptor<ShiftTuple<GenericArray>, readonly string[]>, GenericSeparator>}`
13-
: string;
9+
Depth extends readonly unknown[] = [],
10+
> = Depth["length"] extends 50
11+
? string
12+
: GenericArray extends AnyTuple
13+
? IsEqual<GenericArray["length"], 1> extends true
14+
? GenericArray[0]
15+
: `${GenericArray[0]}${GenericSeparator}${ComputeResult<Adaptor<ShiftTuple<GenericArray>, readonly string[]>, GenericSeparator, [...Depth, 0]>}`
16+
: string;
1417

1518
export function join<
1619
GenericArray extends readonly string[],

tests/array/join.test.ts

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,9 @@ import { type ExpectType, pipe, toString } from "@scripts/common";
22
import { DArray } from "@scripts";
33

44
describe("join", () => {
5-
it("joins array with separator", () => {
6-
expect(DArray.join(["1", "2", "3"], ",")).toBe("1,2,3");
7-
});
8-
9-
it("joins const array with separator", () => {
10-
const result = DArray.join(["1", "2", "3"] as const, "-");
5+
it("join tuple with separator", () => {
6+
const input = ["1", "2", "3"] as const;
7+
const result = DArray.join(input, "-");
118

129
expect(result).toBe("1-2-3");
1310

@@ -18,12 +15,31 @@ describe("join", () => {
1815
>;
1916
});
2017

21-
it("joins array using pipe with separator", () => {
18+
it("join array with separator", () => {
19+
const input = ["a", "b", "c"];
20+
const result = DArray.join(input, "-");
21+
22+
expect(result).toBe("a-b-c");
23+
24+
type check = ExpectType<
25+
typeof result,
26+
string,
27+
"strict"
28+
>;
29+
});
30+
31+
it("join array using pipe with separator", () => {
2232
const result = pipe(
2333
[1, 2, 3],
2434
DArray.map(toString),
2535
DArray.join("-"),
2636
);
2737
expect(result).toBe("1-2-3");
38+
39+
type check = ExpectType<
40+
typeof result,
41+
string,
42+
"strict"
43+
>;
2844
});
2945
});

0 commit comments

Comments
 (0)