00116-medium-replace

Back

type Replace<Source extends string, From extends string, To extends string> =
    From extends ''
        ? Source
        : Source extends `${infer V}${From}${infer R}`
            ? `${V}${To}${R}`
            : Source

Solution by HrOkiG2 #35265

type Replace<S extends string, From extends string, To extends string> = 
  From extends '' ? S 
  : S extends `${infer Left}${From}${infer Right}` ? `${Left}${To}${Right}` 
  : S

Solution by eunsukimme #35138

type Replace<S extends string, From extends string, To extends string> = From extends '' ? S : S extends `${infer L}${From}${infer R}` ? `${L}${To}${R}` : S;

Solution by dev-jaemin #34978

type Replace<S extends string, From extends string, To extends string> = S extends `${infer Head}${From}${infer Tail}`
  ? `${Head}${From extends '' ? '' : To}${Tail}`
  : S;

Solution by holger2138 #34606

type Replace<S extends string, From extends string, To extends string> =
  From extends '' ? (
    S
  ) : S extends `${infer A}${From}${infer B}` ? (
    `${A}${To}${B}`
  ) : S

Solution by Yunjaejo #34600

type Replace<S extends string, From extends string, To extends string> = 
      From extends '' 
      ? S 
      : S extends `${infer V}${From}${infer R}`
        ? `${V}${To}${R}`
        : S

Solution by devshinthant #34574

use Sliding window implementation

type JoinString<F extends string, S extends string> = `${F}${S}`

type RetainFirst<S extends string> = S extends `${infer F}${infer _R}` ? F : S
type RemoveFirst<S extends string> = S extends `${infer _F}${infer R}` ? R : S

type StringToArray<S extends string> = S extends `${infer F}${infer R}` ? [F, ...StringToArray<R>] : []
type LengthOfStr<S extends string> = StringToArray<S>['length']

type Replace<
    S extends string,
    From extends string,
    To extends string,
    Temp extends string = ''
> = S extends `${infer First}${infer Rest}`
    ? LengthOfStr<JoinString<Temp, First>> extends LengthOfStr<From>
        ? JoinString<Temp, First> extends From
            ? `${To}${Rest}`
            : `${RetainFirst<JoinString<Temp, First>>}${Replace<Rest, From, To, RemoveFirst<JoinString<Temp, First>>>}`
        : Replace<Rest, From, To, JoinString<Temp, First>>
    : Temp

Solution by Mumujianguang #34480

type Replace<S extends string, From extends string, To extends string> = From extends '' ? S : S extends `${infer prefix}${From}${infer suffix}` ? `${prefix}${To}${suffix}` : S

Solution by ouzexi #33997

type Replace<S extends string, T extends string, U extends string> =
  T extends ''
    ? S
    : S extends `${infer L}${T}${infer R}`
      ? `${L}${U}${R}`
      : S

Solution by Danny101201 #33848

type ReplaceAll<S extends string, From extends string, To extends string> =
  S extends `${infer A}${From extends '' ? never : From}${infer Rest}`
    ? `${A}${To}${ReplaceAll<Rest, From, To>}`
    : S

Solution by laplace1009 #33783

116 - Replace

by Anthony Fu (@antfu) #medium #template-literal

Question

Implement Replace<S, From, To> which replace the string From with To once in the given string S

For example

type replaced = Replace<'types are fun!', 'fun', 'awesome'> // expected to be 'types are awesome!'

View on GitHub: https://tsch.js.org/116

...

type Replace<S extends string, From extends string, To extends string> = From extends `` 
  ? S 
  : S extends `${infer L}${From}${infer R}` 
    ? `${L}${To}${R}` 
    : S;

Solution by veralex #33781

type Replace<S extends string, K extends string, V extends string>= S extends ${infer F}${K}${infer E} ? ${F}${V}${E}:S

Solution by rookiewxy #33682

type Replace<
  S extends string,
  O extends string,
  T extends string
> = S extends `${infer F}${O}${infer R}` ? `${F}${T}${R}` : S

