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

Add optional ketiv qere syntax #183

Merged
merged 2 commits into from
Sep 19, 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
46 changes: 40 additions & 6 deletions src/text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"];
}

/**
Expand Down Expand Up @@ -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`
*
Expand All @@ -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("הִ֑וא", {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -453,7 +478,12 @@ export class Text {

// validate the shape of the ketivQeres
for (const [index, ketivQere] of ketivQeres.entries()) {
const { input, output, ignoreTaamim, captureTaamim } = ketivQere;
let { input, output } = ketivQere;
const { ketiv, qere, ignoreTaamim, captureTaamim } = ketivQere;

if (!input && ketiv) {
input = ketiv;
}

if (input === undefined) {
throw new Error(`The ketivQere at index ${index} must have an input`);
Expand All @@ -463,6 +493,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`);
}
Expand Down
1 change: 1 addition & 0 deletions test/options.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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] });
Expand Down
Loading