diff --git a/src/string/replace/algo.ts b/src/string/replace/algo.ts new file mode 100644 index 0000000..6e903e7 --- /dev/null +++ b/src/string/replace/algo.ts @@ -0,0 +1,15 @@ +import type { _StrToArr } from "../strToArr/algo" + +export type _ReplaceString< + Str extends string, + StrToReplace extends string, + Replacement extends string, +> = StrToReplace extends "" + ? Str // replacement can be "" to remove a word + : Str extends `${infer First}${StrToReplace}` + ? `${First}${Replacement}` + : Str extends `${infer First}${StrToReplace}${infer Rest}` + ? `${First}${Replacement}${Rest}` + : Str extends `${StrToReplace}${infer Rest}` + ? `${Replacement}${Rest}` + : Str diff --git a/src/string/replace/index.test.ts b/src/string/replace/index.test.ts new file mode 100644 index 0000000..71804bc --- /dev/null +++ b/src/string/replace/index.test.ts @@ -0,0 +1,115 @@ +import { + describe, + it, + expectTypeOf, +} from "vitest" +import type { _ReplaceString } from "./algo" + +describe("ReplaceString type tests", () => { + it("should replace the string at the beginning", () => { + type T1 = _ReplaceString< + "hello world", + "hello", + "goodbye" + > + expectTypeOf().toEqualTypeOf<"goodbye world">() + }) + + it("should replace the string in the middle", () => { + type T2 = _ReplaceString< + "hello world", + "world", + "universe" + > + expectTypeOf().toEqualTypeOf<"hello universe">() + }) + + it("should replace the string at the end", () => { + type T3 = _ReplaceString< + "hello world", + "world", + "universe" + > + expectTypeOf().toEqualTypeOf<"hello universe">() + }) + + it("should not replace if the string is not found", () => { + type T5 = _ReplaceString< + "hello world", + "goodbye", + "universe" + > + expectTypeOf().toEqualTypeOf<"hello world">() + }) + + it("should handle special characters", () => { + type T9 = _ReplaceString< + "hello.world", + ".", + "_" + > + expectTypeOf().toEqualTypeOf<"hello_world">() + }) + + it("should handle special characters in replacement", () => { + type T10 = _ReplaceString< + "hello world", + "world", + "universe!" + > + expectTypeOf().toEqualTypeOf<"hello universe!">() + }) + + it("should handle strings with multibyte characters", () => { + type T11 = _ReplaceString< + "你好世界", + "世界", + "地球" + > + expectTypeOf().toEqualTypeOf<"你好地球">() + }) + + it("should handle cases where the replacement string is longer", () => { + type T12 = _ReplaceString< + "short", + "short", + "very long string" + > + expectTypeOf().toEqualTypeOf<"very long string">() + }) + + it("should handle cases where the replacement string is shorter", () => { + type T13 = _ReplaceString< + "long string", + "long string", + "short" + > + expectTypeOf().toEqualTypeOf<"short">() + }) + it("should handle empty replacement string", () => { + type T6 = _ReplaceString< + "hello world", + "world", + "" + > + expectTypeOf().toEqualTypeOf<"hello ">() // this would make sense if user would like to remove a word from literal, than he can do it again for tailing space + }) + + it("should handle empty string", () => { + type T7 = _ReplaceString< + "", + "world", + "universe" + > + expectTypeOf().toEqualTypeOf<"">() + }) + + it("should handle empty search string", () => { + type T8 = _ReplaceString< + "hello world", + "", + "universe" + > + expectTypeOf().toEqualTypeOf<"hello world">() // this would make no sense + }) +}) diff --git a/src/string/replace/index.ts b/src/string/replace/index.ts new file mode 100644 index 0000000..7b04334 --- /dev/null +++ b/src/string/replace/index.ts @@ -0,0 +1,16 @@ +import type { _ReplaceString } from "./algo" + +/** + * Replace substring on a type level. If substring is not a part of string, the oryginal strnig will be returned. + * + * @template Str - The string type to check against. + * @template StrToReplace - Part of the oryginal string. + * @template Replacement - String that will replace second arg. + * @returns {string} - Returns string with replaced substring or oryginal string if the substring to replace was not found in oryginal string. + */ + +export type ReplaceString< + Str extends string, + StrToReplace extends string, + Replacement extends string, +> = _ReplaceString