-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathcommon.js
136 lines (117 loc) · 3.88 KB
/
common.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
export default {
writeCookie(name, value, days) {
let expires;
if (days) {
const date = new Date();
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
expires = '; expires=' + date.toGMTString();
}
else {
expires = '';
}
document.cookie = name + '=' + value + expires + '; path=/';
},
getIntlMessage: function(messages, path) {
const pathParts = path.split('.');
let message;
try {
message = pathParts.reduce((obj, pathPart) => obj[pathPart], messages);
} finally {
if (message === undefined) {
throw new ReferenceError('Could not find Intl message: ' + path);
}
}
return message;
},
isEmpty: function(toTest) {
return (toTest === undefined ||
toTest === null ||
toTest === '' ||
(toTest instanceof Object && Object.keys(toTest).length === 0) ||
(toTest instanceof Array && toTest.length === 0));
},
timeSince: function(date) {
let seconds = Math.floor((new Date() - date) / 1000);
let interval = Math.floor(seconds / 31536000);
if (interval > 1) {
return interval + ' years';
}
interval = Math.floor(seconds / 2592000);
if (interval > 1) {
return interval + ' months';
}
interval = Math.floor(seconds / 86400);
if (interval > 1) {
return interval + ' days';
}
interval = Math.floor(seconds / 3600);
if (interval > 1) {
return interval + ' hours';
}
interval = Math.floor(seconds / 60);
if (interval > 1) {
return interval + ' minutes';
}
return Math.floor(seconds) + ' seconds';
},
getBrowserLanguage: function() {
let language = navigator.browserLanguage ? navigator.browserLanguage : navigator.language;
if (language.length === 2) {
language += '_' + language.toUpperCase();
}
else {
language = language.replace('-', '_');
}
return language;
},
getIntlLanguage() {
let language = 'en';
if (document.cookie.indexOf('locale=') > 0)
language = document.cookie.substr(document.cookie.indexOf('locale=') + 7, 2);
return language;
},
isLocalStorageOn: function() {
let mod = 'react-count';
try {
localStorage.setItem(mod, mod);
localStorage.removeItem(mod);
return true;
} catch(e) {
return false;
}
},
arraysEqual: (arr1, arr2) => {
if (arr1.length !== arr2.length)
return false;
for(let i = arr1.length; i--;) {
if(arr1[i] !== arr2[i])
return false;
}
return true;
},
arraysContainTheSameIdsInTheirObjects: (a, b) => {
if (a === undefined && b === undefined)
return true;
if ((a === undefined && b !== undefined) || (a !== undefined && b === undefined))
return false;
if (a.length !== b.length)
return false;
for (let key in a) {
const element = a[key];
const found = b.findIndex((e) => {return e.id === element.id;}) !== -1;
if (!found)
return false;
}
for (let key in b) {
const element = b[key];
const found = a.findIndex((e) => {return e.id === element.id;}) !== -1;
if (!found)
return false;
}
return true;
},
isEmailAddress: (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(email);
}
};