Coder Social home page Coder Social logo

Comments (9)

sunboyZgz avatar sunboyZgz commented on May 14, 2024 1

运用以前的Split和JoinStrArray

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

type ReplaceAll<
  S extends string,
  From extends string,
  To extends string
> = JoinStrArray<Split<S, From>, To>;
type R01 = Replace<"", "", "">; // ''
type R11 = Replace<"foobar", "bar", "foo">; // "foofoo"
type R21 = Replace<"foobarbar", "bar", "foo">; // "foofoobar"
type R3 = ReplaceAll<"foobarfoobarob", "ob", "b">; // "fobarfobar"

from awesome-typescript.

zhaoxiongfei avatar zhaoxiongfei commented on May 14, 2024 1
// 实现 Replace 工具类型,用于实现字符串类型的替换操作。具体的使用示例如下所示:

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

type R0 = Replace<"", "", "">; // ''
type R1 = Replace<"foobar", "bar", "foo">; // "foofoo"
type R2 = Replace<"foobarbar", "bar", "foo">; // "foofoobar"
// 此外,继续实现 ReplaceAll 工具类型,用于实现替换所有满足条件的子串。具体的使用示例如下所示:

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

type R0 = ReplaceAll<"", "", "">; // ''
type R1 = ReplaceAll<"barfoo", "bar", "foo">; // "foofoo"
type R2 = ReplaceAll<"foobarbar", "bar", "foo">; // "foofoofoo"
type R3 = ReplaceAll<"foobarfoobar", "ob", "b">; // "fobarfobar"

思路: 利用extends 配合infer 配合字符串模板变量的写法就能提取出指定的子字符串,之后利用递归就可以ReplaceAll

from awesome-typescript.

mingzhans avatar mingzhans commented on May 14, 2024
type Replace<
  S extends string,
  From extends string,
  To extends string
> = S extends `${infer A}${From}${infer B}` ? `${A}${To}${B}` : S// 你的实现代码 
  
type R0 = Replace<'', '', ''> // ''
type R1 = Replace<'foobar', 'bar', 'foo'> // "foofoo"
type R2 = Replace<'foobarbar', 'bar', 'foo'> // "foofoobar"

type ReplaceAll<
  S extends string,
  From extends string,
  To extends string
> = S extends `${infer A}${From}${infer Rest}` ? `${A}${To}${ReplaceAll<Rest,From,To>}` : S// 你的实现代码 
  
type C0 = ReplaceAll<'', '', ''> // ''
type C1 = ReplaceAll<'foobar', 'bar', 'foo'> // "foofoo"
type C2 = ReplaceAll<'foobarbar', 'bar', 'foo'> // "foofoobar"
type C3 = ReplaceAll<'foobarfoobar', 'ob', 'b'> // "fobarfobar"

from awesome-typescript.

jackwangwj avatar jackwangwj commented on May 14, 2024

// 先匹配${infer U}${From}{infer U}
// 然后替换
type Replace<
S extends string,
From extends string,
To extends string

= S extends ${infer Head}${From}${infer Tail} ?
${Head}${To}${Tail} : S
// 你的实现代码

type R0 = Replace<'', '', ''> // ''
type R1 = Replace<'foobar', 'bar', 'foo'> // "foofoo"
type R2 = Replace<'foobarbar', 'bar', 'foo'> // "foofoobar"

from awesome-typescript.

jackwangwj avatar jackwangwj commented on May 14, 2024

// 先匹配${infer U}${From}{infer U}
// 然后替换
type Replace<
S extends string,
From extends string,
To extends string

= S extends ${infer Head}${From}${infer Tail} ?
${Head}${To}${Tail} : S
// 你的实现代码

type R0 = Replace<'', '', ''> // ''
type R1 = Replace<'foobar', 'bar', 'foo'> // "foofoo"
type R2 = Replace<'foobarbar', 'bar', 'foo'> // "foofoobar"

from awesome-typescript.

jackwangwj avatar jackwangwj commented on May 14, 2024

// 在上一个Replace的基础上递归
// 此题有个陷阱,R3用例中"fobarfobar",进一步替换可以变为"fbarfbar",
// 所以得处理下,Head部分和替换部分不参与递归
type ReplaceAll<
S extends string,
From extends string,
To extends string
> = S extends ${infer Head}${From}${infer Tail}
? ${Head}${To}${ReplaceAll<Tail, From, To>}
: S

