corebasic 1.0.228 → 1.0.230
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/dist/libs/utils.js +49 -39
- package/libs/utils.ts +82 -65
- package/package.json +1 -1
- package/tsconfig.json +1 -1
package/dist/libs/utils.js
CHANGED
|
@@ -4,8 +4,7 @@ import crypto from "node:crypto";
|
|
|
4
4
|
import { readFile, writeFile } from 'fs/promises';
|
|
5
5
|
export let pipeline = { execute: execute };
|
|
6
6
|
export function log(...args) {
|
|
7
|
-
|
|
8
|
-
args.forEach(arg => res.push(typeof arg === "object" ? JSON.stringify(arg, null, '\t') : arg));
|
|
7
|
+
const res = args.map(arg => typeof arg === "object" ? JSON.stringify(arg, null, '\t') : arg);
|
|
9
8
|
console.log(...res);
|
|
10
9
|
}
|
|
11
10
|
let UID_COUNTER = crypto.randomBytes(3).readUIntBE(0, 3);
|
|
@@ -23,7 +22,7 @@ export function uid() {
|
|
|
23
22
|
export function isEmpty(str) {
|
|
24
23
|
if (!str)
|
|
25
24
|
return true;
|
|
26
|
-
return str.match(/\S/) ? false : true; // matches a non space character
|
|
25
|
+
return String(str).match(/\S/) ? false : true; // matches a non space character
|
|
27
26
|
}
|
|
28
27
|
export let GLOBAL_META = { company: "GLOBAL", outlet: "GLOBAL" };
|
|
29
28
|
export function isEmptyJson(json) {
|
|
@@ -70,7 +69,12 @@ export function toAppDate(date) {
|
|
|
70
69
|
}
|
|
71
70
|
export function toDate(arg, currentTime) {
|
|
72
71
|
let st = arg.split('/');
|
|
73
|
-
|
|
72
|
+
if (st.length !== 3)
|
|
73
|
+
throw new Error("Invalid arg in Utils.toDate()");
|
|
74
|
+
const day = st[0];
|
|
75
|
+
const month = st[1];
|
|
76
|
+
const year = st[2];
|
|
77
|
+
let temp = new Date(parseInt(year), parseInt(month) - 1, parseInt(day));
|
|
74
78
|
temp.setHours(0);
|
|
75
79
|
temp.setMinutes(0);
|
|
76
80
|
temp.setSeconds(0);
|
|
@@ -99,43 +103,44 @@ export function getDatesBetweenTwoDates(start, end) {
|
|
|
99
103
|
}
|
|
100
104
|
return arr;
|
|
101
105
|
}
|
|
102
|
-
export function deepCopy(obj) {
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
}
|
|
106
|
+
// export function deepCopy(obj) {
|
|
107
|
+
// if (Object.prototype.toString.call(obj) === '[object Array]') {
|
|
108
|
+
// let out = [], i = 0, len = obj.length;
|
|
109
|
+
// for ( ; i < len; i++ ) {
|
|
110
|
+
// if (Object.prototype.toString.call(obj[i]) === '[object Date]')
|
|
111
|
+
// out[i] = new Date(obj[i])
|
|
112
|
+
// else
|
|
113
|
+
// out[i] = arguments.callee(obj[i]);
|
|
114
|
+
// }
|
|
115
|
+
// return out;
|
|
116
|
+
// }
|
|
117
|
+
// if (typeof obj === 'object') {
|
|
118
|
+
// let out = {}, i;
|
|
119
|
+
// for ( i in obj ) {
|
|
120
|
+
// if (Object.prototype.toString.call(obj[i]) === '[object Date]')
|
|
121
|
+
// out[i] = new Date(obj[i])
|
|
122
|
+
// else
|
|
123
|
+
// out[i] = arguments.callee(obj[i]);
|
|
124
|
+
// }
|
|
125
|
+
// return out;
|
|
126
|
+
// }
|
|
127
|
+
// return obj;
|
|
128
|
+
// }
|
|
125
129
|
export function formatAMPM(date) {
|
|
126
130
|
let hours = date.getHours();
|
|
127
131
|
let minutes = date.getMinutes();
|
|
128
132
|
let ampm = hours >= 12 ? 'pm' : 'am';
|
|
129
133
|
hours = hours % 12;
|
|
130
134
|
hours = hours ? hours : 12; // the hour '0' should be '12'
|
|
131
|
-
|
|
132
|
-
let strTime = hours + ':' + minutes + ' ' + ampm;
|
|
135
|
+
let strTime = hours + ':' + (minutes < 10 ? '0' + minutes : minutes) + ' ' + ampm;
|
|
133
136
|
return strTime;
|
|
134
137
|
}
|
|
135
138
|
export function sum(array) {
|
|
136
139
|
let res = 0;
|
|
137
|
-
for (let i in array)
|
|
138
|
-
|
|
140
|
+
for (let i in array) {
|
|
141
|
+
const val = parseFloat(String(array[i] ?? 0));
|
|
142
|
+
res += val ? val : 0;
|
|
143
|
+
}
|
|
139
144
|
return res;
|
|
140
145
|
}
|
|
141
146
|
export function addMonths(date, monthsToAdd) {
|
|
@@ -180,9 +185,11 @@ export function addUTCHours(date, hoursToAdd) {
|
|
|
180
185
|
}
|
|
181
186
|
export function validityToMillisecs(start, validity) {
|
|
182
187
|
const now = new Date(start);
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
188
|
+
const parts = validity.trim().split(' ').filter(item => item.trim());
|
|
189
|
+
if (parts.length !== 2)
|
|
190
|
+
throw new Error("Invalid validity string in validityToUTCMillisecs()");
|
|
191
|
+
const count = parseIntValue(parts[0]);
|
|
192
|
+
const period = parts[1].toLowerCase();
|
|
186
193
|
const timeline = {
|
|
187
194
|
year: () => addYears(now, count),
|
|
188
195
|
years: () => addYears(now, count),
|
|
@@ -202,9 +209,11 @@ export function validityToMillisecs(start, validity) {
|
|
|
202
209
|
}
|
|
203
210
|
export function validityToUTCMillisecs(start, validity) {
|
|
204
211
|
const now = new Date(start);
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
212
|
+
const parts = validity.trim().split(' ').filter(item => item.trim());
|
|
213
|
+
if (parts.length !== 2)
|
|
214
|
+
throw new Error("Invalid validity string in validityToUTCMillisecs()");
|
|
215
|
+
const count = parseIntValue(parts[0]);
|
|
216
|
+
const period = parts[1].toLowerCase();
|
|
208
217
|
const timeline = {
|
|
209
218
|
year: () => addUTCYears(now, count),
|
|
210
219
|
years: () => addUTCYears(now, count),
|
|
@@ -258,7 +267,7 @@ export async function fileToJson(path, file) {
|
|
|
258
267
|
let commaIndex = -1;
|
|
259
268
|
for (let index = 0; index < jsonString.length; index++) {
|
|
260
269
|
const currentCharacter = jsonString[index];
|
|
261
|
-
const nextCharacter = jsonString[index + 1];
|
|
270
|
+
const nextCharacter = jsonString[index + 1] ?? "";
|
|
262
271
|
if (!isInsideComment && currentCharacter === '"') {
|
|
263
272
|
// Enter or exit string
|
|
264
273
|
const escaped = isEscaped(jsonString, index);
|
|
@@ -359,7 +368,8 @@ function startPipeline(pipeline, index, data, errfn) {
|
|
|
359
368
|
index = index == undefined ? 0 : index;
|
|
360
369
|
if (index == pipeline.length)
|
|
361
370
|
return;
|
|
362
|
-
pipeline[index]
|
|
371
|
+
const item = pipeline[index];
|
|
372
|
+
item(data).then((data) => startPipeline(pipeline, index + 1, data, errfn)).catch((err) => { if (errfn)
|
|
363
373
|
errfn(err); });
|
|
364
374
|
}
|
|
365
375
|
// ---------------------------
|
package/libs/utils.ts
CHANGED
|
@@ -6,9 +6,8 @@ import {readFile,writeFile} from 'fs/promises';
|
|
|
6
6
|
|
|
7
7
|
export let pipeline = { execute: execute }
|
|
8
8
|
|
|
9
|
-
export function log(...args) {
|
|
10
|
-
|
|
11
|
-
args.forEach(arg => res.push(typeof arg === "object" ? JSON.stringify(arg,null,'\t') : arg))
|
|
9
|
+
export function log(...args: unknown[]): void {
|
|
10
|
+
const res = args.map(arg => typeof arg === "object" ? JSON.stringify(arg, null,'\t') : arg)
|
|
12
11
|
console.log(...res)
|
|
13
12
|
}
|
|
14
13
|
|
|
@@ -32,21 +31,21 @@ export function uid(): string {
|
|
|
32
31
|
return buf.toString("hex");
|
|
33
32
|
}
|
|
34
33
|
|
|
35
|
-
export function isEmpty(str): boolean { // returns true for undefined
|
|
34
|
+
export function isEmpty(str: undefined | number | string): boolean { // returns true for undefined
|
|
36
35
|
if(!str)
|
|
37
36
|
return true
|
|
38
|
-
return str.match(/\S/) ? false : true // matches a non space character
|
|
37
|
+
return String(str).match(/\S/) ? false : true // matches a non space character
|
|
39
38
|
}
|
|
40
39
|
|
|
41
40
|
export let GLOBAL_META = { company: "GLOBAL", outlet: "GLOBAL" }
|
|
42
41
|
|
|
43
|
-
export function isEmptyJson(json): boolean {
|
|
42
|
+
export function isEmptyJson(json: object): boolean {
|
|
44
43
|
for (let i in json)
|
|
45
44
|
return false
|
|
46
45
|
return true
|
|
47
46
|
}
|
|
48
47
|
|
|
49
|
-
export function uniqueObjects(array, key) {
|
|
48
|
+
export function uniqueObjects<T extends Record<string, unknown>>(array: T[], key: keyof T): T[] {
|
|
50
49
|
// remove duplicates from array of objects with key
|
|
51
50
|
return [
|
|
52
51
|
...new Map(
|
|
@@ -99,7 +98,14 @@ export function toDate(arg: string, currentTime?: boolean): Date | undefined { /
|
|
|
99
98
|
|
|
100
99
|
let st = arg.split('/')
|
|
101
100
|
|
|
102
|
-
|
|
101
|
+
if (st.length !== 3)
|
|
102
|
+
throw new Error("Invalid arg in Utils.toDate()")
|
|
103
|
+
|
|
104
|
+
const day = st[0]!
|
|
105
|
+
const month = st[1]!
|
|
106
|
+
const year = st[2]!
|
|
107
|
+
|
|
108
|
+
let temp = new Date(parseInt(year), parseInt(month) - 1, parseInt(day));
|
|
103
109
|
temp.setHours(0) ; temp.setMinutes(0); temp.setSeconds(0); temp.setMilliseconds(0)
|
|
104
110
|
|
|
105
111
|
if (currentTime) {
|
|
@@ -117,8 +123,8 @@ export function toDate(arg: string, currentTime?: boolean): Date | undefined { /
|
|
|
117
123
|
|
|
118
124
|
|
|
119
125
|
|
|
120
|
-
export function getDatesBetweenTwoDates(start, end) {
|
|
121
|
-
let arr = [];
|
|
126
|
+
export function getDatesBetweenTwoDates(start: number | Date, end: number | Date) {
|
|
127
|
+
let arr: Date[] = [];
|
|
122
128
|
let dt = new Date(start);
|
|
123
129
|
|
|
124
130
|
dt.setHours(0)
|
|
@@ -133,45 +139,46 @@ export function getDatesBetweenTwoDates(start, end) {
|
|
|
133
139
|
return arr;
|
|
134
140
|
}
|
|
135
141
|
|
|
136
|
-
export function deepCopy(obj) {
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
export function formatAMPM(date) {
|
|
142
|
+
// export function deepCopy(obj) {
|
|
143
|
+
// if (Object.prototype.toString.call(obj) === '[object Array]') {
|
|
144
|
+
// let out = [], i = 0, len = obj.length;
|
|
145
|
+
// for ( ; i < len; i++ ) {
|
|
146
|
+
// if (Object.prototype.toString.call(obj[i]) === '[object Date]')
|
|
147
|
+
// out[i] = new Date(obj[i])
|
|
148
|
+
// else
|
|
149
|
+
// out[i] = arguments.callee(obj[i]);
|
|
150
|
+
// }
|
|
151
|
+
// return out;
|
|
152
|
+
// }
|
|
153
|
+
// if (typeof obj === 'object') {
|
|
154
|
+
// let out = {}, i;
|
|
155
|
+
// for ( i in obj ) {
|
|
156
|
+
// if (Object.prototype.toString.call(obj[i]) === '[object Date]')
|
|
157
|
+
// out[i] = new Date(obj[i])
|
|
158
|
+
// else
|
|
159
|
+
// out[i] = arguments.callee(obj[i]);
|
|
160
|
+
// }
|
|
161
|
+
// return out;
|
|
162
|
+
// }
|
|
163
|
+
// return obj;
|
|
164
|
+
// }
|
|
165
|
+
|
|
166
|
+
|
|
167
|
+
export function formatAMPM(date: Date): string {
|
|
162
168
|
let hours = date.getHours();
|
|
163
169
|
let minutes = date.getMinutes();
|
|
164
170
|
let ampm = hours >= 12 ? 'pm' : 'am';
|
|
165
171
|
hours = hours % 12;
|
|
166
172
|
hours = hours ? hours : 12; // the hour '0' should be '12'
|
|
167
|
-
|
|
168
|
-
let strTime = hours + ':' + minutes + ' ' + ampm;
|
|
173
|
+
let strTime = hours + ':' + (minutes < 10 ? '0'+minutes : minutes) + ' ' + ampm;
|
|
169
174
|
return strTime;
|
|
170
175
|
}
|
|
171
|
-
export function sum(array) { // Eg: sum([10,20,undefined, NaN, 40])
|
|
176
|
+
export function sum(array: Array<number | string | undefined>) { // Eg: sum([10,20,undefined, NaN, 40])
|
|
172
177
|
let res = 0
|
|
173
|
-
for (let i in array)
|
|
174
|
-
|
|
178
|
+
for (let i in array) {
|
|
179
|
+
const val = parseFloat(String(array[i] ?? 0))
|
|
180
|
+
res += val ? val : 0
|
|
181
|
+
}
|
|
175
182
|
return res;
|
|
176
183
|
}
|
|
177
184
|
|
|
@@ -219,12 +226,16 @@ export function addUTCHours(date: Date, hoursToAdd: number): Date {
|
|
|
219
226
|
return date
|
|
220
227
|
}
|
|
221
228
|
|
|
222
|
-
export function validityToMillisecs(start, validity) { // start is in ms
|
|
229
|
+
export function validityToMillisecs(start: number, validity: string) { // start is in ms
|
|
223
230
|
const now = new Date(start)
|
|
224
231
|
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
232
|
+
const parts = validity.trim().split(' ').filter(item => item.trim())
|
|
233
|
+
|
|
234
|
+
if (parts.length !== 2)
|
|
235
|
+
throw new Error("Invalid validity string in validityToUTCMillisecs()");
|
|
236
|
+
|
|
237
|
+
const count = parseIntValue(parts[0])
|
|
238
|
+
const period = parts[1]!.toLowerCase()
|
|
228
239
|
|
|
229
240
|
const timeline = {
|
|
230
241
|
year: () => addYears(now, count),
|
|
@@ -241,16 +252,20 @@ export function validityToMillisecs(start, validity) { // start is in ms
|
|
|
241
252
|
minutes: () => now.setMinutes(now.getMinutes() + count),
|
|
242
253
|
}
|
|
243
254
|
|
|
244
|
-
timeline[period]()
|
|
255
|
+
timeline[period as keyof typeof timeline]()
|
|
245
256
|
|
|
246
257
|
return now.getTime()
|
|
247
258
|
}
|
|
248
|
-
export function validityToUTCMillisecs(start, validity) { // start is in ms
|
|
259
|
+
export function validityToUTCMillisecs(start: number, validity: string) { // start is in ms
|
|
249
260
|
const now = new Date(start)
|
|
250
261
|
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
262
|
+
const parts = validity.trim().split(' ').filter(item => item.trim())
|
|
263
|
+
|
|
264
|
+
if (parts.length !== 2)
|
|
265
|
+
throw new Error("Invalid validity string in validityToUTCMillisecs()");
|
|
266
|
+
|
|
267
|
+
const count = parseIntValue(parts[0])
|
|
268
|
+
const period = parts[1]!.toLowerCase()
|
|
254
269
|
|
|
255
270
|
const timeline = {
|
|
256
271
|
year: () => addUTCYears(now, count),
|
|
@@ -267,7 +282,7 @@ export function validityToUTCMillisecs(start, validity) { // start is in ms
|
|
|
267
282
|
minutes: () => now.setUTCMinutes(now.getUTCMinutes() + count),
|
|
268
283
|
}
|
|
269
284
|
|
|
270
|
-
timeline[period]()
|
|
285
|
+
timeline[period as keyof typeof timeline]()
|
|
271
286
|
|
|
272
287
|
return now.getTime()
|
|
273
288
|
}
|
|
@@ -276,7 +291,7 @@ export function validityToUTCMillisecs(start, validity) { // start is in ms
|
|
|
276
291
|
// ----------------
|
|
277
292
|
// JSON
|
|
278
293
|
// ----------------
|
|
279
|
-
export async function fileToJson(path, file) {
|
|
294
|
+
export async function fileToJson(path: string | URL, file: string) {
|
|
280
295
|
let data = (await readFile(new URL(file, path).toString().replace('file://',''), 'utf8'))
|
|
281
296
|
// return JSON.parse((JSON as Record<string, any>).minify(data))
|
|
282
297
|
|
|
@@ -288,9 +303,9 @@ export async function fileToJson(path, file) {
|
|
|
288
303
|
const stripWithoutWhitespace = () => '';
|
|
289
304
|
|
|
290
305
|
// Replace all characters except ASCII spaces, tabs and line endings with regular spaces to ensure valid JSON output.
|
|
291
|
-
const stripWithWhitespace = (string, start, end?: number) => string.slice(start, end).replace(/[^ \t\r\n]/g, ' ');
|
|
306
|
+
const stripWithWhitespace = (string: string, start: number, end?: number): string => string.slice(start, end).replace(/[^ \t\r\n]/g, ' ');
|
|
292
307
|
|
|
293
|
-
const isEscaped = (jsonString, quotePosition) => {
|
|
308
|
+
const isEscaped = (jsonString: string, quotePosition: number): boolean => {
|
|
294
309
|
let index = quotePosition - 1;
|
|
295
310
|
let backslashCount = 0;
|
|
296
311
|
|
|
@@ -302,7 +317,7 @@ export async function fileToJson(path, file) {
|
|
|
302
317
|
return Boolean(backslashCount % 2);
|
|
303
318
|
};
|
|
304
319
|
|
|
305
|
-
function stripJsonComments(jsonString, {whitespace = true, trailingCommas = false} = {}) {
|
|
320
|
+
function stripJsonComments(jsonString: string, {whitespace = true, trailingCommas = false} = {}): string {
|
|
306
321
|
if (typeof jsonString !== 'string') {
|
|
307
322
|
throw new TypeError(`Expected argument \`jsonString\` to be a \`string\`, got \`${typeof jsonString}\``);
|
|
308
323
|
}
|
|
@@ -318,8 +333,8 @@ export async function fileToJson(path, file) {
|
|
|
318
333
|
let commaIndex = -1;
|
|
319
334
|
|
|
320
335
|
for (let index = 0; index < jsonString.length; index++) {
|
|
321
|
-
const currentCharacter = jsonString[index]
|
|
322
|
-
const nextCharacter = jsonString[index + 1];
|
|
336
|
+
const currentCharacter = jsonString[index]!;
|
|
337
|
+
const nextCharacter = jsonString[index + 1] ?? "";
|
|
323
338
|
|
|
324
339
|
if (!isInsideComment && currentCharacter === '"') {
|
|
325
340
|
// Enter or exit string
|
|
@@ -406,10 +421,10 @@ export async function fileToJson(path, file) {
|
|
|
406
421
|
// File
|
|
407
422
|
// ----------------
|
|
408
423
|
|
|
409
|
-
export async function fileToString(file) {
|
|
424
|
+
export async function fileToString(file: string | URL): Promise<string> {
|
|
410
425
|
return await readFile(file, 'utf8')
|
|
411
426
|
}
|
|
412
|
-
export async function stringToFile(file, data) {
|
|
427
|
+
export async function stringToFile(file: string, data: string) {
|
|
413
428
|
return await writeFile(file, data)
|
|
414
429
|
}
|
|
415
430
|
|
|
@@ -418,23 +433,25 @@ export async function stringToFile(file, data) {
|
|
|
418
433
|
// ----------------
|
|
419
434
|
|
|
420
435
|
|
|
421
|
-
function execute(pipeline
|
|
422
|
-
startPipeline(pipeline, 0, undefined, errfn)
|
|
436
|
+
function execute<T>(pipeline: Array<(data: T) => Promise<T>>, errfn?: (err: unknown) => void) {
|
|
437
|
+
startPipeline(pipeline, 0, undefined as T, errfn)
|
|
423
438
|
}
|
|
424
439
|
|
|
425
|
-
function startPipeline(pipeline
|
|
440
|
+
function startPipeline<T>(pipeline: Array<(data: T) => Promise<T>>, index: number, data: T, errfn?: (err: unknown) => void) {
|
|
426
441
|
index = index == undefined ? 0 : index
|
|
427
442
|
if (index == pipeline.length)
|
|
428
443
|
return
|
|
429
|
-
|
|
444
|
+
|
|
445
|
+
const item = pipeline[index]
|
|
446
|
+
item!(data).then((data: T) => startPipeline(pipeline, index + 1, data, errfn)).catch((err: unknown) => { if (errfn) errfn(err) } )
|
|
430
447
|
}
|
|
431
448
|
|
|
432
449
|
|
|
433
450
|
// ---------------------------
|
|
434
451
|
// Express Middleware Exclude
|
|
435
452
|
// ---------------------------
|
|
436
|
-
export const excludeMiddleware = function(middleware, ...paths) {
|
|
437
|
-
return function(req, res, next) {
|
|
453
|
+
export const excludeMiddleware = function(middleware: (...args: unknown[]) => void, ...paths: string[]) {
|
|
454
|
+
return function(req: {path: string}, res: unknown, next: () => void) {
|
|
438
455
|
const pathCheck = paths.some(path => {
|
|
439
456
|
let url = req.path.split('/')
|
|
440
457
|
let cmp = path.split('/')
|
package/package.json
CHANGED