mirror of
https://github.com/denoland/std.git
synced 2024-11-21 12:40:03 +00:00
refactor(yaml): simplify regexp type (#5860)
initial commit Co-authored-by: Asher Gomez <ashersaupingomez@gmail.com>
This commit is contained in:
parent
0b59bfd465
commit
b7efc57d4f
@ -10,35 +10,24 @@ const REGEXP = /^\/(?<regexp>[\s\S]+)\/(?<modifiers>[gismuy]*)$/;
|
||||
export const regexp: Type<"scalar", RegExp> = {
|
||||
tag: "tag:yaml.org,2002:js/regexp",
|
||||
kind: "scalar",
|
||||
resolve(data: string): boolean {
|
||||
if ((data === null) || (!data.length)) {
|
||||
return false;
|
||||
}
|
||||
resolve(data: string | null): boolean {
|
||||
if (data === null || !data.length) return false;
|
||||
|
||||
const regexp = `${data}`;
|
||||
if (regexp.charAt(0) === "/") {
|
||||
if (data.charAt(0) === "/") {
|
||||
// Ensure regex is properly terminated
|
||||
if (!REGEXP.test(data)) {
|
||||
return false;
|
||||
}
|
||||
const groups = data.match(REGEXP)?.groups;
|
||||
if (!groups) return false;
|
||||
// Check no duplicate modifiers
|
||||
const modifiers = [...(regexp.match(REGEXP)?.groups?.modifiers ?? "")];
|
||||
if (new Set(modifiers).size < modifiers.length) {
|
||||
return false;
|
||||
}
|
||||
const modifiers = groups.modifiers ?? "";
|
||||
if (new Set(modifiers).size < modifiers.length) return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
},
|
||||
construct(data: string) {
|
||||
const { regexp = `${data}`, modifiers = "" } =
|
||||
`${data}`.match(REGEXP)?.groups ?? {};
|
||||
construct(data: string): RegExp {
|
||||
const { regexp = data, modifiers = "" } = data.match(REGEXP)?.groups ?? {};
|
||||
return new RegExp(regexp, modifiers);
|
||||
},
|
||||
predicate(object: unknown) {
|
||||
return object instanceof RegExp;
|
||||
},
|
||||
represent(object: RegExp) {
|
||||
return object.toString();
|
||||
},
|
||||
predicate: (object: unknown): object is RegExp => object instanceof RegExp,
|
||||
represent: (object: RegExp): string => object.toString(),
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user