-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsefariaTextType.ts
53 lines (47 loc) · 1.47 KB
/
sefariaTextType.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// eslint-disable-next-line @typescript-eslint/triple-slash-reference,spaced-comment
/// <reference path="sefaria.d.ts" />
export function sefariaTextTypeTransformation(
transformation: (text: string) => string,
): (text: sefaria.TextType) => sefaria.TextType {
const recursive = (text: sefaria.TextType): sefaria.TextType => {
if (Array.isArray(text)) {
return text.map(recursive) as sefaria.TextType;
}
return transformation(text);
};
return recursive;
}
export function firstOrOnlyElement(text: sefaria.TextType): string {
if (typeof text === "string") {
return text;
}
return firstOrOnlyElement(text[0]);
}
export function equalJaggedArrays(hebrew: sefaria.TextType, english: sefaria.TextType): boolean {
if (Array.isArray(hebrew) && Array.isArray(english)) {
if (hebrew.length !== english.length) {
return false;
}
for (let i = 0; i <= hebrew.length; i++) {
if (!equalJaggedArrays(hebrew[i], english[i])) {
return false;
}
}
return true;
}
return hebrew === english;
}
export function flatten(textType: sefaria.TextType): string | undefined {
if (!textType) return undefined;
if (typeof textType === "string") {
return textType;
}
return textType.flat(Infinity).join("\n");
}
export function toFlatArray(textType: sefaria.TextType): string[] | undefined {
if (!textType) return undefined;
if (typeof textType === "string") {
return [textType];
}
return textType.flat(Infinity);
}