Skip to content

Commit

Permalink
Merge pull request #183 from charlesLoder/alias-kq
Browse files Browse the repository at this point in the history
Add optional ketiv qere syntax
  • Loading branch information
charlesLoder authored Sep 19, 2024
2 parents 862d399 + 6d42b9e commit 2d5fa4f
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 6 deletions.
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

0 comments on commit 2d5fa4f

Please sign in to comment.