Solution by Flavour86 #33459

type Replace<S extends string, From extends string, To extends string> = From extends '' ? S : S extends `${infer F}${From}${infer L}` ? `${F}${To}${L}` : S

Solution by Tubring25 #33312

type Replace<S extends string, From extends string, To extends string> =  From extends '' 
      ? S 
      : S extends `${infer left}${From}${infer right}`
        ? `${left}${To}${right}`
        : S

Solution by loevray #33280

// your answers

type Replace<S extends string, From extends string, To extends string> = 
  From extends ''
    ? S
    : S extends `${infer F}${From}${infer R}` 
      ? `${F}${To}${R}`
      : S

Solution by KeithChou #33195

// 解答をここに記入
type Replace<S extends string, From extends string, To extends string> = From extends ''
  ? S
  : S extends `${infer Start}${From}${infer Tail}`
    ? `${Start}${To}${Tail}`
    : S;

Solution by Yasunori-aloha #32903

type Replace<S extends string, From extends string, To extends string> = S extends `${infer First}${From}${infer Rest}` 
                                                                        ? From extends '' ? S : `${First}${To}${Rest}` 
                                                                        : S

Solution by ZhipengYang0605 #32820

type Replace<S extends string, From extends string, To extends string> = 
  From extends ''
  ? S
  : S extends `${infer H}${From}${infer T}`
      ? `${H}${To}${T}`
      : S```

Solution by keyurparalkar #32410

type Replace<S extends string, From extends string, To extends string> = 
  From extends '' 
    ? S 
    : S extends `${infer Head}${From}${infer Tail}` 
      ? `${Head}${To}${Tail}` 
      : S

Solution by dev-hobin #32375

type Replace<S extends string, Target extends string, Replaced extends string> = 
      Target extends '' 
      ? S 
      : S extends `${infer First}${Target }${infer Last}`
        ? `${First}${Replaced }${Last}`
        : S

Solution by rkamely #32059

type Replace<S extends string, From extends string, To extends string> = From extends ""
	? S
	: S extends `${infer F}${From}${infer L}`
	? L extends ""
		? `${F}${To}`
		: `${F}${To}${L}`
	: S;

Solution by gasmg #32049

// 解答をここに記入
type Replace<S extends string, From extends string, To extends string> = From extends '' ? S : S extends `${infer F}${From}${infer R}` ? `${F}${To}${R}` : S;

Solution by pea-sys #31999

type Replace<S extends string, From extends string, To extends string> = From extends '' ? S : S extends `${infer F}${From}${infer R}` ? `${F}${To}${R}` : S;

Solution by kai-phan #31628

// your answers
type Replace<
  S extends string,
  K extends string,
  T extends string
> = S extends `${infer Str1}${K}${infer Str2}` ? `${Str1}${T}${Str2}` : S;

Solution by AhmedRagabKamal #31507

type Replace<
  S extends string,
  From extends string,
  To extends string
> = S extends `${infer Left}${From extends "" ? never : From}${infer Right}`
  ? `${Left}${To}${Right}`
  : S;

Solution by vipulpathak113 #31469

type Replace<S extends string, From extends string, To extends string> = From extends '' ? S : S extends `${infer A}${From}${infer B}` ? `${A}${To}${B}` : S;

Solution by eward957 #31235

// your answers
type Replace<S extends string, From extends string, To extends string> = 
  From extends ''
    ? S
    : S extends `${infer x}${From}${infer y}`
      ? `${x}${To}${y}`
      : S;

Solution by d1zzzzy #31108

type Replace<S extends string, From extends string, To extends string> = 
      From extends '' 
      ? S 
      : S extends `${infer V}${From}${infer R}`
        ? `${V}${To}${R}`
        : S

Solution by MyeonghoonNam #31009

type Replace<S extends string, From extends string, To extends string> = 
S extends `${infer L}${From extends '' ? never : From}${infer R}` ? `${L}${To}${R}` : S

Solution by qiuye-zhou #30417