-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.ts
More file actions
56 lines (52 loc) · 1.86 KB
/
utils.ts
File metadata and controls
56 lines (52 loc) · 1.86 KB
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
54
55
56
import fs from 'fs';
import { Definition, DefinitionType } from './types';
import { DictionaryV2, DictionaryV2_1 } from './v2migration/engine/types';
export function addTagsWithinSpaces(text: string, leftTag: string, rightTag: string) {
const hasLeftSpace = /^\s/.test(text);
const hasRightSpace = /\s$/.test(text);
return (hasLeftSpace ? ' ' : '') + leftTag + text.trim() + rightTag + (hasRightSpace ? ' ' : '');
}
export function aggregateDefinitions(definitions: Definition[]): string[] {
return [
definitions
.map((def) => {
switch (def.type) {
case DefinitionType.Example:
return addTagsWithinSpaces(def.text, '{', '}'); //`{${def.text}}`;
case DefinitionType.Tag:
return addTagsWithinSpaces(def.text, '<', '>'); //`<${def.text}>`;
default:
if (def.type !== DefinitionType.Plain) {
console.error(def.type, 'is not of type', DefinitionType.Plain);
}
return def.text;
}
})
.join(''),
];
}
/**
* Function to write a JSON file
*
* @param {string} filePath Absolute path and name of the JSON file to be written
* @param {Dictionary} data Dictionary data to be written to the JSON file
* @param {boolean} prettyPrint Whether to pretty print the JSON file
*/
export function writeJSONFile(
filePath: string,
data: DictionaryV2 | DictionaryV2_1,
prettyPrint = true,
) {
const fileContent = JSON.stringify(data, null, prettyPrint ? 2 : null);
fs.writeFileSync(filePath, fileContent);
}
export function toLowerCaseLezgi(
lezgiString: string,
options: { capitalize: boolean } = { capitalize: false },
) {
const lowerCased = lezgiString.toLowerCase().replaceAll(/(?<=[кптцчКПТЦЧ])[i1lӏ|!]/g, 'I');
if (options.capitalize) {
return lowerCased.charAt(0).toUpperCase() + lowerCased.slice(1);
}
return lowerCased;
}