@dejwcake/craftable 2.2.0 → 2.3.0
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/index.cjs +1 -1
- package/dist/index.mjs +430 -417
- package/package.json +1 -1
- package/src/index.js +8 -1
- package/src/utils/dateFormatters.js +41 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dejwcake/craftable",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.3.0",
|
|
4
4
|
"description": "This is a frontend dependency for Craftable. Craftable is a Laravel-based open-source toolkit for building administration interfaces.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.cjs",
|
package/src/index.js
CHANGED
|
@@ -61,7 +61,14 @@ export { default as ActivationError } from './auth/ActivationError.vue';
|
|
|
61
61
|
export { default as TranslationListing } from './translation/TranslationListing.vue';
|
|
62
62
|
|
|
63
63
|
// Utilities
|
|
64
|
-
export {
|
|
64
|
+
export {
|
|
65
|
+
formatDate,
|
|
66
|
+
formatDatetime,
|
|
67
|
+
formatTime,
|
|
68
|
+
formatDuration,
|
|
69
|
+
nowInTimezone,
|
|
70
|
+
dayjs,
|
|
71
|
+
} from './utils/dateFormatters.js';
|
|
65
72
|
export { mediaCollectionProp, mediaCollectionDefaults } from './utils/mediaProps.js';
|
|
66
73
|
export { notifySuccess, notifyError } from './utils/notify.js';
|
|
67
74
|
export { getDateFnsLocale, initDateFnsLocale } from './utils/dateFnsLocale.js';
|
|
@@ -23,6 +23,47 @@ export function formatTime(time, format = 'HH:mm') {
|
|
|
23
23
|
return d.isValid() ? d.format(format) : '';
|
|
24
24
|
}
|
|
25
25
|
|
|
26
|
+
const SECONDS_IN_MINUTE = 60;
|
|
27
|
+
const SECONDS_IN_HOUR = 3600;
|
|
28
|
+
const SECONDS_IN_DAY = 86400;
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Formats a number of seconds as a compact duration, e.g. `359d 19h 1m 39s`.
|
|
32
|
+
*
|
|
33
|
+
* Zero-valued units are dropped, so an exact hour reads `2h` rather than `0d 2h 0m 0s`, and a
|
|
34
|
+
* duration of zero reads `0s`. `null`, `undefined`, negative and non-finite values (`NaN`,
|
|
35
|
+
* `Infinity`) return the placeholder, which lets a listing cell or detail row render a missing or
|
|
36
|
+
* invalid value without a guard at the call site.
|
|
37
|
+
*
|
|
38
|
+
* Deliberately plain arithmetic rather than dayjs's duration plugin: that plugin normalises a
|
|
39
|
+
* duration into years and months, so its `hours()` is the component of a year/month/day breakdown
|
|
40
|
+
* and does not line up with a total day count. Mixing the two reports 31,086,099 s as
|
|
41
|
+
* `359d 5h 1m 39s` instead of `359d 19h 1m 39s`.
|
|
42
|
+
*
|
|
43
|
+
* @param {number|null|undefined} seconds total seconds; fractions are floored.
|
|
44
|
+
* @param {string} [placeholder] returned for `null`, `undefined`, negative and non-finite values.
|
|
45
|
+
* @returns {string}
|
|
46
|
+
*/
|
|
47
|
+
export function formatDuration(seconds, placeholder = '—') {
|
|
48
|
+
if (seconds === null || seconds === undefined) {
|
|
49
|
+
return placeholder;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const total = Math.floor(seconds);
|
|
53
|
+
if (!Number.isFinite(total) || total < 0) {
|
|
54
|
+
return placeholder;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const parts = [
|
|
58
|
+
[Math.floor(total / SECONDS_IN_DAY), 'd'],
|
|
59
|
+
[Math.floor((total % SECONDS_IN_DAY) / SECONDS_IN_HOUR), 'h'],
|
|
60
|
+
[Math.floor((total % SECONDS_IN_HOUR) / SECONDS_IN_MINUTE), 'm'],
|
|
61
|
+
[total % SECONDS_IN_MINUTE, 's'],
|
|
62
|
+
].filter(([amount]) => amount > 0);
|
|
63
|
+
|
|
64
|
+
return parts.length === 0 ? '0s' : parts.map(([amount, unit]) => `${amount}${unit}`).join(' ');
|
|
65
|
+
}
|
|
66
|
+
|
|
26
67
|
export function nowInTimezone(tz = 'UTC', format = 'YYYY-MM-DD HH:mm:ss') {
|
|
27
68
|
return dayjs().tz(tz).format(format);
|
|
28
69
|
}
|