diff --git a/front_matter/_formats.ts b/front_matter/_formats.ts new file mode 100644 index 000000000000..1ad4b66a24e2 --- /dev/null +++ b/front_matter/_formats.ts @@ -0,0 +1,70 @@ +// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. + +type Delimiter = string | [begin: string, end: string]; + +export enum Format { + YAML = "yaml", + TOML = "toml", + JSON = "json", + UNKNOWN = "unknown", +} + +const { isArray } = Array; + +function getBeginToken(delimiter: Delimiter): string { + return isArray(delimiter) ? delimiter[0] : delimiter; +} + +function getEndToken(delimiter: Delimiter): string { + return isArray(delimiter) ? delimiter[1] : delimiter; +} + +function createRegExp(...dv: Delimiter[]): [RegExp, RegExp] { + const beginPattern = "(" + dv.map(getBeginToken).join("|") + ")"; + const pattern = "^(" + + "\\ufeff?" + // Maybe byte order mark + beginPattern + + "$([\\s\\S]+?)" + + "^(?:" + dv.map(getEndToken).join("|") + ")\\s*" + + "$" + + (globalThis?.Deno?.build?.os === "windows" ? "\\r?" : "") + + "(?:\\n)?)"; + + return [ + new RegExp("^" + beginPattern + "$", "im"), + new RegExp(pattern, "im"), + ]; +} + +const [RX_RECOGNIZE_YAML, RX_YAML] = createRegExp( + ["---yaml", "---"], + "= yaml =", + "---", +); +const [RX_RECOGNIZE_TOML, RX_TOML] = createRegExp( + ["---toml", "---"], + "\\+\\+\\+", + "= toml =", +); +const [RX_RECOGNIZE_JSON, RX_JSON] = createRegExp( + ["---json", "---"], + "= json =", +); + +export const MAP_FORMAT_TO_RECOGNIZER_RX: Omit< + Record, + Format.UNKNOWN +> = { + [Format.YAML]: RX_RECOGNIZE_YAML, + [Format.TOML]: RX_RECOGNIZE_TOML, + [Format.JSON]: RX_RECOGNIZE_JSON, +}; + +export const MAP_FORMAT_TO_EXTRACTOR_RX: Omit< + Record, + Format.UNKNOWN +> = { + [Format.YAML]: RX_YAML, + [Format.TOML]: RX_TOML, + [Format.JSON]: RX_JSON, +}; diff --git a/front_matter/_test_utils.ts b/front_matter/_test_utils.ts index 76e370ff0887..2c21a67ef1b4 100644 --- a/front_matter/_test_utils.ts +++ b/front_matter/_test_utils.ts @@ -2,7 +2,7 @@ import { assert, assertEquals, assertThrows } from "../assert/mod.ts"; import { dirname, fromFileUrl, join, resolve } from "../path/mod.ts"; -import { Format } from "./mod.ts"; +import { Format } from "./_formats.ts"; const moduleDir = dirname(fromFileUrl(import.meta.url)); const testdataDir = resolve(moduleDir, "testdata"); diff --git a/front_matter/any.ts b/front_matter/any.ts index f3a4873a6fa7..2a848a0f5d09 100644 --- a/front_matter/any.ts +++ b/front_matter/any.ts @@ -1,10 +1,12 @@ // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. -import { createExtractor, Format, Parser, test as _test } from "./mod.ts"; +import { createExtractor, Parser } from "./create_extractor.ts"; +import { Format } from "./_formats.ts"; import { parse as parseYAML } from "../yaml/parse.ts"; import { parse as parseTOML } from "../toml/parse.ts"; -export { Format, test } from "./mod.ts"; +export { Format } from "./_formats.ts"; +export { test } from "./test.ts"; export const extract = createExtractor({ [Format.YAML]: parseYAML as Parser, [Format.TOML]: parseTOML as Parser, diff --git a/front_matter/create_extractor.ts b/front_matter/create_extractor.ts new file mode 100644 index 000000000000..1d889f799820 --- /dev/null +++ b/front_matter/create_extractor.ts @@ -0,0 +1,134 @@ +// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. + +import { + Format, + MAP_FORMAT_TO_EXTRACTOR_RX, + MAP_FORMAT_TO_RECOGNIZER_RX, +} from "./_formats.ts"; + +export type Extract = { + frontMatter: string; + body: string; + attrs: T; +}; + +export type Extractor = >( + str: string, +) => Extract; + +export type Parser = >(str: string) => T; + +function _extract( + str: string, + rx: RegExp, + parse: Parser, +): Extract { + const match = rx.exec(str); + if (!match || match.index !== 0) { + throw new TypeError("Unexpected end of input"); + } + const frontMatter = match.at(-1)?.replace(/^\s+|\s+$/g, "") || ""; + const attrs = parse(frontMatter) as T; + const body = str.replace(match[0], ""); + return { frontMatter, body, attrs }; +} + +/** + * Recognizes the format of the front matter in a string. Supports YAML, TOML and JSON. + * + * @param str String to recognize. + * @param formats A list of formats to recognize. Defaults to all supported formats. + * + * ```ts + * import { recognize, Format } from "https://deno.land/std@$STD_VERSION/front_matter/mod.ts"; + * import { assertEquals } from "https://deno.land/std@$STD_VERSION/assert/assert_equals.ts"; + * + * assertEquals(recognize("---\ntitle: Three dashes marks the spot\n---\n"), Format.YAML); + * assertEquals(recognize("---toml\ntitle = 'Three dashes followed by format marks the spot'\n---\n"), Format.TOML); + * assertEquals(recognize("---json\n{\"title\": \"Three dashes followed by format marks the spot\"}\n---\n"), Format.JSON); + * assertEquals(recognize("---xml\nThree dashes marks the spot\n---\n"), Format.UNKNOWN); + * + * assertEquals(recognize("---json\nThree dashes marks the spot\n---\n", [Format.YAML]), Format.UNKNOWN); + */ +function recognize(str: string, formats?: Format[]): Format { + if (!formats) { + formats = Object.keys(MAP_FORMAT_TO_RECOGNIZER_RX) as Format[]; + } + + const [firstLine] = str.split(/(\r?\n)/); + + for (const format of formats) { + if (format === Format.UNKNOWN) { + continue; + } + + if (MAP_FORMAT_TO_RECOGNIZER_RX[format].test(firstLine)) { + return format; + } + } + + return Format.UNKNOWN; +} + +/** + * Factory that creates a function that extracts front matter from a string with the given parsers. + * Supports YAML, TOML and JSON. + * + * @param formats A descriptor containing Format-parser pairs to use for each format. + * @returns A function that extracts front matter from a string with the given parsers. + * + * ```ts + * import { createExtractor, Format, Parser } from "https://deno.land/std@$STD_VERSION/front_matter/mod.ts"; + * import { assertEquals } from "https://deno.land/std@$STD_VERSION/assert/assert_equals.ts"; + * import { parse as parseYAML } from "https://deno.land/std@$STD_VERSION/yaml/parse.ts"; + * import { parse as parseTOML } from "https://deno.land/std@$STD_VERSION/toml/parse.ts"; + * const extractYAML = createExtractor({ [Format.YAML]: parseYAML as Parser }); + * const extractTOML = createExtractor({ [Format.TOML]: parseTOML as Parser }); + * const extractJSON = createExtractor({ [Format.JSON]: JSON.parse as Parser }); + * const extractYAMLOrJSON = createExtractor({ + * [Format.YAML]: parseYAML as Parser, + * [Format.JSON]: JSON.parse as Parser, + * }); + * + * let { attrs, body, frontMatter } = extractYAML<{ title: string }>("---\ntitle: Three dashes marks the spot\n---\nferret"); + * assertEquals(attrs.title, "Three dashes marks the spot"); + * assertEquals(body, "ferret"); + * assertEquals(frontMatter, "title: Three dashes marks the spot"); + * + * ({ attrs, body, frontMatter } = extractTOML<{ title: string }>("---toml\ntitle = 'Three dashes followed by format marks the spot'\n---\n")); + * assertEquals(attrs.title, "Three dashes followed by format marks the spot"); + * assertEquals(body, ""); + * assertEquals(frontMatter, "title = 'Three dashes followed by format marks the spot'"); + * + * ({ attrs, body, frontMatter } = extractJSON<{ title: string }>("---json\n{\"title\": \"Three dashes followed by format marks the spot\"}\n---\ngoat")); + * assertEquals(attrs.title, "Three dashes followed by format marks the spot"); + * assertEquals(body, "goat"); + * assertEquals(frontMatter, "{\"title\": \"Three dashes followed by format marks the spot\"}"); + * + * ({ attrs, body, frontMatter } = extractYAMLOrJSON<{ title: string }>("---\ntitle: Three dashes marks the spot\n---\nferret")); + * assertEquals(attrs.title, "Three dashes marks the spot"); + * assertEquals(body, "ferret"); + * assertEquals(frontMatter, "title: Three dashes marks the spot"); + * + * ({ attrs, body, frontMatter } = extractYAMLOrJSON<{ title: string }>("---json\n{\"title\": \"Three dashes followed by format marks the spot\"}\n---\ngoat")); + * assertEquals(attrs.title, "Three dashes followed by format marks the spot"); + * assertEquals(body, "goat"); + * assertEquals(frontMatter, "{\"title\": \"Three dashes followed by format marks the spot\"}"); + * ``` + */ +export function createExtractor( + formats: Partial>, +): Extractor { + const formatKeys = Object.keys(formats) as Format[]; + + return function extract(str: string): Extract { + const format = recognize(str, formatKeys); + const parser = formats[format]; + + if (format === Format.UNKNOWN || !parser) { + throw new TypeError(`Unsupported front matter format`); + } + + return _extract(str, MAP_FORMAT_TO_EXTRACTOR_RX[format], parser); + }; +} diff --git a/front_matter/mod_test.ts b/front_matter/create_extractor_test.ts similarity index 71% rename from front_matter/mod_test.ts rename to front_matter/create_extractor_test.ts index f2a012d9fb71..a411a2b988dd 100644 --- a/front_matter/mod_test.ts +++ b/front_matter/create_extractor_test.ts @@ -1,7 +1,7 @@ // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. -import { assert, assertThrows } from "../assert/mod.ts"; -import { createExtractor, Format, Parser, test } from "./mod.ts"; +import { assertThrows } from "../assert/mod.ts"; +import { Format } from "./_formats.ts"; import { parse as parseYAML } from "../yaml/parse.ts"; import { parse as parseTOML } from "../toml/parse.ts"; import { @@ -11,9 +11,8 @@ import { runExtractTypeErrorTests, runExtractYAMLTests1, runExtractYAMLTests2, - runTestInvalidInputTests, - runTestValidInputTests, } from "./_test_utils.ts"; +import { createExtractor, Parser } from "./create_extractor.ts"; const extractYAML = createExtractor({ [Format.YAML]: parseYAML as Parser }); const extractTOML = createExtractor({ [Format.TOML]: parseTOML as Parser }); @@ -28,26 +27,8 @@ const extractAny = createExtractor({ [Format.TOML]: parseTOML as Parser, }); -// GENERAL TESTS // - -Deno.test("[ANY] try to test for unknown format", () => { - assertThrows( - () => test("foo", [Format.UNKNOWN]), - TypeError, - "Unable to test for unknown front matter format", - ); -}); - // YAML // -Deno.test("[YAML] test valid input true", () => { - runTestValidInputTests(Format.YAML, test); -}); - -Deno.test("[YAML] test invalid input false", () => { - runTestInvalidInputTests(Format.YAML, test); -}); - Deno.test("[YAML] extract type error on invalid input", () => { runExtractTypeErrorTests(Format.YAML, extractYAML); }); @@ -67,7 +48,6 @@ Deno.test({ resolveTestDataPath("./horizontal_rules.md"), ); - assert(!test(str)); assertThrows( () => { extractAny(str); @@ -80,14 +60,6 @@ Deno.test({ // JSON // -Deno.test("[JSON] test valid input true", () => { - runTestValidInputTests(Format.JSON, test); -}); - -Deno.test("[JSON] test invalid input false", () => { - runTestInvalidInputTests(Format.JSON, test); -}); - Deno.test("[JSON] extract type error on invalid input", () => { runExtractTypeErrorTests(Format.JSON, extractJSON); }); @@ -98,14 +70,6 @@ Deno.test("[JSON] parse json delineate by ---json", async () => { // TOML // -Deno.test("[TOML] test valid input true", () => { - runTestValidInputTests(Format.TOML, test); -}); - -Deno.test("[TOML] test invalid input false", () => { - runTestInvalidInputTests(Format.TOML, test); -}); - Deno.test("[TOML] extract type error on invalid input", () => { runExtractTypeErrorTests(Format.TOML, extractTOML); }); diff --git a/front_matter/json.ts b/front_matter/json.ts index 26fef3f34fb9..16175f75fd2e 100644 --- a/front_matter/json.ts +++ b/front_matter/json.ts @@ -1,7 +1,10 @@ // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. -import { createExtractor, Format, Parser, test as _test } from "./mod.ts"; -export { Format } from "./mod.ts"; +import { createExtractor, Parser } from "./create_extractor.ts"; +import { Format } from "./_formats.ts"; +import { test as _test } from "./test.ts"; + +export { Format } from "./_formats.ts"; export function test(str: string): boolean { return _test(str, [Format.JSON]); diff --git a/front_matter/mod.ts b/front_matter/mod.ts index 77d28b1b584c..db99d9430990 100644 --- a/front_matter/mod.ts +++ b/front_matter/mod.ts @@ -6,7 +6,7 @@ * [front matter](https://daily-dev-tips.com/posts/what-exactly-is-frontmatter/) * from strings. * - * {@linkcode createExtractor}, {@linkcode recognize} and {@linkcode test} functions + * {@linkcode createExtractor} and {@linkcode test} functions * to handle many forms of front matter. * * Adapted from @@ -155,227 +155,6 @@ * @module */ -type Delimiter = string | [begin: string, end: string]; -export type Parser = >(str: string) => T; -export type Extractor = >( - str: string, -) => Extract; - -export enum Format { - YAML = "yaml", - TOML = "toml", - JSON = "json", - UNKNOWN = "unknown", -} - -export type Extract = { - frontMatter: string; - body: string; - attrs: T; -}; - -const { isArray } = Array; -const [RX_RECOGNIZE_YAML, RX_YAML] = createRegExp( - ["---yaml", "---"], - "= yaml =", - "---", -); -const [RX_RECOGNIZE_TOML, RX_TOML] = createRegExp( - ["---toml", "---"], - "\\+\\+\\+", - "= toml =", -); -const [RX_RECOGNIZE_JSON, RX_JSON] = createRegExp( - ["---json", "---"], - "= json =", -); -const MAP_FORMAT_TO_RECOGNIZER_RX: Omit< - Record, - Format.UNKNOWN -> = { - [Format.YAML]: RX_RECOGNIZE_YAML, - [Format.TOML]: RX_RECOGNIZE_TOML, - [Format.JSON]: RX_RECOGNIZE_JSON, -}; -const MAP_FORMAT_TO_EXTRACTOR_RX: Omit, Format.UNKNOWN> = - { - [Format.YAML]: RX_YAML, - [Format.TOML]: RX_TOML, - [Format.JSON]: RX_JSON, - }; - -function getBeginToken(delimiter: Delimiter): string { - return isArray(delimiter) ? delimiter[0] : delimiter; -} - -function getEndToken(delimiter: Delimiter): string { - return isArray(delimiter) ? delimiter[1] : delimiter; -} - -function createRegExp(...dv: Delimiter[]): [RegExp, RegExp] { - const beginPattern = "(" + dv.map(getBeginToken).join("|") + ")"; - const pattern = "^(" + - "\\ufeff?" + // Maybe byte order mark - beginPattern + - "$([\\s\\S]+?)" + - "^(?:" + dv.map(getEndToken).join("|") + ")\\s*" + - "$" + - (globalThis?.Deno?.build?.os === "windows" ? "\\r?" : "") + - "(?:\\n)?)"; - - return [ - new RegExp("^" + beginPattern + "$", "im"), - new RegExp(pattern, "im"), - ]; -} - -function _extract( - str: string, - rx: RegExp, - parse: Parser, -): Extract { - const match = rx.exec(str); - if (!match || match.index !== 0) { - throw new TypeError("Unexpected end of input"); - } - const frontMatter = match.at(-1)?.replace(/^\s+|\s+$/g, "") || ""; - const attrs = parse(frontMatter) as T; - const body = str.replace(match[0], ""); - return { frontMatter, body, attrs }; -} - -/** - * Factory that creates a function that extracts front matter from a string with the given parsers. - * Supports YAML, TOML and JSON. - * - * @param formats A descriptor containing Format-parser pairs to use for each format. - * @returns A function that extracts front matter from a string with the given parsers. - * - * ```ts - * import { createExtractor, Format, Parser } from "https://deno.land/std@$STD_VERSION/front_matter/mod.ts"; - * import { assertEquals } from "https://deno.land/std@$STD_VERSION/assert/assert_equals.ts"; - * import { parse as parseYAML } from "https://deno.land/std@$STD_VERSION/yaml/parse.ts"; - * import { parse as parseTOML } from "https://deno.land/std@$STD_VERSION/toml/parse.ts"; - * const extractYAML = createExtractor({ [Format.YAML]: parseYAML as Parser }); - * const extractTOML = createExtractor({ [Format.TOML]: parseTOML as Parser }); - * const extractJSON = createExtractor({ [Format.JSON]: JSON.parse as Parser }); - * const extractYAMLOrJSON = createExtractor({ - * [Format.YAML]: parseYAML as Parser, - * [Format.JSON]: JSON.parse as Parser, - * }); - * - * let { attrs, body, frontMatter } = extractYAML<{ title: string }>("---\ntitle: Three dashes marks the spot\n---\nferret"); - * assertEquals(attrs.title, "Three dashes marks the spot"); - * assertEquals(body, "ferret"); - * assertEquals(frontMatter, "title: Three dashes marks the spot"); - * - * ({ attrs, body, frontMatter } = extractTOML<{ title: string }>("---toml\ntitle = 'Three dashes followed by format marks the spot'\n---\n")); - * assertEquals(attrs.title, "Three dashes followed by format marks the spot"); - * assertEquals(body, ""); - * assertEquals(frontMatter, "title = 'Three dashes followed by format marks the spot'"); - * - * ({ attrs, body, frontMatter } = extractJSON<{ title: string }>("---json\n{\"title\": \"Three dashes followed by format marks the spot\"}\n---\ngoat")); - * assertEquals(attrs.title, "Three dashes followed by format marks the spot"); - * assertEquals(body, "goat"); - * assertEquals(frontMatter, "{\"title\": \"Three dashes followed by format marks the spot\"}"); - * - * ({ attrs, body, frontMatter } = extractYAMLOrJSON<{ title: string }>("---\ntitle: Three dashes marks the spot\n---\nferret")); - * assertEquals(attrs.title, "Three dashes marks the spot"); - * assertEquals(body, "ferret"); - * assertEquals(frontMatter, "title: Three dashes marks the spot"); - * - * ({ attrs, body, frontMatter } = extractYAMLOrJSON<{ title: string }>("---json\n{\"title\": \"Three dashes followed by format marks the spot\"}\n---\ngoat")); - * assertEquals(attrs.title, "Three dashes followed by format marks the spot"); - * assertEquals(body, "goat"); - * assertEquals(frontMatter, "{\"title\": \"Three dashes followed by format marks the spot\"}"); - * ``` - */ -export function createExtractor( - formats: Partial>, -): Extractor { - const formatKeys = Object.keys(formats) as Format[]; - - return function extract(str: string): Extract { - const format = recognize(str, formatKeys); - const parser = formats[format]; - - if (format === Format.UNKNOWN || !parser) { - throw new TypeError(`Unsupported front matter format`); - } - - return _extract(str, MAP_FORMAT_TO_EXTRACTOR_RX[format], parser); - }; -} - -/** - * Tests if a string has valid front matter. Supports YAML, TOML and JSON. - * - * @param str String to test. - * @param formats A list of formats to test for. Defaults to all supported formats. - * - * ```ts - * import { test, Format } from "https://deno.land/std@$STD_VERSION/front_matter/mod.ts"; - * import { assert } from "https://deno.land/std@$STD_VERSION/assert/assert.ts"; - * - * assert(test("---\ntitle: Three dashes marks the spot\n---\n")); - * assert(test("---toml\ntitle = 'Three dashes followed by format marks the spot'\n---\n")); - * assert(test("---json\n{\"title\": \"Three dashes followed by format marks the spot\"}\n---\n")); - * - * assert(!test("---json\n{\"title\": \"Three dashes followed by format marks the spot\"}\n---\n", [Format.YAML])); - * ``` - */ -export function test(str: string, formats?: Format[]): boolean { - if (!formats) { - formats = Object.keys(MAP_FORMAT_TO_EXTRACTOR_RX) as Format[]; - } - - for (const format of formats) { - if (format === Format.UNKNOWN) { - throw new TypeError("Unable to test for unknown front matter format"); - } - - const match = MAP_FORMAT_TO_EXTRACTOR_RX[format].exec(str); - if (match?.index === 0) { - return true; - } - } - - return false; -} - -/** - * Recognizes the format of the front matter in a string. Supports YAML, TOML and JSON. - * - * @param str String to recognize. - * @param formats A list of formats to recognize. Defaults to all supported formats. - * - * ```ts - * import { recognize, Format } from "https://deno.land/std@$STD_VERSION/front_matter/mod.ts"; - * import { assertEquals } from "https://deno.land/std@$STD_VERSION/assert/assert_equals.ts"; - * - * assertEquals(recognize("---\ntitle: Three dashes marks the spot\n---\n"), Format.YAML); - * assertEquals(recognize("---toml\ntitle = 'Three dashes followed by format marks the spot'\n---\n"), Format.TOML); - * assertEquals(recognize("---json\n{\"title\": \"Three dashes followed by format marks the spot\"}\n---\n"), Format.JSON); - * assertEquals(recognize("---xml\nThree dashes marks the spot\n---\n"), Format.UNKNOWN); - * - * assertEquals(recognize("---json\nThree dashes marks the spot\n---\n", [Format.YAML]), Format.UNKNOWN); - */ -function recognize(str: string, formats?: Format[]): Format { - if (!formats) { - formats = Object.keys(MAP_FORMAT_TO_RECOGNIZER_RX) as Format[]; - } - - const [firstLine] = str.split(/(\r?\n)/); - - for (const format of formats) { - if (format === Format.UNKNOWN) { - continue; - } - - if (MAP_FORMAT_TO_RECOGNIZER_RX[format].test(firstLine)) { - return format; - } - } - - return Format.UNKNOWN; -} +export * from "./create_extractor.ts"; +export * from "./test.ts"; +export { Format } from "./_formats.ts"; diff --git a/front_matter/test.ts b/front_matter/test.ts new file mode 100644 index 000000000000..56a77048e2cd --- /dev/null +++ b/front_matter/test.ts @@ -0,0 +1,39 @@ +// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. + +import { Format, MAP_FORMAT_TO_EXTRACTOR_RX } from "./_formats.ts"; + +/** + * Tests if a string has valid front matter. Supports YAML, TOML and JSON. + * + * @param str String to test. + * @param formats A list of formats to test for. Defaults to all supported formats. + * + * ```ts + * import { test, Format } from "https://deno.land/std@$STD_VERSION/front_matter/mod.ts"; + * import { assert } from "https://deno.land/std@$STD_VERSION/assert/assert.ts"; + * + * assert(test("---\ntitle: Three dashes marks the spot\n---\n")); + * assert(test("---toml\ntitle = 'Three dashes followed by format marks the spot'\n---\n")); + * assert(test("---json\n{\"title\": \"Three dashes followed by format marks the spot\"}\n---\n")); + * + * assert(!test("---json\n{\"title\": \"Three dashes followed by format marks the spot\"}\n---\n", [Format.YAML])); + * ``` + */ +export function test(str: string, formats?: Format[]): boolean { + if (!formats) { + formats = Object.keys(MAP_FORMAT_TO_EXTRACTOR_RX) as Format[]; + } + + for (const format of formats) { + if (format === Format.UNKNOWN) { + throw new TypeError("Unable to test for unknown front matter format"); + } + + const match = MAP_FORMAT_TO_EXTRACTOR_RX[format].exec(str); + if (match?.index === 0) { + return true; + } + } + + return false; +} diff --git a/front_matter/test_test.ts b/front_matter/test_test.ts new file mode 100644 index 000000000000..2715bb16d2ae --- /dev/null +++ b/front_matter/test_test.ts @@ -0,0 +1,61 @@ +// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. + +import { assert, assertThrows } from "../assert/mod.ts"; +import { + resolveTestDataPath, + runTestInvalidInputTests, + runTestValidInputTests, +} from "./_test_utils.ts"; +import { Format } from "./_formats.ts"; +import { test } from "./test.ts"; + +// GENERAL TESTS // + +Deno.test("[ANY] try to test for unknown format", () => { + assertThrows( + () => test("foo", [Format.UNKNOWN]), + TypeError, + "Unable to test for unknown front matter format", + ); +}); + +// YAML // + +Deno.test("[YAML] test valid input true", () => { + runTestValidInputTests(Format.YAML, test); +}); + +Deno.test("[YAML] test invalid input false", () => { + runTestInvalidInputTests(Format.YAML, test); +}); + +Deno.test({ + name: "[YAML] text between horizontal rules should not be recognized", + async fn() { + const str = await Deno.readTextFile( + resolveTestDataPath("./horizontal_rules.md"), + ); + + assert(!test(str)); + }, +}); + +// JSON // + +Deno.test("[JSON] test valid input true", () => { + runTestValidInputTests(Format.JSON, test); +}); + +Deno.test("[JSON] test invalid input false", () => { + runTestInvalidInputTests(Format.JSON, test); +}); + +// TOML // + +Deno.test("[TOML] test valid input true", () => { + runTestValidInputTests(Format.TOML, test); +}); + +Deno.test("[TOML] test invalid input false", () => { + runTestInvalidInputTests(Format.TOML, test); +}); diff --git a/front_matter/toml.ts b/front_matter/toml.ts index 4299c58368f0..8695be90c987 100644 --- a/front_matter/toml.ts +++ b/front_matter/toml.ts @@ -1,9 +1,11 @@ // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. -import { createExtractor, Format, Parser, test as _test } from "./mod.ts"; +import { createExtractor, Parser } from "./create_extractor.ts"; +import { Format } from "./_formats.ts"; +import { test as _test } from "./test.ts"; import { parse } from "../toml/parse.ts"; -export { Format } from "./mod.ts"; +export { Format } from "./_formats.ts"; export function test(str: string): boolean { return _test(str, [Format.TOML]); diff --git a/front_matter/yaml.ts b/front_matter/yaml.ts index bf050c7fce98..18bd199c81e4 100644 --- a/front_matter/yaml.ts +++ b/front_matter/yaml.ts @@ -1,8 +1,11 @@ // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. -import { createExtractor, Format, Parser, test as _test } from "./mod.ts"; +import { createExtractor, Parser } from "./create_extractor.ts"; +import { Format } from "./_formats.ts"; +import { test as _test } from "./test.ts"; import { parse } from "../yaml/parse.ts"; -export { Format } from "./mod.ts"; + +export { Format } from "./_formats.ts"; export function test(str: string): boolean { return _test(str, [Format.YAML]);