-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathuseful.js
61 lines (46 loc) · 1.38 KB
/
useful.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
"use strict";
const fs = require('fs');
class StringOperations{
static strContainsOnlyDoubleEscapedChars(strToCheck){
if(strToCheck.match(/(?:\\x[0-9a-fA-F][0-9a-fA-F])+/)){
const allEscapedCharMatches = [...strToCheck.matchAll(/\\x([0-9a-fA-F][0-9a-fA-F])/g)];
allEscapedCharMatches.forEach((escapedCharMatchStr) => {
const asciiCode = Number(`0x${escapedCharMatchStr}`);
if(asciiCode > 0x7F){
return false;
}
});
return true;
}
return false;
}
static unescapeStrWithDoubleEscapedChars(strToParse){
return eval(`"${strToParse}"`);
}
static isValidIdentifierName(strToCheck){
if(strToCheck.length == 0){
return false;
}
const firstChar = strToCheck[0];
if((firstChar.toLowerCase() == firstChar.toUpperCase()) && (firstChar != '$') && (firstChar != '_')){
return false;
}
if(strToCheck.indexOf('-') != -1){
return false;
}
return true;
}
}
class NumberOperations{
static numberIsFloat(nr){
return nr.toString().indexOf('.') != -1;
}
}
function writeTextFile(path, content){
fs.writeFileSync(path, content, {flag: 'w'});
}
module.exports = {
StringOperations,
NumberOperations,
writeTextFile
};