This repository has been archived by the owner on Jan 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathutils.js
102 lines (84 loc) · 1.89 KB
/
utils.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
// Var for convert text number to arabic number
const mTxtNumber = [
[1, "one"],
[2, "two"],
[3, "three"],
[4, "four"],
[5, "five"],
[6, "six"],
[7, "seven"],
[8, "eight"],
[9, "nine"],
[10, "ten"]
]
// Deep copy an object
function deepCopy(val){
return JSON.parse(JSON.stringify(val))
}
// Get a timestamp
function getTS () {
return Date.parse( new Date())
}
// Convert text number to arabic number
function matchNum(str){
var res = -1
for(let i=0; i<mTxtNumber.length; i++){
if(str.toLowerCase() == mTxtNumber[i][1]){
res = mTxtNumber[i][0]
}
}
return res
}
// Id index in array, output all index as an array
function idIdxsInArr(target, arr){
var res = []
for(let i=0;i<arr.length;i++){
if(arr[i].indexOf(target) != -1){
res.push(i)
}
}
return res.length > 0 ? res : -1
}
// Id index in array object, output all index as an array
function idIdxsInArrWithId(target, arr, id){
var res = []
for(let i=0;i<arr.length;i++){
if(arr[i][id].indexOf(target) != -1){
res.push(i)
}
}
return res.length > 0 ? res : -1
}
function isJson(str) {
try {
JSON.parse(str);
} catch (e) {
return false;
}
return true;
}
// Remove all front and back space
function removeFBSpace(str){
return str.replace(/(^\s*)|(\s*$)/g, "");
}
function tsToDate(ts){
let d = new Date(ts)
return d.getFullYear() + "-" + addZero(d.getMonth() + 1) + "-" + addZero(d.getDate())
}
function addZero(str){
if(str < 10){
return "0" + str
} else {
return str
}
}
module.exports = {
deepCopy: deepCopy,
getTS: getTS,
idIdxsInArr: idIdxsInArr,
idIdxsInArrWithId: idIdxsInArrWithId,
matchNum: matchNum,
isJson: isJson,
tsToDate: tsToDate,
removeFBSpace: removeFBSpace
}