Skip to content

Commit 9bd54b4

Browse files
authored
Merge pull request #261 from ryoppippi/feature/257
feat(concat): Add concat function and tests
2 parents 5ac9673 + 815d147 commit 9bd54b4

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-0
lines changed

src/cocat.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { assertEquals } from "assert";
2+
import type { AssertTrue, IsExact } from "type-testing";
3+
import { concat } from "./concat.ts";
4+
5+
Deno.test("should concat one string", () => {
6+
const concated = concat("a");
7+
const expected = "a" as const;
8+
assertEquals(concated, expected);
9+
type _ = AssertTrue<IsExact<typeof expected, typeof concated>>;
10+
});
11+
12+
Deno.test("should concat multiple strings", () => {
13+
const concated = concat("hello", "world");
14+
const expected = "helloworld" as const;
15+
assertEquals(concated, expected);
16+
type _ = AssertTrue<IsExact<typeof expected, typeof concated>>;
17+
});
18+
19+
Deno.test("should concat multiple strings with a separator", () => {
20+
const concated = concat("hello", " ", "world");
21+
const expected = "hello world" as const;
22+
assertEquals(concated, expected);
23+
type _ = AssertTrue<IsExact<typeof expected, typeof concated>>;
24+
});

src/concat.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import type * as TF from "type-fest";
2+
3+
type ConcatingLoop<
4+
T extends readonly string[],
5+
R extends string = "",
6+
> = [] extends T ? R
7+
: TF.IsStringLiteral<R> extends true
8+
? T extends [infer A, ...infer B]
9+
? B extends readonly string[]
10+
? A extends string ? ConcatingLoop<B, `${R}${A}`>
11+
: string
12+
: string
13+
: string
14+
: string;
15+
16+
type ConcatedString<T extends readonly string[]> = ConcatingLoop<T>;
17+
18+
/**
19+
* concat strings
20+
*
21+
* ```ts
22+
* import { concat } from '@ryoppippi/str-fns'
23+
* const _: 'a' = concat('a');
24+
* const __: 'helloworld' = concat('hello', 'world');
25+
* const ___: 'hello world' = concat('hello', ' ', 'world');
26+
* ```
27+
*
28+
* @param ...inputs - strings to concat
29+
*/
30+
export function concat<T extends readonly string[]>(
31+
...inputs: T
32+
): ConcatedString<T> {
33+
return "".concat(...inputs) as ConcatedString<T>;
34+
}

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ export * from "./uncapitalize.ts";
33
export * from "./lowercase.ts";
44
export * from "./uppercase.ts";
55
export * from "./split.ts";
6+
export * from "./concat.ts";

0 commit comments

Comments
 (0)