quasar-ui-danx 0.4.91 → 0.4.92

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.
@@ -1,489 +1 @@
1
- import { DateTime, IANAZone } from "luxon";
2
- import { parse as parseYAML, stringify as stringifyYAML } from "yaml";
3
- import { ActionTargetItem, fDateOptions } from "../types";
4
- import { isJSON } from "./utils";
5
-
6
- const SERVER_TZ = new IANAZone("America/Chicago");
7
-
8
- export { DateTime, SERVER_TZ };
9
-
10
- /**
11
- * Converts a date string from the server's time zone to the user's time zone.
12
- * @param {String} dateTimeString
13
- * @returns {DateTime}
14
- */
15
- export function localizedDateTime(dateTimeString: string) {
16
- dateTimeString = dateTimeString?.replace("T", " ");
17
- // noinspection JSCheckFunctionSignatures
18
- return DateTime.fromSQL(dateTimeString, { zone: SERVER_TZ }).setZone("local");
19
- }
20
-
21
- /**
22
- * Converts a date string from the user's time zone to the server's time zone.
23
- * @param dateTimeString
24
- * @returns {DateTime}
25
- */
26
- export function remoteDateTime(dateTimeString: string) {
27
- dateTimeString = dateTimeString?.replace("T", " ");
28
- // noinspection JSCheckFunctionSignatures
29
- return DateTime.fromSQL(dateTimeString, { zone: "local" }).setZone(SERVER_TZ);
30
- }
31
-
32
- /**
33
- * Formats a Luxon DateTime object into a Quasar formatted date string
34
- * @param date
35
- * @returns {string}
36
- */
37
- export function fQDate(date: string) {
38
- return fDate(date, { format: "yyyy/MM/dd" });
39
- }
40
-
41
- /**
42
- *
43
- * @param {String} dateTimeString
44
- * @param options
45
- * @returns {string}
46
- */
47
- export function fLocalizedDateTime(dateTimeString: string, options = {}) {
48
- return fDateTime(localizedDateTime(dateTimeString), options);
49
- }
50
-
51
- /**
52
- * Formats a date/time object or string into a human-readable format
53
- *
54
- * @param {String|Object} dateTime
55
- * @param format
56
- * @param {String|null} empty
57
- * @returns {string}
58
- */
59
- export function fDateTime(
60
- dateTime: string | DateTime | null = null,
61
- { format = "M/d/yy h:mma", empty = "- -" }: fDateOptions = {}
62
- ) {
63
- const formatted = parseDateTime(dateTime)?.toFormat(format).toLowerCase();
64
- return formatted || empty;
65
- }
66
-
67
- export function fDateTimeMs(
68
- dateTime: string | DateTime | null = null,
69
- { empty = "- -" }: fDateOptions = {}
70
- ) {
71
- const formatted = parseDateTime(dateTime)?.toFormat("M/d/yy H:mm:ss.SSS").toLowerCase();
72
- return formatted || empty;
73
- }
74
-
75
- /**
76
- * Formats a date/time object or string into the best format for DB input
77
- * @param dateTime
78
- * @returns {string}
79
- */
80
- export function dbDateTime(dateTime: string | DateTime | null = null) {
81
- return fDateTime(dateTime, { format: "yyyy-MM-dd HH:mm:ss", empty: undefined });
82
- }
83
-
84
- /**
85
- * Formats a date object or string into a human-readable format
86
- * @param {String|Object} dateTime
87
- * @param {String|null} empty
88
- * @param format
89
- * @returns {string}
90
- */
91
- export function fDate(dateTime: string | DateTime | null, { empty = "--", format = "M/d/yy" }: fDateOptions = {}) {
92
- const formatted = parseDateTime(dateTime)?.toFormat(format || "M/d/yy");
93
- return formatted || empty;
94
- }
95
-
96
-
97
- /**
98
- * Parses a date string into a Luxon DateTime object
99
- */
100
- export function parseDateTime(dateTime: string | DateTime | number | null): DateTime<boolean> | null {
101
- if (typeof dateTime === "number") {
102
- return DateTime.fromMillis(dateTime as number);
103
- }
104
- if (typeof dateTime === "string") {
105
- return parseGenericDateTime(dateTime);
106
- }
107
- return dateTime as DateTime<boolean> || DateTime.fromSQL("0000-00-00 00:00:00");
108
- }
109
-
110
- /**
111
- * Parses a SQL formatted date string into a Luxon DateTime object
112
- */
113
- export function parseSqlDateTime(dateTime: string) {
114
- const parsed = DateTime.fromSQL(dateTime.replace("T", " ").replace(/\//g, "-"));
115
- return parsed.isValid ? parsed : null;
116
- }
117
-
118
- /**
119
- * Parses a Quasar formatted date string into a Luxon DateTime object
120
- */
121
- export function parseQDate(date: string, format = "yyyy/MM/dd"): DateTime<boolean> | null {
122
- const parsed = DateTime.fromFormat(date, format);
123
- return parsed.isValid ? parsed : null;
124
- }
125
-
126
- /**
127
- * Parses a Quasar formatted date/time string into a Luxon DateTime object
128
- */
129
- export function parseQDateTime(date: string, format = "yyyy/MM/dd HH:mm:ss"): DateTime<boolean> | null {
130
- const parsed = DateTime.fromFormat(date, format);
131
- return parsed.isValid ? parsed : null;
132
- }
133
-
134
- /**
135
- * Parses a date string in various formats into a Luxon DateTime object.
136
- * Tries a list of common formats until one works.
137
- *
138
- * @param {string} dateTimeString - The date string to parse.
139
- * @param {string} [defaultZone="local"] - The default time zone to use if not specified.
140
- * @returns {DateTime | null} - A Luxon DateTime object if parsing succeeds, otherwise null.
141
- */
142
- export function parseGenericDateTime(dateTimeString: string, defaultZone = "local"): DateTime | null {
143
- if (!dateTimeString) return null;
144
-
145
- const formats = [
146
- "yyyy-MM-dd", // ISO date
147
- "yyyy-MM-dd HH:mm:ss", // ISO date with time
148
- "MM/dd/yyyy", // US-style date
149
- "dd/MM/yyyy", // European-style date
150
- "MM/dd/yy", // US short date
151
- "dd/MM/yy", // European short date
152
- "yyyy/MM/dd", // Alternative ISO
153
- "MM-dd-yyyy", // US with dashes
154
- "dd-MM-yyyy", // European with dashes
155
- "M/d/yyyy", // US date without leading zeros
156
- "d/M/yyyy", // European date without leading zeros
157
- "yyyyMMdd" // Compact ISO
158
- ];
159
-
160
- for (const format of formats) {
161
- const parsed = DateTime.fromFormat(dateTimeString, format, { zone: defaultZone });
162
- if (parsed.isValid) {
163
- return parsed;
164
- }
165
- }
166
-
167
- // Fallback to ISO parsing for strings like "2022-11-18T10:10:10Z"
168
- const isoParsed = DateTime.fromISO(dateTimeString, { zone: defaultZone });
169
- if (isoParsed.isValid) {
170
- return isoParsed;
171
- }
172
-
173
- // Fallback to SQL parsing for strings like "2022-11-18 10:10:10"
174
- const sqlParsed = DateTime.fromSQL(dateTimeString, { zone: defaultZone });
175
- if (sqlParsed.isValid) {
176
- return sqlParsed;
177
- }
178
-
179
- return null;
180
- }
181
-
182
- /**
183
- * Formats a number of seconds into Hours / Minutes / Seconds or just Minutes and Seconds
184
- *
185
- * @param second
186
- * @returns {string}
187
- */
188
- export function fSecondsToTime(second: number) {
189
- const time = DateTime.now().setZone("UTC").startOf("year").set({ second });
190
- const hours = Math.floor(second / 3600);
191
- return (hours ? hours + ":" : "") + time.toFormat("mm:ss");
192
- }
193
-
194
- /**
195
- * Formats a number of seconds into a duration string in 00h 00m 00s format
196
- */
197
- export function fSecondsToDuration(seconds: number) {
198
- const hours = Math.floor(seconds / 3600);
199
- const minutes = Math.floor((seconds % 3600) / 60);
200
- const secs = Math.floor(seconds % 60);
201
- return `${hours ? hours + "h " : ""}${minutes ? minutes + "m " : ""}${secs}s`;
202
- }
203
-
204
- /**
205
- * Formats a number of milliseconds into a duration string in 00h 00m 00s 000ms format
206
- */
207
- export function fMillisecondsToDuration(milliseconds: number) {
208
- const durStr = fSecondsToDuration(Math.floor(milliseconds / 1000));
209
- return (durStr === "0s" ? "" : durStr) + ` ${Math.floor(milliseconds % 1000)}ms`;
210
- }
211
-
212
-
213
- /**
214
- * Formats a duration between two date strings in 00h 00m 00s format
215
- */
216
- export function fDuration(start: string | number, end?: string | number) {
217
- const endDateTime = end ? parseDateTime(end) : DateTime.now();
218
- const diff = endDateTime?.diff(parseDateTime(start) || DateTime.now(), ["hours", "minutes", "seconds"]);
219
- if (!diff?.isValid) {
220
- return "-";
221
- }
222
- const totalSeconds = diff.as("seconds");
223
- return fSecondsToDuration(totalSeconds);
224
- }
225
-
226
- /**
227
- * Formats an amount into USD currency format
228
- */
229
- export function fCurrency(amount: number, options?: object) {
230
- if (amount === null || amount === undefined || isNaN(amount)) {
231
- return "$-";
232
- }
233
- return new Intl.NumberFormat("en-US", {
234
- style: "currency",
235
- currency: "USD",
236
- ...options
237
- }).format(amount);
238
- }
239
-
240
- /**
241
- * Formats an amount into USD currency format without cents
242
- */
243
- export function fCurrencyNoCents(amount: number, options?: object) {
244
- return fCurrency(amount, {
245
- maximumFractionDigits: 0,
246
- ...options
247
- });
248
- }
249
-
250
- /**
251
- * Formats a number into a human-readable format
252
- */
253
- export function fNumber(number: number, options?: object) {
254
- return new Intl.NumberFormat("en-US", options).format(number);
255
- }
256
-
257
- /**
258
- * Formats a currency into a shorthand human-readable format (ie: $1.2M or $5K)
259
- */
260
- export function fShortCurrency(value: string | number, options?: { round: boolean }) {
261
- return "$" + fShortNumber(value, options);
262
- }
263
-
264
- /**
265
- * Formats a number into a shorthand human-readable format (ie: 1.2M or 5K)
266
- */
267
- export function fShortNumber(value: string | number, options?: { round: boolean }) {
268
- if (value === "" || value === null || value === undefined) {
269
- return "-";
270
- }
271
- const shorts = [
272
- { pow: 3, unit: "K" },
273
- { pow: 6, unit: "M" },
274
- { pow: 9, unit: "B" },
275
- { pow: 12, unit: "T" },
276
- { pow: 15, unit: "Q" }
277
- ];
278
-
279
- let n = Math.round(+value);
280
-
281
- const short = shorts.find(({ pow }) => Math.pow(10, pow) < n && Math.pow(10, pow + 3) > n) || null;
282
-
283
- if (short) {
284
- n = n / Math.pow(10, short.pow);
285
- return options?.round
286
- ? n + short.unit
287
- : n.toFixed(n > 100 ? 0 : 1) + short.unit;
288
- }
289
-
290
- return n;
291
- }
292
-
293
- /**
294
- * Formats a number into a human-readable size format (ie: 1.2MB or 5KB)
295
- */
296
- export function fShortSize(value: string | number) {
297
- const powers = [
298
- { pow: 0, unit: "B" },
299
- { pow: 10, unit: "KB" },
300
- { pow: 20, unit: "MB" },
301
- { pow: 30, unit: "GB" },
302
- { pow: 40, unit: "TB" },
303
- { pow: 50, unit: "PB" },
304
- { pow: 60, unit: "EB" },
305
- { pow: 70, unit: "ZB" },
306
- { pow: 80, unit: "YB" }
307
- ];
308
-
309
- const n = Math.round(+value);
310
- const power = powers.find((p, i) => {
311
- const nextPower = powers[i + 1];
312
- return !nextPower || n < Math.pow(2, nextPower.pow + 10);
313
- }) || powers[powers.length - 1];
314
-
315
- const div = Math.pow(2, power.pow);
316
-
317
- return Math.round(n / div) + " " + power.unit;
318
- }
319
-
320
- export function fBoolean(value?: boolean | string | any) {
321
- switch (value) {
322
- case "Yes":
323
- case "No":
324
- return value;
325
- }
326
-
327
- return (value === undefined || value === null) ? "-" : (value ? "Yes" : "No");
328
- }
329
-
330
- /**
331
- * Truncates the string by removing chars from the middle of the string
332
- * @param str
333
- * @param maxLength
334
- * @returns {string|*}
335
- */
336
- export function centerTruncate(str: string, maxLength: number) {
337
- if (str.length > maxLength) {
338
- const frontCharCount = Math.floor((maxLength - 3) / 2);
339
- const backCharCount = maxLength - frontCharCount - 3;
340
- return (
341
- str.substring(0, frontCharCount) +
342
- "..." +
343
- str.substring(str.length - backCharCount)
344
- );
345
- } else {
346
- return str;
347
- }
348
- }
349
-
350
- interface FPercentOptions {
351
- multiplier?: number,
352
- maximumFractionDigits?: number,
353
- NaN?: string
354
- }
355
-
356
- /**
357
- * Formats a number into a percentage
358
- * @param num
359
- * @param options
360
- * @returns {string}
361
- */
362
- export function fPercent(num: string | number, options: FPercentOptions = {}) {
363
- options = { multiplier: 100, maximumFractionDigits: 1, NaN: "N/A", ...options };
364
-
365
- num = parseFloat("" + num);
366
-
367
- if (isNaN(num)) {
368
- return options.NaN;
369
- }
370
-
371
- return fNumber(num * (options.multiplier || 100), options) + "%";
372
- }
373
-
374
-
375
- export function fPhone(value: string | number) {
376
- if (!value || typeof value !== "string") {
377
- return value || "";
378
- }
379
-
380
- const input = value.replace(/\D/g, "").split("");
381
- let phone = "";
382
-
383
- const startsWithOne = input.length > 0 && input[0] === "1";
384
- const shift = startsWithOne ? 1 : 0;
385
-
386
- input.map((number, index) => {
387
- switch (index) {
388
- case shift:
389
- phone += "(";
390
- break;
391
- case shift + 3:
392
- phone += ") ";
393
- break;
394
- case shift + 6:
395
- phone += "-";
396
- break;
397
- case shift + 10:
398
- phone += " x";
399
- break;
400
- }
401
- if (index === 0 && number === "1") {
402
- phone += "+1 ";
403
- } else {
404
- phone += number;
405
- }
406
- });
407
-
408
- if (value === "+1 (") {
409
- return "";
410
- }
411
-
412
- return phone;
413
- }
414
-
415
- export function fNameOrCount(items: ActionTargetItem[] | ActionTargetItem, label: string) {
416
- return Array.isArray(items) ? `${items?.length} ${label}` : `${items ? items.title || items.name || items.id : ""}`;
417
- }
418
-
419
- export function fJSON(string: string | object) {
420
- if (!string) {
421
- return string;
422
- }
423
-
424
- try {
425
- if (typeof string === "object") {
426
- return JSON.stringify(string, null, 2);
427
- }
428
- return JSON.stringify(JSON.parse(string), null, 2);
429
- } catch (e) {
430
- return string;
431
- }
432
- }
433
-
434
- /**
435
- * Convert markdown formatted string into a valid JSON object
436
- */
437
- export function parseMarkdownJSON(string: string | object): object | null | false {
438
- if (!string) return null;
439
- if (typeof string === "object") return string as object;
440
-
441
- try {
442
- return JSON.parse(parseMarkdownCode(string));
443
- } catch (e) {
444
- return false;
445
- }
446
- }
447
-
448
- export function parseMarkdownYAML(string: string): object | null | false {
449
- if (!string) return null;
450
-
451
- try {
452
- return parseYAML(parseMarkdownCode(string)) || (string ? undefined : null);
453
- } catch (e) {
454
- return false;
455
- }
456
- }
457
-
458
- /**
459
- * Parse a markdown formatted string and return the code block content
460
- */
461
- export function parseMarkdownCode(string: string): string {
462
- return string.replace(/^```[a-z0-9]{0,6}\s/, "").replace(/```$/, "");
463
- }
464
-
465
- /**
466
- * Convert a JSON object or string of code into a markdown formatted JSON string
467
- * ie: a valid JSON string with a ```json prefix and ``` postfix
468
- */
469
- export function fMarkdownCode(type: string, string: string | object): string {
470
- if (typeof string === "object" || isJSON(string)) {
471
- switch (type) {
472
- case "yaml":
473
- string = stringifyYAML(typeof string === "string" ? JSON.parse(string) : string);
474
- break;
475
- case "ts":
476
- default:
477
- string = fJSON(string);
478
- }
479
- }
480
-
481
- const regex = new RegExp(`\`\`\`${type}`, "g");
482
- string = (string || "") as string;
483
- if (!string.match(regex)) {
484
- string = parseMarkdownCode(string as string);
485
- return `\`\`\`${type}\n${string}\n\`\`\``;
486
- }
487
-
488
- return string as string;
489
- }
1
+ export * from "./formats/index";