-
Notifications
You must be signed in to change notification settings - Fork 51
/
utilities.js
289 lines (241 loc) · 9.35 KB
/
utilities.js
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
// contains different utility functions to make things easier
const fs = require('fs');
const path = require('path')
const fetch = (...args) => import('node-fetch').then(({default: fetch}) => fetch(...args));
function renameInnerJSONKey(obj, oldKey, newKey){
for(let [key, value] of Object.entries(obj)){
if(isObject(value))
renameInnerJSONKey(value, oldKey, newKey)
if(key == oldKey)
renameJSONKey(obj, oldKey, newKey)
}
}
function renameJSONKey ( obj, oldKey, newKey ) {
obj[newKey] = obj[oldKey];
delete obj[oldKey];
}
// replace json values with default values
function replaceInnerJSON(obj,num=0, arr=[], inner){
for(let key of Object.keys(obj)) {
if(isObject(obj[key])) {
obj[key] = replaceJSON(obj[key],num, arr)
replaceInnerJSON(obj[key],num,arr,true);
}
}
obj = replaceJSON(obj,num, arr)
if(!inner)
return obj
}
function replaceJSON(obj,num=0,arr=[]){
for(let [key, value] of Object.entries(obj)){
if(Array.isArray(value))
obj[key] = arr
else if(!isNaN(value))
obj[key] = num
}
return obj
}
// values in arr is given first preferences & then by alphabetical order
// values in arr is given first preferences & then by alphabetical order
function sortJSON(jsonObj,arr=[]){
let objectKeys = Object.keys(jsonObj)
// sort numbers properly
if(!objectKeys.some(isNaN))
objectKeys.sort((a, b) => parseFloat(a)-parseFloat(b))
else
objectKeys.sort()
return arr.concat(objectKeys).reduce(
(obj, key) => {
if(key in jsonObj)
obj[key] = jsonObj[key];
return obj;
},
{}
);
}
function isObject(obj) {
return obj === Object(obj);
}
const capitalize = words => words.toLowerCase().replace(/(^\w{1})|(\s+\w{1})/g, match => match.toUpperCase()).trim()
async function getJSON(path, isLink){
if(isLink)
return await fetch(path).then(res => res.json())
return JSON.parse(fs.readFileSync(path).toString())
}
// gets the JSON from end of array, returns [jsondata, i], where i is the position from end where jsondata was parsed successfully
function getJSONInArray(arr) {
var i = 0
while (!isValidJSON(arr.slice(--i).join('\n')) && i > -100);
if (i != -100)
return [JSON.parse(arr.slice(i).join('\n')), i]
}
// This function checks the direction of the language and returns either rtl or ltr
// https://playwright.dev/#version=v1.3.0&path=docs%2Fcore-concepts.md&q=evaluation
async function dirCheck(str,page) {
var result = await page.evaluate(str => {
var divelem = document.createElement("div");
divelem.dir = "auto"
divelem.innerHTML = str;
document.body.appendChild(divelem)
return window.getComputedStyle(divelem).getPropertyValue('direction')
}, str);
return result
}
// Returns the iso name ,iso2 of the language
function isoLangMap(arrval,isocodes) {
for (var [lang, val] of Object.entries(isocodes)) {
if (arrval[0].toLowerCase().replace(/[^A-Za-z\(\)]+/gi, "").trim() == lang.toLowerCase().replace(/[^A-Za-z\(\)]+/gi, "").trim())
return [lang, val.iso2]
}
if (arrval[1]) {
for (var [lang, val] of Object.entries(isocodes)) {
if (val.iso1 == arrval[1] || val.iso2 == arrval[1])
return [lang, val.iso2]
}
}
}
// reads the text file and returns [originaljson, cleanedjson, cleanarr jsondata]
// orignalarr orignalfile as json,
// cleanedjson - No empty lines in it & no numbers etc
// jsondata - JSON data at the end of file, return undefined if doens't exists
function readDBTxt(pathToFile) {
var orgarr = fs.readFileSync(pathToFile).toString().split(/\r?\n/)
// now remove all lines with empty strings or spaces or tabs
// https://stackoverflow.com/a/281335
// return elememnt only if they are not spaces/tabs and emptyline
// var filterarr = orgarr.filter(elem => !/^\s*$/.test(elem))
// search & validate JSON in array
var temp = getJSONInArray(orgarr)
// If the json exists, then Remove the json from the file
if (Array.isArray(temp))
orgarr = orgarr.slice(0, temp[1])
// find index of element which doesn't follow pattern of number | text
let indexProblem = orgarr.findIndex(e=>!/^\d+\.?\d*\s*\|\s*/.test(e) && !/^\s*$/.test(e))
if(indexProblem != -1){
logmsg("problem at index "+indexProblem+" in file "+path.basename(pathToFile)+" skipping this")
return
}
// convert it into json for ease
var orgjson = orgarr.map(e=>[e.split('|')[0].trim(),e.split('|').slice(1).join(' ').trim()])
orgjson = Object.fromEntries(orgjson)
// remove empty lines from json
cleanjson = validateCleanTrans(orgjson)
//sort the decimals in json
cleanjson = sortJSON(cleanjson)
// If the json exists then return json with the array
if (Array.isArray(temp))
return [orgjson, cleanjson , temp[0]]
// return without json
return [orgjson, cleanjson]
}
function validateCleanTrans(json) {
// remove empty values from json
Object.keys(json).forEach(k => !json[k] && delete json[k]);
return cleanTrans(json)
}
// Cleaning translation from numbers, special symbols etc
function cleanTrans(json) {
for (let key of Object.keys(json)) {
// https://en.wikipedia.org/wiki/List_of_Unicode_characters#Basic_Latin
// This will remove all special symbols and numbers from starting and special symbols ending of verse
json[key] = json[key].replace(/^[\u0020-\u0040|\u005b-\u0060|\u007b-\u007e|\s|\n|\r|\p{N}]{1,20}/u, " ").replace(/^\s*\w{1}\s*(\.|\)|\}|>|\])+[\u0020-\u0040|\u005b-\u0060|\u007b-\u007e|\s|\n|\r|\p{N}]{0,7}/ui, " ").replace(/[\u0020-\u002F|\u005b-\u0060|\u007b-\u007e|\s|\n|\r|\p{N}]{1,15}$/u, " ").replace(/[\r\n]/g, " ").replace(/\s\s+/g, " ").trim()
// Checking partially open/close bracket exists or not at begninning of verse
var bracket1 = json[key].match(/^[^\[|\(|\<|\{]+(\]|\)|\>|\})/)
// Checking partially open/close bracket exists or not at end of verse
var bracket2 = json[key].match(/(\[|\(|\<|\{)[^\]|\)|\>|\}]+$/)
// closing partially open/close bracket in the verse
// closing partially open/close bracket at the beginning of verse
if (bracket1)
json[key] = getOppoBracket(bracket1[0].slice(-1)) + json[key]
// closing partially open/close bracket at the end of verse
if (bracket2)
json[key] = json[key] + getOppoBracket(bracket2[0].slice(0, 1))
}
return json
}
// returns opposite bracket
function getOppoBracket(str) {
switch (str) {
case '(':
return ')'
case ')':
return '('
case '<':
return '>'
case '>':
return '<'
case '[':
return ']'
case ']':
return '['
case '{':
return '}'
case '}':
return '{'
default:
return ''
}
}
// function which checks whether a string is valid json or not
function isValidJSON(str) {
try {
JSON.parse(str);
return true;
} catch (e) {
return false;
}
}
// cleans the json
function cleanifyObject(jsondata) {
// lowercase for all json , trimming white spaces and also removing empty json and also cleaning the keys and values
//https://stackoverflow.com/a/54985484/2437224
var newjson = Object.fromEntries(
Object.entries(jsondata).map(([k, v]) => {
if (v != undefined && typeof v !== 'boolean' && v)
return ["" + k.replace(/[^A-Za-z]+/gi, "").trim().toLowerCase(), "" + v.replace(/\s\s+/gi, " ").trim()]
return ["", ""]
})
);
// removing empty keys
delete newjson[""]
return newjson
}
// clean the string from special symbols,numbers,multiple spaces etc , this is used for string comparision
function cleanify(str) {
return str.replace(/[\u0020-\u0040|\u005b-\u0060|\u007b-\u007e|\s|\n|\p{N}]+/ugi, " ").replace(/^\s*\w{1}\s+/i, " ").replace(/\s\s+/g, " ").trim().toLowerCase()
}
// Stores all the log, to help in reviewing PR and checking for any mistake by the user
function logmsg(str, skipconsole) {
fs.appendFileSync(path.join(path.dirname(process.argv[1]),'log.txt'), '\n'+str)
if (!skipconsole)
console.log(str)
}
function saveJSON(jsondata, pathToFile, indent) {
if(indent)
fs.writeFileSync(pathToFile,JSON.stringify(jsondata,null,indent))
else
fs.writeFileSync(pathToFile,JSON.stringify(jsondata))
}
function getJSONKeyByValue(object, value) {
return Object.keys(object).find(key => object[key] === value);
}
// reads the file using streams, start is the starting byte and end is the bytes to read
async function streamRead(pathtofile, start=0, end=Infinity) {
if(end<0||end<start)
end=Infinity
var readstream = fs.createReadStream(pathtofile, {start:Math.max(start,0),end})
var data = ''
for await (var chunk of readstream) {
data = data + chunk.toString()
}
return data
}
function mode(arr){
return arr.sort((a,b) =>
arr.filter(v => v===a).length
- arr.filter(v => v===b).length
).pop();
}
module.exports = {
mode,cleanify,replaceInnerJSON,replaceJSON,streamRead,sortJSON,getJSONKeyByValue,renameInnerJSONKey,saveJSON, renameJSONKey,isObject,capitalize,getJSON,getJSONInArray,dirCheck,isoLangMap,readDBTxt,isValidJSON,cleanifyObject,logmsg
};