forked from isjeffcom/coronvirusFigureUK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
82 lines (67 loc) · 1.48 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
// 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;
}
module.exports = {
deepCopy: deepCopy,
getTS: getTS,
idIdxsInArr: idIdxsInArr,
idIdxsInArrWithId: idIdxsInArrWithId,
matchNum: matchNum,
isJson: isJson
}