Skip to content

Commit

Permalink
feat(concat): update concat function and tests for non-literal strings
Browse files Browse the repository at this point in the history
This commit updates the concat function to handle non-literal strings
and adds corresponding test cases. It also refactors the ConcatingLoop
type for better readability and performance. The changes include:

- Refactoring the ConcatingLoop type in concat.ts
- Adding new test cases in cocat.test.ts for non-literal strings
- Updating the documentation in concat.ts
  • Loading branch information
ryoppippi committed May 1, 2024
1 parent 0fa9052 commit 11821d9
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
25 changes: 22 additions & 3 deletions src/cocat.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,40 @@ import { concat } from "./concat.ts";

Deno.test("should concat one string", () => {
const concated = concat("a");
const expected = "a" as const;
const expected = "a" as const satisfies "a";
assertEquals(concated, expected);
type _ = AssertTrue<IsExact<typeof expected, typeof concated>>;
});

Deno.test("should concat multiple strings", () => {
const concated = concat("hello", "world");
const expected = "helloworld" as const;
const expected = "helloworld" as const satisfies "helloworld";
assertEquals(concated, expected);
type _ = AssertTrue<IsExact<typeof expected, typeof concated>>;
});

Deno.test("should concat multiple strings with a separator", () => {
const concated = concat("hello", " ", "world");
const expected = "hello world" as const;
const expected = "hello world" as const satisfies "hello world";
assertEquals(concated, expected);
type _ = AssertTrue<IsExact<typeof expected, typeof concated>>;
});

Deno.test("should concat a string and a non-literal string and generate literal type", () => {
const random = Math.random();
const concated = concat("a", random.toString());
const expected = `a${random.toString()}` as const satisfies `a${string}`;
assertEquals(concated, expected);
type _ = AssertTrue<IsExact<typeof expected, typeof concated>>;
});

Deno.test("should concat a string and a non-literal string and generate literal type", () => {
const randoms = [
Math.random().toString(),
Math.random().toString(),
] as const;
const concated = concat("ab", ...randoms, "c");
const expected = `ab${randoms.join("")}c` as const satisfies `ab${string}c`;
assertEquals(concated, expected);
type _ = AssertTrue<IsExact<typeof expected, typeof concated>>;
});
12 changes: 4 additions & 8 deletions src/concat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,7 @@ type ConcatingLoop<
T extends readonly string[],
R extends string = "",
> = [] extends T ? R
: TF.IsStringLiteral<R> extends true
? T extends [infer A, ...infer B]
? B extends readonly string[]
? A extends string ? ConcatingLoop<B, `${R}${A}`>
: string
: string
: string
: string;
: ConcatingLoop<TF.ArraySlice<T, 0, -1>, `${TF.LastArrayElement<T>}${R}`>;

type ConcatedString<T extends readonly string[]> = ConcatingLoop<T>;

Expand All @@ -23,6 +16,9 @@ type ConcatedString<T extends readonly string[]> = ConcatingLoop<T>;
* const _: 'a' = concat('a');
* const __: 'helloworld' = concat('hello', 'world');
* const ___: 'hello world' = concat('hello', ' ', 'world');
*
* const random = Math.random();
* const _: `a${string}` = concat("a", random.tostring());
* ```
*
* @param ...inputs - strings to concat
Expand Down

0 comments on commit 11821d9

Please sign in to comment.