-
Notifications
You must be signed in to change notification settings - Fork 1
/
utilities.js
188 lines (167 loc) · 4.95 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
// NSF/NSd/utilities.js
// Description:
// "utilities.js" provides general function to be widely used.
// Copyright 2018 NOOXY. All Rights Reserved.
var fs = require('fs');
const BACKSPACE = String.fromCharCode(127);
function validateEmail(email) {
var re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
return re.test(String(email).toLowerCase());
}
function returnPassword(prompt) {
if (prompt) {
process.stdout.write(prompt);
}
var stdin = process.stdin;
stdin.resume();
stdin.setRawMode(true);
stdin.resume();
stdin.setEncoding('utf8');
var password = '';
stdin.on('data', function (ch) {
ch = ch.toString('utf8');
switch (ch) {
case "\n":
case "\r":
case "\u0004":
// They've finished typing their password
process.stdout.write('\n');
stdin.setRawMode(false);
stdin.pause();
return password;
break;
case "\u0003":
// Ctrl-C
return null;
break;
case BACKSPACE:
password = password.slice(0, password.length - 1);
process.stdout.clearLine();
process.stdout.cursorTo(0);
process.stdout.write(prompt);
process.stdout.write(password.split('').map(function () {
return '*';
}).join(''));
break;
default:
// More passsword characters
process.stdout.write('*');
password += ch;
break;
}
});
}
let isEnglish = (string) => {
return /^[A-Za-z0-9]*$/.test(string);
};
// read a file and return a parsed JSON obj
returnJSONfromFile = (filename) => {
return JSON.parse(fs.readFileSync(filename, 'utf8'));;
};
String.prototype.replaceAll = function(search, replacement) {
var target = this;
return target.split(search).join(replacement);
};
// print the NSF LOGO
printLOGO = (version, copyright) => {
console.log('88b 88 dP\'Yb dP\'Yb Yb dP Yb dP TM')
console.log('88Yb88 dP Yb dP Yb YbdP YbdP ')
console.log('88 Y88 Yb dP Yb dP dPYb 88 ')
console.log('88 Y8 YbodP YbodP dP Yb 88 Service Framework. ')
console.log('')
console.log('')
console.log('ver. '+version+'. '+copyright)
console.log('For more information or update -> www.nooxy.tk')
console.log('')
};
// print log with tag
tagLog = (tag, logstring) => {
if(typeof(logstring)!='string') {
logstring = JSON.stringify(logstring, null, 2);
}
let _space = 10;
tag = tag.substring(0, _space);
for(var i=0; i < _space-tag.length; i++) {
if(i%2 != 1) {
tag = tag + ' ';
}
else {
tag = ' ' + tag;
}
}
console.log('['+tag+'] '+logstring.replaceAll('\n', '\n['+tag+'] '));
};
// generateGUID
generateUniqueID = () => {
return '_' + Math.random().toString(36).substr(2, 9);
};
hashString = (s) => {
var s = 0, i, chr;
if (s.length === 0) return hash;
for (i = 0; i < s.length; i++) {
chr = s.charCodeAt(i);
hash = ((hash << 5) - hash) + chr;
hash |= 0; // Convert to 32bit integer
}
return hash;
};
removeHTML = function(str) {
return String(str).replace(/&/g, '&').replace(/</g, '<').replace(/>/g,
'>').replace(/"/g, '"');
}
function s4() {
return Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1);
}
addDays = (date, days)=> {
var dat = date;
dat.setDate(dat.getDate() + days);
return dat;
}
generateGUID = () => {
return s4() + s4() + '-' + s4() + '-' + s4() + '-' +s4() + '-' + s4() + s4() +
s4();
}
searchObject = (object, value)=> {
for (let prop in object) {
if (object.hasOwnProperty(prop)) {
if (object[prop] === value) {
return prop;
}
}
}
};
SQLtoDate = (sqlDate) => {
let sqlDateArr1 = sqlDate.split("-");
let sYear = sqlDateArr1[0];
let sMonth = (Number(sqlDateArr1[1]) - 1).toString();
let sqlDateArr2 = sqlDateArr1[2].split(" ");
let sDay = sqlDateArr2[0];
let sqlDateArr3 = sqlDateArr2[1].split(":");
let sHour = sqlDateArr3[0];
let sMinute = sqlDateArr3[1];
let sqlDateArr4 = sqlDateArr3[2].split(".");
let sSecond = sqlDateArr4[0];
let sMillisecond = sqlDateArr4[1];
// console.log(sYear, ' ', sMonth, ' ', sDay, ' ', sHour, ' ', sMinute, ' ', sSecond, ' ', sMillisecond);
return new Date(sYear, sMonth, sDay, sHour, sMinute, sSecond);
}
DatetoSQL = (JsDate) => {
iso = JsDate.toISOString();
return iso.slice(0, 19).replace('T', ' ');
}
module.exports = {
validateEmail: validateEmail,
isEnglish: isEnglish,
returnPassword: returnPassword,
returnJSONfromFile: returnJSONfromFile,
printLOGO: printLOGO,
tagLog: tagLog,
generateUniqueID: generateUniqueID,
hashString: hashString,
removeHTML: removeHTML,
generateGUID: generateGUID,
searchObject: searchObject,
addDays: addDays,
DatetoSQL: DatetoSQL,
SQLtoDate: SQLtoDate
}