date-vir 0.4.1 → 0.5.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.
@@ -3,10 +3,15 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.fullDateShape = exports.datePartShape = exports.timePartShape = exports.FullDatePartEnum = void 0;
4
4
  const object_shape_tester_1 = require("object-shape-tester");
5
5
  const timezones_1 = require("../timezone/timezones");
6
+ /**
7
+ * Represents various parts of a FullDate. Each value is also a valid "type" attribute for an
8
+ * <input> HTML element.
9
+ */
6
10
  var FullDatePartEnum;
7
11
  (function (FullDatePartEnum) {
8
12
  FullDatePartEnum["Date"] = "date";
9
13
  FullDatePartEnum["Time"] = "time";
14
+ FullDatePartEnum["DateTime"] = "datetime-local";
10
15
  })(FullDatePartEnum || (exports.FullDatePartEnum = FullDatePartEnum = {}));
11
16
  exports.timePartShape = (0, object_shape_tester_1.defineShape)({
12
17
  /** Hour of the day in 24 time: 0-23 */
@@ -47,47 +47,54 @@ exports.toLocaleString = toLocaleString;
47
47
  * already has many formatting options.
48
48
  */
49
49
  function toFormattedString(fullDate, format, localeOverride = locales_1.userLocale) {
50
- const formattedString = (0, luxon_date_time_conversion_1.toLuxonDateTime)(fullDate)
51
- .toFormat(format, { locale: localeOverride })
52
- .replace(/ /g, ' ');
50
+ const formattedString = (0, luxon_date_time_conversion_1.toLuxonDateTime)(fullDate).toFormat(format, { locale: localeOverride });
53
51
  return fixChromiumSpace(formattedString);
54
52
  }
55
53
  exports.toFormattedString = toFormattedString;
56
54
  function toHtmlInputString(fullDate, inputType, includeSeconds) {
55
+ const internalFullDate = fullDate;
57
56
  if (inputType === full_date_shape_1.FullDatePartEnum.Date) {
58
- if (fullDate.year == undefined) {
57
+ if (internalFullDate.year == undefined) {
59
58
  throw new Error(`Tried to create date string but no year was provided.`);
60
59
  }
61
- if (fullDate.month == undefined) {
60
+ if (internalFullDate.month == undefined) {
62
61
  throw new Error(`Tried to create date string but no month was provided.`);
63
62
  }
64
- if (fullDate.day == undefined) {
63
+ if (internalFullDate.day == undefined) {
65
64
  throw new Error(`Tried to create date string but no day was provided.`);
66
65
  }
67
66
  const dateParts = [
68
- String(Math.abs(fullDate.year)).padStart(4, '0'),
69
- String(Math.abs(fullDate.month)).padStart(2, '0'),
70
- String(Math.abs(fullDate.day)).padStart(2, '0'),
67
+ String(Math.abs(internalFullDate.year)).padStart(4, '0'),
68
+ String(Math.abs(internalFullDate.month)).padStart(2, '0'),
69
+ String(Math.abs(internalFullDate.day)).padStart(2, '0'),
71
70
  ];
72
71
  return dateParts.join('-');
73
72
  }
74
73
  else if (inputType === full_date_shape_1.FullDatePartEnum.Time) {
75
- if (includeSeconds && fullDate.second == undefined) {
74
+ const seconds = includeSeconds
75
+ ? String(Math.abs(internalFullDate.second ?? 0)).padStart(2, '0')
76
+ : undefined;
77
+ if (includeSeconds && internalFullDate.second == undefined) {
76
78
  throw new Error('Tried to include seconds in the time string but no seconds were provided.');
77
79
  }
78
- if (fullDate.hour == undefined) {
80
+ if (internalFullDate.hour == undefined) {
79
81
  throw new Error(`Tried to create time string but no hour was provided.`);
80
82
  }
81
- if (fullDate.minute == undefined) {
83
+ if (internalFullDate.minute == undefined) {
82
84
  throw new Error(`Tried to create time string but no minutes were provided.`);
83
85
  }
84
86
  const timeParts = [
85
- String(Math.abs(fullDate.hour)).padStart(2, '0'),
86
- String(Math.abs(fullDate.minute)).padStart(2, '0'),
87
- includeSeconds ? String(Math.abs(fullDate.second ?? 0)).padStart(2, '0') : undefined,
87
+ String(Math.abs(internalFullDate.hour)).padStart(2, '0'),
88
+ String(Math.abs(internalFullDate.minute)).padStart(2, '0'),
89
+ seconds,
88
90
  ].filter((entry) => entry != undefined);
89
91
  return timeParts.join(':');
90
92
  }
93
+ else if (inputType === full_date_shape_1.FullDatePartEnum.DateTime) {
94
+ const datePart = toHtmlInputString(fullDate, full_date_shape_1.FullDatePartEnum.Date);
95
+ const timePart = toHtmlInputString(fullDate, full_date_shape_1.FullDatePartEnum.Time, includeSeconds);
96
+ return `${datePart}T${timePart}`;
97
+ }
91
98
  else {
92
99
  throw new Error(`Unexpected inputTyped: '${inputType}'. Expected usage of FullDatePartEnum or one of ${(0, common_1.getEnumTypedValues)(full_date_shape_1.FullDatePartEnum).join(', ')}.`);
93
100
  }
@@ -1,8 +1,13 @@
1
1
  import { Overwrite, RequiredBy } from '@augment-vir/common';
2
2
  import { Timezone } from '../timezone/timezone-names';
3
+ /**
4
+ * Represents various parts of a FullDate. Each value is also a valid "type" attribute for an
5
+ * <input> HTML element.
6
+ */
3
7
  export declare enum FullDatePartEnum {
4
8
  Date = "date",
5
- Time = "time"
9
+ Time = "time",
10
+ DateTime = "datetime-local"
6
11
  }
7
12
  export declare const timePartShape: import("object-shape-tester").ShapeDefinition<{
8
13
  /** Hour of the day in 24 time: 0-23 */
@@ -66,3 +71,6 @@ export declare const fullDateShape: import("object-shape-tester").ShapeDefinitio
66
71
  export type FullDate = Overwrite<(typeof fullDateShape)['runTimeType'], {
67
72
  timezone: Timezone;
68
73
  }>;
74
+ export type OnlyDatePart = Omit<DatePart, 'timezone'>;
75
+ export type OnlyHourMinutePart = Pick<Partial<FullDate>, 'hour' | 'minute'>;
76
+ export type OnlyHourMinuteSecondPart = Pick<Partial<FullDate>, 'hour' | 'minute' | 'second'>;
@@ -1,9 +1,14 @@
1
1
  import { and, defineShape } from 'object-shape-tester';
2
2
  import { utcTimezone } from '../timezone/timezones';
3
+ /**
4
+ * Represents various parts of a FullDate. Each value is also a valid "type" attribute for an
5
+ * <input> HTML element.
6
+ */
3
7
  export var FullDatePartEnum;
4
8
  (function (FullDatePartEnum) {
5
9
  FullDatePartEnum["Date"] = "date";
6
10
  FullDatePartEnum["Time"] = "time";
11
+ FullDatePartEnum["DateTime"] = "datetime-local";
7
12
  })(FullDatePartEnum || (FullDatePartEnum = {}));
8
13
  export const timePartShape = defineShape({
9
14
  /** Hour of the day in 24 time: 0-23 */
@@ -1,7 +1,6 @@
1
- import { PartialAndUndefined, RequiredBy } from '@augment-vir/common';
2
- import { SetOptional } from 'type-fest';
3
- import { DatePart, FullDate, FullDatePartEnum } from './full-date-shape';
4
- import { JustDateString, JustTimeString, JustTimeWithSecondsString, UtcIsoString } from './string-format-types';
1
+ import { PartialAndUndefined } from '@augment-vir/common';
2
+ import { FullDate, FullDatePartEnum, OnlyDatePart, OnlyHourMinutePart, OnlyHourMinuteSecondPart } from './full-date-shape';
3
+ import { DateTimeString, DateTimeWithSeconds, JustDateString, JustTimeString, JustTimeWithSecondsString, UtcIsoString } from './string-format-types';
5
4
  /** Convert a FullDate into a unix timestamp with milliseconds. */
6
5
  export declare function toTimestamp(fullDate: FullDate): number;
7
6
  /** Convert a FullDate into an ISO string in UTC; always ends in Z. */
@@ -18,9 +17,6 @@ export declare function toLocaleString(fullDate: FullDate, formatOptions?: (Intl
18
17
  * already has many formatting options.
19
18
  */
20
19
  export declare function toFormattedString(fullDate: FullDate, format: string, localeOverride?: string): string;
21
- type OnlyDatePart = SetOptional<DatePart, 'timezone'>;
22
- type HourMinutePart = RequiredBy<Partial<FullDate>, 'hour' | 'minute'>;
23
- type HourMinuteSecondPart = RequiredBy<Partial<FullDate>, 'hour' | 'minute' | 'second'>;
24
20
  /**
25
21
  * Converts a FullDate object into a string that is understood by type="date" or type="time"
26
22
  * HTMLInputElement instances. Can optionally include seconds as well for type="time".
@@ -29,11 +25,12 @@ type HourMinuteSecondPart = RequiredBy<Partial<FullDate>, 'hour' | 'minute' | 's
29
25
  * concatenated into a string. If you wish to convert time zones, first call toNewTimezone on your
30
26
  * FullDate object.
31
27
  */
28
+ export declare function toHtmlInputString(fullDate: Omit<FullDate, 'timezone'>, inputType: FullDatePartEnum.DateTime, includeSeconds: true): DateTimeWithSeconds;
29
+ export declare function toHtmlInputString(fullDate: Omit<FullDate, 'timezone'>, inputType: FullDatePartEnum.DateTime, includeSeconds?: false | undefined): DateTimeString;
32
30
  export declare function toHtmlInputString(fullDate: OnlyDatePart, inputType: FullDatePartEnum.Date): JustDateString;
33
- export declare function toHtmlInputString(fullDate: HourMinutePart, inputType: FullDatePartEnum.Time, includeSeconds?: false | undefined): JustTimeString;
34
- export declare function toHtmlInputString(fullDate: HourMinuteSecondPart, inputType: FullDatePartEnum.Time, includeSeconds: true): JustTimeWithSecondsString;
35
- export declare function toHtmlInputString(fullDate: HourMinutePart | HourMinuteSecondPart, inputType: FullDatePartEnum.Time, includeSeconds?: boolean | undefined): JustTimeWithSecondsString | JustTimeString;
36
- export declare function toHtmlInputString(fullDate: HourMinuteSecondPart | OnlyDatePart, inputType: FullDatePartEnum, includeSeconds: true): JustDateString | JustTimeWithSecondsString;
37
- export declare function toHtmlInputString(fullDate: HourMinutePart | OnlyDatePart, inputType: FullDatePartEnum, includeSeconds?: false | undefined): JustDateString | JustTimeString;
38
- export declare function toHtmlInputString(fullDate: HourMinutePart | HourMinuteSecondPart | OnlyDatePart, inputType: FullDatePartEnum, includeSeconds?: boolean | undefined): JustDateString | JustTimeWithSecondsString | JustTimeString;
39
- export {};
31
+ export declare function toHtmlInputString(fullDate: OnlyHourMinutePart, inputType: FullDatePartEnum.Time, includeSeconds?: false | undefined): JustTimeString;
32
+ export declare function toHtmlInputString(fullDate: OnlyHourMinuteSecondPart, inputType: FullDatePartEnum.Time, includeSeconds: true): JustTimeWithSecondsString;
33
+ export declare function toHtmlInputString(fullDate: OnlyHourMinutePart | OnlyHourMinuteSecondPart, inputType: FullDatePartEnum.Time, includeSeconds?: boolean | undefined): JustTimeWithSecondsString | JustTimeString;
34
+ export declare function toHtmlInputString(fullDate: OnlyHourMinuteSecondPart | OnlyDatePart, inputType: FullDatePartEnum, includeSeconds: true): JustDateString | JustTimeWithSecondsString;
35
+ export declare function toHtmlInputString(fullDate: OnlyHourMinutePart | OnlyDatePart, inputType: FullDatePartEnum, includeSeconds?: false | undefined): JustDateString | JustTimeString;
36
+ export declare function toHtmlInputString(fullDate: Omit<FullDate, 'timezone'> | OnlyHourMinutePart | OnlyHourMinuteSecondPart | OnlyDatePart, inputType: FullDatePartEnum, includeSeconds?: boolean | undefined): JustDateString | JustTimeWithSecondsString | JustTimeString | DateTimeWithSeconds | DateTimeString;
@@ -1,7 +1,7 @@
1
1
  import { getEnumTypedValues } from '@augment-vir/common';
2
2
  import { userLocale } from '../locales';
3
3
  import { utcTimezone } from '../timezone/timezones';
4
- import { FullDatePartEnum } from './full-date-shape';
4
+ import { FullDatePartEnum, } from './full-date-shape';
5
5
  import { toLuxonDateTime } from './luxon-date-time-conversion';
6
6
  /** Convert a FullDate into a unix timestamp with milliseconds. */
7
7
  export function toTimestamp(fullDate) {
@@ -41,46 +41,53 @@ export function toLocaleString(fullDate, formatOptions) {
41
41
  * already has many formatting options.
42
42
  */
43
43
  export function toFormattedString(fullDate, format, localeOverride = userLocale) {
44
- const formattedString = toLuxonDateTime(fullDate)
45
- .toFormat(format, { locale: localeOverride })
46
- .replace(/ /g, ' ');
44
+ const formattedString = toLuxonDateTime(fullDate).toFormat(format, { locale: localeOverride });
47
45
  return fixChromiumSpace(formattedString);
48
46
  }
49
47
  export function toHtmlInputString(fullDate, inputType, includeSeconds) {
48
+ const internalFullDate = fullDate;
50
49
  if (inputType === FullDatePartEnum.Date) {
51
- if (fullDate.year == undefined) {
50
+ if (internalFullDate.year == undefined) {
52
51
  throw new Error(`Tried to create date string but no year was provided.`);
53
52
  }
54
- if (fullDate.month == undefined) {
53
+ if (internalFullDate.month == undefined) {
55
54
  throw new Error(`Tried to create date string but no month was provided.`);
56
55
  }
57
- if (fullDate.day == undefined) {
56
+ if (internalFullDate.day == undefined) {
58
57
  throw new Error(`Tried to create date string but no day was provided.`);
59
58
  }
60
59
  const dateParts = [
61
- String(Math.abs(fullDate.year)).padStart(4, '0'),
62
- String(Math.abs(fullDate.month)).padStart(2, '0'),
63
- String(Math.abs(fullDate.day)).padStart(2, '0'),
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'),
64
63
  ];
65
64
  return dateParts.join('-');
66
65
  }
67
66
  else if (inputType === FullDatePartEnum.Time) {
68
- if (includeSeconds && fullDate.second == undefined) {
67
+ const seconds = includeSeconds
68
+ ? String(Math.abs(internalFullDate.second ?? 0)).padStart(2, '0')
69
+ : undefined;
70
+ if (includeSeconds && internalFullDate.second == undefined) {
69
71
  throw new Error('Tried to include seconds in the time string but no seconds were provided.');
70
72
  }
71
- if (fullDate.hour == undefined) {
73
+ if (internalFullDate.hour == undefined) {
72
74
  throw new Error(`Tried to create time string but no hour was provided.`);
73
75
  }
74
- if (fullDate.minute == undefined) {
76
+ if (internalFullDate.minute == undefined) {
75
77
  throw new Error(`Tried to create time string but no minutes were provided.`);
76
78
  }
77
79
  const timeParts = [
78
- String(Math.abs(fullDate.hour)).padStart(2, '0'),
79
- String(Math.abs(fullDate.minute)).padStart(2, '0'),
80
- includeSeconds ? String(Math.abs(fullDate.second ?? 0)).padStart(2, '0') : undefined,
80
+ String(Math.abs(internalFullDate.hour)).padStart(2, '0'),
81
+ String(Math.abs(internalFullDate.minute)).padStart(2, '0'),
82
+ seconds,
81
83
  ].filter((entry) => entry != undefined);
82
84
  return timeParts.join(':');
83
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
+ }
84
91
  else {
85
92
  throw new Error(`Unexpected inputTyped: '${inputType}'. Expected usage of FullDatePartEnum or one of ${getEnumTypedValues(FullDatePartEnum).join(', ')}.`);
86
93
  }
@@ -1,4 +1,6 @@
1
1
  export type JustDateString = `${number}-${number}-${number}`;
2
2
  export type JustTimeWithSecondsString = `${number}:${number}:${number}`;
3
3
  export type JustTimeString = `${number}:${number}`;
4
+ export type DateTimeWithSeconds = `${JustDateString}T${JustTimeWithSecondsString}`;
5
+ export type DateTimeString = `${JustDateString}T${JustTimeString}`;
4
6
  export type UtcIsoString = `${JustDateString}T${JustTimeWithSecondsString}.${number}Z`;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "date-vir",
3
- "version": "0.4.1",
3
+ "version": "0.5.0",
4
4
  "description": "Easy and explicit dates and times",
5
5
  "keywords": [
6
6
  "date",
@@ -40,15 +40,15 @@
40
40
  "test:watch": "web-test-runner --watch --color --config configs/web-test-runner.config.mjs"
41
41
  },
42
42
  "dependencies": {
43
- "@augment-vir/common": "^15.4.0",
44
- "@types/luxon": "^3.3.0",
43
+ "@augment-vir/common": "^16.2.0",
44
+ "@types/luxon": "^3.3.1",
45
45
  "luxon": "^3.3.0",
46
- "object-shape-tester": "^0.3.0",
47
- "type-fest": "^4.0.0"
46
+ "object-shape-tester": "^0.4.2",
47
+ "type-fest": "^4.2.0"
48
48
  },
49
49
  "devDependencies": {
50
- "@augment-vir/browser": "^15.4.0",
51
- "@augment-vir/browser-testing": "^15.4.0",
50
+ "@augment-vir/browser": "^16.2.0",
51
+ "@augment-vir/browser-testing": "^16.2.0",
52
52
  "@open-wc/testing": "^3.2.0",
53
53
  "@types/chai": "^4.3.5",
54
54
  "@types/mocha": "^10.0.1",
@@ -57,12 +57,12 @@
57
57
  "@web/test-runner-commands": "^0.8.0",
58
58
  "@web/test-runner-playwright": "^0.10.1",
59
59
  "@web/test-runner-visual-regression": "^0.8.2",
60
- "cspell": "^6.31.2",
61
- "element-vir": "^14.2.1",
62
- "esbuild": "^0.18.15",
60
+ "cspell": "^6.31.3",
61
+ "element-vir": "^16.3.1",
62
+ "esbuild": "^0.19.0",
63
63
  "istanbul-smart-text-reporter": "^1.1.2",
64
64
  "markdown-code-example-inserter": "^0.3.1",
65
- "npm-check-updates": "^16.10.16",
65
+ "npm-check-updates": "^16.10.18",
66
66
  "prettier": "^2.8.8",
67
67
  "prettier-plugin-interpolated-html-tags": "^0.0.4",
68
68
  "prettier-plugin-jsdoc": "^0.4.2",
@@ -72,6 +72,6 @@
72
72
  "prettier-plugin-sort-json": "^1.0.0",
73
73
  "prettier-plugin-toml": "^0.3.1",
74
74
  "typescript": "^5.1.6",
75
- "virmator": "^7.2.5"
75
+ "virmator": "^7.3.1"
76
76
  }
77
77
  }