Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(front-matter): replace regexp objects with maps #5379

Merged
merged 3 commits into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 3 additions & 7 deletions front_matter/_create_extractor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,11 @@ function _extract<T>(
* @param str String to recognize.
* @param formats A list of formats to recognize. Defaults to all supported formats.
*/
function recognize(str: string, formats?: Format[]): Format {
if (!formats) {
formats = Object.keys(RECOGNIZE_REGEXP_MAP) as Format[];
}

function recognize(str: string, formats: Format[]): Format {
const [firstLine] = str.split(/(\r?\n)/) as [string];

for (const format of formats) {
if (RECOGNIZE_REGEXP_MAP[format].test(firstLine)) {
if (RECOGNIZE_REGEXP_MAP.get(format)?.test(firstLine)) {
return format;
}
}
Expand Down Expand Up @@ -71,7 +67,7 @@ export function createExtractor(

const parser = formats[format];
if (!parser) throw new TypeError(`Unsupported front matter format`);
const regexp = EXTRACT_REGEXP_MAP[format];
const regexp = EXTRACT_REGEXP_MAP.get(format);
if (!regexp) throw new TypeError(`Unsupported front matter format`);

return _extract(str, regexp, parser);
Expand Down
22 changes: 11 additions & 11 deletions front_matter/_formats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,14 @@ const [RECOGNIZE_JSON_REGEXP, EXTRACT_JSON_REGEXP] = createRegExps(
],
);

export const RECOGNIZE_REGEXP_MAP = {
yaml: RECOGNIZE_YAML_REGEXP,
toml: RECOGNIZE_TOML_REGEXP,
json: RECOGNIZE_JSON_REGEXP,
} as const;

export const EXTRACT_REGEXP_MAP = {
yaml: EXTRACT_YAML_REGEXP,
toml: EXTRACT_TOML_REGEXP,
json: EXTRACT_JSON_REGEXP,
} as const;
export const RECOGNIZE_REGEXP_MAP = new Map([
["yaml", RECOGNIZE_YAML_REGEXP],
["toml", RECOGNIZE_TOML_REGEXP],
["json", RECOGNIZE_JSON_REGEXP],
]);

export const EXTRACT_REGEXP_MAP = new Map([
["yaml", EXTRACT_YAML_REGEXP],
["toml", EXTRACT_TOML_REGEXP],
["json", EXTRACT_JSON_REGEXP],
]);
4 changes: 2 additions & 2 deletions front_matter/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@ export type { Format };
* ```
*/
export function test(str: string, formats?: Format[]): boolean {
if (!formats) formats = Object.keys(EXTRACT_REGEXP_MAP) as Format[];
if (!formats) formats = [...EXTRACT_REGEXP_MAP.keys()] as Format[];

for (const format of formats) {
const regexp = EXTRACT_REGEXP_MAP[format];
const regexp = EXTRACT_REGEXP_MAP.get(format);
if (!regexp) {
throw new TypeError(`Unable to test for ${format} front matter format`);
}
Expand Down