date-vir 0.2.3 → 0.2.5

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.
Files changed (43) hide show
  1. package/dist/esm/full-date/calculate-relative-date.js +4 -0
  2. package/dist/{full-date → esm/full-date}/combine-dates.js +5 -9
  3. package/dist/esm/full-date/create-full-date.js +62 -0
  4. package/dist/esm/full-date/date-like.js +1 -0
  5. package/dist/esm/full-date/format-presets.js +29 -0
  6. package/dist/esm/full-date/full-date-shape.js +35 -0
  7. package/dist/esm/full-date/is-valid-full-date.js +19 -0
  8. package/dist/{full-date → esm/full-date}/luxon-date-time-conversion.js +5 -10
  9. package/dist/{full-date → esm/full-date}/parsing.js +10 -15
  10. package/dist/{full-date → esm/full-date}/primitive-conversions.js +18 -26
  11. package/dist/esm/full-date/string-format-types.js +1 -0
  12. package/dist/esm/index.js +15 -0
  13. package/dist/esm/locales.js +2 -0
  14. package/dist/{timezone → esm/timezone}/timezone-checks.js +4 -9
  15. package/dist/{timezone → esm/timezone}/timezone-names.js +1 -4
  16. package/dist/esm/timezone/timezones.js +11 -0
  17. package/package.json +7 -6
  18. package/dist/full-date/calculate-relative-date.js +0 -8
  19. package/dist/full-date/create-full-date.js +0 -70
  20. package/dist/full-date/date-like.js +0 -2
  21. package/dist/full-date/format-presets.js +0 -32
  22. package/dist/full-date/full-date-shape.js +0 -38
  23. package/dist/full-date/is-valid-full-date.js +0 -24
  24. package/dist/full-date/string-format-types.js +0 -2
  25. package/dist/index.js +0 -31
  26. package/dist/locales.js +0 -5
  27. package/dist/timezone/timezones.js +0 -14
  28. /package/dist/{full-date → esm/full-date}/calculate-relative-date.d.ts +0 -0
  29. /package/dist/{full-date → esm/full-date}/combine-dates.d.ts +0 -0
  30. /package/dist/{full-date → esm/full-date}/create-full-date.d.ts +0 -0
  31. /package/dist/{full-date → esm/full-date}/date-like.d.ts +0 -0
  32. /package/dist/{full-date → esm/full-date}/format-presets.d.ts +0 -0
  33. /package/dist/{full-date → esm/full-date}/full-date-shape.d.ts +0 -0
  34. /package/dist/{full-date → esm/full-date}/is-valid-full-date.d.ts +0 -0
  35. /package/dist/{full-date → esm/full-date}/luxon-date-time-conversion.d.ts +0 -0
  36. /package/dist/{full-date → esm/full-date}/parsing.d.ts +0 -0
  37. /package/dist/{full-date → esm/full-date}/primitive-conversions.d.ts +0 -0
  38. /package/dist/{full-date → esm/full-date}/string-format-types.d.ts +0 -0
  39. /package/dist/{index.d.ts → esm/index.d.ts} +0 -0
  40. /package/dist/{locales.d.ts → esm/locales.d.ts} +0 -0
  41. /package/dist/{timezone → esm/timezone}/timezone-checks.d.ts +0 -0
  42. /package/dist/{timezone → esm/timezone}/timezone-names.d.ts +0 -0
  43. /package/dist/{timezone → esm/timezone}/timezones.d.ts +0 -0
@@ -0,0 +1,4 @@
1
+ import { parseLuxonDateTime, toLuxonDateTime } from './luxon-date-time-conversion';
2
+ export function calculateRelativeDate(fullDate, relativeCalculation) {
3
+ return parseLuxonDateTime(toLuxonDateTime(fullDate).plus(relativeCalculation));
4
+ }
@@ -1,10 +1,7 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.combineDateParts = void 0;
4
- const object_shape_tester_1 = require("object-shape-tester");
5
- const full_date_shape_1 = require("./full-date-shape");
6
- function combineDateParts(maybeDateParts) {
7
- const timePart = (0, object_shape_tester_1.isValidShape)(maybeDateParts.time, full_date_shape_1.timePartShape, {
1
+ import { isValidShape } from 'object-shape-tester';
2
+ import { FullDatePartEnum, datePartShape, timePartShape, } from './full-date-shape';
3
+ export function combineDateParts(maybeDateParts) {
4
+ const timePart = isValidShape(maybeDateParts.time, timePartShape, {
8
5
  allowExtraKeys: true,
9
6
  })
10
7
  ? {
@@ -15,7 +12,7 @@ function combineDateParts(maybeDateParts) {
15
12
  timezone: maybeDateParts.time.timezone,
16
13
  }
17
14
  : undefined;
18
- const datePart = (0, object_shape_tester_1.isValidShape)(maybeDateParts.date, full_date_shape_1.datePartShape, {
15
+ const datePart = isValidShape(maybeDateParts.date, datePartShape, {
19
16
  allowExtraKeys: true,
20
17
  })
21
18
  ? {
@@ -42,4 +39,3 @@ function combineDateParts(maybeDateParts) {
42
39
  }
43
40
  return undefined;
44
41
  }
45
- exports.combineDateParts = combineDateParts;
@@ -0,0 +1,62 @@
1
+ import { isRuntimeTypeOf } from '@augment-vir/common';
2
+ import { DateTime } from 'luxon';
3
+ import { userTimezone, utcTimezone } from '../timezone/timezones';
4
+ import { isValidFullDate } from './is-valid-full-date';
5
+ import { parseLuxonDateTime, toLuxonDateTime } from './luxon-date-time-conversion';
6
+ export function createFullDateInUserTimezone(dateLike) {
7
+ return createFullDate(dateLike, userTimezone);
8
+ }
9
+ export function createUtcFullDate(dateLike) {
10
+ return createFullDate(dateLike, utcTimezone);
11
+ }
12
+ /**
13
+ * Parses the given dateLike and then reads the fullDate properties from that parsed value as if it
14
+ * were in the given timezone.
15
+ */
16
+ export function createFullDate(
17
+ /** The original date representation to be converted into a FullDate. */
18
+ dateLike,
19
+ /** The timezone that this date/time is meant for / originated from. */
20
+ timezone) {
21
+ const dateTime = convertDateLikeToLuxonDateTime(dateLike, timezone);
22
+ if (!dateTime || !dateTime.isValid) {
23
+ throw new Error(`Failed to parse date input '${dateLike}'`);
24
+ }
25
+ return parseLuxonDateTime(dateTime);
26
+ }
27
+ /**
28
+ * Creates and returns a new FullDate object with its values shifted to be the same original time
29
+ * but represented in a new timezone.
30
+ */
31
+ export function toNewTimezone(fullDate, timezone) {
32
+ if (fullDate.timezone === timezone) {
33
+ return fullDate;
34
+ }
35
+ return createFullDate(fullDate, timezone);
36
+ }
37
+ function convertDateLikeToLuxonDateTime(dateLike, timezone) {
38
+ if (isValidFullDate(dateLike)) {
39
+ dateLike = toLuxonDateTime(dateLike).toMillis();
40
+ }
41
+ if (DateTime.isDateTime(dateLike)) {
42
+ return dateLike.setZone(timezone);
43
+ }
44
+ else if (isRuntimeTypeOf(dateLike, 'number')) {
45
+ return DateTime.fromMillis(dateLike, { zone: utcTimezone }).setZone(timezone);
46
+ }
47
+ else if (isRuntimeTypeOf(dateLike, 'string')) {
48
+ const dateTime = DateTime.fromISO(dateLike, { zone: timezone });
49
+ if (!dateTime.isValid) {
50
+ throw new Error(`Failed to parse date string '${dateLike}': unexpected string format. Please use parseStringToFullDate instead to have more control over the parsing format.`);
51
+ }
52
+ return dateTime;
53
+ }
54
+ else if (dateLike instanceof Date) {
55
+ const dateTime = DateTime.fromJSDate(dateLike);
56
+ return dateTime.setZone(timezone);
57
+ }
58
+ return undefined;
59
+ }
60
+ export function getNowFullDate(timezone) {
61
+ return createFullDate(Date.now(), timezone);
62
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,29 @@
1
+ import { DateTime } from 'luxon';
2
+ /**
3
+ * Format preset re-exported from luxon to be used with toLocaleString. To see examples of all
4
+ * presets, go here: https://moment.github.io/luxon/#/formatting?id=presets
5
+ */
6
+ export const formatPresets = {
7
+ DateShort: DateTime.DATE_SHORT,
8
+ DateMed: DateTime.DATE_MED,
9
+ DateMedWithWeekday: DateTime.DATE_MED_WITH_WEEKDAY,
10
+ DateFull: DateTime.DATE_FULL,
11
+ DateHuge: DateTime.DATE_HUGE,
12
+ TimeSimple: DateTime.TIME_SIMPLE,
13
+ TimeWithSeconds: DateTime.TIME_WITH_SECONDS,
14
+ TimeWithShortOffset: DateTime.TIME_WITH_SHORT_OFFSET,
15
+ TimeWithLongOffset: DateTime.TIME_WITH_LONG_OFFSET,
16
+ Time24Simple: DateTime.TIME_24_SIMPLE,
17
+ Time24WithSeconds: DateTime.TIME_24_WITH_SECONDS,
18
+ Time24WithShortOffset: DateTime.TIME_24_WITH_SHORT_OFFSET,
19
+ Time24WithLongOffset: DateTime.TIME_24_WITH_LONG_OFFSET,
20
+ DatetimeShort: DateTime.DATETIME_SHORT,
21
+ DatetimeShortWithSeconds: DateTime.DATETIME_SHORT_WITH_SECONDS,
22
+ DatetimeMed: DateTime.DATETIME_MED,
23
+ DatetimeMedWithSeconds: DateTime.DATETIME_MED_WITH_SECONDS,
24
+ DatetimeMedWithWeekday: DateTime.DATETIME_MED_WITH_WEEKDAY,
25
+ DatetimeFull: DateTime.DATETIME_FULL,
26
+ DatetimeFullWithSeconds: DateTime.DATETIME_FULL_WITH_SECONDS,
27
+ DatetimeHuge: DateTime.DATETIME_HUGE,
28
+ DatetimeHugeWithSeconds: DateTime.DATETIME_HUGE_WITH_SECONDS,
29
+ };
@@ -0,0 +1,35 @@
1
+ import { and, defineShape } from 'object-shape-tester';
2
+ import { utcTimezone } from '../timezone/timezones';
3
+ export var FullDatePartEnum;
4
+ (function (FullDatePartEnum) {
5
+ FullDatePartEnum["Date"] = "date";
6
+ FullDatePartEnum["Time"] = "time";
7
+ })(FullDatePartEnum || (FullDatePartEnum = {}));
8
+ export const timePartShape = defineShape({
9
+ /** Hour of the day in 24 time: 0-23 */
10
+ hour: 14,
11
+ /** Minute of the hour: 0-59 */
12
+ minute: 19,
13
+ /** Second of the minute: 0-59 */
14
+ second: 0,
15
+ /** Millisecond of the second: 0-999 */
16
+ millisecond: 870,
17
+ /** The timezone that this date/time is meant for / originated from. */
18
+ timezone: utcTimezone,
19
+ });
20
+ export const datePartShape = defineShape({
21
+ /**
22
+ * The full, four digit year.
23
+ *
24
+ * @example
25
+ * 2023;
26
+ */
27
+ year: 2023,
28
+ /** A month of the year: 1-12 */
29
+ month: 6,
30
+ /** A day of the month: 1-31 depending on the month */
31
+ day: 5,
32
+ /** The timezone that this date/time is meant for / originated from. */
33
+ timezone: utcTimezone,
34
+ });
35
+ export const fullDateShape = defineShape(and(datePartShape, timePartShape));
@@ -0,0 +1,19 @@
1
+ import { assertValidShape } from 'object-shape-tester';
2
+ import { assertValidTimezone } from '../timezone/timezone-checks';
3
+ import { fullDateShape } from './full-date-shape';
4
+ import { toLuxonDateTime } from './luxon-date-time-conversion';
5
+ export function assertIsValidFullDate(input) {
6
+ assertValidShape(input, fullDateShape);
7
+ assertValidTimezone(input.timezone);
8
+ /** Check that the input can be converted to a luxon date correctly. */
9
+ toLuxonDateTime(input);
10
+ }
11
+ export function isValidFullDate(input) {
12
+ try {
13
+ assertIsValidFullDate(input);
14
+ return true;
15
+ }
16
+ catch (error) {
17
+ return false;
18
+ }
19
+ }
@@ -1,15 +1,12 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.parseLuxonDateTime = exports.toLuxonDateTime = void 0;
4
- const common_1 = require("@augment-vir/common");
5
- const luxon_1 = require("luxon");
1
+ import { omitObjectKeys } from '@augment-vir/common';
2
+ import { DateTime } from 'luxon';
6
3
  /**
7
4
  * Converts a FullDate object into a Luxon DateTime library. This is only needed if you need complex
8
5
  * operations on dates. It does, however, internally power a lot of the date-vir functionality as it
9
6
  * uses the Luxon library for conversions between timezones.
10
7
  */
11
- function toLuxonDateTime(fullDateInput) {
12
- const dateTime = luxon_1.DateTime.fromObject((0, common_1.omitObjectKeys)(fullDateInput, ['timezone']), {
8
+ export function toLuxonDateTime(fullDateInput) {
9
+ const dateTime = DateTime.fromObject(omitObjectKeys(fullDateInput, ['timezone']), {
13
10
  zone: fullDateInput.timezone,
14
11
  });
15
12
  /** Ignore this edge case in coverage cause idk how to trigger it. */
@@ -19,13 +16,12 @@ function toLuxonDateTime(fullDateInput) {
19
16
  }
20
17
  return dateTime;
21
18
  }
22
- exports.toLuxonDateTime = toLuxonDateTime;
23
19
  /**
24
20
  * Convert a Luxon DateTime object into a DateTime object. Used internally inside of date-vir, but
25
21
  * could be helpful if you need it. Usually you should prefer using the createFullDate function
26
22
  * instead.
27
23
  */
28
- function parseLuxonDateTime(dateTimeInput) {
24
+ export function parseLuxonDateTime(dateTimeInput) {
29
25
  if (!dateTimeInput.isValid) {
30
26
  throw new Error(`Invalid input: '${dateTimeInput.toISO()}'`);
31
27
  }
@@ -40,4 +36,3 @@ function parseLuxonDateTime(dateTimeInput) {
40
36
  timezone: dateTimeInput.zoneName,
41
37
  };
42
38
  }
43
- exports.parseLuxonDateTime = parseLuxonDateTime;
@@ -1,10 +1,7 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.parseInputElementValue = exports.parseStrangeString = void 0;
4
- const common_1 = require("@augment-vir/common");
5
- const luxon_1 = require("luxon");
6
- const create_full_date_1 = require("./create-full-date");
7
- const luxon_date_time_conversion_1 = require("./luxon-date-time-conversion");
1
+ import { isRuntimeTypeOf } from '@augment-vir/common';
2
+ import { DateTime } from 'luxon';
3
+ import { createFullDate } from './create-full-date';
4
+ import { parseLuxonDateTime } from './luxon-date-time-conversion';
8
5
  /**
9
6
  * Parses any strangely formatted dateString using the given formatString and timezone.
10
7
  *
@@ -14,35 +11,33 @@ const luxon_date_time_conversion_1 = require("./luxon-date-time-conversion");
14
11
  * The formatString input is passed to Luxon. All format options are listed here:
15
12
  * https://moment.github.io/luxon/#/parsing?id=table-of-tokens
16
13
  */
17
- function parseStrangeString({ dateString, formatString, timezone, }) {
14
+ export function parseStrangeString({ dateString, formatString, timezone, }) {
18
15
  try {
19
- const dateTime = luxon_1.DateTime.fromFormat(dateString, formatString, {
16
+ const dateTime = DateTime.fromFormat(dateString, formatString, {
20
17
  zone: timezone,
21
18
  setZone: true,
22
19
  });
23
- return (0, luxon_date_time_conversion_1.parseLuxonDateTime)(dateTime);
20
+ return parseLuxonDateTime(dateTime);
24
21
  }
25
22
  catch (error) {
26
23
  throw new Error(`Failed to parse string '${dateString}' with format '${formatString}'`);
27
24
  }
28
25
  }
29
- exports.parseStrangeString = parseStrangeString;
30
26
  /**
31
27
  * Read either an HTMLInputElement's value directly, or the value string retrieved from an
32
28
  * HTMLInputElement. Handles both type="date" and type="time" <input> elements.
33
29
  */
34
- function parseInputElementValue(elementOrValue, timezone) {
30
+ export function parseInputElementValue(elementOrValue, timezone) {
35
31
  if (!elementOrValue) {
36
32
  return undefined;
37
33
  }
38
- const value = (0, common_1.isRuntimeTypeOf)(elementOrValue, 'string')
34
+ const value = isRuntimeTypeOf(elementOrValue, 'string')
39
35
  ? elementOrValue
40
36
  : elementOrValue.value;
41
37
  try {
42
- return (0, create_full_date_1.createFullDate)(value, timezone);
38
+ return createFullDate(value, timezone);
43
39
  }
44
40
  catch (error) {
45
41
  return undefined;
46
42
  }
47
43
  }
48
- exports.parseInputElementValue = parseInputElementValue;
@@ -1,19 +1,15 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.toHtmlInputString = exports.toFormattedString = exports.toLocaleString = exports.toIsoString = exports.toTimestamp = void 0;
4
- const common_1 = require("@augment-vir/common");
5
- const locales_1 = require("../locales");
6
- const timezones_1 = require("../timezone/timezones");
7
- const full_date_shape_1 = require("./full-date-shape");
8
- const luxon_date_time_conversion_1 = require("./luxon-date-time-conversion");
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';
9
6
  /** Convert a FullDate into a unix timestamp with milliseconds. */
10
- function toTimestamp(fullDate) {
11
- return (0, luxon_date_time_conversion_1.toLuxonDateTime)(fullDate).toMillis();
7
+ export function toTimestamp(fullDate) {
8
+ return toLuxonDateTime(fullDate).toMillis();
12
9
  }
13
- exports.toTimestamp = toTimestamp;
14
10
  /** Convert a FullDate into an ISO string in UTC; always ends in Z. */
15
- function toIsoString(fullDate) {
16
- const isoString = (0, luxon_date_time_conversion_1.toLuxonDateTime)(fullDate).setZone(timezones_1.utcTimezone).toISO();
11
+ export function toIsoString(fullDate) {
12
+ const isoString = toLuxonDateTime(fullDate).setZone(utcTimezone).toISO();
17
13
  /** Ignore this edge case in the types and idk how to trigger it. */
18
14
  /* c8 ignore next 3 */
19
15
  if (!isoString) {
@@ -21,16 +17,14 @@ function toIsoString(fullDate) {
21
17
  }
22
18
  return isoString;
23
19
  }
24
- exports.toIsoString = toIsoString;
25
20
  /** Converts the given FullDate into a string in the user's locale, or the given localeOverride. */
26
- function toLocaleString(fullDate, formatOptions) {
27
- return (0, luxon_date_time_conversion_1.toLuxonDateTime)(fullDate).toLocaleString({
21
+ export function toLocaleString(fullDate, formatOptions) {
22
+ return toLuxonDateTime(fullDate).toLocaleString({
28
23
  ...formatOptions,
29
24
  }, {
30
- locale: formatOptions?.locale ?? locales_1.userLocale,
25
+ locale: formatOptions?.locale ?? userLocale,
31
26
  });
32
27
  }
33
- exports.toLocaleString = toLocaleString;
34
28
  /**
35
29
  * Provides arbitrary date formatting using Luxon. For full details on the options for the format
36
30
  * string input, see https://moment.github.io/luxon/#/formatting?id=table-of-tokens
@@ -38,12 +32,11 @@ exports.toLocaleString = toLocaleString;
38
32
  * You probably should **prefer toLocaleString** so your users see the format they expect. It
39
33
  * already has many formatting options.
40
34
  */
41
- function toFormattedString(fullDate, format, localeOverride = locales_1.userLocale) {
42
- return (0, luxon_date_time_conversion_1.toLuxonDateTime)(fullDate).toFormat(format, { locale: localeOverride });
35
+ export function toFormattedString(fullDate, format, localeOverride = userLocale) {
36
+ return toLuxonDateTime(fullDate).toFormat(format, { locale: localeOverride });
43
37
  }
44
- exports.toFormattedString = toFormattedString;
45
- function toHtmlInputString(fullDate, inputType, includeSeconds) {
46
- if (inputType === full_date_shape_1.FullDatePartEnum.Date) {
38
+ export function toHtmlInputString(fullDate, inputType, includeSeconds) {
39
+ if (inputType === FullDatePartEnum.Date) {
47
40
  if (fullDate.year == undefined) {
48
41
  throw new Error(`Tried to create date string but no year was provided.`);
49
42
  }
@@ -60,7 +53,7 @@ function toHtmlInputString(fullDate, inputType, includeSeconds) {
60
53
  ];
61
54
  return dateParts.join('-');
62
55
  }
63
- else if (inputType === full_date_shape_1.FullDatePartEnum.Time) {
56
+ else if (inputType === FullDatePartEnum.Time) {
64
57
  if (includeSeconds && fullDate.second == undefined) {
65
58
  throw new Error('Tried to include seconds in the time string but no seconds were provided.');
66
59
  }
@@ -78,7 +71,6 @@ function toHtmlInputString(fullDate, inputType, includeSeconds) {
78
71
  return timeParts.join(':');
79
72
  }
80
73
  else {
81
- throw new Error(`Unexpected inputTyped: '${inputType}'. Expected usage of FullDatePartEnum or one of ${(0, common_1.getEnumTypedValues)(full_date_shape_1.FullDatePartEnum).join(', ')}.`);
74
+ throw new Error(`Unexpected inputTyped: '${inputType}'. Expected usage of FullDatePartEnum or one of ${getEnumTypedValues(FullDatePartEnum).join(', ')}.`);
82
75
  }
83
76
  }
84
- exports.toHtmlInputString = toHtmlInputString;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,15 @@
1
+ export * from './full-date/calculate-relative-date';
2
+ export * from './full-date/combine-dates';
3
+ export * from './full-date/create-full-date';
4
+ export * from './full-date/date-like';
5
+ export * from './full-date/format-presets';
6
+ export * from './full-date/full-date-shape';
7
+ export * from './full-date/is-valid-full-date';
8
+ export * from './full-date/luxon-date-time-conversion';
9
+ export * from './full-date/parsing';
10
+ export * from './full-date/primitive-conversions';
11
+ export * from './full-date/string-format-types';
12
+ export * from './locales';
13
+ export * from './timezone/timezone-checks';
14
+ export * from './timezone/timezone-names';
15
+ export * from './timezone/timezones';
@@ -0,0 +1,2 @@
1
+ import { Settings } from 'luxon';
2
+ export const userLocale = Settings.defaultLocale;
@@ -1,7 +1,4 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.isValidTimezone = exports.assertValidTimezone = void 0;
4
- const luxon_1 = require("luxon");
1
+ import { Info } from 'luxon';
5
2
  /**
6
3
  * Asserts that the given timezoneName is a valid timezone name.
7
4
  *
@@ -9,12 +6,11 @@ const luxon_1 = require("luxon");
9
6
  * because this function uses luxon to check that it is able to generate a date with the given
10
7
  * timezone, regardless of whether it's in our typed list or not.
11
8
  */
12
- function assertValidTimezone(potentialTimezoneName) {
13
- if (!luxon_1.Info.isValidIANAZone(potentialTimezoneName)) {
9
+ export function assertValidTimezone(potentialTimezoneName) {
10
+ if (!Info.isValidIANAZone(potentialTimezoneName)) {
14
11
  throw new Error(`'${potentialTimezoneName}' is not a valid time zone`);
15
12
  }
16
13
  }
17
- exports.assertValidTimezone = assertValidTimezone;
18
14
  /**
19
15
  * Checks that the given timezoneName is a valid timezone name.
20
16
  *
@@ -22,7 +18,7 @@ exports.assertValidTimezone = assertValidTimezone;
22
18
  * because this function uses luxon to check that it is able to generate a date with the given
23
19
  * timezone, regardless of whether it's in our typed list or not.
24
20
  */
25
- function isValidTimezone(potentialTimezoneName) {
21
+ export function isValidTimezone(potentialTimezoneName) {
26
22
  try {
27
23
  assertValidTimezone(potentialTimezoneName);
28
24
  return true;
@@ -31,4 +27,3 @@ function isValidTimezone(potentialTimezoneName) {
31
27
  return false;
32
28
  }
33
29
  }
34
- exports.isValidTimezone = isValidTimezone;
@@ -1,13 +1,10 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.allTimezoneNames = void 0;
4
1
  /**
5
2
  * This list was generated from Firefox 106.0.2 (which gave me the longest list out of all the
6
3
  * browsers I had immediately available to me at the time) with the following code:
7
4
  *
8
5
  * Intl.supportedValuesOf('timeZone').map(zone => `'${zone}'`).join(',')
9
6
  */
10
- exports.allTimezoneNames = [
7
+ export const allTimezoneNames = [
11
8
  'Africa/Abidjan',
12
9
  'Africa/Accra',
13
10
  'Africa/Addis_Ababa',
@@ -0,0 +1,11 @@
1
+ import { Settings } from 'luxon';
2
+ import { allTimezoneNames } from './timezone-names';
3
+ /** A typed object of all known timezones, as of the writing of this message. */
4
+ export const timezones = allTimezoneNames.reduce((accum, timezoneName) => {
5
+ accum[timezoneName] = timezoneName;
6
+ return accum;
7
+ }, {});
8
+ /** The timezone of the current user's environment. */
9
+ export const userTimezone = Settings.defaultZone.name;
10
+ /** The UTC timezone. */
11
+ export const utcTimezone = timezones.UTC;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "date-vir",
3
- "version": "0.2.3",
3
+ "version": "0.2.5",
4
4
  "description": "Easy and explicit dates and times",
5
5
  "keywords": [
6
6
  "date",
@@ -20,11 +20,12 @@
20
20
  "name": "electrovir",
21
21
  "url": "https://github.com/electrovir"
22
22
  },
23
- "main": "dist/index.js",
24
- "types": "dist/index.d.ts",
23
+ "main": "dist/cjs/index.js",
24
+ "module": "dist/esm/index.js",
25
+ "types": "dist/esm/index.d.ts",
25
26
  "scripts": {
26
- "compile": "tsc --project tsconfig.json && tsc --project tsconfig.cjs.json",
27
- "docs:update": "virmator code-in-markdown",
27
+ "compile": "rm -rf dist && concurrently --colors --kill-others-on-fail -c auto --names esm,cjs \"tsc --project tsconfig.json\" \"tsc --project tsconfig.cjs.json\"",
28
+ "docs:update": "virmator code-in-markdown --index src/index.ts README.md",
28
29
  "format": "virmator format",
29
30
  "publish": "virmator publish \"npm run compile && npm run test:all\"",
30
31
  "start": "npm install && virmator frontend",
@@ -32,7 +33,7 @@
32
33
  "test:all": "concurrently --colors --kill-others-on-fail -c auto --names types,tests,spelling,format,docs \"npm run test:types\" \"npm run test:coverage\" \"npm run test:spelling\" \"npm run test:format\" \"npm run test:docs\"",
33
34
  "test:coverage": "npm run test coverage",
34
35
  "test:debug": "web-test-runner --watch --color --config configs/web-test-runner.config.mjs --only-one-browser",
35
- "test:docs": "virmator code-in-markdown check",
36
+ "test:docs": "virmator code-in-markdown check --index src/index.ts README.md",
36
37
  "test:format": "virmator format check",
37
38
  "test:spelling": "virmator spellcheck",
38
39
  "test:types": "tsc --noEmit",
@@ -1,8 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.calculateRelativeDate = void 0;
4
- const luxon_date_time_conversion_1 = require("./luxon-date-time-conversion");
5
- function calculateRelativeDate(fullDate, relativeCalculation) {
6
- return (0, luxon_date_time_conversion_1.parseLuxonDateTime)((0, luxon_date_time_conversion_1.toLuxonDateTime)(fullDate).plus(relativeCalculation));
7
- }
8
- exports.calculateRelativeDate = calculateRelativeDate;
@@ -1,70 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getNowFullDate = exports.toNewTimezone = exports.createFullDate = exports.createUtcFullDate = exports.createFullDateInUserTimezone = void 0;
4
- const common_1 = require("@augment-vir/common");
5
- const luxon_1 = require("luxon");
6
- const timezones_1 = require("../timezone/timezones");
7
- const is_valid_full_date_1 = require("./is-valid-full-date");
8
- const luxon_date_time_conversion_1 = require("./luxon-date-time-conversion");
9
- function createFullDateInUserTimezone(dateLike) {
10
- return createFullDate(dateLike, timezones_1.userTimezone);
11
- }
12
- exports.createFullDateInUserTimezone = createFullDateInUserTimezone;
13
- function createUtcFullDate(dateLike) {
14
- return createFullDate(dateLike, timezones_1.utcTimezone);
15
- }
16
- exports.createUtcFullDate = createUtcFullDate;
17
- /**
18
- * Parses the given dateLike and then reads the fullDate properties from that parsed value as if it
19
- * were in the given timezone.
20
- */
21
- function createFullDate(
22
- /** The original date representation to be converted into a FullDate. */
23
- dateLike,
24
- /** The timezone that this date/time is meant for / originated from. */
25
- timezone) {
26
- const dateTime = convertDateLikeToLuxonDateTime(dateLike, timezone);
27
- if (!dateTime || !dateTime.isValid) {
28
- throw new Error(`Failed to parse date input '${dateLike}'`);
29
- }
30
- return (0, luxon_date_time_conversion_1.parseLuxonDateTime)(dateTime);
31
- }
32
- exports.createFullDate = createFullDate;
33
- /**
34
- * Creates and returns a new FullDate object with its values shifted to be the same original time
35
- * but represented in a new timezone.
36
- */
37
- function toNewTimezone(fullDate, timezone) {
38
- if (fullDate.timezone === timezone) {
39
- return fullDate;
40
- }
41
- return createFullDate(fullDate, timezone);
42
- }
43
- exports.toNewTimezone = toNewTimezone;
44
- function convertDateLikeToLuxonDateTime(dateLike, timezone) {
45
- if ((0, is_valid_full_date_1.isValidFullDate)(dateLike)) {
46
- dateLike = (0, luxon_date_time_conversion_1.toLuxonDateTime)(dateLike).toMillis();
47
- }
48
- if (luxon_1.DateTime.isDateTime(dateLike)) {
49
- return dateLike.setZone(timezone);
50
- }
51
- else if ((0, common_1.isRuntimeTypeOf)(dateLike, 'number')) {
52
- return luxon_1.DateTime.fromMillis(dateLike, { zone: timezones_1.utcTimezone }).setZone(timezone);
53
- }
54
- else if ((0, common_1.isRuntimeTypeOf)(dateLike, 'string')) {
55
- const dateTime = luxon_1.DateTime.fromISO(dateLike, { zone: timezone });
56
- if (!dateTime.isValid) {
57
- throw new Error(`Failed to parse date string '${dateLike}': unexpected string format. Please use parseStringToFullDate instead to have more control over the parsing format.`);
58
- }
59
- return dateTime;
60
- }
61
- else if (dateLike instanceof Date) {
62
- const dateTime = luxon_1.DateTime.fromJSDate(dateLike);
63
- return dateTime.setZone(timezone);
64
- }
65
- return undefined;
66
- }
67
- function getNowFullDate(timezone) {
68
- return createFullDate(Date.now(), timezone);
69
- }
70
- exports.getNowFullDate = getNowFullDate;
@@ -1,2 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
@@ -1,32 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.formatPresets = void 0;
4
- const luxon_1 = require("luxon");
5
- /**
6
- * Format preset re-exported from luxon to be used with toLocaleString. To see examples of all
7
- * presets, go here: https://moment.github.io/luxon/#/formatting?id=presets
8
- */
9
- exports.formatPresets = {
10
- DateShort: luxon_1.DateTime.DATE_SHORT,
11
- DateMed: luxon_1.DateTime.DATE_MED,
12
- DateMedWithWeekday: luxon_1.DateTime.DATE_MED_WITH_WEEKDAY,
13
- DateFull: luxon_1.DateTime.DATE_FULL,
14
- DateHuge: luxon_1.DateTime.DATE_HUGE,
15
- TimeSimple: luxon_1.DateTime.TIME_SIMPLE,
16
- TimeWithSeconds: luxon_1.DateTime.TIME_WITH_SECONDS,
17
- TimeWithShortOffset: luxon_1.DateTime.TIME_WITH_SHORT_OFFSET,
18
- TimeWithLongOffset: luxon_1.DateTime.TIME_WITH_LONG_OFFSET,
19
- Time24Simple: luxon_1.DateTime.TIME_24_SIMPLE,
20
- Time24WithSeconds: luxon_1.DateTime.TIME_24_WITH_SECONDS,
21
- Time24WithShortOffset: luxon_1.DateTime.TIME_24_WITH_SHORT_OFFSET,
22
- Time24WithLongOffset: luxon_1.DateTime.TIME_24_WITH_LONG_OFFSET,
23
- DatetimeShort: luxon_1.DateTime.DATETIME_SHORT,
24
- DatetimeShortWithSeconds: luxon_1.DateTime.DATETIME_SHORT_WITH_SECONDS,
25
- DatetimeMed: luxon_1.DateTime.DATETIME_MED,
26
- DatetimeMedWithSeconds: luxon_1.DateTime.DATETIME_MED_WITH_SECONDS,
27
- DatetimeMedWithWeekday: luxon_1.DateTime.DATETIME_MED_WITH_WEEKDAY,
28
- DatetimeFull: luxon_1.DateTime.DATETIME_FULL,
29
- DatetimeFullWithSeconds: luxon_1.DateTime.DATETIME_FULL_WITH_SECONDS,
30
- DatetimeHuge: luxon_1.DateTime.DATETIME_HUGE,
31
- DatetimeHugeWithSeconds: luxon_1.DateTime.DATETIME_HUGE_WITH_SECONDS,
32
- };
@@ -1,38 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.fullDateShape = exports.datePartShape = exports.timePartShape = exports.FullDatePartEnum = void 0;
4
- const object_shape_tester_1 = require("object-shape-tester");
5
- const timezones_1 = require("../timezone/timezones");
6
- var FullDatePartEnum;
7
- (function (FullDatePartEnum) {
8
- FullDatePartEnum["Date"] = "date";
9
- FullDatePartEnum["Time"] = "time";
10
- })(FullDatePartEnum = exports.FullDatePartEnum || (exports.FullDatePartEnum = {}));
11
- exports.timePartShape = (0, object_shape_tester_1.defineShape)({
12
- /** Hour of the day in 24 time: 0-23 */
13
- hour: 14,
14
- /** Minute of the hour: 0-59 */
15
- minute: 19,
16
- /** Second of the minute: 0-59 */
17
- second: 0,
18
- /** Millisecond of the second: 0-999 */
19
- millisecond: 870,
20
- /** The timezone that this date/time is meant for / originated from. */
21
- timezone: timezones_1.utcTimezone,
22
- });
23
- exports.datePartShape = (0, object_shape_tester_1.defineShape)({
24
- /**
25
- * The full, four digit year.
26
- *
27
- * @example
28
- * 2023;
29
- */
30
- year: 2023,
31
- /** A month of the year: 1-12 */
32
- month: 6,
33
- /** A day of the month: 1-31 depending on the month */
34
- day: 5,
35
- /** The timezone that this date/time is meant for / originated from. */
36
- timezone: timezones_1.utcTimezone,
37
- });
38
- exports.fullDateShape = (0, object_shape_tester_1.defineShape)((0, object_shape_tester_1.and)(exports.datePartShape, exports.timePartShape));
@@ -1,24 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.isValidFullDate = exports.assertIsValidFullDate = void 0;
4
- const object_shape_tester_1 = require("object-shape-tester");
5
- const timezone_checks_1 = require("../timezone/timezone-checks");
6
- const full_date_shape_1 = require("./full-date-shape");
7
- const luxon_date_time_conversion_1 = require("./luxon-date-time-conversion");
8
- function assertIsValidFullDate(input) {
9
- (0, object_shape_tester_1.assertValidShape)(input, full_date_shape_1.fullDateShape);
10
- (0, timezone_checks_1.assertValidTimezone)(input.timezone);
11
- /** Check that the input can be converted to a luxon date correctly. */
12
- (0, luxon_date_time_conversion_1.toLuxonDateTime)(input);
13
- }
14
- exports.assertIsValidFullDate = assertIsValidFullDate;
15
- function isValidFullDate(input) {
16
- try {
17
- assertIsValidFullDate(input);
18
- return true;
19
- }
20
- catch (error) {
21
- return false;
22
- }
23
- }
24
- exports.isValidFullDate = isValidFullDate;
@@ -1,2 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
package/dist/index.js DELETED
@@ -1,31 +0,0 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
- };
16
- Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./full-date/calculate-relative-date"), exports);
18
- __exportStar(require("./full-date/combine-dates"), exports);
19
- __exportStar(require("./full-date/create-full-date"), exports);
20
- __exportStar(require("./full-date/date-like"), exports);
21
- __exportStar(require("./full-date/format-presets"), exports);
22
- __exportStar(require("./full-date/full-date-shape"), exports);
23
- __exportStar(require("./full-date/is-valid-full-date"), exports);
24
- __exportStar(require("./full-date/luxon-date-time-conversion"), exports);
25
- __exportStar(require("./full-date/parsing"), exports);
26
- __exportStar(require("./full-date/primitive-conversions"), exports);
27
- __exportStar(require("./full-date/string-format-types"), exports);
28
- __exportStar(require("./locales"), exports);
29
- __exportStar(require("./timezone/timezone-checks"), exports);
30
- __exportStar(require("./timezone/timezone-names"), exports);
31
- __exportStar(require("./timezone/timezones"), exports);
package/dist/locales.js DELETED
@@ -1,5 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.userLocale = void 0;
4
- const luxon_1 = require("luxon");
5
- exports.userLocale = luxon_1.Settings.defaultLocale;
@@ -1,14 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.utcTimezone = exports.userTimezone = exports.timezones = void 0;
4
- const luxon_1 = require("luxon");
5
- const timezone_names_1 = require("./timezone-names");
6
- /** A typed object of all known timezones, as of the writing of this message. */
7
- exports.timezones = timezone_names_1.allTimezoneNames.reduce((accum, timezoneName) => {
8
- accum[timezoneName] = timezoneName;
9
- return accum;
10
- }, {});
11
- /** The timezone of the current user's environment. */
12
- exports.userTimezone = luxon_1.Settings.defaultZone.name;
13
- /** The UTC timezone. */
14
- exports.utcTimezone = exports.timezones.UTC;
File without changes
File without changes
File without changes
File without changes