type R0 = ReplaceAll<'', '', ''> // ''
type R1 = ReplaceAll<'barfoo', 'bar', 'foo'> // "foofoo"
type R2 = ReplaceAll<'foobarbar', 'bar', 'foo'> // "foofoofoo"
type R3 = ReplaceAll<'foobarfoobar', 'ob', 'b'> // "fobarfobar"

from awesome-typescript.

ChangerHe avatar ChangerHe commented on May 14, 2024
// 实现 Replace 工具类型,用于实现字符串类型的替换操作

// 解法: 通过模板字符串结合infer即可完成替换
type Replace<
  S extends string,
  From extends string,
  To extends string
> = S extends `${infer F}${From}${infer L}` ? `${F}${To}${L}` : S

// 继续实现 ReplaceAll 工具类型,用于实现替换所有满足条件的子串

// 解法: 在replace的基础上, 递归对前后字符串进行处理即可
type ReplaceAll<
  S extends string,
  From extends string,
  To extends string
> = S extends `${infer F}${From}${infer L}` ? `${ReplaceAll<F, From, To>}${To}${ReplaceAll<L, From, To>}` : S

from awesome-typescript.

ChuTingzj avatar ChuTingzj commented on May 14, 2024
// 实现 Replace 工具类型,用于实现字符串类型的替换操作。具体的使用示例如下所示:
// type Replace<
//   S extends string,
//   From extends string,
//   To extends string
// > = // 你的实现代码
// type R0 = Replace<'', '', ''> // ''
// type R1 = Replace<'foobar', 'bar', 'foo'> // "foofoo"
// type R2 = Replace<'foobarbar', 'bar', 'foo'> // "foofoobar"

type Replace<S extends string, From extends string, To extends string> = S extends `${From}${infer Rest}`
  ? `${To}${Rest}`
  : S extends `${infer Head}${From}${infer Rest}`
  ? `${Head}${To}${Rest}`
  : S extends `${infer Head}${infer From}`
  ? `${Head}${To}`
  : S
type R0 = Replace<'', '', ''> // ''
type R11 = Replace<'foobar', 'bar', 'foo'> // "foofoo"
type R2 = Replace<'foobarbar', 'bar', 'foo'> // "foofoobar"

// 此外,继续实现 ReplaceAll 工具类型,用于实现替换所有满足条件的子串。具体的使用示例如下所示:
// type ReplaceAll<
//   S extends string,
//   From extends string,
//   To extends string
// > = // 你的实现代码
// type R0 = ReplaceAll<'', '', ''> // ''
// type R1 = ReplaceAll<'barfoo', 'bar', 'foo'> // "foofoo"
// type R2 = ReplaceAll<'foobarbar', 'bar', 'foo'> // "foofoofoo"
// type R3 = ReplaceAll<'foobarfoobar', 'ob', 'b'> // "fobarfobar"

type ReplaceAll<S extends string, From extends string, To extends string> = S extends `${From}`
  ? S
  : S extends `${From}${infer Rest}`
  ? `${To}${ReplaceAll<Rest, From, To>}`
  : S extends `${infer Head}${From}${infer Rest}`
  ? `${Head}${To}${ReplaceAll<Rest, From, To>}`
  : S extends `${infer Head}${infer From}`
  ? `${Head}${To}`
  : S
type R00 = ReplaceAll<'', '', ''> // ''
type R111 = ReplaceAll<'barfoo', 'bar', 'foo'> // "foofoo"
type R22 = ReplaceAll<'foobarbar', 'bar', 'foo'> // "foofoofoo"
type R33 = ReplaceAll<'foobarfoobar', 'ob', 'b'> // "fobarfobar"

from awesome-typescript.

dolphin0618 avatar dolphin0618 commented on May 14, 2024
type Replace<
  S extends string,
  From extends string,
  To extends string
> = S extends `${infer A}${From}${infer B}` ? `${A}${To}${B}` : S

type ReplaceAll<
  S extends string,
  From extends string,
  To extends string
> = S extends `${infer A}${From}${infer B}` ? `${A}${To}${ReplaceAll<B, From, To>}` : S
  
type R0 = ReplaceAll<'', '', ''> // ''
type R1 = ReplaceAll<'barfoo', 'bar', 'foo'> // "foofoo"
type R2 = ReplaceAll<'foobarbar', 'bar', 'foo'> // "foofoofoo"
type R3 = ReplaceAll<'foobarfoobar', 'ob', 'b'> // "fobarfobar"

from awesome-typescript.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.