@darkpos/utils 1.0.5 → 1.0.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +3 -3
- package/lib/{date → constants}/supportedTimeZones.json +0 -0
- package/lib/date.js +89 -0
- package/lib/helpers.js +88 -7
- package/lib/hooks/useLocalization.js +56 -0
- package/lib/{localization/helpers.js → localization.js} +42 -46
- package/lib/math.js +40 -20
- package/lib/system.js +35 -33
- package/package.json +15 -16
- package/lib/date/index.js +0 -116
- package/lib/localization/index.js +0 -49
package/index.js
CHANGED
|
@@ -2,13 +2,13 @@ const helpers = require('./lib/helpers');
|
|
|
2
2
|
const math = require('./lib/math');
|
|
3
3
|
const system = require('./lib/system');
|
|
4
4
|
const date = require('./lib/date');
|
|
5
|
-
const
|
|
5
|
+
const useLocalization = require('./lib/hooks/useLocalization');
|
|
6
6
|
|
|
7
7
|
//
|
|
8
8
|
module.exports = {
|
|
9
|
-
|
|
9
|
+
helpers,
|
|
10
10
|
system,
|
|
11
11
|
math,
|
|
12
12
|
date,
|
|
13
|
-
|
|
13
|
+
useLocalization,
|
|
14
14
|
};
|
|
File without changes
|
package/lib/date.js
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
const moment = require('moment-timezone');
|
|
2
|
+
const supportedTimeZones = require('./constants/supportedTimeZones.json');
|
|
3
|
+
|
|
4
|
+
const getTimeZones = supportedTimeZones;
|
|
5
|
+
|
|
6
|
+
const isBefore = (first, second, param) =>
|
|
7
|
+
moment(first).isBefore(moment(second), param);
|
|
8
|
+
|
|
9
|
+
const isEqual = (first, second, param) =>
|
|
10
|
+
moment(first).isSame(moment(second), param);
|
|
11
|
+
|
|
12
|
+
const isToday = value => {
|
|
13
|
+
const today = moment();
|
|
14
|
+
const date = moment(value);
|
|
15
|
+
|
|
16
|
+
return (
|
|
17
|
+
today.year() === date.year() &&
|
|
18
|
+
today.month() === date.month() &&
|
|
19
|
+
today.dayOfYear() === date.dayOfYear()
|
|
20
|
+
);
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
const isDate = d => moment(d, moment.ISO_8601, true).isValid();
|
|
24
|
+
|
|
25
|
+
const isAfter = (first, second) => moment(first).isAfter(moment(second));
|
|
26
|
+
|
|
27
|
+
const getTimestamp = date => moment(date).getTime();
|
|
28
|
+
|
|
29
|
+
const diff = (from, to) => moment(from).diff(moment(to));
|
|
30
|
+
|
|
31
|
+
const duration = date => moment.duration(date);
|
|
32
|
+
|
|
33
|
+
const toISOString = date => moment(date).toISOString();
|
|
34
|
+
|
|
35
|
+
const toMoment = date => moment(date);
|
|
36
|
+
|
|
37
|
+
const isValid = date => moment(date).isValid();
|
|
38
|
+
|
|
39
|
+
const subtract = (date, value, of) => moment(date).subtract(value, of);
|
|
40
|
+
|
|
41
|
+
const add = (date, value, to) => moment(date).add(value, to);
|
|
42
|
+
|
|
43
|
+
const startOf = (value, param = 'day') => moment(value).startOf(param);
|
|
44
|
+
|
|
45
|
+
const endOf = (value, param = 'day') => moment(value).endOf(param);
|
|
46
|
+
|
|
47
|
+
const isBetween = ({ dateToCompare, from, to }) => {
|
|
48
|
+
if (from && (!isValid(from) || isBefore(dateToCompare, from, 'day'))) {
|
|
49
|
+
return false;
|
|
50
|
+
}
|
|
51
|
+
if (to && (!isValid(to) || isBefore(to, dateToCompare, 'day'))) {
|
|
52
|
+
return false;
|
|
53
|
+
}
|
|
54
|
+
return true;
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
const getDate = date =>
|
|
58
|
+
new Date(
|
|
59
|
+
date.getFullYear(),
|
|
60
|
+
date.getMonth(),
|
|
61
|
+
date.getDate(),
|
|
62
|
+
date.getHours(),
|
|
63
|
+
date.getMinutes(),
|
|
64
|
+
date.getSeconds()
|
|
65
|
+
);
|
|
66
|
+
|
|
67
|
+
const today = () => moment();
|
|
68
|
+
|
|
69
|
+
module.exports = {
|
|
70
|
+
getTimeZones,
|
|
71
|
+
isBefore,
|
|
72
|
+
isAfter,
|
|
73
|
+
isDate,
|
|
74
|
+
getTimestamp,
|
|
75
|
+
diff,
|
|
76
|
+
toISOString,
|
|
77
|
+
duration,
|
|
78
|
+
toMoment,
|
|
79
|
+
isValid,
|
|
80
|
+
subtract,
|
|
81
|
+
add,
|
|
82
|
+
isEqual,
|
|
83
|
+
startOf,
|
|
84
|
+
endOf,
|
|
85
|
+
isBetween,
|
|
86
|
+
isToday,
|
|
87
|
+
getDate,
|
|
88
|
+
today,
|
|
89
|
+
};
|
package/lib/helpers.js
CHANGED
|
@@ -1,9 +1,28 @@
|
|
|
1
|
+
const _ = require('lodash');
|
|
1
2
|
const ObjectID = require('bson-objectid');
|
|
2
3
|
|
|
3
|
-
//
|
|
4
|
-
const
|
|
4
|
+
// IDs HELPERS
|
|
5
|
+
const toObjectID = value => ObjectID(value);
|
|
6
|
+
const getObjectID = () => ObjectID().toString();
|
|
7
|
+
const hasProperty = (entity, key) =>
|
|
8
|
+
Object.prototype.hasOwnProperty.call(entity, key);
|
|
9
|
+
const isObjectId = value => {
|
|
10
|
+
try {
|
|
11
|
+
if (!ObjectID.isValid(toObjectID(value))) return false;
|
|
12
|
+
return String(value) === String(toObjectID(value));
|
|
13
|
+
} catch (err) {
|
|
14
|
+
return false;
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
const hasObjectIds = (arr = []) => {
|
|
18
|
+
let noObjectId = true;
|
|
19
|
+
arr.forEach(value => {
|
|
20
|
+
if (!isObjectId(value)) noObjectId = false;
|
|
21
|
+
});
|
|
22
|
+
return noObjectId;
|
|
23
|
+
};
|
|
5
24
|
|
|
6
|
-
//
|
|
25
|
+
// ARRAY HELPERS
|
|
7
26
|
const groupBy = (items, key) =>
|
|
8
27
|
items.reduce(
|
|
9
28
|
(result, item) => ({
|
|
@@ -12,12 +31,74 @@ const groupBy = (items, key) =>
|
|
|
12
31
|
}),
|
|
13
32
|
{}
|
|
14
33
|
);
|
|
34
|
+
const uniqueArr = (values = []) => Array.from(new Set(values));
|
|
35
|
+
const removeDuplicates = (array, key) => {
|
|
36
|
+
const lookup = new Set();
|
|
37
|
+
return array.filter(obj => !lookup.has(obj[key]) && lookup.add(obj[key]));
|
|
38
|
+
};
|
|
39
|
+
const flatArray = arr => (Array.isArray(arr) ? [].concat(...arr) : []);
|
|
40
|
+
const arrToObj = (arr, keys = []) => {
|
|
41
|
+
const result = {};
|
|
42
|
+
keys.forEach((key, i) => {
|
|
43
|
+
if (typeof arr[i] !== 'undefined') result[key] = arr[i];
|
|
44
|
+
});
|
|
45
|
+
return result;
|
|
46
|
+
};
|
|
47
|
+
const mergeArrays = (a, b, p) =>
|
|
48
|
+
a.filter(aa => !b.find(bb => aa[p] === bb[p])).concat(b);
|
|
49
|
+
const isArrayEqual = (x, y) =>
|
|
50
|
+
!!x && !!y && _(x).differenceWith(y, _.isEqual).isEmpty();
|
|
15
51
|
|
|
16
|
-
//
|
|
17
|
-
const
|
|
52
|
+
// Object HELPERS
|
|
53
|
+
const clearEmptyValues = (obj = {}) => {
|
|
54
|
+
const out = {};
|
|
55
|
+
Object.keys(obj).forEach(key => {
|
|
56
|
+
let value = obj[key];
|
|
57
|
+
|
|
58
|
+
if (typeof value === 'object' && !(value == null || value === {}))
|
|
59
|
+
value = clearEmptyValues(value);
|
|
60
|
+
if (
|
|
61
|
+
typeof value === 'boolean' ||
|
|
62
|
+
typeof value === 'number' ||
|
|
63
|
+
!_.isEmpty(value)
|
|
64
|
+
) {
|
|
65
|
+
out[key] = value;
|
|
66
|
+
}
|
|
67
|
+
});
|
|
18
68
|
|
|
69
|
+
return out;
|
|
70
|
+
};
|
|
71
|
+
const checkKeys = (json, keys = []) => {
|
|
72
|
+
keys.forEach(key => {
|
|
73
|
+
if (!_.hasIn(json, key) || _.isEmpty(json[key])) {
|
|
74
|
+
throw new Error(`Required fields missing: ${keys}`);
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
return true;
|
|
79
|
+
};
|
|
80
|
+
const checkKey = (json, key) => {
|
|
81
|
+
if (!_.hasIn(json, key) || _.isEmpty(json[key])) {
|
|
82
|
+
return false;
|
|
83
|
+
}
|
|
84
|
+
return true;
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
//
|
|
19
88
|
module.exports = {
|
|
20
|
-
objId,
|
|
21
|
-
groupBy,
|
|
22
89
|
hasProperty,
|
|
90
|
+
checkKeys,
|
|
91
|
+
checkKey,
|
|
92
|
+
isObjectId,
|
|
93
|
+
toObjectID,
|
|
94
|
+
hasObjectIds,
|
|
95
|
+
groupBy,
|
|
96
|
+
uniqueArr,
|
|
97
|
+
removeDuplicates,
|
|
98
|
+
getObjectID,
|
|
99
|
+
flatArray,
|
|
100
|
+
arrToObj,
|
|
101
|
+
mergeArrays,
|
|
102
|
+
isArrayEqual,
|
|
103
|
+
clearEmptyValues,
|
|
23
104
|
};
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
const utils = require('../localization');
|
|
2
|
+
|
|
3
|
+
module.exports = (settings = {}) => {
|
|
4
|
+
const locale = settings.locale || 'en-US';
|
|
5
|
+
const currency = settings.currency || 'USD';
|
|
6
|
+
const defaultPhoneFormat = settings.phoneFormat || 'E.164';
|
|
7
|
+
const timezone = settings.timezone || '';
|
|
8
|
+
const countryCode =
|
|
9
|
+
(settings.defaultCountry && settings.defaultCountry.value) || 'US';
|
|
10
|
+
const { dateFormat } = settings;
|
|
11
|
+
const { hour12 } = settings;
|
|
12
|
+
return {
|
|
13
|
+
options: {
|
|
14
|
+
locale,
|
|
15
|
+
currency,
|
|
16
|
+
defaultPhoneFormat,
|
|
17
|
+
timezone,
|
|
18
|
+
countryCode,
|
|
19
|
+
dateFormat,
|
|
20
|
+
hour12,
|
|
21
|
+
},
|
|
22
|
+
formatAmount: amount =>
|
|
23
|
+
utils.formatAmountWithCurrency(amount, currency, locale),
|
|
24
|
+
formatDate: (date, options = 'simple-date', ignoreTimezone = false) =>
|
|
25
|
+
utils.formatDateWithLocale({
|
|
26
|
+
date,
|
|
27
|
+
options,
|
|
28
|
+
locale,
|
|
29
|
+
timezone,
|
|
30
|
+
ignoreTimezone,
|
|
31
|
+
dateFormat,
|
|
32
|
+
hour12,
|
|
33
|
+
}),
|
|
34
|
+
formatNumber: number => utils.formatNumberWithLocale(number, locale),
|
|
35
|
+
dateFormatPattern: () => utils.dateFormatPattern(locale),
|
|
36
|
+
monthAndDateFormatPattern: () => utils.monthAndDateFormatPattern(locale),
|
|
37
|
+
formatPhone: (phoneNumber, format) =>
|
|
38
|
+
utils.formatPhone(phoneNumber, format || defaultPhoneFormat, countryCode),
|
|
39
|
+
formatRelativeDate: date =>
|
|
40
|
+
utils.formatRelativeDateWithLocale(
|
|
41
|
+
date,
|
|
42
|
+
locale,
|
|
43
|
+
timezone,
|
|
44
|
+
dateFormat,
|
|
45
|
+
hour12
|
|
46
|
+
),
|
|
47
|
+
getFormat: monthAndDateFormat => {
|
|
48
|
+
const date = monthAndDateFormat
|
|
49
|
+
? utils.monthAndDateFormatPattern(locale)
|
|
50
|
+
: utils.dateFormatPattern(locale);
|
|
51
|
+
let format = `${date}, HH:mm`;
|
|
52
|
+
if (hour12 === 'true') format += ' A';
|
|
53
|
+
return format;
|
|
54
|
+
},
|
|
55
|
+
};
|
|
56
|
+
};
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
const { parsePhoneNumberFromString } = require('libphonenumber-js');
|
|
2
|
-
const dateUtil = require('
|
|
2
|
+
const dateUtil = require('./date');
|
|
3
|
+
const math = require('./math');
|
|
3
4
|
|
|
4
5
|
const formats = {
|
|
5
6
|
'en-US': 'MM/DD/YYYY',
|
|
@@ -13,7 +14,13 @@ const monthAndDateFormats = {
|
|
|
13
14
|
'en-GB': 'DD/MM',
|
|
14
15
|
};
|
|
15
16
|
|
|
16
|
-
const formatDuration = (
|
|
17
|
+
const formatDuration = (
|
|
18
|
+
date,
|
|
19
|
+
options,
|
|
20
|
+
locale,
|
|
21
|
+
timezone,
|
|
22
|
+
ignoreTimezone = false
|
|
23
|
+
) => {
|
|
17
24
|
const fromDate =
|
|
18
25
|
date && date.from && typeof date.from !== 'function'
|
|
19
26
|
? dateUtil.toMoment(date.from)
|
|
@@ -30,8 +37,14 @@ const formatDuration = (date, options, locale, timezone, ignoreTimezone = false)
|
|
|
30
37
|
};
|
|
31
38
|
if (timezone && !ignoreTimezone) newOptions.timeZone = timezone;
|
|
32
39
|
try {
|
|
33
|
-
return `${
|
|
34
|
-
duration.get('
|
|
40
|
+
return `${
|
|
41
|
+
duration.get('hours') < 10
|
|
42
|
+
? `0${duration.get('hours')}`
|
|
43
|
+
: duration.get('hours')
|
|
44
|
+
}:${
|
|
45
|
+
duration.get('minutes') < 10
|
|
46
|
+
? `0${duration.get('minutes')}`
|
|
47
|
+
: duration.get('minutes')
|
|
35
48
|
}`;
|
|
36
49
|
} catch (e) {
|
|
37
50
|
console.error(e.message);
|
|
@@ -77,30 +90,19 @@ const getDateFormat = dateFormat => {
|
|
|
77
90
|
};
|
|
78
91
|
};
|
|
79
92
|
|
|
80
|
-
const getTimeFormat = hour12 => {
|
|
81
|
-
|
|
93
|
+
const getTimeFormat = (hour12, seconds) => {
|
|
94
|
+
const options = {
|
|
82
95
|
hour: '2-digit',
|
|
83
96
|
minute: '2-digit',
|
|
84
97
|
hour12: hour12 === 'true',
|
|
85
98
|
};
|
|
99
|
+
if (seconds)
|
|
100
|
+
return {
|
|
101
|
+
...options,
|
|
102
|
+
second: '2-digit',
|
|
103
|
+
};
|
|
104
|
+
return options;
|
|
86
105
|
};
|
|
87
|
-
|
|
88
|
-
/**
|
|
89
|
-
{
|
|
90
|
-
date | { from: ?string, to: ?string } | Date | void,
|
|
91
|
-
options:
|
|
92
|
-
| 'date-time'
|
|
93
|
-
| 'simple-date'
|
|
94
|
-
| 'simple-time'
|
|
95
|
-
| 'duration-time'
|
|
96
|
-
| OptionType,
|
|
97
|
-
locale,
|
|
98
|
-
timezone,
|
|
99
|
-
dateFormat,
|
|
100
|
-
hour12,
|
|
101
|
-
ignoreTimezone,
|
|
102
|
-
};
|
|
103
|
-
* */
|
|
104
106
|
const formatDateWithLocale = ({
|
|
105
107
|
date,
|
|
106
108
|
options,
|
|
@@ -116,7 +118,7 @@ const formatDateWithLocale = ({
|
|
|
116
118
|
case 'date-time':
|
|
117
119
|
newOptions = {
|
|
118
120
|
...getDateFormat(dateFormat),
|
|
119
|
-
...getTimeFormat(hour12),
|
|
121
|
+
...getTimeFormat(hour12, true),
|
|
120
122
|
};
|
|
121
123
|
break;
|
|
122
124
|
case 'simple-date':
|
|
@@ -127,7 +129,7 @@ const formatDateWithLocale = ({
|
|
|
127
129
|
case 'simple-time':
|
|
128
130
|
case 'duration-time':
|
|
129
131
|
newOptions = {
|
|
130
|
-
...getTimeFormat(hour12),
|
|
132
|
+
...getTimeFormat(hour12, false),
|
|
131
133
|
};
|
|
132
134
|
break;
|
|
133
135
|
default:
|
|
@@ -142,7 +144,9 @@ const formatDateWithLocale = ({
|
|
|
142
144
|
}
|
|
143
145
|
newOptions = newOptions || getDateFormat(dateFormat);
|
|
144
146
|
try {
|
|
145
|
-
return new Intl.DateTimeFormat(locale, newOptions).format(
|
|
147
|
+
return new Intl.DateTimeFormat(locale, newOptions).format(
|
|
148
|
+
dateUtil.toMoment(date).toDate()
|
|
149
|
+
);
|
|
146
150
|
} catch (e) {
|
|
147
151
|
console.error(e.message);
|
|
148
152
|
return '';
|
|
@@ -159,35 +163,28 @@ const formatPhone = (phone, format, countryCode) => {
|
|
|
159
163
|
return '';
|
|
160
164
|
};
|
|
161
165
|
|
|
162
|
-
const formatTextWithLocale = (wordId, params, lang, words) => {
|
|
163
|
-
if (words[lang][wordId]) {
|
|
164
|
-
let result = words[lang][wordId];
|
|
165
|
-
if (params && Array.isArray(params) && params.length > 0) {
|
|
166
|
-
result = result.replace(/{(\d+)}/g, (match, number) =>
|
|
167
|
-
Array.isArray(params) && typeof params[number] !== 'undefined' ? params[number] : match
|
|
168
|
-
);
|
|
169
|
-
}
|
|
170
|
-
return result;
|
|
171
|
-
}
|
|
172
|
-
// console.error(`There is no text for id: ${wordId} in language: ${lang}`);
|
|
173
|
-
return wordId;
|
|
174
|
-
};
|
|
175
|
-
|
|
176
166
|
const dateFormatPattern = lang => formats[lang];
|
|
177
167
|
|
|
178
168
|
const monthAndDateFormatPattern = lang => monthAndDateFormats[lang];
|
|
179
169
|
|
|
180
170
|
const formatAmountWithCurrency = (amount, currency, locale) =>
|
|
181
|
-
typeof amount === 'number'
|
|
171
|
+
typeof amount === 'number' || math.isNumber(amount)
|
|
182
172
|
? new Intl.NumberFormat(locale, {
|
|
183
173
|
style: 'currency',
|
|
184
174
|
currency,
|
|
185
175
|
}).format(Number(amount))
|
|
186
176
|
: '';
|
|
187
177
|
|
|
188
|
-
const formatNumberWithLocale = (number, locale) =>
|
|
178
|
+
const formatNumberWithLocale = (number, locale) =>
|
|
179
|
+
new Intl.NumberFormat(locale).format(number);
|
|
189
180
|
|
|
190
|
-
const formatRelativeDateWithLocale = (
|
|
181
|
+
const formatRelativeDateWithLocale = (
|
|
182
|
+
date,
|
|
183
|
+
locale,
|
|
184
|
+
timezone,
|
|
185
|
+
dateFormat,
|
|
186
|
+
hour12
|
|
187
|
+
) => {
|
|
191
188
|
const now = dateUtil.toMoment();
|
|
192
189
|
if (!dateUtil.isBefore(date, undefined, 'day')) {
|
|
193
190
|
return formatDateWithLocale({
|
|
@@ -200,8 +197,8 @@ const formatRelativeDateWithLocale = (date, locale, timezone, dateFormat, hour12
|
|
|
200
197
|
});
|
|
201
198
|
}
|
|
202
199
|
if (!dateUtil.isBefore(date, undefined, 'week')) {
|
|
203
|
-
return now.
|
|
204
|
-
?
|
|
200
|
+
return now.subtract(1, 'day').day() === dateUtil.toMoment(date).day()
|
|
201
|
+
? 'Yesterday'
|
|
205
202
|
: formatDateWithLocale({
|
|
206
203
|
date,
|
|
207
204
|
options: { weekday: 'long' },
|
|
@@ -232,7 +229,6 @@ const formatRelativeDateWithLocale = (date, locale, timezone, dateFormat, hour12
|
|
|
232
229
|
};
|
|
233
230
|
|
|
234
231
|
module.exports = {
|
|
235
|
-
formatTextWithLocale,
|
|
236
232
|
formatAmountWithCurrency,
|
|
237
233
|
formatDateWithLocale,
|
|
238
234
|
formatNumberWithLocale,
|
package/lib/math.js
CHANGED
|
@@ -1,46 +1,61 @@
|
|
|
1
1
|
const Decimal = require('decimal.js');
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
const getDefaultNumbers = numbers => {
|
|
4
|
+
if (!Array.isArray(numbers)) return [];
|
|
5
|
+
return numbers.map(num => num || 0);
|
|
6
|
+
};
|
|
7
|
+
|
|
4
8
|
const add = (...args) => {
|
|
5
|
-
if (!args || !Array.isArray(args) || !args.length)
|
|
9
|
+
if (!args || !Array.isArray(args) || !args.length)
|
|
10
|
+
throw new Error('Invalid argument');
|
|
6
11
|
|
|
7
|
-
const
|
|
12
|
+
const numbers = getDefaultNumbers(args);
|
|
13
|
+
const sum = numbers.reduce(
|
|
14
|
+
(acc, current) => acc.add(Decimal(current)),
|
|
15
|
+
Decimal(0)
|
|
16
|
+
);
|
|
8
17
|
|
|
9
|
-
// console.log('add =>', args, ' : ', sum);
|
|
10
18
|
return Decimal(sum.toFixed(2, 1)).toNumber();
|
|
11
19
|
};
|
|
12
20
|
|
|
13
21
|
//
|
|
14
22
|
const sub = (...args) => {
|
|
15
|
-
if (!args || !Array.isArray(args) || !args.length)
|
|
23
|
+
if (!args || !Array.isArray(args) || !args.length)
|
|
24
|
+
throw new Error('Invalid argument');
|
|
16
25
|
|
|
17
|
-
const numbers =
|
|
26
|
+
const numbers = getDefaultNumbers(args);
|
|
18
27
|
const first = numbers.shift();
|
|
19
|
-
const sum = numbers.reduce(
|
|
28
|
+
const sum = numbers.reduce(
|
|
29
|
+
(acc, current) => acc.sub(Decimal(current)),
|
|
30
|
+
Decimal(first)
|
|
31
|
+
);
|
|
20
32
|
|
|
21
|
-
// console.log('sub =>', args, ' : ', sum);
|
|
22
33
|
return Decimal(sum.toFixed(2, 1)).toNumber();
|
|
23
34
|
};
|
|
24
35
|
|
|
25
|
-
//
|
|
26
36
|
const mul = (...args) => {
|
|
27
|
-
if (!args || !Array.isArray(args) || !args.length)
|
|
37
|
+
if (!args || !Array.isArray(args) || !args.length)
|
|
38
|
+
throw new Error('Invalid argument');
|
|
28
39
|
|
|
29
|
-
const sum = args.reduce(
|
|
40
|
+
const sum = args.reduce(
|
|
41
|
+
(acc, current) => acc.mul(Decimal(current)),
|
|
42
|
+
Decimal(1)
|
|
43
|
+
);
|
|
30
44
|
|
|
31
|
-
// console.log('mul =>', args, ' : ', sum);
|
|
32
45
|
return Decimal(sum.toFixed(2, 1)).toNumber();
|
|
33
46
|
};
|
|
34
47
|
|
|
35
|
-
//
|
|
36
48
|
const div = (...args) => {
|
|
37
|
-
if (!args || !Array.isArray(args) || !args.length)
|
|
49
|
+
if (!args || !Array.isArray(args) || !args.length)
|
|
50
|
+
throw new Error('Invalid argument');
|
|
38
51
|
|
|
39
52
|
const numbers = [...args];
|
|
40
53
|
const first = numbers.shift();
|
|
41
|
-
const sum = numbers.reduce(
|
|
54
|
+
const sum = numbers.reduce(
|
|
55
|
+
(acc, current) => acc.div(Decimal(current)),
|
|
56
|
+
Decimal(first)
|
|
57
|
+
);
|
|
42
58
|
|
|
43
|
-
// console.log('div =>', args, ' : ', sum);
|
|
44
59
|
return Decimal(sum.toFixed(2, 1)).toNumber();
|
|
45
60
|
};
|
|
46
61
|
|
|
@@ -57,10 +72,13 @@ const gt = (x, y) => Decimal(x).gt(Decimal(y));
|
|
|
57
72
|
const gte = (x, y) => Decimal(x).gte(Decimal(y));
|
|
58
73
|
|
|
59
74
|
//
|
|
60
|
-
const
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
75
|
+
const eq = (x, y) => Decimal(x).eq(Decimal(y));
|
|
76
|
+
|
|
77
|
+
//
|
|
78
|
+
const isZero = x => Decimal(x).eq(0);
|
|
79
|
+
|
|
80
|
+
//
|
|
81
|
+
const abs = x => Decimal(x).abs().toNumber();
|
|
64
82
|
|
|
65
83
|
//
|
|
66
84
|
const toFixed = x => Decimal(x).toFixed(2, 1);
|
|
@@ -75,5 +93,7 @@ module.exports = {
|
|
|
75
93
|
lte,
|
|
76
94
|
gt,
|
|
77
95
|
gte,
|
|
96
|
+
eq,
|
|
97
|
+
isZero,
|
|
78
98
|
toFixed,
|
|
79
99
|
};
|
package/lib/system.js
CHANGED
|
@@ -1,27 +1,11 @@
|
|
|
1
1
|
const os = require('os');
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
const interval = process.env.DRK_LOGGER_INTERVAL || 60 * 60;
|
|
4
4
|
const freeMemory = () => Math.round(os.freemem() / (1024 * 1024));
|
|
5
5
|
const totalMemory = () => Math.round(os.totalmem() / (1024 * 1024));
|
|
6
|
+
const cache = {};
|
|
6
7
|
|
|
7
|
-
|
|
8
|
-
const interval = process.env.DRK_LOGGER_INTERVAL || 60;
|
|
9
|
-
let preCPUs = [];
|
|
10
|
-
|
|
11
|
-
const systemInfo = {
|
|
12
|
-
hostname: os.hostname(),
|
|
13
|
-
uptime: os.uptime(),
|
|
14
|
-
os_arch: os.arch(),
|
|
15
|
-
os_platform: os.platform(),
|
|
16
|
-
os_release: os.release(),
|
|
17
|
-
memory_total: totalMemory(),
|
|
18
|
-
memory_free: freeMemory(),
|
|
19
|
-
cpu_count: os.cpus().length,
|
|
20
|
-
cpu_free: getCPUFree(),
|
|
21
|
-
node: process.versions && process.versions.node
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
function getCPUInfo() {
|
|
8
|
+
const cpuInfo = () => {
|
|
25
9
|
const cpus = os.cpus();
|
|
26
10
|
let user = 0;
|
|
27
11
|
let nice = 0;
|
|
@@ -29,8 +13,10 @@ function getCPUInfo() {
|
|
|
29
13
|
let idle = 0;
|
|
30
14
|
let irq = 0;
|
|
31
15
|
let total = 0;
|
|
16
|
+
// eslint-disable-next-line no-restricted-syntax
|
|
32
17
|
for (const cpu in cpus) {
|
|
33
|
-
|
|
18
|
+
// eslint-disable-next-line no-continue
|
|
19
|
+
if (!(cpu in cpus)) continue;
|
|
34
20
|
user += cpus[cpu].times.user;
|
|
35
21
|
nice += cpus[cpu].times.nice;
|
|
36
22
|
sys += cpus[cpu].times.sys;
|
|
@@ -42,27 +28,43 @@ function getCPUInfo() {
|
|
|
42
28
|
idle,
|
|
43
29
|
total,
|
|
44
30
|
};
|
|
45
|
-
}
|
|
31
|
+
};
|
|
46
32
|
|
|
47
|
-
|
|
48
|
-
const currCPUs =
|
|
49
|
-
const idle = (currCPUs.idle - preCPUs.idle) / interval;
|
|
50
|
-
const total = (currCPUs.total - preCPUs.total) / interval;
|
|
33
|
+
const cpuFree = () => {
|
|
34
|
+
const currCPUs = cpuInfo();
|
|
35
|
+
const idle = (currCPUs.idle - cache.preCPUs.idle) / interval;
|
|
36
|
+
const total = (currCPUs.total - cache.preCPUs.total) / interval;
|
|
51
37
|
const free = Math.round((idle / total) * 100);
|
|
52
|
-
preCPUs = currCPUs;
|
|
38
|
+
cache.preCPUs = currCPUs;
|
|
53
39
|
return free;
|
|
54
|
-
}
|
|
40
|
+
};
|
|
55
41
|
|
|
56
|
-
const getSystemInfo = () => systemInfo;
|
|
42
|
+
const getSystemInfo = () => cache.systemInfo;
|
|
57
43
|
|
|
58
|
-
const
|
|
59
|
-
|
|
44
|
+
const systemInfo = () => ({
|
|
45
|
+
hostname: os.hostname(),
|
|
46
|
+
uptime: os.uptime(),
|
|
47
|
+
os_arch: os.arch(),
|
|
48
|
+
os_platform: os.platform(),
|
|
49
|
+
os_release: os.release(),
|
|
50
|
+
memory_total: totalMemory(),
|
|
51
|
+
memory_free: freeMemory(),
|
|
52
|
+
cpu_count: os.cpus().length,
|
|
53
|
+
cpu_free: cpuFree(),
|
|
54
|
+
node: process.versions && process.versions.node,
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
const invokeSystemInfo = fn => {
|
|
60
58
|
if (fn) {
|
|
61
|
-
fn('SERVICE_START', { ...getSystemInfo() })
|
|
62
|
-
}
|
|
59
|
+
fn('SERVICE_START', { ...getSystemInfo() });
|
|
60
|
+
}
|
|
63
61
|
setInterval(() => {
|
|
64
|
-
|
|
62
|
+
cache.systemInfo = systemInfo();
|
|
63
|
+
if (fn) fn('SERVICE_HEALTHCHECK', { ...getSystemInfo() });
|
|
65
64
|
}, interval * 1000);
|
|
66
65
|
};
|
|
67
66
|
|
|
67
|
+
cache.preCPUs = cpuInfo();
|
|
68
|
+
cache.systemInfo = systemInfo();
|
|
69
|
+
|
|
68
70
|
module.exports = { getSystemInfo, invokeSystemInfo };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@darkpos/utils",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.8",
|
|
4
4
|
"description": "General purpose utilities",
|
|
5
5
|
"author": "Dark POS",
|
|
6
6
|
"homepage": "https://gitlab.com/darkpos/packages/dark#readme",
|
|
@@ -23,22 +23,21 @@
|
|
|
23
23
|
"url": "https://gitlab.com/darkpos/packages/dark/issues"
|
|
24
24
|
},
|
|
25
25
|
"dependencies": {
|
|
26
|
-
"bson-objectid": "^
|
|
27
|
-
"decimal.js": "^10.
|
|
28
|
-
"libphonenumber-js": "^1.
|
|
29
|
-
"
|
|
26
|
+
"bson-objectid": "^2.0.3",
|
|
27
|
+
"decimal.js": "^10.3.1",
|
|
28
|
+
"libphonenumber-js": "^1.10.6",
|
|
29
|
+
"lodash": "^4.17.21",
|
|
30
|
+
"moment-timezone": "^0.5.34"
|
|
30
31
|
},
|
|
31
32
|
"devDependencies": {
|
|
32
|
-
"eslint": "^
|
|
33
|
-
"eslint-config-airbnb": "^
|
|
34
|
-
"eslint-config-airbnb-base": "^
|
|
35
|
-
"eslint-config-prettier": "^
|
|
36
|
-
"eslint-plugin-import": "^2.
|
|
37
|
-
"eslint-plugin-
|
|
38
|
-
"eslint-plugin-prettier": "^
|
|
39
|
-
"
|
|
40
|
-
"eslint-plugin-react-hooks": "^2.5.1",
|
|
41
|
-
"prettier": "^1.19.1"
|
|
33
|
+
"eslint": "^8.2.0",
|
|
34
|
+
"eslint-config-airbnb": "^19.0.4",
|
|
35
|
+
"eslint-config-airbnb-base": "^15.0.0",
|
|
36
|
+
"eslint-config-prettier": "^8.5.0",
|
|
37
|
+
"eslint-plugin-import": "^2.26.0",
|
|
38
|
+
"eslint-plugin-jest": "^26.5.3",
|
|
39
|
+
"eslint-plugin-prettier": "^4.0.0",
|
|
40
|
+
"prettier": "^2.7.0"
|
|
42
41
|
},
|
|
43
|
-
"gitHead": "
|
|
42
|
+
"gitHead": "1c4565d3c097c833093794654d8adc18e37767b9"
|
|
44
43
|
}
|
package/lib/date/index.js
DELETED
|
@@ -1,116 +0,0 @@
|
|
|
1
|
-
const moment = require('moment');
|
|
2
|
-
const supportedTimeZones = require('./supportedTimeZones.json');
|
|
3
|
-
|
|
4
|
-
const isBefore = (first, second, param) => moment(first).isBefore(moment(second), param);
|
|
5
|
-
|
|
6
|
-
const isEqual = (first, second, param) => moment(first).isSame(moment(second), param);
|
|
7
|
-
|
|
8
|
-
const isToday = value => {
|
|
9
|
-
const today = moment();
|
|
10
|
-
const date = moment(value);
|
|
11
|
-
|
|
12
|
-
return (
|
|
13
|
-
today.year() === date.year() &&
|
|
14
|
-
today.month() === date.month() &&
|
|
15
|
-
today.dayOfYear() === date.dayOfYear()
|
|
16
|
-
);
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
const diff = (from, to) => moment(from).diff(moment(to));
|
|
20
|
-
|
|
21
|
-
const duration = date => moment.duration(date);
|
|
22
|
-
|
|
23
|
-
const toISOString = date => moment(date).toISOString();
|
|
24
|
-
|
|
25
|
-
const getTimeZones = supportedTimeZones;
|
|
26
|
-
|
|
27
|
-
const toMoment = date => moment(date);
|
|
28
|
-
|
|
29
|
-
const isValid = date => moment(date).isValid();
|
|
30
|
-
|
|
31
|
-
const subtract = (date, value, of) => moment(date).subtract(value, of);
|
|
32
|
-
|
|
33
|
-
const add = (date, value, to) => moment(date).add(value, to);
|
|
34
|
-
|
|
35
|
-
const startOf = (value, param = 'day') => moment(value).startOf(param);
|
|
36
|
-
|
|
37
|
-
const endOf = (value, param = 'day') => moment(value).endOf(param);
|
|
38
|
-
|
|
39
|
-
const isBetween = ({ dateToCompare, from, to }) => {
|
|
40
|
-
if (from && (!isValid(from) || isBefore(dateToCompare, from, 'day'))) {
|
|
41
|
-
return false;
|
|
42
|
-
}
|
|
43
|
-
if (to && (!isValid(to) || isBefore(to, dateToCompare, 'day'))) {
|
|
44
|
-
return false;
|
|
45
|
-
}
|
|
46
|
-
return true;
|
|
47
|
-
};
|
|
48
|
-
|
|
49
|
-
const stringValues = {
|
|
50
|
-
normal: {
|
|
51
|
-
day: ' Day',
|
|
52
|
-
days: ' Days',
|
|
53
|
-
hour: ' Hr',
|
|
54
|
-
hours: ' Hrs',
|
|
55
|
-
minute: ' Min',
|
|
56
|
-
minutes: ' Mins',
|
|
57
|
-
},
|
|
58
|
-
short: {
|
|
59
|
-
day: 'D',
|
|
60
|
-
days: 'D',
|
|
61
|
-
hour: 'H',
|
|
62
|
-
hours: 'H',
|
|
63
|
-
minute: 'M',
|
|
64
|
-
minutes: 'M',
|
|
65
|
-
},
|
|
66
|
-
};
|
|
67
|
-
|
|
68
|
-
const getElapsedTime = (from, to, offset = 0, stringType = 'normal') => {
|
|
69
|
-
const time = duration(diff(from, to) + offset);
|
|
70
|
-
let timer = '';
|
|
71
|
-
const strings = stringValues[stringType];
|
|
72
|
-
if (time) {
|
|
73
|
-
if (time.days() > 0) {
|
|
74
|
-
timer += `${time.days()}${time.days() === 1 ? strings.day : strings.days}`;
|
|
75
|
-
}
|
|
76
|
-
if (time.hours() > 0) {
|
|
77
|
-
if (time.days() > 0) timer += ',';
|
|
78
|
-
if (timer.length > 0) timer += ' ';
|
|
79
|
-
timer += `${time.hours()}${time.hours() === 1 ? strings.hour : strings.hours}`;
|
|
80
|
-
}
|
|
81
|
-
if (time.minutes() >= 0) {
|
|
82
|
-
if (timer.length > 0) timer += ' ';
|
|
83
|
-
timer += `${time.minutes()}${time.minutes() <= 1 ? strings.minute : strings.minutes}`;
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
return timer;
|
|
87
|
-
};
|
|
88
|
-
|
|
89
|
-
const combineDateAndTime = (date, time) =>
|
|
90
|
-
new Date(
|
|
91
|
-
date.getFullYear(),
|
|
92
|
-
date.getMonth(),
|
|
93
|
-
date.getDate(),
|
|
94
|
-
time.getHours(),
|
|
95
|
-
time.getMinutes(),
|
|
96
|
-
time.getSeconds()
|
|
97
|
-
);
|
|
98
|
-
|
|
99
|
-
module.exports = {
|
|
100
|
-
isBefore,
|
|
101
|
-
diff,
|
|
102
|
-
toISOString,
|
|
103
|
-
getTimeZones,
|
|
104
|
-
duration,
|
|
105
|
-
toMoment,
|
|
106
|
-
isValid,
|
|
107
|
-
subtract,
|
|
108
|
-
add,
|
|
109
|
-
isEqual,
|
|
110
|
-
startOf,
|
|
111
|
-
endOf,
|
|
112
|
-
getElapsedTime,
|
|
113
|
-
combineDateAndTime,
|
|
114
|
-
isBetween,
|
|
115
|
-
isToday,
|
|
116
|
-
};
|
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
const utils = require('./helpers');
|
|
2
|
-
|
|
3
|
-
const defaults = {
|
|
4
|
-
locale: 'en-US',
|
|
5
|
-
currency: 'USD',
|
|
6
|
-
defaultPhoneFormat: 'E.164',
|
|
7
|
-
timezone: 'America/Newyork',
|
|
8
|
-
countryCode: 'US',
|
|
9
|
-
dateFormat: '',
|
|
10
|
-
hour12: true,
|
|
11
|
-
words: {
|
|
12
|
-
'en-US': {},
|
|
13
|
-
'es-ES': {},
|
|
14
|
-
},
|
|
15
|
-
};
|
|
16
|
-
|
|
17
|
-
module.exports = (localization = {}) => {
|
|
18
|
-
const {
|
|
19
|
-
locale,
|
|
20
|
-
currency,
|
|
21
|
-
defaultPhoneFormat,
|
|
22
|
-
timezone,
|
|
23
|
-
countryCode,
|
|
24
|
-
dateFormat,
|
|
25
|
-
hour12,
|
|
26
|
-
words,
|
|
27
|
-
} = { ...defaults, ...localization };
|
|
28
|
-
return {
|
|
29
|
-
formatText: (wordId, ...params) => utils.formatTextWithLocale(wordId, params, locale, words),
|
|
30
|
-
formatAmount: amount => utils.formatAmountWithCurrency(amount, currency, locale),
|
|
31
|
-
formatDate: (date, options = 'simple-date', ignoreTimezone = false) =>
|
|
32
|
-
utils.formatDateWithLocale({
|
|
33
|
-
date,
|
|
34
|
-
options,
|
|
35
|
-
locale,
|
|
36
|
-
timezone,
|
|
37
|
-
ignoreTimezone,
|
|
38
|
-
dateFormat,
|
|
39
|
-
hour12,
|
|
40
|
-
}),
|
|
41
|
-
formatNumber: number => utils.formatNumberWithLocale(number, locale),
|
|
42
|
-
dateFormatPattern: () => utils.dateFormatPattern(locale),
|
|
43
|
-
monthAndDateFormatPattern: () => utils.monthAndDateFormatPattern(locale),
|
|
44
|
-
formatPhone: (phoneNumber, format) =>
|
|
45
|
-
utils.formatPhone(phoneNumber, format || defaultPhoneFormat, countryCode),
|
|
46
|
-
formatRelativeDate: date =>
|
|
47
|
-
utils.formatRelativeDateWithLocale(date, locale, timezone, dateFormat, hour12),
|
|
48
|
-
};
|
|
49
|
-
};
|