-
-
Notifications
You must be signed in to change notification settings - Fork 169
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #217 from samchon/v3.3
Fix #216 - json schema's pattern does use `/`
- Loading branch information
Showing
9 changed files
with
56 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { Metadata } from "../../metadata/Metadata"; | ||
|
||
import { ArrayUtil } from "../../utils/ArrayUtil"; | ||
import { PatternUtil } from "../../utils/PatternUtil"; | ||
|
||
import { template_to_pattern } from "./template_to_pattern"; | ||
|
||
export function metadata_to_pattern(meta: Metadata): string { | ||
if (meta.atomics.find((type) => type === "string") !== undefined) | ||
return "(.*)"; | ||
|
||
const values: string[] = ArrayUtil.flat( | ||
meta.constants.map((c) => { | ||
if (c.type !== "string") return c.values.map((v) => v.toString()); | ||
return c.values.map((str) => PatternUtil.escape(str)); | ||
}), | ||
); | ||
for (const type of meta.atomics) | ||
if (type === "number" || type === "bigint") | ||
values.push(PatternUtil.NUMBER); | ||
else if (type === "boolean") values.push(PatternUtil.BOOLEAN); | ||
for (const childTpl of meta.templates) | ||
values.push("(" + template_to_pattern(childTpl) + ")"); | ||
|
||
return values.length === 1 ? values[0]! : "(" + values.join("|") + ")"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,9 @@ | ||
import { Metadata } from "../../metadata/Metadata"; | ||
|
||
import { ArrayUtil } from "../../utils/ArrayUtil"; | ||
import { metadata_to_pattern } from "./metadata_to_pattern"; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
export const template_to_pattern = (template: Metadata[]) => | ||
template | ||
.map((meta) => { | ||
if (meta.atomics.find((type) => type === "string") !== undefined) | ||
return "(.*)"; | ||
|
||
const values: string[] = ArrayUtil.flat( | ||
meta.constants.map((c) => { | ||
if (c.type !== "string") | ||
return c.values.map((v) => v.toString()); | ||
return c.values.map((str) => escape(str)); | ||
}), | ||
); | ||
for (const type of meta.atomics) | ||
if (type === "number" || type === "bigint") | ||
values.push("\\d+(\\.\\d+)?"); | ||
else if (type === "boolean") values.push("true|false"); | ||
for (const childTpl of meta.templates) | ||
values.push("(" + template_to_pattern(childTpl) + ")"); | ||
return "(" + values.join("|") + ")"; | ||
}) | ||
.join(""); | ||
|
||
const escape = (str: string) => | ||
str.replace(/[|\\{}()[\]^$+*?.]/g, "\\$&").replace(/-/g, "\\x2d"); | ||
template.map((meta) => metadata_to_pattern(meta)).join(""); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
export namespace PatternUtil { | ||
export function escape(str: string): string { | ||
return str | ||
.replace(/[|\\{}()[\]^$+*?.]/g, "\\$&") | ||
.replace(/-/g, "\\x2d"); | ||
} | ||
|
||
export const NUMBER = "\\d+(\\.\\d+)?"; | ||
export const BOOLEAN = "true|false"; | ||
export const STRING = "(.*)"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters