date-vir 3.0.0 → 4.0.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/cjs/date-operations/diff-dates.js +57 -4
- package/dist/cjs/duration.js +30 -9
- package/dist/cjs/extra-utils/has-date-props.js +25 -0
- package/dist/cjs/extra-utils/js-date-conversion.js +2 -2
- package/dist/cjs/extra-utils/now.js +2 -2
- package/dist/cjs/formatting/formatted-string.js +34 -0
- package/dist/cjs/formatting/html-input.js +37 -0
- package/dist/cjs/formatting/relative-string.js +52 -0
- package/dist/cjs/formatting/simple-strings.js +58 -0
- package/dist/cjs/formatting/timestamp.js +21 -0
- package/dist/cjs/index.js +7 -3
- package/dist/esm/date-operations/diff-dates.d.ts +25 -6
- package/dist/esm/date-operations/diff-dates.js +56 -4
- package/dist/esm/duration.d.ts +42 -14
- package/dist/esm/duration.js +29 -8
- package/dist/esm/extra-utils/has-date-props.d.ts +8 -0
- package/dist/esm/extra-utils/has-date-props.js +20 -0
- package/dist/esm/extra-utils/js-date-conversion.js +1 -1
- package/dist/esm/extra-utils/now.d.ts +1 -1
- package/dist/esm/extra-utils/now.js +1 -1
- package/dist/esm/extra-utils/zero.d.ts +1 -1
- package/dist/esm/formatting/formatted-string.d.ts +14 -0
- package/dist/esm/formatting/formatted-string.js +29 -0
- package/dist/esm/{full-date/primitive-conversions.d.ts → formatting/html-input.d.ts} +2 -19
- package/dist/esm/formatting/html-input.js +33 -0
- package/dist/esm/formatting/relative-string.d.ts +25 -0
- package/dist/esm/formatting/relative-string.js +48 -0
- package/dist/esm/formatting/simple-strings.d.ts +71 -0
- package/dist/esm/formatting/simple-strings.js +51 -0
- package/dist/esm/formatting/timestamp.d.ts +6 -0
- package/dist/esm/formatting/timestamp.js +16 -0
- package/dist/esm/index.d.ts +7 -3
- package/dist/esm/index.js +7 -3
- package/package.json +23 -23
- package/dist/cjs/augments/type.js +0 -2
- package/dist/cjs/full-date/primitive-conversions.js +0 -102
- package/dist/esm/augments/type.d.ts +0 -2
- package/dist/esm/augments/type.js +0 -1
- package/dist/esm/full-date/primitive-conversions.js +0 -94
- /package/dist/cjs/{full-date → formatting}/string-format-types.js +0 -0
- /package/dist/esm/{full-date → formatting}/string-format-types.d.ts +0 -0
- /package/dist/esm/{full-date → formatting}/string-format-types.js +0 -0
|
@@ -1,94 +0,0 @@
|
|
|
1
|
-
import { getEnumTypedValues } from '@augment-vir/common';
|
|
2
|
-
import { userLocale } from '../locales';
|
|
3
|
-
import { utcTimezone } from '../timezone/timezones';
|
|
4
|
-
import { FullDatePartEnum, } from './full-date-shape';
|
|
5
|
-
import { toLuxonDateTime } from './luxon-date-time-conversion';
|
|
6
|
-
/** Convert a FullDate into a unix timestamp with milliseconds. */
|
|
7
|
-
export function toTimestamp(fullDate) {
|
|
8
|
-
return toLuxonDateTime(fullDate).toMillis();
|
|
9
|
-
}
|
|
10
|
-
/** Convert a FullDate into an ISO string in UTC; always ends in Z. */
|
|
11
|
-
export function toIsoString(fullDate) {
|
|
12
|
-
const isoString = toLuxonDateTime(fullDate).setZone(utcTimezone).toISO();
|
|
13
|
-
/** Ignore this edge case in the types and idk how to trigger it. */
|
|
14
|
-
/* c8 ignore next 3 */
|
|
15
|
-
if (!isoString) {
|
|
16
|
-
throw new Error(`Unable to get ISO string for '${JSON.stringify(fullDate)}'`);
|
|
17
|
-
}
|
|
18
|
-
return isoString;
|
|
19
|
-
}
|
|
20
|
-
/**
|
|
21
|
-
* Chromium has a weird behavior where the "NARROW NO-BREAK SPACE" character (code 8239) is used
|
|
22
|
-
* instead of a space (code 32) before AM/PM.
|
|
23
|
-
*/
|
|
24
|
-
function fixChromiumSpace(original) {
|
|
25
|
-
return original.replace(new RegExp(String.fromCharCode(8239), 'g'), ' ');
|
|
26
|
-
}
|
|
27
|
-
/** Converts the given FullDate into a string in the user's locale, or the given localeOverride. */
|
|
28
|
-
export function toLocaleString(fullDate, formatOptions) {
|
|
29
|
-
const localeString = toLuxonDateTime(fullDate).toLocaleString({
|
|
30
|
-
...formatOptions,
|
|
31
|
-
}, {
|
|
32
|
-
locale: formatOptions?.locale ?? userLocale,
|
|
33
|
-
});
|
|
34
|
-
return fixChromiumSpace(localeString);
|
|
35
|
-
}
|
|
36
|
-
/**
|
|
37
|
-
* Provides arbitrary date formatting using Luxon. For full details on the options for the format
|
|
38
|
-
* string input, see https://moment.github.io/luxon/#/formatting?id=table-of-tokens
|
|
39
|
-
*
|
|
40
|
-
* You probably should **prefer toLocaleString** so your users see the format they expect. It
|
|
41
|
-
* already has many formatting options.
|
|
42
|
-
*/
|
|
43
|
-
export function toFormattedString(fullDate, format, localeOverride = userLocale) {
|
|
44
|
-
const formattedString = toLuxonDateTime(fullDate).toFormat(format, { locale: localeOverride });
|
|
45
|
-
return fixChromiumSpace(formattedString);
|
|
46
|
-
}
|
|
47
|
-
export function toHtmlInputString(fullDate, inputType, includeSeconds) {
|
|
48
|
-
const internalFullDate = fullDate;
|
|
49
|
-
if (inputType === FullDatePartEnum.Date) {
|
|
50
|
-
if (internalFullDate.year == undefined) {
|
|
51
|
-
throw new Error(`Tried to create date string but no year was provided.`);
|
|
52
|
-
}
|
|
53
|
-
if (internalFullDate.month == undefined) {
|
|
54
|
-
throw new Error(`Tried to create date string but no month was provided.`);
|
|
55
|
-
}
|
|
56
|
-
if (internalFullDate.day == undefined) {
|
|
57
|
-
throw new Error(`Tried to create date string but no day was provided.`);
|
|
58
|
-
}
|
|
59
|
-
const dateParts = [
|
|
60
|
-
String(Math.abs(internalFullDate.year)).padStart(4, '0'),
|
|
61
|
-
String(Math.abs(internalFullDate.month)).padStart(2, '0'),
|
|
62
|
-
String(Math.abs(internalFullDate.day)).padStart(2, '0'),
|
|
63
|
-
];
|
|
64
|
-
return dateParts.join('-');
|
|
65
|
-
}
|
|
66
|
-
else if (inputType === FullDatePartEnum.Time) {
|
|
67
|
-
const seconds = includeSeconds
|
|
68
|
-
? String(Math.abs(internalFullDate.second ?? 0)).padStart(2, '0')
|
|
69
|
-
: undefined;
|
|
70
|
-
if (includeSeconds && internalFullDate.second == undefined) {
|
|
71
|
-
throw new Error('Tried to include seconds in the time string but no seconds were provided.');
|
|
72
|
-
}
|
|
73
|
-
if (internalFullDate.hour == undefined) {
|
|
74
|
-
throw new Error(`Tried to create time string but no hour was provided.`);
|
|
75
|
-
}
|
|
76
|
-
if (internalFullDate.minute == undefined) {
|
|
77
|
-
throw new Error(`Tried to create time string but no minutes were provided.`);
|
|
78
|
-
}
|
|
79
|
-
const timeParts = [
|
|
80
|
-
String(Math.abs(internalFullDate.hour)).padStart(2, '0'),
|
|
81
|
-
String(Math.abs(internalFullDate.minute)).padStart(2, '0'),
|
|
82
|
-
seconds,
|
|
83
|
-
].filter((entry) => entry != undefined);
|
|
84
|
-
return timeParts.join(':');
|
|
85
|
-
}
|
|
86
|
-
else if (inputType === FullDatePartEnum.DateTime) {
|
|
87
|
-
const datePart = toHtmlInputString(fullDate, FullDatePartEnum.Date);
|
|
88
|
-
const timePart = toHtmlInputString(fullDate, FullDatePartEnum.Time, includeSeconds);
|
|
89
|
-
return `${datePart}T${timePart}`;
|
|
90
|
-
}
|
|
91
|
-
else {
|
|
92
|
-
throw new Error(`Unexpected inputTyped: '${inputType}'. Expected usage of FullDatePartEnum or one of ${getEnumTypedValues(FullDatePartEnum).join(', ')}.`);
|
|
93
|
-
}
|
|
94
|
-
}
|
|
File without changes
|
|
File without changes
|
|
File without changes
|