@kivacore/app-utils 0.0.0 → 0.22.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/README.md +1515 -1
- package/dist/index.d.ts +82 -0
- package/dist/index.js +2304 -0
- package/dist/index.js.map +1 -0
- package/dist/index.native.js +4695 -0
- package/dist/index.native.js.map +1 -0
- package/dist/query.d.ts +54 -0
- package/dist/query.js +269 -0
- package/dist/query.js.map +1 -0
- package/dist/types-GNkXRpu4.d.ts +754 -0
- package/package.json +57 -3
- package/src/app-config-client.ts +48 -0
- package/src/async-each-helper.ts +98 -0
- package/src/brand-client.ts +80 -0
- package/src/data-form-client.ts +85 -0
- package/src/data-source-client.ts +298 -0
- package/src/data-source-queries.ts +149 -0
- package/src/data-view-client.ts +92 -0
- package/src/date-utils.test.ts +48 -0
- package/src/date-utils.ts +146 -0
- package/src/envelope-interceptor.ts +65 -0
- package/src/get-tenant-preferences.test.ts +42 -0
- package/src/get-tenant-preferences.ts +30 -0
- package/src/handlebars-async-helpers.d.ts +12 -0
- package/src/index.native.ts +9 -0
- package/src/index.ts +88 -0
- package/src/inventory-client.ts +725 -0
- package/src/jsonata-helpers.ts +545 -0
- package/src/number-utils.ts +305 -0
- package/src/php-date-formatter.d.ts +8 -0
- package/src/query.ts +5 -0
- package/src/shims/stream.ts +11 -0
- package/src/template-engine.test.ts +47 -0
- package/src/template-engine.ts +298 -0
- package/src/types.ts +871 -0
- package/src/unwrap-envelope.ts +51 -0
- package/src/user-app-config-client.ts +44 -0
- package/src/user-identity-client.ts +21 -0
- package/tsconfig.json +9 -0
- package/tsup.config.ts +71 -0
- package/vitest.config.mts +13 -0
|
@@ -0,0 +1,545 @@
|
|
|
1
|
+
import {
|
|
2
|
+
addYears,
|
|
3
|
+
addMonths,
|
|
4
|
+
addWeeks,
|
|
5
|
+
addDays,
|
|
6
|
+
addHours,
|
|
7
|
+
addMinutes,
|
|
8
|
+
addSeconds,
|
|
9
|
+
addMilliseconds,
|
|
10
|
+
subYears,
|
|
11
|
+
subMonths,
|
|
12
|
+
subWeeks,
|
|
13
|
+
subDays,
|
|
14
|
+
subHours,
|
|
15
|
+
subMinutes,
|
|
16
|
+
subSeconds,
|
|
17
|
+
subMilliseconds,
|
|
18
|
+
isAfter,
|
|
19
|
+
isBefore,
|
|
20
|
+
isEqual as isDatesEqual,
|
|
21
|
+
isPast,
|
|
22
|
+
differenceInYears,
|
|
23
|
+
differenceInMonths,
|
|
24
|
+
differenceInWeeks,
|
|
25
|
+
differenceInDays,
|
|
26
|
+
differenceInHours,
|
|
27
|
+
differenceInMinutes,
|
|
28
|
+
differenceInSeconds,
|
|
29
|
+
differenceInBusinessDays,
|
|
30
|
+
format as fnsFormat,
|
|
31
|
+
formatDistance,
|
|
32
|
+
formatDistanceStrict,
|
|
33
|
+
formatDistanceToNow,
|
|
34
|
+
formatDistanceToNowStrict,
|
|
35
|
+
formatDuration,
|
|
36
|
+
formatISO,
|
|
37
|
+
formatRelative,
|
|
38
|
+
startOfDay,
|
|
39
|
+
startOfMonth,
|
|
40
|
+
startOfWeek,
|
|
41
|
+
startOfToday,
|
|
42
|
+
startOfQuarter,
|
|
43
|
+
endOfDay,
|
|
44
|
+
endOfMonth,
|
|
45
|
+
endOfWeek,
|
|
46
|
+
endOfToday,
|
|
47
|
+
endOfQuarter
|
|
48
|
+
} from 'date-fns';
|
|
49
|
+
|
|
50
|
+
/*
|
|
51
|
+
* ---------------------------------------------------------------------------
|
|
52
|
+
* Internal helpers
|
|
53
|
+
* ---------------------------------------------------------------------------
|
|
54
|
+
*/
|
|
55
|
+
|
|
56
|
+
function dateWrapper<T extends (...args: any[]) => any>(fn: T): T {
|
|
57
|
+
return ((...args: any[]) => {
|
|
58
|
+
if (args[0] == null || args[0] === '') return null;
|
|
59
|
+
if (typeof args[0] === 'string') args[0] = new Date(args[0]);
|
|
60
|
+
|
|
61
|
+
return fn(...args);
|
|
62
|
+
}) as T;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/*
|
|
66
|
+
* ---------------------------------------------------------------------------
|
|
67
|
+
* Format helpers (ported from @docyrus/jsonata-helpers formatFunctions)
|
|
68
|
+
* ---------------------------------------------------------------------------
|
|
69
|
+
*/
|
|
70
|
+
|
|
71
|
+
function formatDecimal(...args: any[]): string {
|
|
72
|
+
if (!args || args.length < 1) {
|
|
73
|
+
throw new Error('Missing required arguments.');
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const numberValue = args[0] as number;
|
|
77
|
+
|
|
78
|
+
if (typeof numberValue !== 'number' || isNaN(numberValue) || !isFinite(numberValue)) {
|
|
79
|
+
throw new Error('The first argument must be a valid number.');
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
if (typeof args[1] === 'string' && (args[1] as string).includes('-')) {
|
|
83
|
+
return new Intl.NumberFormat(args[1] as string).format(numberValue);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const decimalPrecision = (args[1] as number | undefined) ?? 2;
|
|
87
|
+
const decimalSeparator = (args[2] as string | undefined) || '.';
|
|
88
|
+
const thousandsSeparator = (args[3] as string | undefined) || (decimalSeparator === ',' ? '.' : ',');
|
|
89
|
+
|
|
90
|
+
if (!Number.isInteger(decimalPrecision)) {
|
|
91
|
+
throw new Error('Invalid decimal precision.');
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const validSeparators = ['.', ','];
|
|
95
|
+
|
|
96
|
+
if (
|
|
97
|
+
decimalSeparator === thousandsSeparator
|
|
98
|
+
|| !validSeparators.includes(decimalSeparator)
|
|
99
|
+
|| !validSeparators.includes(thousandsSeparator)
|
|
100
|
+
) {
|
|
101
|
+
throw new Error('Invalid separators.');
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
const integerPart = Math.floor(numberValue);
|
|
105
|
+
const decimalPart = Math.abs(numberValue - integerPart);
|
|
106
|
+
const integerString = integerPart.toString().replace(/\B(?=(\d{3})+(?!\d))/g, thousandsSeparator);
|
|
107
|
+
const decimalString = decimalPart.toFixed(decimalPrecision).slice(2);
|
|
108
|
+
|
|
109
|
+
return decimalPrecision > 0 ? `${integerString}${decimalSeparator}${decimalString}` : integerString;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function formatMoney(...args: any[]): string {
|
|
113
|
+
if (!args || args.length < 1) {
|
|
114
|
+
throw new Error('Missing required arguments.');
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
const numberValue = args[0] as number;
|
|
118
|
+
|
|
119
|
+
if (typeof numberValue !== 'number' || isNaN(numberValue) || !isFinite(numberValue)) {
|
|
120
|
+
throw new Error('The first argument must be a valid number.');
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
if (typeof args[1] === 'string' && (args[1] as string).includes('-')) {
|
|
124
|
+
return new Intl.NumberFormat(args[1] as string, {
|
|
125
|
+
style: 'currency',
|
|
126
|
+
currency: args[2] as string
|
|
127
|
+
}).format(numberValue);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
const currency = (args[4] as string | undefined) || '$';
|
|
131
|
+
const currencyPosition = (args[5] as string | undefined) || 'start';
|
|
132
|
+
|
|
133
|
+
if (currencyPosition !== 'start' && currencyPosition !== 'end') {
|
|
134
|
+
throw new Error("Invalid currency position. It must be 'start' or 'end'.");
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
const formattedNumber = formatDecimal(numberValue, args[1], args[2], args[3]);
|
|
138
|
+
|
|
139
|
+
return currencyPosition === 'start' ? `${currency}${formattedNumber}` : `${formattedNumber}${currency}`;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
function percentage(...args: any[]): string {
|
|
143
|
+
const pct = ((args[0] as number) / (args[1] as number)) * 100;
|
|
144
|
+
|
|
145
|
+
return pct.toFixed(args[2] as number);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
function truncate(...args: any[]): string {
|
|
149
|
+
const text = args[0] as string;
|
|
150
|
+
const limit = args[1] as number;
|
|
151
|
+
|
|
152
|
+
return text.length > limit ? `${text.substring(0, limit)}...` : text;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
function join(...args: any[]): string {
|
|
156
|
+
return (args[0] as any[]).join(args[1] as string);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
function formatDurationInSeconds(durationInSeconds: number): string {
|
|
160
|
+
return formatDuration({
|
|
161
|
+
hours: Math.floor(durationInSeconds / 3600),
|
|
162
|
+
minutes: Math.floor((durationInSeconds % 3600) / 60),
|
|
163
|
+
seconds: durationInSeconds % 60
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/*
|
|
168
|
+
* ---------------------------------------------------------------------------
|
|
169
|
+
* String / misc helpers (ported from @docyrus/jsonata-helpers)
|
|
170
|
+
* ---------------------------------------------------------------------------
|
|
171
|
+
*/
|
|
172
|
+
|
|
173
|
+
async function sha256(message: string): Promise<string> {
|
|
174
|
+
if (!message) return '';
|
|
175
|
+
|
|
176
|
+
const msgUint8 = new TextEncoder().encode(message);
|
|
177
|
+
const hashBuffer = await crypto.subtle.digest('SHA-256', msgUint8);
|
|
178
|
+
const hashArray = Array.from(new Uint8Array(hashBuffer));
|
|
179
|
+
|
|
180
|
+
return hashArray.map(b => b.toString(16).padStart(2, '0')).join('');
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
function ascii(str: string): string {
|
|
184
|
+
if (!str) return '';
|
|
185
|
+
|
|
186
|
+
let s = str.replace(/^\s+|\s+$/g, '');
|
|
187
|
+
const from = 'ÁÄÂÀÃÅČÇĆĎÉĚËÈÊẼĔȆĞÍÌÎÏİŇÑÓÖÒÔÕØŘŔŠŞŤÚŮÜÙÛÝŸŽáäâàãåčçćďéěëèêẽĕȇğíìîïıňñóöòôõøðřŕšşťúůüùûýÿžþÞĐđ߯a';
|
|
188
|
+
const to = 'AAAAAACCCDEEEEEEEEGIIIIINNOOOOOORRSSTUUUUUYYZaaaaaacccdeeeeeeeegiiiiinnooooooorrsstuuuuuyyzbBDdBAa';
|
|
189
|
+
|
|
190
|
+
for (let i = 0; i < from.length; i++) {
|
|
191
|
+
s = s.replace(new RegExp(from.charAt(i), 'g'), to.charAt(i));
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
return s.replace(/[^a-zA-Z0-9\s\-.@]/g, '');
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
function extractNameFromEmail(email: string, nameOriginal?: string): string {
|
|
198
|
+
const localPart = typeof email === 'string' ? email.split('@')[0] : '';
|
|
199
|
+
|
|
200
|
+
function capitalize(s: string): string {
|
|
201
|
+
return s.length === 0 ? '' : s.charAt(0).toLocaleUpperCase() + s.slice(1).toLowerCase();
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
function clean(s: string): string {
|
|
205
|
+
return capitalize(s.replace(/[^a-zA-Z]/g, ''));
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
if (localPart) {
|
|
209
|
+
if (localPart.includes('.')) {
|
|
210
|
+
const parts = localPart.split('.');
|
|
211
|
+
|
|
212
|
+
return `${clean(parts[0])} ${clean(parts[1])}`;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
return `${clean(localPart)} `;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
return nameOriginal || '';
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
function slug(str: string, separator = '-'): string {
|
|
222
|
+
let s = str.replace(/^\s+|\s+$/g, '').toLowerCase();
|
|
223
|
+
|
|
224
|
+
const from = 'ÁÄÂÀÃÅČÇĆĎÉĚËÈÊẼĔȆĞÍÌÎÏİŇÑÓÖÒÔÕØŘŔŠŞŤÚŮÜÙÛÝŸŽáäâàãåčçćďéěëèêẽĕȇğíìîïıňñóöòôõøðřŕšşťúůüùûýÿžþÞĐđ߯a·/_,:;';
|
|
225
|
+
const to = 'AAAAAACCCDEEEEEEEEGIIIIINNOOOOOORRSSTUUUUUYYZaaaaaacccdeeeeeeeegiiiiinnooooooorrsstuuuuuyyzbBDdBAa------';
|
|
226
|
+
|
|
227
|
+
for (let i = 0; i < from.length; i++) {
|
|
228
|
+
s = s.replace(new RegExp(from.charAt(i), 'g'), to.charAt(i));
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
return s
|
|
232
|
+
.replace(/[^a-z0-9 -]/g, '')
|
|
233
|
+
.replace(/\s+/g, separator)
|
|
234
|
+
.replace(/-+/g, separator);
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
function filterEmpty(arr: any[]): any[] {
|
|
238
|
+
if (arr && arr.filter) {
|
|
239
|
+
return arr.filter(el => el != null && el !== '');
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
return arr;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
function padLeft(value: any, totalLength: number, padChar = '0'): string {
|
|
246
|
+
let s = String(value);
|
|
247
|
+
|
|
248
|
+
while (s.length < totalLength) {
|
|
249
|
+
s = padChar + s;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
return s;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
function padRight(value: any, totalLength: number, padChar = '0'): string {
|
|
256
|
+
let s = String(value);
|
|
257
|
+
|
|
258
|
+
while (s.length < totalLength) {
|
|
259
|
+
s = s + padChar;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
return s;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
function ifNull(value: any, alternativeValue: any): any {
|
|
266
|
+
return value === null || value === undefined ? alternativeValue : value;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
function objectToString(objectValue: any): string {
|
|
270
|
+
let result = '';
|
|
271
|
+
|
|
272
|
+
if (objectValue instanceof Object) {
|
|
273
|
+
for (const [key, value] of Object.entries(objectValue)) {
|
|
274
|
+
result = `${key}:${value}`;
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
return result;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
/*
|
|
282
|
+
* ---------------------------------------------------------------------------
|
|
283
|
+
* Equality helpers (ported from @docyrus/utils)
|
|
284
|
+
* ---------------------------------------------------------------------------
|
|
285
|
+
*/
|
|
286
|
+
|
|
287
|
+
function isEqual(a: any, b: any): boolean {
|
|
288
|
+
if (a == null && b == null) return true;
|
|
289
|
+
if (a == null || b == null) return false;
|
|
290
|
+
|
|
291
|
+
if (a instanceof Date || b instanceof Date) {
|
|
292
|
+
const da = typeof a === 'string' ? new Date(a) : a;
|
|
293
|
+
const db = typeof b === 'string' ? new Date(b) : b;
|
|
294
|
+
|
|
295
|
+
return da.getTime() === db.getTime();
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
if (typeof a === 'object' && typeof b === 'object') {
|
|
299
|
+
if (a.id && b.id) return a.id === b.id;
|
|
300
|
+
|
|
301
|
+
return JSON.stringify(a) === JSON.stringify(b);
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
if (typeof a === 'object' && a.id && (typeof b === 'string' || typeof b === 'number')) {
|
|
305
|
+
return a.id === b;
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
if ((typeof a === 'string' || typeof a === 'number') && typeof b === 'object' && b.id) {
|
|
309
|
+
return a === b.id;
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
return a === b;
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
function isEqualOrContained(needle: any, haystack: any): boolean {
|
|
316
|
+
if (Array.isArray(haystack)) {
|
|
317
|
+
return haystack.some(item => isEqual(item, needle));
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
if (typeof needle === 'object' && typeof haystack === 'object' && needle?.id && haystack?.id) {
|
|
321
|
+
return needle.id === haystack.id;
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
return isEqual(needle, haystack);
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
function getDbValue(value: any): any {
|
|
328
|
+
if (typeof value === 'object' && value !== null) {
|
|
329
|
+
if (value.id) return value.id;
|
|
330
|
+
if (Array.isArray(value) && value.length > 0 && value[0]?.id) return value[0].id;
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
return value;
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
/*
|
|
337
|
+
* ---------------------------------------------------------------------------
|
|
338
|
+
* Business days custom (ported from @docyrus/jsonata-helpers)
|
|
339
|
+
* ---------------------------------------------------------------------------
|
|
340
|
+
*/
|
|
341
|
+
|
|
342
|
+
interface BusinessDayOptions {
|
|
343
|
+
workStartHour?: number;
|
|
344
|
+
workEndHour?: number;
|
|
345
|
+
lunchBreakHours?: number;
|
|
346
|
+
lunchStartHour?: number;
|
|
347
|
+
lunchEndHour?: number;
|
|
348
|
+
includePartialDays?: boolean;
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
function differenceInBusinessDaysCustom(
|
|
352
|
+
startDate: string | Date | null,
|
|
353
|
+
endDate: string | Date | null,
|
|
354
|
+
options: BusinessDayOptions = {}
|
|
355
|
+
): number | null {
|
|
356
|
+
if (!startDate || !endDate) return null;
|
|
357
|
+
|
|
358
|
+
const start = new Date(startDate as any);
|
|
359
|
+
const end = new Date(endDate as any);
|
|
360
|
+
|
|
361
|
+
const config = {
|
|
362
|
+
workStartHour: 9,
|
|
363
|
+
workEndHour: 18,
|
|
364
|
+
lunchBreakHours: 1,
|
|
365
|
+
lunchStartHour: 12.5,
|
|
366
|
+
lunchEndHour: 13.5,
|
|
367
|
+
includePartialDays: true,
|
|
368
|
+
...options
|
|
369
|
+
};
|
|
370
|
+
|
|
371
|
+
const workingHoursPerDay = config.workEndHour - config.workStartHour - config.lunchBreakHours;
|
|
372
|
+
const businessDays = differenceInBusinessDays(end, start);
|
|
373
|
+
|
|
374
|
+
if (!config.includePartialDays) return businessDays;
|
|
375
|
+
|
|
376
|
+
if (businessDays > 0) {
|
|
377
|
+
const startHour = start.getHours() + (start.getMinutes() / 60);
|
|
378
|
+
const endHour = end.getHours() + (end.getMinutes() / 60);
|
|
379
|
+
|
|
380
|
+
if (
|
|
381
|
+
(startHour < config.workStartHour || startHour > config.workEndHour)
|
|
382
|
+
&& (endHour < config.workStartHour || endHour > config.workEndHour)
|
|
383
|
+
) {
|
|
384
|
+
return 0;
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
let firstDayHours = 0;
|
|
388
|
+
|
|
389
|
+
if (startHour >= config.workStartHour && startHour <= config.workEndHour) {
|
|
390
|
+
firstDayHours = config.workEndHour - startHour;
|
|
391
|
+
|
|
392
|
+
if (startHour < config.lunchStartHour) {
|
|
393
|
+
firstDayHours -= config.lunchBreakHours;
|
|
394
|
+
} else if (startHour >= config.lunchStartHour && startHour < config.lunchEndHour) {
|
|
395
|
+
firstDayHours -= config.lunchEndHour - startHour;
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
let lastDayHours = 0;
|
|
400
|
+
|
|
401
|
+
if (endHour >= config.workStartHour) {
|
|
402
|
+
const effectiveEndHour = Math.min(endHour, config.workEndHour);
|
|
403
|
+
|
|
404
|
+
lastDayHours = effectiveEndHour - config.workStartHour;
|
|
405
|
+
|
|
406
|
+
if (effectiveEndHour > config.lunchEndHour) {
|
|
407
|
+
lastDayHours -= config.lunchBreakHours;
|
|
408
|
+
} else if (effectiveEndHour > config.lunchStartHour && effectiveEndHour <= config.lunchEndHour) {
|
|
409
|
+
lastDayHours -= effectiveEndHour - config.lunchStartHour;
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
const adjustedBusinessDays = businessDays - 1;
|
|
414
|
+
const totalExtraHours = firstDayHours + lastDayHours;
|
|
415
|
+
const hourDecimal = Math.max(0, totalExtraHours) / workingHoursPerDay;
|
|
416
|
+
|
|
417
|
+
return Math.round((adjustedBusinessDays + hourDecimal) * 1000) / 1000;
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
// Same-day calculation
|
|
421
|
+
const startHour = start.getHours() + (start.getMinutes() / 60);
|
|
422
|
+
const endHour = end.getHours() + (end.getMinutes() / 60);
|
|
423
|
+
|
|
424
|
+
if (businessDays === 0) {
|
|
425
|
+
const dayOfWeek = start.getDay();
|
|
426
|
+
|
|
427
|
+
if (dayOfWeek === 0 || dayOfWeek === 6) return 0;
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
if (businessDays === 0 && (startHour > config.workEndHour || endHour < config.workStartHour)) {
|
|
431
|
+
return 0;
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
let workingHours = Math.max(0, endHour - startHour);
|
|
435
|
+
|
|
436
|
+
if (startHour < config.lunchEndHour && endHour > config.lunchStartHour) {
|
|
437
|
+
const lunchOverlap = Math.min(endHour, config.lunchEndHour) - Math.max(startHour, config.lunchStartHour);
|
|
438
|
+
|
|
439
|
+
workingHours -= Math.max(0, lunchOverlap);
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
return Math.round(Math.max(0, workingHours / workingHoursPerDay) * 1000) / 1000;
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
/*
|
|
446
|
+
* ---------------------------------------------------------------------------
|
|
447
|
+
* Wrap all date functions
|
|
448
|
+
* ---------------------------------------------------------------------------
|
|
449
|
+
*/
|
|
450
|
+
|
|
451
|
+
const dateFunctions: Record<string, any> = {
|
|
452
|
+
formatDate: fnsFormat,
|
|
453
|
+
formatDistance,
|
|
454
|
+
formatDistanceStrict,
|
|
455
|
+
formatDistanceToNow,
|
|
456
|
+
formatDistanceToNowStrict,
|
|
457
|
+
formatDuration,
|
|
458
|
+
formatISO,
|
|
459
|
+
formatRelative,
|
|
460
|
+
addYears,
|
|
461
|
+
addMonths,
|
|
462
|
+
addWeeks,
|
|
463
|
+
addDays,
|
|
464
|
+
addHours,
|
|
465
|
+
addMinutes,
|
|
466
|
+
addSeconds,
|
|
467
|
+
addMilliseconds,
|
|
468
|
+
subYears,
|
|
469
|
+
subMonths,
|
|
470
|
+
subWeeks,
|
|
471
|
+
subDays,
|
|
472
|
+
subHours,
|
|
473
|
+
subMinutes,
|
|
474
|
+
subSeconds,
|
|
475
|
+
subMilliseconds,
|
|
476
|
+
isAfter,
|
|
477
|
+
isBefore,
|
|
478
|
+
isDatesEqual,
|
|
479
|
+
isPast,
|
|
480
|
+
differenceInYears,
|
|
481
|
+
differenceInMonths,
|
|
482
|
+
differenceInWeeks,
|
|
483
|
+
differenceInDays,
|
|
484
|
+
differenceInHours,
|
|
485
|
+
differenceInMinutes,
|
|
486
|
+
differenceInSeconds,
|
|
487
|
+
differenceInBusinessDays,
|
|
488
|
+
differenceInBusinessDaysCustom,
|
|
489
|
+
startOfDay,
|
|
490
|
+
startOfMonth,
|
|
491
|
+
startOfWeek,
|
|
492
|
+
startOfToday,
|
|
493
|
+
startOfQuarter,
|
|
494
|
+
endOfDay,
|
|
495
|
+
endOfMonth,
|
|
496
|
+
endOfWeek,
|
|
497
|
+
endOfToday,
|
|
498
|
+
endOfQuarter
|
|
499
|
+
};
|
|
500
|
+
|
|
501
|
+
for (const key of Object.keys(dateFunctions)) {
|
|
502
|
+
dateFunctions[key] = dateWrapper(dateFunctions[key]);
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
/*
|
|
506
|
+
* ---------------------------------------------------------------------------
|
|
507
|
+
* Export
|
|
508
|
+
* ---------------------------------------------------------------------------
|
|
509
|
+
*/
|
|
510
|
+
|
|
511
|
+
export const jsonataHelpers: Record<string, any> = {
|
|
512
|
+
...dateFunctions,
|
|
513
|
+
|
|
514
|
+
// Format functions
|
|
515
|
+
formatDecimal,
|
|
516
|
+
formatMoney,
|
|
517
|
+
join,
|
|
518
|
+
truncate,
|
|
519
|
+
percentage,
|
|
520
|
+
formatDurationInSeconds,
|
|
521
|
+
|
|
522
|
+
// String / misc
|
|
523
|
+
sha256,
|
|
524
|
+
ascii,
|
|
525
|
+
extractNameFromEmail,
|
|
526
|
+
slug,
|
|
527
|
+
filterEmpty,
|
|
528
|
+
startsWith: (haystack: string, needle: string) => haystack.startsWith(needle),
|
|
529
|
+
endsWith: (haystack: string, needle: string) => haystack.endsWith(needle),
|
|
530
|
+
includes: (haystack: string | null, needle: string | null) => {
|
|
531
|
+
if (!haystack || !needle) return false;
|
|
532
|
+
|
|
533
|
+
return haystack.includes(needle);
|
|
534
|
+
},
|
|
535
|
+
objectToString,
|
|
536
|
+
padLeft,
|
|
537
|
+
padRight,
|
|
538
|
+
ifNull,
|
|
539
|
+
|
|
540
|
+
// Equality / DB helpers
|
|
541
|
+
isEqual,
|
|
542
|
+
isEqualOrContained,
|
|
543
|
+
isNotEqualOrContained: (needle: any, haystack: any) => !isEqualOrContained(needle, haystack),
|
|
544
|
+
getDbValue
|
|
545
|
+
};
|