Skip to content

Commit

Permalink
Merge pull request #163 from charlesLoder/refactor-chars
Browse files Browse the repository at this point in the history
Refactor chars
  • Loading branch information
charlesLoder authored Apr 12, 2024
2 parents 91e8877 + 79d2574 commit 306ed6f
Show file tree
Hide file tree
Showing 7 changed files with 768 additions and 137 deletions.
241 changes: 204 additions & 37 deletions src/char.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import { Cluster } from "./cluster";
import { taamim } from "./utils/regularExpressions";
const consonants = /[\u{05D0}-\u{05F2}]/u;
const ligature = /[\u{05C1}-\u{05C2}]/u;
const dagesh = /[\u{05BC}\u{05BF}]/u; // includes rafe
const niqqud = /[\u{05B0}-\u{05BB}\u{05C7}]/u;
import { consonants, dagesh, ligatures, vowels, rafe, sheva, taamim } from "./utils/regularExpressions";
import { CharToNameMap, charToNameMap, NameToCharMap, nameToCharMap } from "./utils/charMap";

/**
* A Hebrew character and its positioning number for being sequenced correctly.
Expand All @@ -12,45 +9,222 @@ const niqqud = /[\u{05B0}-\u{05BB}\u{05C7}]/u;
export class Char {
#text: string;
#cluster: Cluster | null = null;
#sequencePosition: number;

constructor(char: string) {
this.#text = char;
}

/**
* @returns the text of the Char
*
* ```typescript
* const text: Text = new Text("אֱלֹהִ֑ים");
* text.chars[0].text;
* // "א"
* ```
*/
get text(): string {
return this.#text;
this.#sequencePosition = this.findPos();
}

private findPos(): number {
const char = this.text;
if (consonants.test(char)) {
if (Char.consonants.test(char)) {
return 0;
}
if (ligature.test(char)) {
if (Char.ligatures.test(char)) {
return 1;
}
if (dagesh.test(char)) {
if (Char.dagesh.test(char)) {
return 2;
}
if (niqqud.test(char)) {
if (Char.rafe.test(char)) {
return 2;
}
if (Char.vowels.test(char)) {
return 3;
}
if (Char.sheva.test(char)) {
return 3;
}
if (taamim.test(char)) {
if (Char.taamim.test(char)) {
return 4;
}
// i.e. any non-hebrew char
return 10;
}

private isCharKeyOfCharToNameMap(char: string): char is keyof CharToNameMap {
return char in charToNameMap;
}

private static get consonants() {
return consonants;
}

private static get dagesh() {
return dagesh;
}

private static get ligatures() {
return ligatures;
}

private static get rafe() {
return rafe;
}

private static get sheva() {
return sheva;
}

private static get taamim() {
return taamim;
}

private static get vowels() {
return vowels;
}

/**
* The parent `Cluster` of the `Char`, if any.
*
* ```typescript
* const text: Text = new Text("דָּבָר");
* const firstChar = text.chars[0];
* firstChar.text;
* // "ד"
* firstChar.cluster?.text;
* // "דָּ"
* ```
*/
get cluster(): Cluster | null {
return this.#cluster;
}

set cluster(cluster: Cluster | null) {
this.#cluster = cluster;
}

isCharacterName(name: keyof NameToCharMap): boolean {
if (!nameToCharMap[name]) {
throw new Error(`${name} is not a valid value`);
}

const match = this.#text.match(nameToCharMap[name]);

return !!match;
}

/**
* Returns `true` if the `Char` is a consonant
*
* ```typescript
* const text: Text = new Text("אֱלֹהִ֑ים");
* text.chars[0].isConsonant;
* // true
* ```
*/
get isConsonant(): boolean {
return Char.consonants.test(this.#text);
}

/**
* Returns `true` if the `Char` is a ligature
*
* ```typescript
* const text: Text = new Text("שָׁלֽוֹם");
* text.chars[1].isLigature;
* // true
* ```
*/
get isLigature(): boolean {
return Char.ligatures.test(this.#text);
}

/**
* Returns `true` if the `Char` is a dagesh
*
* ```typescript
* const text: Text = new Text("בּ");
* text.chars[1].isDagesh;
* // true
*/
get isDagesh(): boolean {
return Char.dagesh.test(this.#text);
}

/**
* Returns `true` if the `Char` is a rafe
*
* ```typescript
* const text: Text = new Text("בֿ");
* text.chars[1].isRafe;
* // true
* ```
*/
get isRafe(): boolean {
return Char.rafe.test(this.#text);
}

/**
* Returns `true` if the `Char` is a sheva
* ```typescript
* const text: Text = new Text("בְ");
* text.chars[1].isSheva;
* // true
* ```
*/
get isSheva(): boolean {
return Char.sheva.test(this.#text);
}

/**
* Returns `true` if the `Char` is a sheva
*
*
* ```typescript
* const text: Text = new Text("בֺ");
* text.chars[1].isVowel;
* // true
* ```
*/
get isVowel(): boolean {
return Char.vowels.test(this.#text);
}

/**
* Returns `true` if the `Char` is a taamim
*
* ```typescript
* const text: Text = new Text("בֺ֨");
* text.chars[2].isTaamim;
* // true
* ```
*/
get isTaamim(): boolean {
return Char.taamim.test(this.#text);
}

/**
* Returns `true` if the `Char` is not a Hebrew character
*
* ```typescript
* const text: Text = new Text("a");
* text.chars[0].isNotHebrew;
* // true
* ```
*/
get isNotHebrew(): boolean {
return this.sequencePosition === 10;
}

/**
* Returns the name of the character
*
* ```typescript
* const text: Text = new Text("אֱלֹהִ֑ים");
* text.chars[0].characterName;
* // "ALEF"
* ```
*/
get characterName(): CharToNameMap[keyof CharToNameMap] | null {
const text = this.#text;
if (this.isCharKeyOfCharToNameMap(text)) {
return charToNameMap[text];
}
return null;
}

/**
* @returns a number used for sequencing
*
Expand All @@ -69,26 +243,19 @@ export class Char {
* ```
*/
get sequencePosition(): number {
return this.findPos();
return this.#sequencePosition;
}

/**
* The parent `Cluster` of the `Char`, if any.
* @returns the text of the Char
*
* ```typescript
* const text: Text = new Text("דָּבָר");
* const firstChar = text.chars[0];
* firstChar.text;
* // "ד"
* firstChar.cluster?.text;
* // "דָּ"
* const text: Text = new Text("אֱלֹהִ֑ים");
* text.chars[0].text;
* // "א"
* ```
*/
get cluster(): Cluster | null {
return this.#cluster;
}

set cluster(cluster: Cluster | null) {
this.#cluster = cluster;
get text(): string {
return this.#text;
}
}
2 changes: 1 addition & 1 deletion src/cluster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Char } from "./char";
import { Node } from "./node";
import { Syllable } from "./syllable";
import { taamim, hebChars, vowelsCaptureGroup, punctuation } from "./utils/regularExpressions";
import { charToNameMap, CharToNameMap, NameToCharMap, nameToCharMap } from "./utils/vowelMap";
import { charToNameMap, CharToNameMap, NameToCharMap, nameToCharMap } from "./utils/charMap";

/**
* A cluster is group of Hebrew character constituted by:
Expand Down
2 changes: 1 addition & 1 deletion src/syllable.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Cluster } from "./cluster";
import { Char } from "./char";
import { CharToNameMap, charToNameMap, NameToCharMap, nameToCharMap } from "./utils/vowelMap";
import { CharToNameMap, charToNameMap, NameToCharMap, nameToCharMap } from "./utils/charMap";
import { vowelsCaptureGroupWithSheva } from "./utils/regularExpressions";
import { removeTaamim } from "./utils/removeTaamim";
import { Node } from "./node";
Expand Down
Loading

0 comments on commit 306ed6f

Please sign in to comment.