File tree Expand file tree Collapse file tree 3 files changed +59
-0
lines changed Expand file tree Collapse file tree 3 files changed +59
-0
lines changed Original file line number Diff line number Diff line change
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
+ } ) ;
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -3,3 +3,4 @@ export * from "./uncapitalize.ts";
3
3
export * from "./lowercase.ts" ;
4
4
export * from "./uppercase.ts" ;
5
5
export * from "./split.ts" ;
6
+ export * from "./concat.ts" ;
You can’t perform that action at this time.
0 commit comments