From df3b044ee1c2b863fab9e3a183c79e01d0f039f9 Mon Sep 17 00:00:00 2001 From: Asher Gomez Date: Wed, 3 Jul 2024 12:07:30 +1000 Subject: [PATCH 1/2] BREAKING(front-matter): remove `createExtractor()` --- ...eate_extractor.ts => _create_extractor.ts} | 80 ------------------- ...ctor_test.ts => _create_extractor_test.ts} | 2 +- front_matter/any.ts | 2 +- front_matter/deno.json | 1 - front_matter/json.ts | 22 +---- front_matter/mod.ts | 1 - front_matter/toml.ts | 2 +- front_matter/yaml.ts | 2 +- 8 files changed, 5 insertions(+), 107 deletions(-) rename front_matter/{create_extractor.ts => _create_extractor.ts} (50%) rename front_matter/{create_extractor_test.ts => _create_extractor_test.ts} (97%) diff --git a/front_matter/create_extractor.ts b/front_matter/_create_extractor.ts similarity index 50% rename from front_matter/create_extractor.ts rename to front_matter/_create_extractor.ts index e16e527edbe1..5ff69d96e268 100644 --- a/front_matter/create_extractor.ts +++ b/front_matter/_create_extractor.ts @@ -75,86 +75,6 @@ function recognize(str: string, formats?: Format[]): Format { * * @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. - * - * @example Extract YAML front matter - * ```ts - * import { createExtractor, Parser } from "@std/front-matter"; - * import { assertEquals } from "@std/assert"; - * import { parse as parseYaml } from "@std/yaml/parse"; - * - * const extractYaml = createExtractor({ yaml: parseYaml as Parser }); - * const { attrs, body, frontMatter } = extractYaml<{ title: string }>( - * `--- - * title: Three dashes marks the spot - * --- - * ferret`); - * assertEquals(attrs.title, "Three dashes marks the spot"); - * assertEquals(body, "ferret"); - * assertEquals(frontMatter, "title: Three dashes marks the spot"); - * ``` - * - * @example Extract TOML front matter - * ```ts - * import { createExtractor, Parser } from "@std/front-matter"; - * import { assertEquals } from "@std/assert"; - * import { parse as parseToml } from "@std/toml/parse"; - * - * const extractToml = createExtractor({ toml: parseToml as Parser }); - * const { attrs, body, frontMatter } = extractToml<{ title: string }>( - * `---toml - * title = 'Three dashes followed by format marks the spot' - * --- - * `); - * assertEquals(attrs.title, "Three dashes followed by format marks the spot"); - * assertEquals(body, ""); - * assertEquals(frontMatter, "title = 'Three dashes followed by format marks the spot'"); - * ``` - * - * @example Extract JSON front matter - * ```ts - * import { createExtractor, Parser } from "@std/front-matter"; - * import { assertEquals } from "@std/assert"; - * - * const extractJson = createExtractor({ json: JSON.parse as Parser }); - * const { attrs, body, frontMatter } = extractJson<{ title: string }>( - * `---json - * {"title": "Three dashes followed by format marks the spot"} - * --- - * goat`); - * 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"}`); - * ``` - * - * @example Extract YAML or JSON front matter - * ```ts - * import { createExtractor, Parser } from "@std/front-matter"; - * import { assertEquals } from "@std/assert"; - * import { parse as parseYaml } from "@std/yaml/parse"; - * - * const extractYamlOrJson = createExtractor({ - * yaml: parseYaml as Parser, - * json: JSON.parse as Parser, - * }); - * - * let { attrs, body, frontMatter } = extractYamlOrJson<{ title: string }>( - * `--- - * title: Three dashes marks the spot - * --- - * ferret`); - * 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 - * {"title": "Three dashes followed by format marks the spot"} - * --- - * goat`)); - * 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>, diff --git a/front_matter/create_extractor_test.ts b/front_matter/_create_extractor_test.ts similarity index 97% rename from front_matter/create_extractor_test.ts rename to front_matter/_create_extractor_test.ts index 1a3b0df0c822..ce19f81ee62c 100644 --- a/front_matter/create_extractor_test.ts +++ b/front_matter/_create_extractor_test.ts @@ -11,7 +11,7 @@ import { runExtractYamlTests1, runExtractYamlTests2, } from "./_test_utils.ts"; -import { createExtractor, type Parser } from "./create_extractor.ts"; +import { createExtractor, type Parser } from "./_create_extractor.ts"; const extractYaml = createExtractor({ "yaml": parseYaml as Parser }); const extractToml = createExtractor({ "toml": parseToml as Parser }); diff --git a/front_matter/any.ts b/front_matter/any.ts index e169f5d74287..ede4cd8d8192 100644 --- a/front_matter/any.ts +++ b/front_matter/any.ts @@ -4,7 +4,7 @@ import { createExtractor, type Extractor, type Parser, -} from "./create_extractor.ts"; +} from "./_create_extractor.ts"; import { parse as parseYaml } from "@std/yaml/parse"; import { parse as parseToml } from "@std/toml/parse"; diff --git a/front_matter/deno.json b/front_matter/deno.json index cbf79dc947ef..2af97b01eb95 100644 --- a/front_matter/deno.json +++ b/front_matter/deno.json @@ -4,7 +4,6 @@ "exports": { ".": "./mod.ts", "./any": "./any.ts", - "./create-extractor": "./create_extractor.ts", "./json": "./json.ts", "./test": "./test.ts", "./toml": "./toml.ts", diff --git a/front_matter/json.ts b/front_matter/json.ts index 56f20cdaaea2..705c78e6ad1f 100644 --- a/front_matter/json.ts +++ b/front_matter/json.ts @@ -4,31 +4,11 @@ import { createExtractor, type Extractor, type Parser, -} from "./create_extractor.ts"; +} from "./_create_extractor.ts"; /** * Extracts and parses {@link https://www.json.org/ | JSON } from the metadata * of front matter content. - * - * @example Extract JSON front matter - * ```ts - * import { extract } from "@std/front-matter/json"; - * import { assertEquals } from "@std/assert"; - * - * const output = `---json - * { - * "title": "Three dashes marks the spot" - * } - * --- - * Hello, world!`; - * const result = extract(output); - * - * assertEquals(result, { - * frontMatter: '{\n "title": "Three dashes marks the spot"\n}', - * body: "Hello, world!", - * attrs: { title: "Three dashes marks the spot" }, - * }); - * ``` */ export const extract: Extractor = createExtractor({ json: JSON.parse as Parser, diff --git a/front_matter/mod.ts b/front_matter/mod.ts index 227392f73794..c944e8e670ae 100644 --- a/front_matter/mod.ts +++ b/front_matter/mod.ts @@ -124,7 +124,6 @@ import { extract as extractJson } from "./json.ts"; import { extract as extractToml } from "./toml.ts"; import { extract as extractYaml } from "./yaml.ts"; -export * from "./create_extractor.ts"; export * from "./test.ts"; export { extractJson, extractToml, extractYaml }; diff --git a/front_matter/toml.ts b/front_matter/toml.ts index 98718e59515b..e5f97eb400b5 100644 --- a/front_matter/toml.ts +++ b/front_matter/toml.ts @@ -4,7 +4,7 @@ import { createExtractor, type Extractor, type Parser, -} from "./create_extractor.ts"; +} from "./_create_extractor.ts"; import { parse } from "@std/toml/parse"; /** diff --git a/front_matter/yaml.ts b/front_matter/yaml.ts index cb4a0a9d134b..77130318190f 100644 --- a/front_matter/yaml.ts +++ b/front_matter/yaml.ts @@ -4,7 +4,7 @@ import { createExtractor, type Extractor, type Parser, -} from "./create_extractor.ts"; +} from "./_create_extractor.ts"; import { parse } from "@std/yaml/parse"; /** From 82178bfca384a6e4e693fe711be1b05bbb7a348b Mon Sep 17 00:00:00 2001 From: Asher Gomez Date: Wed, 3 Jul 2024 15:50:30 +1000 Subject: [PATCH 2/2] work --- front_matter/_create_extractor.ts | 13 +------------ front_matter/any.ts | 9 ++++----- front_matter/deno.json | 3 ++- front_matter/json.ts | 9 ++++----- front_matter/mod.ts | 1 + front_matter/toml.ts | 9 ++++----- front_matter/types.ts | 16 ++++++++++++++++ front_matter/yaml.ts | 9 ++++----- 8 files changed, 36 insertions(+), 33 deletions(-) create mode 100644 front_matter/types.ts diff --git a/front_matter/_create_extractor.ts b/front_matter/_create_extractor.ts index 5ff69d96e268..4b6dcaf15810 100644 --- a/front_matter/_create_extractor.ts +++ b/front_matter/_create_extractor.ts @@ -2,18 +2,7 @@ import { EXTRACT_REGEXP_MAP, RECOGNIZE_REGEXP_MAP } from "./_formats.ts"; import type { Format } from "./_types.ts"; - -/** Return type for {@linkcode Extractor}. */ -export type Extract = { - frontMatter: string; - body: string; - attrs: T; -}; - -/** Function return type for {@linkcode createExtractor}. */ -export type Extractor = >( - str: string, -) => Extract; +import type { Extract, Extractor } from "./types.ts"; /** Parser function type used alongside {@linkcode createExtractor}. */ export type Parser = >(str: string) => T; diff --git a/front_matter/any.ts b/front_matter/any.ts index ede4cd8d8192..957eae3dd065 100644 --- a/front_matter/any.ts +++ b/front_matter/any.ts @@ -1,12 +1,11 @@ // Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. -import { - createExtractor, - type Extractor, - type Parser, -} from "./_create_extractor.ts"; +import { createExtractor, type Parser } from "./_create_extractor.ts"; import { parse as parseYaml } from "@std/yaml/parse"; import { parse as parseToml } from "@std/toml/parse"; +import type { Extractor } from "./types.ts"; + +export type { Extractor }; /** * Extracts and parses {@link https://yaml.org | YAML}, {@link https://toml.io | diff --git a/front_matter/deno.json b/front_matter/deno.json index 2af97b01eb95..0eafb2706aee 100644 --- a/front_matter/deno.json +++ b/front_matter/deno.json @@ -7,6 +7,7 @@ "./json": "./json.ts", "./test": "./test.ts", "./toml": "./toml.ts", - "./yaml": "./yaml.ts" + "./yaml": "./yaml.ts", + "./types": "./types.ts" } } diff --git a/front_matter/json.ts b/front_matter/json.ts index 705c78e6ad1f..71b38cf7885b 100644 --- a/front_matter/json.ts +++ b/front_matter/json.ts @@ -1,10 +1,9 @@ // Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. -import { - createExtractor, - type Extractor, - type Parser, -} from "./_create_extractor.ts"; +import { createExtractor, type Parser } from "./_create_extractor.ts"; +import type { Extractor } from "./types.ts"; + +export type { Extractor }; /** * Extracts and parses {@link https://www.json.org/ | JSON } from the metadata diff --git a/front_matter/mod.ts b/front_matter/mod.ts index c944e8e670ae..890e65af1aa9 100644 --- a/front_matter/mod.ts +++ b/front_matter/mod.ts @@ -125,5 +125,6 @@ import { extract as extractToml } from "./toml.ts"; import { extract as extractYaml } from "./yaml.ts"; export * from "./test.ts"; +export * from "./types.ts"; export { extractJson, extractToml, extractYaml }; diff --git a/front_matter/toml.ts b/front_matter/toml.ts index e5f97eb400b5..d0026fc2fdf4 100644 --- a/front_matter/toml.ts +++ b/front_matter/toml.ts @@ -1,11 +1,10 @@ // Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. -import { - createExtractor, - type Extractor, - type Parser, -} from "./_create_extractor.ts"; +import { createExtractor, type Parser } from "./_create_extractor.ts"; import { parse } from "@std/toml/parse"; +import type { Extractor } from "./types.ts"; + +export type { Extractor }; /** * Extracts and parses {@link https://toml.io | TOML} from the metadata of diff --git a/front_matter/types.ts b/front_matter/types.ts new file mode 100644 index 000000000000..3fd8ea5e38e1 --- /dev/null +++ b/front_matter/types.ts @@ -0,0 +1,16 @@ +// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. + +/** Return type for functions of the {@linkcode Extractor} type. */ +export type Extract = { + frontMatter: string; + body: string; + attrs: T; +}; + +/** + * Type for function that accepts an input string and returns + * {@linkcode Extract}. + */ +export type Extractor = >( + str: string, +) => Extract; diff --git a/front_matter/yaml.ts b/front_matter/yaml.ts index 77130318190f..b1ad4442eae5 100644 --- a/front_matter/yaml.ts +++ b/front_matter/yaml.ts @@ -1,11 +1,10 @@ // Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. -import { - createExtractor, - type Extractor, - type Parser, -} from "./_create_extractor.ts"; +import { createExtractor, type Parser } from "./_create_extractor.ts"; import { parse } from "@std/yaml/parse"; +import type { Extractor } from "./types.ts"; + +export type { Extractor }; /** * Extracts and parses {@link https://yaml.org | YAML} from the metadata of