Skip to content

v6.9.0

Compare
Choose a tag to compare
@samchon samchon released this 20 Aug 21:21
· 350 commits to master since this release
0771e23

tags.Pattern type has been changed like below.

By such changement, tags.Pattern type no more misses escaped characters.

Before

export type Pattern<Value extends string> = TagBase<{
  target: "string";
  kind: "pattern";
  value: Value;
  validate: `/${Value}/.test($input)`;
  exclusive: ["format", "pattern"];
  schema: {
    pattern: Value;
  };
}>;

After

export type Pattern<Value extends string> = TagBase<{
  target: "string";
  kind: "pattern";
  value: Value;
  validate: `RegExp("${Serialize<Value>}").test($input)`;
  exclusive: ["format", "pattern"];
  schema: {
    pattern: Value;
  };
}>;

/// reference: https://github.com/type-challenges/type-challenges/issues/22394#issuecomment-1397158205
type Serialize<T extends string, Output extends string = ""> = string extends T
  ? never
  : T extends ""
    ? Output
    : T extends `${infer P}${infer R}`
      ? Serialize<R, `${Output}${P extends keyof Escaper ? Escaper[P] : P}`>
      : never;

type Escaper = {
  '"': '\\"';
  "\\": "\\\\";
  "\b": "\\b";
  "\f": "\\f";
  "\n": "\\n";
  "\r": "\\r";
  "\t": "\\t";
};

What's Changed

Full Changelog: v6.8.0...v6.9.0