From 89b9c0caf1428c9960be0ff6174f5e50a586def3 Mon Sep 17 00:00:00 2001 From: charlesLoder Date: Thu, 19 Sep 2024 12:49:50 -0400 Subject: [PATCH 1/2] Add optional ketiv qere syntax --- src/text.ts | 45 ++++++++++++++++++++++++++++++++++++++------ test/options.test.ts | 1 + 2 files changed, 40 insertions(+), 6 deletions(-) diff --git a/src/text.ts b/src/text.ts index 5098d8f..af9f6be 100644 --- a/src/text.ts +++ b/src/text.ts @@ -34,6 +34,14 @@ export interface KetivQere { * @defaultValue false */ captureTaamim?: boolean; + /** + * Optional syntax for the {@link input} + */ + ketiv?: KetivQere["input"]; + /** + * Optional syntax for the {@link output} + */ + qere?: KetivQere["output"]; } /** @@ -120,7 +128,7 @@ export interface SylOpts { */ holemHaser?: "update" | "preserve" | "remove"; /** - * An array of KetivQere objects + * An array of KetivQere objects for mimicing the Ketiv and Qere system found in manuscripts and texts * * @defaultValue `undefined` * @@ -140,6 +148,21 @@ export interface SylOpts { * ``` * * @example + * Using optional syntax + * ```ts + * const text = new Text("הִ֑וא", { + * ketivQeres: [ + * { + * ketiv: "הִוא", + * qere: "הִיא" + * } + * ] + * }); + * console.log(text.words[0].text); + * // הִיא + * ``` + * + * @example * `captureTaamim` set to `true` * ```ts * const text = new Text("הִ֑וא", { @@ -376,15 +399,17 @@ export class Text { } #applyKetivQere(text: string, kq: KetivQere) { - if (kq.input instanceof RegExp) { + const input = kq.input ?? kq.ketiv; + const output = kq.output ?? kq.qere; + if (input instanceof RegExp) { const match = text.match(kq.input); if (match) { - return typeof kq.output === "string" ? kq.output : kq.output(text, kq.input); + return typeof output === "string" ? output : output(text, input); } } - if (kq.input === text) { - return typeof kq.output === "string" ? kq.output : kq.output(text, kq.input); + if (input === text) { + return typeof output === "string" ? output : output(text, input); } return null; @@ -453,7 +478,11 @@ export class Text { // validate the shape of the ketivQeres for (const [index, ketivQere] of ketivQeres.entries()) { - const { input, output, ignoreTaamim, captureTaamim } = ketivQere; + let { input, ketiv, output, qere, ignoreTaamim, captureTaamim } = ketivQere; + + if (!input && ketiv) { + input = ketiv; + } if (input === undefined) { throw new Error(`The ketivQere at index ${index} must have an input`); @@ -463,6 +492,10 @@ export class Text { throw new Error(`The input property of the ketivQere at index ${index} must be a string or RegExp`); } + if (!output && qere) { + output = qere; + } + if (output === undefined) { throw new Error(`The ketivQere at index ${index} must have an output`); } diff --git a/test/options.test.ts b/test/options.test.ts index 34bc581..e1488c7 100644 --- a/test/options.test.ts +++ b/test/options.test.ts @@ -116,6 +116,7 @@ describe.each` ${"3fs qere perpetuum (captureTaamim true, ignoreTaamim false, no match)"} | ${"הִוא"} | ${{ input: "הִ֑וא", output: "הִיא", captureTaamim: true, ignoreTaamim: false }} | ${"הִוא"} | ${"הִוא"} ${"3fs qere perpetuum (captureTaamim true, ignoreTaamim false, match)"} | ${"הִ֑וא"} | ${{ input: "הִ֑וא", output: "הִיא", captureTaamim: true, ignoreTaamim: false }} | ${"הִ֑וא"} | ${"הִ֑יא"} ${"quiesced alef using input as regex and output as callback"} | ${"וַיָּבִיאּוּ"} | ${{ input: /אּ/, output: (word: string, input: RegExp) => word.replace(input, "א") }} | ${"וַיָּבִיאּוּ"} | ${"וַיָּבִיאוּ"} + ${"3fs qere perpetuum (using optional syntax)"} | ${"הִ֑וא"} | ${{ ketiv: "הִוא", qere: "הִיא" }} | ${"הִ֑וא"} | ${"הִיא"} `("ketivQeres", ({ description, input, options, original, output }) => { test(description, () => { const text = new Text(input, { ketivQeres: [options] }); From 6d42b9edde6cc5121ab19365696289ab9fa45a78 Mon Sep 17 00:00:00 2001 From: charlesLoder Date: Thu, 19 Sep 2024 12:55:17 -0400 Subject: [PATCH 2/2] Fix linting errors --- src/text.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/text.ts b/src/text.ts index af9f6be..bf0ace5 100644 --- a/src/text.ts +++ b/src/text.ts @@ -478,7 +478,8 @@ export class Text { // validate the shape of the ketivQeres for (const [index, ketivQere] of ketivQeres.entries()) { - let { input, ketiv, output, qere, ignoreTaamim, captureTaamim } = ketivQere; + let { input, output } = ketivQere; + const { ketiv, qere, ignoreTaamim, captureTaamim } = ketivQere; if (!input && ketiv) { input = ketiv;