@btffamily/vacepey 1.0.1
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/build/brokers/nats/listener.ev.d.ts +29 -0
- package/build/brokers/nats/listener.ev.js +44 -0
- package/build/brokers/nats/publisher.ev.d.ts +19 -0
- package/build/brokers/nats/publisher.ev.js +28 -0
- package/build/brokers/nats/subjects.ev.d.ts +84 -0
- package/build/brokers/nats/subjects.ev.js +91 -0
- package/build/brokers/rabbitMQ/consumer.d.ts +35 -0
- package/build/brokers/rabbitMQ/consumer.js +74 -0
- package/build/brokers/rabbitMQ/exchanges.d.ts +36 -0
- package/build/brokers/rabbitMQ/exchanges.js +43 -0
- package/build/brokers/rabbitMQ/producer.d.ts +17 -0
- package/build/brokers/rabbitMQ/producer.js +40 -0
- package/build/dtos/word.dto.d.ts +11 -0
- package/build/dtos/word.dto.js +2 -0
- package/build/events/listener.ev.d.ts +29 -0
- package/build/events/listener.ev.js +44 -0
- package/build/events/publisher.ev.d.ts +19 -0
- package/build/events/publisher.ev.js +28 -0
- package/build/events/subjects.ev.d.ts +84 -0
- package/build/events/subjects.ev.js +91 -0
- package/build/index.d.ts +14 -0
- package/build/index.js +109 -0
- package/build/middleware/async.mw.d.ts +3 -0
- package/build/middleware/async.mw.js +4 -0
- package/build/middleware/auth.mw.d.ts +24 -0
- package/build/middleware/auth.mw.js +98 -0
- package/build/middleware/db.mw.d.ts +6 -0
- package/build/middleware/db.mw.js +43 -0
- package/build/middleware/redis.mw.d.ts +19 -0
- package/build/middleware/redis.mw.js +28 -0
- package/build/middleware/role.mw.d.ts +4 -0
- package/build/middleware/role.mw.js +19 -0
- package/build/services/mongo.service.d.ts +7 -0
- package/build/services/mongo.service.js +14 -0
- package/build/services/random.service.d.ts +8 -0
- package/build/services/random.service.js +30 -0
- package/build/services/word.service.d.ts +8 -0
- package/build/services/word.service.js +73 -0
- package/build/src/ca-certificate.crt +32 -0
- package/build/src/index.ts +192 -0
- package/build/utils/cache.util.d.ts +3 -0
- package/build/utils/cache.util.js +7 -0
- package/build/utils/encryption.util.d.ts +66 -0
- package/build/utils/encryption.util.js +208 -0
- package/build/utils/functions.util.d.ts +133 -0
- package/build/utils/functions.util.js +1032 -0
- package/build/utils/snippets/decryption.snippet.d.ts +0 -0
- package/build/utils/snippets/decryption.snippet.js +255 -0
- package/build/utils/types.util.d.ts +58 -0
- package/build/utils/types.util.js +2 -0
- package/package.json +49 -0
|
@@ -0,0 +1,1032 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.toDecimal = exports.hasDecimal = exports.isPrecise = exports.isDefined = exports.notDefined = exports.generate = exports.daysFromDates = exports.yesterdayFromDate = exports.dateIsFuture = exports.dateIsPast = exports.dateIsEqual = exports.dateIsYesterday = exports.dateIsToday = exports.checkTimeFormat = exports.checkDateFormat = exports.isZero = exports.isNumber = exports.isNeg = exports.isPos = exports.isObjectId = exports.rearrangeArray = exports.sortData = exports.dateToday = exports.formatDate = exports.monthsOfYear = exports.daysOfWeek = exports.leadingNum = exports.urlToBase64 = exports.convertUrlToBase64 = exports.capitalize = exports.getBase64Mime = exports.isBase64 = exports.isEmptyObject = exports.dateToWordRaw = exports.dateToWord = exports.rangeFromMinutes = exports.timeToMinutes = exports.listIncludes = exports.arrayIncludes = exports.arrayIncludesEs6 = exports.strIncludesEs6 = exports.strIncludes = exports.strToArrayEs6 = exports.strToArray = exports.isArray = exports.isString = exports.isObject = exports.wordLen = exports.charLen = exports.slugit = void 0;
|
|
7
|
+
exports.toMongoId = exports.weekEndDate = exports.weekStartDate = exports.dateFromWeekNumber = exports.base64ToString = exports.stringToBase64 = exports.arrayToSingleObject = exports.arrayToObject = exports.isJson = exports.objectToArray = exports.enumToArray = exports.hasSAC = exports.formatISO = exports.lastDayOfMonth = exports.firstDayOfMonth = exports.getDaysFromDates = exports.getCodeFromName = exports.validateBase64 = exports.getWeeksInMonth = exports.getDatesInRange = exports.getWeeksDates = exports.compareISODate = exports.UIID = void 0;
|
|
8
|
+
const crypto_1 = __importDefault(require("crypto"));
|
|
9
|
+
const mongoose_1 = __importDefault(require("mongoose"));
|
|
10
|
+
const slugify_1 = __importDefault(require("slugify"));
|
|
11
|
+
const dayjs_1 = __importDefault(require("dayjs"));
|
|
12
|
+
const customParseFormat_1 = __importDefault(require("dayjs/plugin/customParseFormat"));
|
|
13
|
+
const weekOfYear_1 = __importDefault(require("dayjs/plugin/weekOfYear"));
|
|
14
|
+
const weekYear_1 = __importDefault(require("dayjs/plugin/weekYear"));
|
|
15
|
+
dayjs_1.default.extend(customParseFormat_1.default);
|
|
16
|
+
dayjs_1.default.extend(weekOfYear_1.default);
|
|
17
|
+
dayjs_1.default.extend(weekYear_1.default);
|
|
18
|
+
// import https
|
|
19
|
+
const https_1 = __importDefault(require("https"));
|
|
20
|
+
const random_service_1 = __importDefault(require("../services/random.service"));
|
|
21
|
+
const moment_1 = __importDefault(require("moment"));
|
|
22
|
+
const mongo_service_1 = __importDefault(require("../services/mongo.service"));
|
|
23
|
+
const axios_1 = __importDefault(require("axios"));
|
|
24
|
+
const slugit = (payload) => {
|
|
25
|
+
let _op = { lowercase: true, separator: '-' };
|
|
26
|
+
const { data, options } = payload;
|
|
27
|
+
if (options && (0, exports.isDefined)(options.lowercase, true)) {
|
|
28
|
+
_op.lowercase = options.lowercase;
|
|
29
|
+
}
|
|
30
|
+
if (options && options.separator) {
|
|
31
|
+
_op.separator = options.separator;
|
|
32
|
+
}
|
|
33
|
+
const result = (0, slugify_1.default)(data, { lower: _op.lowercase, replacement: _op.separator });
|
|
34
|
+
return result;
|
|
35
|
+
};
|
|
36
|
+
exports.slugit = slugit;
|
|
37
|
+
const charLen = (data) => {
|
|
38
|
+
return data.length;
|
|
39
|
+
};
|
|
40
|
+
exports.charLen = charLen;
|
|
41
|
+
const wordLen = (data) => {
|
|
42
|
+
const array = data.trim().split(/\s+/);
|
|
43
|
+
return array.length;
|
|
44
|
+
};
|
|
45
|
+
exports.wordLen = wordLen;
|
|
46
|
+
const isObject = (arg) => {
|
|
47
|
+
const ty = typeof arg;
|
|
48
|
+
return ty === 'object' ? true : false;
|
|
49
|
+
};
|
|
50
|
+
exports.isObject = isObject;
|
|
51
|
+
const isString = (arg) => {
|
|
52
|
+
const ty = typeof arg;
|
|
53
|
+
return ty === 'string' ? true : false;
|
|
54
|
+
};
|
|
55
|
+
exports.isString = isString;
|
|
56
|
+
const isArray = (arg) => {
|
|
57
|
+
let res = false;
|
|
58
|
+
if (Array.isArray) {
|
|
59
|
+
res = Array.isArray(arg);
|
|
60
|
+
}
|
|
61
|
+
return res;
|
|
62
|
+
};
|
|
63
|
+
exports.isArray = isArray;
|
|
64
|
+
const strToArray = (arg, split) => {
|
|
65
|
+
return arg.split(split);
|
|
66
|
+
};
|
|
67
|
+
exports.strToArray = strToArray;
|
|
68
|
+
const strToArrayEs6 = (arg, split) => {
|
|
69
|
+
return arg.split(split);
|
|
70
|
+
};
|
|
71
|
+
exports.strToArrayEs6 = strToArrayEs6;
|
|
72
|
+
const strIncludes = (arg, inc) => {
|
|
73
|
+
return arg.indexOf(inc) ? true : false;
|
|
74
|
+
};
|
|
75
|
+
exports.strIncludes = strIncludes;
|
|
76
|
+
const strIncludesEs6 = (arg, inc) => {
|
|
77
|
+
return arg.includes(inc) ? true : false;
|
|
78
|
+
};
|
|
79
|
+
exports.strIncludesEs6 = strIncludesEs6;
|
|
80
|
+
const arrayIncludesEs6 = (arr, inc) => {
|
|
81
|
+
return arr.includes(inc) ? true : false;
|
|
82
|
+
};
|
|
83
|
+
exports.arrayIncludesEs6 = arrayIncludesEs6;
|
|
84
|
+
const arrayIncludes = (arr, inc) => {
|
|
85
|
+
let flag = false;
|
|
86
|
+
for (let i = 0; i < arr.length; i++) {
|
|
87
|
+
if (arr[i].toString() === inc.toString()) {
|
|
88
|
+
flag = true;
|
|
89
|
+
break;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
return flag;
|
|
93
|
+
};
|
|
94
|
+
exports.arrayIncludes = arrayIncludes;
|
|
95
|
+
const listIncludes = (data) => {
|
|
96
|
+
let flag = false;
|
|
97
|
+
const { arr, list, type } = data;
|
|
98
|
+
if (type === 'some') {
|
|
99
|
+
flag = list.some((x) => arr.includes(x));
|
|
100
|
+
}
|
|
101
|
+
if (type === 'every') {
|
|
102
|
+
flag = list.every((x) => arr.includes(x));
|
|
103
|
+
}
|
|
104
|
+
return flag;
|
|
105
|
+
};
|
|
106
|
+
exports.listIncludes = listIncludes;
|
|
107
|
+
const timeToMinutes = (data) => {
|
|
108
|
+
const seconds = (data.hr * 3600) + (data.min * 60) + data.sec;
|
|
109
|
+
const minutes = seconds / 60;
|
|
110
|
+
return parseFloat(minutes.toFixed(2));
|
|
111
|
+
};
|
|
112
|
+
exports.timeToMinutes = timeToMinutes;
|
|
113
|
+
const rangeFromMinutes = (dur) => {
|
|
114
|
+
const start = new Date();
|
|
115
|
+
const end = new Date(start.getTime() + dur * 60 * 1000);
|
|
116
|
+
const msdiff = end.getTime() - start.getTime();
|
|
117
|
+
const mindiff = msdiff / (1000 * 60);
|
|
118
|
+
return { start, end, diff: mindiff };
|
|
119
|
+
};
|
|
120
|
+
exports.rangeFromMinutes = rangeFromMinutes;
|
|
121
|
+
const dateToWord = (date) => {
|
|
122
|
+
const theDate = (0, dayjs_1.default)(date).toString();
|
|
123
|
+
return theDate;
|
|
124
|
+
};
|
|
125
|
+
exports.dateToWord = dateToWord;
|
|
126
|
+
const dateToWordRaw = () => {
|
|
127
|
+
const theDate = (0, dayjs_1.default)().toString();
|
|
128
|
+
return theDate;
|
|
129
|
+
};
|
|
130
|
+
exports.dateToWordRaw = dateToWordRaw;
|
|
131
|
+
const isEmptyObject = (obj) => {
|
|
132
|
+
return Object.keys(obj).length === 0;
|
|
133
|
+
};
|
|
134
|
+
exports.isEmptyObject = isEmptyObject;
|
|
135
|
+
const isBase64 = (data) => {
|
|
136
|
+
let result = false;
|
|
137
|
+
const mime = data.split(';base64')[0].split(':')[1];
|
|
138
|
+
if (mime && mime !== '') {
|
|
139
|
+
result = true;
|
|
140
|
+
}
|
|
141
|
+
return result;
|
|
142
|
+
};
|
|
143
|
+
exports.isBase64 = isBase64;
|
|
144
|
+
const getBase64Mime = (data) => {
|
|
145
|
+
let result = '';
|
|
146
|
+
if ((0, exports.isBase64)(data)) {
|
|
147
|
+
result = data.split(';base64')[0].split(':')[1];
|
|
148
|
+
}
|
|
149
|
+
return result;
|
|
150
|
+
};
|
|
151
|
+
exports.getBase64Mime = getBase64Mime;
|
|
152
|
+
const capitalize = (word, each = false) => {
|
|
153
|
+
let result = '';
|
|
154
|
+
let tempList = [];
|
|
155
|
+
if (!each) {
|
|
156
|
+
result = word.charAt(0).toUpperCase() + word.slice(1);
|
|
157
|
+
}
|
|
158
|
+
if (each) {
|
|
159
|
+
let split = word.split(' ');
|
|
160
|
+
split.forEach((x) => {
|
|
161
|
+
tempList.push(x.charAt(0).toUpperCase() + x.slice(1));
|
|
162
|
+
});
|
|
163
|
+
result = tempList.join(' ');
|
|
164
|
+
}
|
|
165
|
+
return result;
|
|
166
|
+
};
|
|
167
|
+
exports.capitalize = capitalize;
|
|
168
|
+
const convertUrlToBase64 = async (url) => {
|
|
169
|
+
let body = null;
|
|
170
|
+
return new Promise((resolve, reject) => {
|
|
171
|
+
https_1.default.get(url, (resp) => {
|
|
172
|
+
resp.setEncoding('base64');
|
|
173
|
+
body = "data:" + resp.headers["content-type"] + ";base64,";
|
|
174
|
+
resp.on('data', (data) => { body += data; });
|
|
175
|
+
resp.on('end', () => {
|
|
176
|
+
try {
|
|
177
|
+
resolve(body);
|
|
178
|
+
}
|
|
179
|
+
catch (e) {
|
|
180
|
+
reject(e.message);
|
|
181
|
+
}
|
|
182
|
+
});
|
|
183
|
+
}).on('error', (e) => {
|
|
184
|
+
reject(`Got error: ${e.message}`);
|
|
185
|
+
});
|
|
186
|
+
});
|
|
187
|
+
};
|
|
188
|
+
exports.convertUrlToBase64 = convertUrlToBase64;
|
|
189
|
+
const urlToBase64 = async (url, headers = {}) => {
|
|
190
|
+
try {
|
|
191
|
+
const response = await axios_1.default.get(url, {
|
|
192
|
+
responseType: 'arraybuffer',
|
|
193
|
+
headers: headers,
|
|
194
|
+
});
|
|
195
|
+
const contentType = response.headers['content-type'];
|
|
196
|
+
const buffer = Buffer.from(response.data);
|
|
197
|
+
const base64String = buffer.toString('base64');
|
|
198
|
+
return contentType ? `data:${contentType};base64,${base64String}` : base64String;
|
|
199
|
+
}
|
|
200
|
+
catch (err) {
|
|
201
|
+
console.error('Failed to get file from url:', err);
|
|
202
|
+
}
|
|
203
|
+
};
|
|
204
|
+
exports.urlToBase64 = urlToBase64;
|
|
205
|
+
const leadingNum = (val, length = 2, pad = '0') => {
|
|
206
|
+
let lead = '';
|
|
207
|
+
if (val >= 0) {
|
|
208
|
+
lead = val.toString().padStart(length, pad);
|
|
209
|
+
}
|
|
210
|
+
return lead;
|
|
211
|
+
};
|
|
212
|
+
exports.leadingNum = leadingNum;
|
|
213
|
+
const daysOfWeek = () => {
|
|
214
|
+
let result = [
|
|
215
|
+
{ id: 0, name: 'sunday' },
|
|
216
|
+
{ id: 1, name: 'monday' },
|
|
217
|
+
{ id: 2, name: 'tuesday' },
|
|
218
|
+
{ id: 3, name: 'wednesday' },
|
|
219
|
+
{ id: 4, name: 'thursday' },
|
|
220
|
+
{ id: 5, name: 'friday' },
|
|
221
|
+
{ id: 6, name: 'saturday' },
|
|
222
|
+
];
|
|
223
|
+
return result;
|
|
224
|
+
};
|
|
225
|
+
exports.daysOfWeek = daysOfWeek;
|
|
226
|
+
const monthsOfYear = () => {
|
|
227
|
+
let result = [
|
|
228
|
+
{ id: 0, name: 'january' },
|
|
229
|
+
{ id: 1, name: 'february' },
|
|
230
|
+
{ id: 2, name: 'march' },
|
|
231
|
+
{ id: 3, name: 'april' },
|
|
232
|
+
{ id: 4, name: 'may' },
|
|
233
|
+
{ id: 5, name: 'june' },
|
|
234
|
+
{ id: 6, name: 'july' },
|
|
235
|
+
{ id: 7, name: 'august' },
|
|
236
|
+
{ id: 8, name: 'september' },
|
|
237
|
+
{ id: 9, name: 'october' },
|
|
238
|
+
{ id: 10, name: 'november' },
|
|
239
|
+
{ id: 11, name: 'december' },
|
|
240
|
+
];
|
|
241
|
+
return result;
|
|
242
|
+
};
|
|
243
|
+
exports.monthsOfYear = monthsOfYear;
|
|
244
|
+
const formatDate = (date, type) => {
|
|
245
|
+
let result = '';
|
|
246
|
+
if (type === 'basic') {
|
|
247
|
+
result = (0, moment_1.default)(date).format('Do MMM, YYYY');
|
|
248
|
+
}
|
|
249
|
+
if (type === 'datetime') {
|
|
250
|
+
result = (0, moment_1.default)(date).format('Do MMM, YYYY HH:mm:ss A');
|
|
251
|
+
}
|
|
252
|
+
if (type === 'datetime-slash') {
|
|
253
|
+
result = (0, moment_1.default)(date).format('YYYY/MM/DD HH:mm:ss');
|
|
254
|
+
}
|
|
255
|
+
if (type === 'datetime-separated') {
|
|
256
|
+
result = (0, moment_1.default)(date).format('YYYY-MM-DD HH:mm:ss');
|
|
257
|
+
}
|
|
258
|
+
if (type === 'localtime') {
|
|
259
|
+
result = (0, moment_1.default)(date).format('h:mm A');
|
|
260
|
+
}
|
|
261
|
+
if (type === 'separated') {
|
|
262
|
+
result = (0, moment_1.default)(date).format('YYYY-MM-DD');
|
|
263
|
+
}
|
|
264
|
+
if (type === 'slashed') {
|
|
265
|
+
result = (0, moment_1.default)(date).format('YYYY/MM/DD');
|
|
266
|
+
}
|
|
267
|
+
return result;
|
|
268
|
+
};
|
|
269
|
+
exports.formatDate = formatDate;
|
|
270
|
+
const dateToday = (d = null) => {
|
|
271
|
+
const today = d !== null && d !== undefined && d !== '' ? new Date(d) : new Date();
|
|
272
|
+
const _dayjs = d !== null && d !== undefined && d !== '' ? (0, dayjs_1.default)(d) : (0, dayjs_1.default)();
|
|
273
|
+
const daysOfWeek = [
|
|
274
|
+
{ id: 0, name: 'sunday' },
|
|
275
|
+
{ id: 1, name: 'monday' },
|
|
276
|
+
{ id: 2, name: 'tuesday' },
|
|
277
|
+
{ id: 3, name: 'wednesday' },
|
|
278
|
+
{ id: 4, name: 'thursday' },
|
|
279
|
+
{ id: 5, name: 'friday' },
|
|
280
|
+
{ id: 6, name: 'saturday' },
|
|
281
|
+
];
|
|
282
|
+
const monthsOfYear = [
|
|
283
|
+
{ id: 0, name: 'january' },
|
|
284
|
+
{ id: 1, name: 'february' },
|
|
285
|
+
{ id: 2, name: 'march' },
|
|
286
|
+
{ id: 3, name: 'april' },
|
|
287
|
+
{ id: 4, name: 'may' },
|
|
288
|
+
{ id: 5, name: 'june' },
|
|
289
|
+
{ id: 6, name: 'july' },
|
|
290
|
+
{ id: 7, name: 'august' },
|
|
291
|
+
{ id: 8, name: 'september' },
|
|
292
|
+
{ id: 9, name: 'october' },
|
|
293
|
+
{ id: 10, name: 'november' },
|
|
294
|
+
{ id: 11, name: 'december' },
|
|
295
|
+
];
|
|
296
|
+
const date = today.getDate();
|
|
297
|
+
const _d = today.getDay();
|
|
298
|
+
const day = today.getDay() + 1;
|
|
299
|
+
const dayName = daysOfWeek.find((d) => d.id === _d)?.name;
|
|
300
|
+
const _m = today.getMonth();
|
|
301
|
+
const month = today.getMonth() + 1;
|
|
302
|
+
const monthName = monthsOfYear.find((m) => m.id === _m)?.name;
|
|
303
|
+
const year = today.getFullYear();
|
|
304
|
+
const week = _dayjs.week();
|
|
305
|
+
const hour = today.getHours();
|
|
306
|
+
const min = today.getMinutes();
|
|
307
|
+
const sec = today.getSeconds();
|
|
308
|
+
const milli = today.getMilliseconds();
|
|
309
|
+
const iso = today.toISOString();
|
|
310
|
+
const timestamp = today.getTime();
|
|
311
|
+
return { year: year, month: month, monthName: monthName, date: date, week: week, day: day, dayName: dayName, hour: hour, min: min, sec: sec, milli: milli, ISO: iso, dayjs: today, dateTime: timestamp };
|
|
312
|
+
};
|
|
313
|
+
exports.dateToday = dateToday;
|
|
314
|
+
const sortData = (data, prop = '') => {
|
|
315
|
+
let sorted = [];
|
|
316
|
+
if (prop !== '') {
|
|
317
|
+
sorted = data.sort((a, b) => {
|
|
318
|
+
if (a[prop].toString() < b[prop].toString()) {
|
|
319
|
+
return -1;
|
|
320
|
+
}
|
|
321
|
+
else if (a[prop].toString() > b[prop].toString()) {
|
|
322
|
+
return 1;
|
|
323
|
+
}
|
|
324
|
+
else {
|
|
325
|
+
return 0;
|
|
326
|
+
}
|
|
327
|
+
});
|
|
328
|
+
}
|
|
329
|
+
if (prop === '') {
|
|
330
|
+
sorted = data.sort((a, b) => {
|
|
331
|
+
if (a.toString() < b.toString()) {
|
|
332
|
+
return -1;
|
|
333
|
+
}
|
|
334
|
+
else if (a.toString() > b.toString()) {
|
|
335
|
+
return 1;
|
|
336
|
+
}
|
|
337
|
+
else {
|
|
338
|
+
return 0;
|
|
339
|
+
}
|
|
340
|
+
});
|
|
341
|
+
}
|
|
342
|
+
return sorted;
|
|
343
|
+
};
|
|
344
|
+
exports.sortData = sortData;
|
|
345
|
+
const rearrangeArray = (from, to, arr) => {
|
|
346
|
+
let result = [];
|
|
347
|
+
let temp = [];
|
|
348
|
+
// save array temporarily
|
|
349
|
+
temp = [...arr];
|
|
350
|
+
// remove the item at the 'from' index in the array
|
|
351
|
+
// get that item and save it in variable 'item'
|
|
352
|
+
const item = arr.splice(from, 1)[0];
|
|
353
|
+
if (item && item !== undefined && item !== null) {
|
|
354
|
+
// copy the remaining items in the array into 'result'
|
|
355
|
+
result = [...arr];
|
|
356
|
+
// add the item removed above to the 'result' array but to a new index
|
|
357
|
+
result.splice(to, 0, item);
|
|
358
|
+
}
|
|
359
|
+
else {
|
|
360
|
+
result = [...temp];
|
|
361
|
+
}
|
|
362
|
+
return result;
|
|
363
|
+
};
|
|
364
|
+
exports.rearrangeArray = rearrangeArray;
|
|
365
|
+
const isObjectId = (val) => {
|
|
366
|
+
let flag = false;
|
|
367
|
+
if (mongoose_1.default.Types.ObjectId.isValid(val) && (String)(new mongoose_1.default.Types.ObjectId(val)) === val) {
|
|
368
|
+
flag = true;
|
|
369
|
+
}
|
|
370
|
+
else {
|
|
371
|
+
flag === false;
|
|
372
|
+
}
|
|
373
|
+
return flag;
|
|
374
|
+
};
|
|
375
|
+
exports.isObjectId = isObjectId;
|
|
376
|
+
const isPos = (val) => {
|
|
377
|
+
let flag = false;
|
|
378
|
+
if (!Number.isNaN(val) && val >= 0) {
|
|
379
|
+
flag = true;
|
|
380
|
+
}
|
|
381
|
+
else {
|
|
382
|
+
flag = false;
|
|
383
|
+
}
|
|
384
|
+
return flag;
|
|
385
|
+
};
|
|
386
|
+
exports.isPos = isPos;
|
|
387
|
+
const isNeg = (val) => {
|
|
388
|
+
let flag = false;
|
|
389
|
+
if (!Number.isNaN(val) && val < 0) {
|
|
390
|
+
flag = true;
|
|
391
|
+
}
|
|
392
|
+
else {
|
|
393
|
+
flag = false;
|
|
394
|
+
}
|
|
395
|
+
return flag;
|
|
396
|
+
};
|
|
397
|
+
exports.isNeg = isNeg;
|
|
398
|
+
const isNumber = (val) => {
|
|
399
|
+
let flag = false;
|
|
400
|
+
if (!Number.isNaN(val) && typeof (val) === 'number') {
|
|
401
|
+
flag = true;
|
|
402
|
+
}
|
|
403
|
+
else {
|
|
404
|
+
flag = false;
|
|
405
|
+
}
|
|
406
|
+
return flag;
|
|
407
|
+
};
|
|
408
|
+
exports.isNumber = isNumber;
|
|
409
|
+
const isZero = (val) => {
|
|
410
|
+
let flag = false;
|
|
411
|
+
if ((0, exports.isNumber)(val)) {
|
|
412
|
+
if (val === 0) {
|
|
413
|
+
flag = true;
|
|
414
|
+
}
|
|
415
|
+
else {
|
|
416
|
+
flag = false;
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
return flag;
|
|
420
|
+
};
|
|
421
|
+
exports.isZero = isZero;
|
|
422
|
+
const checkDateFormat = (date) => {
|
|
423
|
+
let flag = false;
|
|
424
|
+
if (!(0, exports.strIncludesEs6)(date, '-') && !(0, exports.strIncludesEs6)(date, '/')) {
|
|
425
|
+
flag = false;
|
|
426
|
+
}
|
|
427
|
+
else {
|
|
428
|
+
if ((0, exports.strIncludesEs6)(date, '-')) {
|
|
429
|
+
const split = date.split('-');
|
|
430
|
+
const y = split[0];
|
|
431
|
+
const m = split[1];
|
|
432
|
+
const d = split[2];
|
|
433
|
+
if (y.length !== 4) {
|
|
434
|
+
flag = false;
|
|
435
|
+
}
|
|
436
|
+
else if (m.length !== 2) {
|
|
437
|
+
flag = false;
|
|
438
|
+
}
|
|
439
|
+
else if (d.length !== 2) {
|
|
440
|
+
flag = false;
|
|
441
|
+
}
|
|
442
|
+
else {
|
|
443
|
+
flag = true;
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
if ((0, exports.strIncludesEs6)(date, '/')) {
|
|
447
|
+
const split = date.split('/');
|
|
448
|
+
const y = split[0];
|
|
449
|
+
const m = split[1];
|
|
450
|
+
const d = split[2];
|
|
451
|
+
if (y.length !== 4) {
|
|
452
|
+
flag = false;
|
|
453
|
+
}
|
|
454
|
+
else if (m.length !== 2) {
|
|
455
|
+
flag = false;
|
|
456
|
+
}
|
|
457
|
+
else if (d.length !== 2) {
|
|
458
|
+
flag = false;
|
|
459
|
+
}
|
|
460
|
+
else {
|
|
461
|
+
flag = true;
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
return flag;
|
|
466
|
+
};
|
|
467
|
+
exports.checkDateFormat = checkDateFormat;
|
|
468
|
+
const checkTimeFormat = (time) => {
|
|
469
|
+
let flag = false;
|
|
470
|
+
if (!(0, exports.strIncludesEs6)(time, ':')) {
|
|
471
|
+
flag = false;
|
|
472
|
+
}
|
|
473
|
+
else {
|
|
474
|
+
const split = time.split(':');
|
|
475
|
+
const h = split[0];
|
|
476
|
+
const m = split[1];
|
|
477
|
+
if (h.length !== 2) {
|
|
478
|
+
flag = false;
|
|
479
|
+
}
|
|
480
|
+
else if (m.length !== 2) {
|
|
481
|
+
flag = false;
|
|
482
|
+
}
|
|
483
|
+
else {
|
|
484
|
+
flag = true;
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
return flag;
|
|
488
|
+
};
|
|
489
|
+
exports.checkTimeFormat = checkTimeFormat;
|
|
490
|
+
const dateIsToday = (date) => {
|
|
491
|
+
let flag = false;
|
|
492
|
+
const today = Date.now();
|
|
493
|
+
const conv = (0, exports.dateToday)(today);
|
|
494
|
+
const dc = (0, exports.dateToday)(date);
|
|
495
|
+
const format = `${conv.year}-${(0, exports.leadingNum)(conv.month)}-${(0, exports.leadingNum)(conv.date)}`;
|
|
496
|
+
const check = `${dc.year}-${(0, exports.leadingNum)(dc.month)}-${(0, exports.leadingNum)(dc.date)}`;
|
|
497
|
+
if (format.toString() === check.toString()) {
|
|
498
|
+
flag = true;
|
|
499
|
+
}
|
|
500
|
+
else {
|
|
501
|
+
flag = false;
|
|
502
|
+
}
|
|
503
|
+
return flag;
|
|
504
|
+
};
|
|
505
|
+
exports.dateIsToday = dateIsToday;
|
|
506
|
+
const dateIsYesterday = (date) => {
|
|
507
|
+
let flag = false;
|
|
508
|
+
const today = Date.now();
|
|
509
|
+
const conv = (0, exports.dateToday)(today);
|
|
510
|
+
const dc = (0, exports.dateToday)(date);
|
|
511
|
+
const diffDate = conv.date - 1;
|
|
512
|
+
if (dc.date === diffDate) {
|
|
513
|
+
flag = true;
|
|
514
|
+
}
|
|
515
|
+
else {
|
|
516
|
+
flag = false;
|
|
517
|
+
}
|
|
518
|
+
return flag;
|
|
519
|
+
};
|
|
520
|
+
exports.dateIsYesterday = dateIsYesterday;
|
|
521
|
+
const dateIsEqual = (fDate, lDate) => {
|
|
522
|
+
let flag = false;
|
|
523
|
+
const fConv = (0, exports.dateToday)(fDate);
|
|
524
|
+
const lConv = (0, exports.dateToday)(lDate);
|
|
525
|
+
const format = `${fConv.year}-${(0, exports.leadingNum)(fConv.month)}-${(0, exports.leadingNum)(fConv.date)}`;
|
|
526
|
+
const check = `${lConv.year}-${(0, exports.leadingNum)(lConv.month)}-${(0, exports.leadingNum)(lConv.date)}`;
|
|
527
|
+
if (format.toString() === check.toString()) {
|
|
528
|
+
flag = true;
|
|
529
|
+
}
|
|
530
|
+
else {
|
|
531
|
+
flag = false;
|
|
532
|
+
}
|
|
533
|
+
return flag;
|
|
534
|
+
};
|
|
535
|
+
exports.dateIsEqual = dateIsEqual;
|
|
536
|
+
const dateIsPast = (data) => {
|
|
537
|
+
let flag = false;
|
|
538
|
+
const { currentDate, compareDate } = data;
|
|
539
|
+
const today = (0, exports.dateToday)(currentDate);
|
|
540
|
+
const compare = (0, exports.dateToday)(compareDate);
|
|
541
|
+
if (today.year > compare.year) {
|
|
542
|
+
flag = true;
|
|
543
|
+
}
|
|
544
|
+
else if (today.month > compare.month) {
|
|
545
|
+
flag = true;
|
|
546
|
+
}
|
|
547
|
+
else if (today.date > compare.date) {
|
|
548
|
+
flag = true;
|
|
549
|
+
}
|
|
550
|
+
return flag;
|
|
551
|
+
};
|
|
552
|
+
exports.dateIsPast = dateIsPast;
|
|
553
|
+
const dateIsFuture = (today, date) => {
|
|
554
|
+
let flag = false;
|
|
555
|
+
const tDate = (0, exports.dateToday)(today);
|
|
556
|
+
const cDate = (0, exports.dateToday)(date);
|
|
557
|
+
const yearFq = (cDate.year === tDate.year) || (cDate.year > tDate.year);
|
|
558
|
+
const monthFq = (cDate.month === tDate.month) || (cDate.month > tDate.month);
|
|
559
|
+
const dateFq = cDate.date > tDate.date;
|
|
560
|
+
if (yearFq && monthFq && dateFq) {
|
|
561
|
+
flag = true;
|
|
562
|
+
}
|
|
563
|
+
else {
|
|
564
|
+
flag = false;
|
|
565
|
+
}
|
|
566
|
+
return flag;
|
|
567
|
+
};
|
|
568
|
+
exports.dateIsFuture = dateIsFuture;
|
|
569
|
+
const yesterdayFromDate = (date) => {
|
|
570
|
+
const today = (0, exports.dateToday)(date);
|
|
571
|
+
const formatted = `${today.year}-${(0, exports.leadingNum)(today.month)}-${(0, exports.leadingNum)(today.date - 1)}`;
|
|
572
|
+
const yesterday = (0, exports.dateToday)(formatted);
|
|
573
|
+
return yesterday.ISO;
|
|
574
|
+
};
|
|
575
|
+
exports.yesterdayFromDate = yesterdayFromDate;
|
|
576
|
+
const daysFromDates = (data) => {
|
|
577
|
+
let result = 0;
|
|
578
|
+
const startDate = new Date(data.start);
|
|
579
|
+
const endDate = new Date(data.end);
|
|
580
|
+
if (data.dir === 'default') {
|
|
581
|
+
result = Math.round((endDate.getTime() - startDate.getTime()) / (1000 * 3600 * 24));
|
|
582
|
+
}
|
|
583
|
+
else if (data.dir === 'reverse') {
|
|
584
|
+
result = Math.round((startDate.getTime() - endDate.getTime()) / (1000 * 3600 * 24));
|
|
585
|
+
}
|
|
586
|
+
return result;
|
|
587
|
+
};
|
|
588
|
+
exports.daysFromDates = daysFromDates;
|
|
589
|
+
const generate = (size, options) => {
|
|
590
|
+
let result = '';
|
|
591
|
+
if (options.type === 'code') {
|
|
592
|
+
result = random_service_1.default.randomCode(size, options.alpha);
|
|
593
|
+
}
|
|
594
|
+
if (options.type === 'alpha') {
|
|
595
|
+
result = random_service_1.default.randomAlpha(size);
|
|
596
|
+
}
|
|
597
|
+
if (options.type === 'num') {
|
|
598
|
+
result = random_service_1.default.randomNum(size);
|
|
599
|
+
}
|
|
600
|
+
return result;
|
|
601
|
+
};
|
|
602
|
+
exports.generate = generate;
|
|
603
|
+
const notDefined = (val, truthy = false) => {
|
|
604
|
+
let result = false;
|
|
605
|
+
if (!truthy) {
|
|
606
|
+
if (val === undefined || val === null || val.toString() === '') {
|
|
607
|
+
result = true;
|
|
608
|
+
}
|
|
609
|
+
}
|
|
610
|
+
if (truthy) {
|
|
611
|
+
if (val.toString() !== 'true' && val.toString() !== 'false') {
|
|
612
|
+
result = true;
|
|
613
|
+
}
|
|
614
|
+
else if (val === undefined || val === null || val.toString() === '') {
|
|
615
|
+
result = true;
|
|
616
|
+
}
|
|
617
|
+
}
|
|
618
|
+
return result;
|
|
619
|
+
};
|
|
620
|
+
exports.notDefined = notDefined;
|
|
621
|
+
const isDefined = (val, truthy = false) => {
|
|
622
|
+
let result = false;
|
|
623
|
+
if (!truthy) {
|
|
624
|
+
if (val !== undefined && val !== null && val.toString() !== '') {
|
|
625
|
+
result = true;
|
|
626
|
+
}
|
|
627
|
+
}
|
|
628
|
+
if (truthy) {
|
|
629
|
+
if (val !== undefined && val !== null && (val.toString() === 'true' || val.toString() === 'false')) {
|
|
630
|
+
result = true;
|
|
631
|
+
}
|
|
632
|
+
else if (val !== undefined && val !== null && val.toString() !== '') {
|
|
633
|
+
result = true;
|
|
634
|
+
}
|
|
635
|
+
}
|
|
636
|
+
return result;
|
|
637
|
+
};
|
|
638
|
+
exports.isDefined = isDefined;
|
|
639
|
+
const isPrecise = (data) => {
|
|
640
|
+
let result = false;
|
|
641
|
+
const { value, length } = data;
|
|
642
|
+
const converted = value.toString();
|
|
643
|
+
if ((0, exports.strIncludesEs6)(converted, '.')) {
|
|
644
|
+
const split = converted.split('.');
|
|
645
|
+
const deci = split[1].length;
|
|
646
|
+
if (deci === length) {
|
|
647
|
+
result = true;
|
|
648
|
+
}
|
|
649
|
+
}
|
|
650
|
+
return result;
|
|
651
|
+
};
|
|
652
|
+
exports.isPrecise = isPrecise;
|
|
653
|
+
const hasDecimal = (value) => {
|
|
654
|
+
let result = false;
|
|
655
|
+
const converted = value.toString();
|
|
656
|
+
if ((0, exports.strIncludesEs6)(converted, '.')) {
|
|
657
|
+
const split = converted.split('.');
|
|
658
|
+
if (split.length > 1) {
|
|
659
|
+
result = true;
|
|
660
|
+
}
|
|
661
|
+
}
|
|
662
|
+
return result;
|
|
663
|
+
};
|
|
664
|
+
exports.hasDecimal = hasDecimal;
|
|
665
|
+
const toDecimal = (value, places) => {
|
|
666
|
+
let result = value;
|
|
667
|
+
const converted = value.toFixed(places);
|
|
668
|
+
result = parseFloat(converted);
|
|
669
|
+
return result;
|
|
670
|
+
};
|
|
671
|
+
exports.toDecimal = toDecimal;
|
|
672
|
+
const UIID = (batch = 0) => {
|
|
673
|
+
let result = '';
|
|
674
|
+
const uid = crypto_1.default.randomUUID();
|
|
675
|
+
const split = uid.split('-');
|
|
676
|
+
if (batch === 0) {
|
|
677
|
+
result = uid;
|
|
678
|
+
}
|
|
679
|
+
else {
|
|
680
|
+
if (batch === 1) {
|
|
681
|
+
result = split[0];
|
|
682
|
+
}
|
|
683
|
+
else if (batch === 2) {
|
|
684
|
+
result = `${split[0]}-${split[1]}${split[2]}`;
|
|
685
|
+
}
|
|
686
|
+
else if (batch === 3) {
|
|
687
|
+
result = `${split[0]}-${split[1]}${split[2]}-${split[split.length - 1]}`;
|
|
688
|
+
}
|
|
689
|
+
else {
|
|
690
|
+
result = uid;
|
|
691
|
+
}
|
|
692
|
+
}
|
|
693
|
+
return result;
|
|
694
|
+
};
|
|
695
|
+
exports.UIID = UIID;
|
|
696
|
+
const compareISODate = (dateA, dateB, type) => {
|
|
697
|
+
let result = false;
|
|
698
|
+
const dt = (0, exports.dateToday)(dateA);
|
|
699
|
+
const dn = (0, exports.dateToday)(dateB);
|
|
700
|
+
if (type === 'equal') {
|
|
701
|
+
if (dt.year === dn.year && dt.month === dn.month && dt.date === dn.date) {
|
|
702
|
+
result = true;
|
|
703
|
+
}
|
|
704
|
+
else {
|
|
705
|
+
result = false;
|
|
706
|
+
}
|
|
707
|
+
}
|
|
708
|
+
if (type === 'greaterthan') {
|
|
709
|
+
if (dt.dateTime > dn.dateTime) {
|
|
710
|
+
result = true;
|
|
711
|
+
}
|
|
712
|
+
else {
|
|
713
|
+
result = false;
|
|
714
|
+
}
|
|
715
|
+
}
|
|
716
|
+
if (type === 'greaterequal') {
|
|
717
|
+
if (dt.dateTime >= dn.dateTime) {
|
|
718
|
+
result = true;
|
|
719
|
+
}
|
|
720
|
+
else {
|
|
721
|
+
result = false;
|
|
722
|
+
}
|
|
723
|
+
}
|
|
724
|
+
if (type === 'lessthan') {
|
|
725
|
+
if (dt.dateTime < dn.dateTime) {
|
|
726
|
+
result = true;
|
|
727
|
+
}
|
|
728
|
+
else {
|
|
729
|
+
result = false;
|
|
730
|
+
}
|
|
731
|
+
}
|
|
732
|
+
if (type === 'lessequal') {
|
|
733
|
+
if (dt.dateTime <= dn.dateTime) {
|
|
734
|
+
result = true;
|
|
735
|
+
}
|
|
736
|
+
else {
|
|
737
|
+
result = false;
|
|
738
|
+
}
|
|
739
|
+
}
|
|
740
|
+
return result;
|
|
741
|
+
};
|
|
742
|
+
exports.compareISODate = compareISODate;
|
|
743
|
+
const getWeeksDates = (year, month, data) => {
|
|
744
|
+
let result = [];
|
|
745
|
+
if (data.length > 0) {
|
|
746
|
+
for (let i = 0; i < data.length; i++) {
|
|
747
|
+
const week = data[i];
|
|
748
|
+
result.push({
|
|
749
|
+
label: `Week ${i + 1}`,
|
|
750
|
+
start: (0, exports.dateToday)(`${year}-${month}-${week.start}`).ISO,
|
|
751
|
+
end: (0, exports.dateToday)(`${year}-${month}-${week.end}`).ISO,
|
|
752
|
+
dates: []
|
|
753
|
+
});
|
|
754
|
+
week.dates.forEach((dt) => {
|
|
755
|
+
let p = (0, exports.dateToday)(`${year}-${month}-${dt}`);
|
|
756
|
+
result[i].dates.push(p.ISO);
|
|
757
|
+
});
|
|
758
|
+
}
|
|
759
|
+
}
|
|
760
|
+
return result;
|
|
761
|
+
};
|
|
762
|
+
exports.getWeeksDates = getWeeksDates;
|
|
763
|
+
const getDatesInRange = (start, end) => {
|
|
764
|
+
let result = [];
|
|
765
|
+
let dates = [];
|
|
766
|
+
let currentDate = new Date(start);
|
|
767
|
+
let endDate = new Date(end);
|
|
768
|
+
while (currentDate <= endDate) {
|
|
769
|
+
dates.push(new Date(currentDate)); // avoid modifying original date
|
|
770
|
+
currentDate.setDate(currentDate.getDate() + 1); // increase
|
|
771
|
+
}
|
|
772
|
+
dates.forEach((d) => {
|
|
773
|
+
result.push((0, exports.dateToday)(d).ISO);
|
|
774
|
+
});
|
|
775
|
+
return result;
|
|
776
|
+
};
|
|
777
|
+
exports.getDatesInRange = getDatesInRange;
|
|
778
|
+
const getWeeksInMonth = (year, month) => {
|
|
779
|
+
let weeks = [];
|
|
780
|
+
let dwc = 0;
|
|
781
|
+
const fd = new Date(year, month, 1); // first date
|
|
782
|
+
const ld = new Date(year, month + 1, 0); // last date
|
|
783
|
+
const nod = ld.getDate(); // number of days
|
|
784
|
+
dwc = fd.getDate(); // init counter ( this always gives 1)
|
|
785
|
+
for (let i = 1; i <= nod; i++) {
|
|
786
|
+
// NB: {i} is the date here
|
|
787
|
+
if (weeks.length === 0) {
|
|
788
|
+
weeks.push([]);
|
|
789
|
+
}
|
|
790
|
+
if (weeks[weeks.length - 1].length < 7) {
|
|
791
|
+
weeks[weeks.length - 1].push(i);
|
|
792
|
+
}
|
|
793
|
+
else {
|
|
794
|
+
weeks.push([]);
|
|
795
|
+
weeks[weeks.length - 1].push(i);
|
|
796
|
+
}
|
|
797
|
+
dwc = (dwc + 1) % 7;
|
|
798
|
+
}
|
|
799
|
+
const rs = weeks.map((w) => ({
|
|
800
|
+
start: w[0],
|
|
801
|
+
end: w[w.length - 1],
|
|
802
|
+
dates: w
|
|
803
|
+
}));
|
|
804
|
+
return rs;
|
|
805
|
+
};
|
|
806
|
+
exports.getWeeksInMonth = getWeeksInMonth;
|
|
807
|
+
const validateBase64 = (data) => {
|
|
808
|
+
const mime = data.split(';base64')[0].split(':')[1];
|
|
809
|
+
return mime && mime !== undefined && mime !== null ? true : false;
|
|
810
|
+
};
|
|
811
|
+
exports.validateBase64 = validateBase64;
|
|
812
|
+
const getCodeFromName = (name) => {
|
|
813
|
+
let result = '';
|
|
814
|
+
if (name) {
|
|
815
|
+
const split = name.split(' ');
|
|
816
|
+
if (split.length > 1) {
|
|
817
|
+
for (let i = 0; i < split.length; i++) {
|
|
818
|
+
result = result + split[i].substring(0, 1).toUpperCase();
|
|
819
|
+
}
|
|
820
|
+
}
|
|
821
|
+
else {
|
|
822
|
+
result = name.substring(0, 3).toUpperCase();
|
|
823
|
+
}
|
|
824
|
+
}
|
|
825
|
+
return result;
|
|
826
|
+
};
|
|
827
|
+
exports.getCodeFromName = getCodeFromName;
|
|
828
|
+
/**
|
|
829
|
+
* @name getDaysFromDates
|
|
830
|
+
* @param prev
|
|
831
|
+
* @param next
|
|
832
|
+
* @returns
|
|
833
|
+
*/
|
|
834
|
+
const getDaysFromDates = (then, now) => {
|
|
835
|
+
let result = 0;
|
|
836
|
+
const thenDate = new Date(then);
|
|
837
|
+
const nowDate = new Date(now);
|
|
838
|
+
const dateThen = (0, exports.dateToday)(thenDate);
|
|
839
|
+
const covNow = (0, exports.dateToday)(nowDate);
|
|
840
|
+
const dateNow = (0, dayjs_1.default)(covNow.ISO);
|
|
841
|
+
const diff = dateNow.diff(dateThen.ISO, 'day', true);
|
|
842
|
+
result = Math.floor(diff);
|
|
843
|
+
return result;
|
|
844
|
+
};
|
|
845
|
+
exports.getDaysFromDates = getDaysFromDates;
|
|
846
|
+
/**
|
|
847
|
+
* @name firstDayOfMonth
|
|
848
|
+
* @param d
|
|
849
|
+
* @returns
|
|
850
|
+
*/
|
|
851
|
+
const firstDayOfMonth = (d = '') => {
|
|
852
|
+
const today = d ? new Date(d) : new Date();
|
|
853
|
+
const convToday = (0, exports.dateToday)(today);
|
|
854
|
+
const first = new Date(convToday.year, (convToday.month - 1), 1);
|
|
855
|
+
const convFirst = (0, exports.dateToday)(first);
|
|
856
|
+
return { date: first, converted: convFirst };
|
|
857
|
+
};
|
|
858
|
+
exports.firstDayOfMonth = firstDayOfMonth;
|
|
859
|
+
/**
|
|
860
|
+
* @name lastDayOfMonth
|
|
861
|
+
* @param d
|
|
862
|
+
* @returns
|
|
863
|
+
*/
|
|
864
|
+
const lastDayOfMonth = (d = '') => {
|
|
865
|
+
const today = d ? new Date(d) : new Date();
|
|
866
|
+
const convToday = (0, exports.dateToday)(today);
|
|
867
|
+
const last = new Date(convToday.year, convToday.month, 1);
|
|
868
|
+
const convLast = (0, exports.dateToday)(last);
|
|
869
|
+
return { date: last, converted: convLast };
|
|
870
|
+
};
|
|
871
|
+
exports.lastDayOfMonth = lastDayOfMonth;
|
|
872
|
+
const formatISO = (ISO) => {
|
|
873
|
+
let result = { date: '', time: '' };
|
|
874
|
+
const split = ISO.split('T');
|
|
875
|
+
if (split.length > 0) {
|
|
876
|
+
result.date = split[0];
|
|
877
|
+
result.time = split[1].split('.')[0];
|
|
878
|
+
}
|
|
879
|
+
return result;
|
|
880
|
+
};
|
|
881
|
+
exports.formatISO = formatISO;
|
|
882
|
+
const hasSAC = (value) => {
|
|
883
|
+
let result = false;
|
|
884
|
+
const match = /^[a-zA-Z0-9-_]+$/;
|
|
885
|
+
if (!match.test(value)) {
|
|
886
|
+
result = true;
|
|
887
|
+
}
|
|
888
|
+
return result;
|
|
889
|
+
};
|
|
890
|
+
exports.hasSAC = hasSAC;
|
|
891
|
+
const enumToArray = (data, type) => {
|
|
892
|
+
let result = [];
|
|
893
|
+
const list = Object.entries(data).map(([key, value]) => ({ key, value }));
|
|
894
|
+
if (type === 'all') {
|
|
895
|
+
result = list;
|
|
896
|
+
}
|
|
897
|
+
else if (type === 'values-only') {
|
|
898
|
+
result = list.map((x) => x.value);
|
|
899
|
+
}
|
|
900
|
+
else if (type === 'keys-only') {
|
|
901
|
+
result = list.map((x) => x.key);
|
|
902
|
+
}
|
|
903
|
+
return result;
|
|
904
|
+
};
|
|
905
|
+
exports.enumToArray = enumToArray;
|
|
906
|
+
const objectToArray = (data) => {
|
|
907
|
+
const _keys = Object.keys(data);
|
|
908
|
+
const _values = Object.values(data);
|
|
909
|
+
let hook = [];
|
|
910
|
+
for (let i = 0; i < _keys.length; i++) {
|
|
911
|
+
if (typeof (_values[i]) === 'object') {
|
|
912
|
+
hook.push({ key: _keys[i], value: JSON.stringify(_values[i]), type: typeof (_values[i]) });
|
|
913
|
+
}
|
|
914
|
+
else {
|
|
915
|
+
hook.push({ key: _keys[i], value: _values[i], type: typeof (_values[i]) });
|
|
916
|
+
}
|
|
917
|
+
}
|
|
918
|
+
return hook;
|
|
919
|
+
};
|
|
920
|
+
exports.objectToArray = objectToArray;
|
|
921
|
+
const isJson = (value) => {
|
|
922
|
+
let result = false;
|
|
923
|
+
try {
|
|
924
|
+
const data = JSON.parse(value);
|
|
925
|
+
result = true;
|
|
926
|
+
}
|
|
927
|
+
catch (error) {
|
|
928
|
+
result = false;
|
|
929
|
+
}
|
|
930
|
+
return result;
|
|
931
|
+
};
|
|
932
|
+
exports.isJson = isJson;
|
|
933
|
+
const arrayToObject = (data) => {
|
|
934
|
+
let result = {};
|
|
935
|
+
for (let i = 0; i < data.length; i++) {
|
|
936
|
+
let item = data[i];
|
|
937
|
+
let value = item.value;
|
|
938
|
+
if (item.type === 'object') {
|
|
939
|
+
if (typeof (item.value) === 'string' && (0, exports.isJson)(item.value)) {
|
|
940
|
+
value = JSON.parse(item.value);
|
|
941
|
+
}
|
|
942
|
+
}
|
|
943
|
+
let _m = {};
|
|
944
|
+
_m[item.key] = value;
|
|
945
|
+
Object.assign(result, _m);
|
|
946
|
+
}
|
|
947
|
+
return result;
|
|
948
|
+
};
|
|
949
|
+
exports.arrayToObject = arrayToObject;
|
|
950
|
+
const arrayToSingleObject = (data) => {
|
|
951
|
+
let keyList = [];
|
|
952
|
+
let valList = [];
|
|
953
|
+
let result = {};
|
|
954
|
+
// get keys
|
|
955
|
+
data.forEach((x) => {
|
|
956
|
+
if (typeof (x) === 'object') {
|
|
957
|
+
keyList = keyList.concat(Object.keys(x));
|
|
958
|
+
}
|
|
959
|
+
});
|
|
960
|
+
// get values
|
|
961
|
+
data.forEach((x) => {
|
|
962
|
+
if (typeof (x) === 'object') {
|
|
963
|
+
valList = valList.concat(Object.values(x));
|
|
964
|
+
}
|
|
965
|
+
});
|
|
966
|
+
if (keyList.length > 0) {
|
|
967
|
+
for (let i = 0; i < keyList.length; i++) {
|
|
968
|
+
result[keyList[i]] = valList[i];
|
|
969
|
+
}
|
|
970
|
+
}
|
|
971
|
+
else {
|
|
972
|
+
result = Object.assign({}, data);
|
|
973
|
+
}
|
|
974
|
+
return result;
|
|
975
|
+
};
|
|
976
|
+
exports.arrayToSingleObject = arrayToSingleObject;
|
|
977
|
+
const stringToBase64 = (data) => {
|
|
978
|
+
let result = '';
|
|
979
|
+
const { type, payload } = data;
|
|
980
|
+
if (type === 'buffer') {
|
|
981
|
+
const buffered = Buffer.from(payload).toString('base64');
|
|
982
|
+
result = buffered;
|
|
983
|
+
}
|
|
984
|
+
if (type === 'direct') {
|
|
985
|
+
result = payload;
|
|
986
|
+
}
|
|
987
|
+
return result;
|
|
988
|
+
};
|
|
989
|
+
exports.stringToBase64 = stringToBase64;
|
|
990
|
+
const base64ToString = (data) => {
|
|
991
|
+
let result = '';
|
|
992
|
+
const { type, payload } = data;
|
|
993
|
+
if (type === 'buffer') {
|
|
994
|
+
const buffered = Buffer.from(payload, 'base64').toString('ascii');
|
|
995
|
+
result = buffered;
|
|
996
|
+
}
|
|
997
|
+
if (type === 'direct') {
|
|
998
|
+
result = payload;
|
|
999
|
+
}
|
|
1000
|
+
return result;
|
|
1001
|
+
};
|
|
1002
|
+
exports.base64ToString = base64ToString;
|
|
1003
|
+
const dateFromWeekNumber = (weekNum) => {
|
|
1004
|
+
const dw = (0, dayjs_1.default)().week(weekNum);
|
|
1005
|
+
const weekDate = (0, exports.dateToday)(dw);
|
|
1006
|
+
return weekDate;
|
|
1007
|
+
};
|
|
1008
|
+
exports.dateFromWeekNumber = dateFromWeekNumber;
|
|
1009
|
+
const weekStartDate = (date) => {
|
|
1010
|
+
const dx = new Date(date);
|
|
1011
|
+
const day = dx.getDay();
|
|
1012
|
+
const diff = dx.getDate() - day + (0); // week starts on sunday
|
|
1013
|
+
// const diff = dx.getDate() - day + (day === 0 ? 0 : 1); // week starts on monday
|
|
1014
|
+
const newDate = new Date(dx.setDate(diff));
|
|
1015
|
+
const weekStart = (0, exports.dateToday)(newDate);
|
|
1016
|
+
return weekStart;
|
|
1017
|
+
};
|
|
1018
|
+
exports.weekStartDate = weekStartDate;
|
|
1019
|
+
const weekEndDate = (date) => {
|
|
1020
|
+
const startDate = (0, exports.weekStartDate)(date);
|
|
1021
|
+
const start = new Date(startDate.ISO);
|
|
1022
|
+
let first = start.getDate() - start.getDay();
|
|
1023
|
+
let last = first + 6;
|
|
1024
|
+
const lastDate = new Date(start.setDate(last));
|
|
1025
|
+
const weekEnd = (0, exports.dateToday)(lastDate);
|
|
1026
|
+
return weekEnd;
|
|
1027
|
+
};
|
|
1028
|
+
exports.weekEndDate = weekEndDate;
|
|
1029
|
+
const toMongoId = (id) => {
|
|
1030
|
+
return mongo_service_1.default.toMongoId(id);
|
|
1031
|
+
};
|
|
1032
|
+
exports.toMongoId = toMongoId;
|