date-vir 0.2.0 → 0.2.2

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,22 +1,45 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.combineDateParts = void 0;
4
+ const object_shape_tester_1 = require("object-shape-tester");
4
5
  const full_date_shape_1 = require("./full-date-shape");
5
- /**
6
- * Combine two parts of a FullDate objects into a filled-in FullDate. Note that the timezone is
7
- * taken from the time part. If two complete FullDate objects are passed in, only the respective
8
- * parts of each is used.
9
- */
10
- function combineDateParts(partialDates) {
11
- return {
12
- hour: partialDates[full_date_shape_1.FullDatePartEnum.Time].hour,
13
- minute: partialDates[full_date_shape_1.FullDatePartEnum.Time].minute,
14
- second: partialDates[full_date_shape_1.FullDatePartEnum.Time].second,
15
- millisecond: partialDates[full_date_shape_1.FullDatePartEnum.Time].millisecond,
16
- timezone: partialDates[full_date_shape_1.FullDatePartEnum.Time].timezone,
17
- year: partialDates[full_date_shape_1.FullDatePartEnum.Date].year,
18
- month: partialDates[full_date_shape_1.FullDatePartEnum.Date].month,
19
- day: partialDates[full_date_shape_1.FullDatePartEnum.Date].day,
20
- };
6
+ function combineDateParts(maybeDateParts) {
7
+ const timePart = (0, object_shape_tester_1.isValidShape)(maybeDateParts.time, full_date_shape_1.timePartShape, {
8
+ allowExtraKeys: true,
9
+ })
10
+ ? {
11
+ hour: maybeDateParts.time.hour,
12
+ minute: maybeDateParts.time.minute,
13
+ second: maybeDateParts.time.second,
14
+ millisecond: maybeDateParts.time.millisecond,
15
+ timezone: maybeDateParts.time.timezone,
16
+ }
17
+ : undefined;
18
+ const datePart = (0, object_shape_tester_1.isValidShape)(maybeDateParts.date, full_date_shape_1.datePartShape, {
19
+ allowExtraKeys: true,
20
+ })
21
+ ? {
22
+ year: maybeDateParts.date.year,
23
+ month: maybeDateParts.date.month,
24
+ day: maybeDateParts.date.day,
25
+ timezone: maybeDateParts.date.timezone,
26
+ }
27
+ : undefined;
28
+ if (timePart && datePart) {
29
+ if (timePart.timezone !== datePart.timezone) {
30
+ throw new Error(`Time and date parts have mismatching timezones. Got '${timePart.timezone}' and '${datePart.timezone}'.`);
31
+ }
32
+ return {
33
+ ...timePart,
34
+ ...datePart,
35
+ };
36
+ }
37
+ if (timePart) {
38
+ return timePart;
39
+ }
40
+ if (datePart) {
41
+ return datePart;
42
+ }
43
+ return undefined;
21
44
  }
22
45
  exports.combineDateParts = combineDateParts;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.fullDateShape = exports.dateOnlyShape = exports.timeOnlyShape = exports.FullDatePartEnum = void 0;
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
6
  var FullDatePartEnum;
@@ -8,7 +8,7 @@ var FullDatePartEnum;
8
8
  FullDatePartEnum["Date"] = "date";
9
9
  FullDatePartEnum["Time"] = "time";
10
10
  })(FullDatePartEnum = exports.FullDatePartEnum || (exports.FullDatePartEnum = {}));
11
- exports.timeOnlyShape = (0, object_shape_tester_1.defineShape)({
11
+ exports.timePartShape = (0, object_shape_tester_1.defineShape)({
12
12
  /** Hour of the day in 24 time: 0-23 */
13
13
  hour: 14,
14
14
  /** Minute of the hour: 0-59 */
@@ -20,7 +20,7 @@ exports.timeOnlyShape = (0, object_shape_tester_1.defineShape)({
20
20
  /** The timezone that this date/time is meant for / originated from. */
21
21
  timezone: timezones_1.utcTimezone,
22
22
  });
23
- exports.dateOnlyShape = (0, object_shape_tester_1.defineShape)({
23
+ exports.datePartShape = (0, object_shape_tester_1.defineShape)({
24
24
  /**
25
25
  * The full, four digit year.
26
26
  *
@@ -35,4 +35,4 @@ exports.dateOnlyShape = (0, object_shape_tester_1.defineShape)({
35
35
  /** The timezone that this date/time is meant for / originated from. */
36
36
  timezone: timezones_1.utcTimezone,
37
37
  });
38
- exports.fullDateShape = (0, object_shape_tester_1.defineShape)((0, object_shape_tester_1.and)(exports.dateOnlyShape, exports.timeOnlyShape));
38
+ exports.fullDateShape = (0, object_shape_tester_1.defineShape)((0, object_shape_tester_1.and)(exports.datePartShape, exports.timePartShape));
@@ -1,10 +1,37 @@
1
1
  import { DatePart, FullDate, FullDatePartEnum, TimePart } from './full-date-shape';
2
+ type MaybeDateParts = Partial<{
3
+ [FullDatePartEnum.Date]: DatePart | undefined | Partial<FullDate>;
4
+ [FullDatePartEnum.Time]: TimePart | undefined | Partial<FullDate>;
5
+ }>;
6
+ export type MaybeDatePart = FullDate | Partial<FullDate> | DatePart | TimePart | undefined;
2
7
  /**
3
- * Combine two parts of a FullDate objects into a filled-in FullDate. Note that the timezone is
4
- * taken from the time part. If two complete FullDate objects are passed in, only the respective
5
- * parts of each is used.
8
+ * Combine two parts of a FullDate objects into a filled-in FullDate. Note that the timezones must
9
+ * match for both inputs. If two complete FullDate objects are passed in, only the respective parts
10
+ * of each is used.
6
11
  */
7
- export declare function combineDateParts(partialDates: {
12
+ export declare function combineDateParts(maybeDateParts: {
8
13
  [FullDatePartEnum.Date]: DatePart;
9
14
  [FullDatePartEnum.Time]: TimePart;
10
15
  }): FullDate;
16
+ export declare function combineDateParts(maybeDateParts: {
17
+ [FullDatePartEnum.Date]?: undefined;
18
+ [FullDatePartEnum.Time]?: undefined;
19
+ }): undefined;
20
+ export declare function combineDateParts(maybeDateParts: {
21
+ [FullDatePartEnum.Date]: DatePart;
22
+ [FullDatePartEnum.Time]?: undefined;
23
+ }): DatePart;
24
+ export declare function combineDateParts(maybeDateParts: {
25
+ [FullDatePartEnum.Date]: DatePart;
26
+ [FullDatePartEnum.Time]?: TimePart | undefined | Partial<FullDate>;
27
+ }): FullDate | DatePart;
28
+ export declare function combineDateParts(maybeDateParts: {
29
+ [FullDatePartEnum.Date]?: undefined;
30
+ [FullDatePartEnum.Time]: TimePart;
31
+ }): TimePart;
32
+ export declare function combineDateParts(maybeDateParts: {
33
+ [FullDatePartEnum.Date]?: DatePart | undefined | Partial<FullDate>;
34
+ [FullDatePartEnum.Time]: TimePart;
35
+ }): FullDate | TimePart;
36
+ export declare function combineDateParts(maybeDateParts: MaybeDateParts): MaybeDatePart;
37
+ export {};
@@ -1,22 +1,45 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.combineDateParts = void 0;
4
+ const object_shape_tester_1 = require("object-shape-tester");
4
5
  const full_date_shape_1 = require("./full-date-shape");
5
- /**
6
- * Combine two parts of a FullDate objects into a filled-in FullDate. Note that the timezone is
7
- * taken from the time part. If two complete FullDate objects are passed in, only the respective
8
- * parts of each is used.
9
- */
10
- function combineDateParts(partialDates) {
11
- return {
12
- hour: partialDates[full_date_shape_1.FullDatePartEnum.Time].hour,
13
- minute: partialDates[full_date_shape_1.FullDatePartEnum.Time].minute,
14
- second: partialDates[full_date_shape_1.FullDatePartEnum.Time].second,
15
- millisecond: partialDates[full_date_shape_1.FullDatePartEnum.Time].millisecond,
16
- timezone: partialDates[full_date_shape_1.FullDatePartEnum.Time].timezone,
17
- year: partialDates[full_date_shape_1.FullDatePartEnum.Date].year,
18
- month: partialDates[full_date_shape_1.FullDatePartEnum.Date].month,
19
- day: partialDates[full_date_shape_1.FullDatePartEnum.Date].day,
20
- };
6
+ function combineDateParts(maybeDateParts) {
7
+ const timePart = (0, object_shape_tester_1.isValidShape)(maybeDateParts.time, full_date_shape_1.timePartShape, {
8
+ allowExtraKeys: true,
9
+ })
10
+ ? {
11
+ hour: maybeDateParts.time.hour,
12
+ minute: maybeDateParts.time.minute,
13
+ second: maybeDateParts.time.second,
14
+ millisecond: maybeDateParts.time.millisecond,
15
+ timezone: maybeDateParts.time.timezone,
16
+ }
17
+ : undefined;
18
+ const datePart = (0, object_shape_tester_1.isValidShape)(maybeDateParts.date, full_date_shape_1.datePartShape, {
19
+ allowExtraKeys: true,
20
+ })
21
+ ? {
22
+ year: maybeDateParts.date.year,
23
+ month: maybeDateParts.date.month,
24
+ day: maybeDateParts.date.day,
25
+ timezone: maybeDateParts.date.timezone,
26
+ }
27
+ : undefined;
28
+ if (timePart && datePart) {
29
+ if (timePart.timezone !== datePart.timezone) {
30
+ throw new Error(`Time and date parts have mismatching timezones. Got '${timePart.timezone}' and '${datePart.timezone}'.`);
31
+ }
32
+ return {
33
+ ...timePart,
34
+ ...datePart,
35
+ };
36
+ }
37
+ if (timePart) {
38
+ return timePart;
39
+ }
40
+ if (datePart) {
41
+ return datePart;
42
+ }
43
+ return undefined;
21
44
  }
22
45
  exports.combineDateParts = combineDateParts;
@@ -4,10 +4,7 @@ export declare enum FullDatePartEnum {
4
4
  Date = "date",
5
5
  Time = "time"
6
6
  }
7
- export type WithTimezone = {
8
- timezone: Timezone;
9
- };
10
- export declare const timeOnlyShape: import("object-shape-tester").ShapeDefinition<{
7
+ export declare const timePartShape: import("object-shape-tester").ShapeDefinition<{
11
8
  /** Hour of the day in 24 time: 0-23 */
12
9
  hour: number;
13
10
  /** Minute of the hour: 0-59 */
@@ -19,9 +16,8 @@ export declare const timeOnlyShape: import("object-shape-tester").ShapeDefinitio
19
16
  /** The timezone that this date/time is meant for / originated from. */
20
17
  timezone: "UTC";
21
18
  }>;
22
- type TimeOnlyPart = Overwrite<(typeof timeOnlyShape)['runTimeType'], WithTimezone>;
23
- export type TimePart = RequiredBy<Partial<FullDate>, keyof TimeOnlyPart>;
24
- export declare const dateOnlyShape: import("object-shape-tester").ShapeDefinition<{
19
+ export type TimePart = RequiredBy<Partial<FullDate>, keyof (typeof timePartShape)['runTimeType']>;
20
+ export declare const datePartShape: import("object-shape-tester").ShapeDefinition<{
25
21
  /**
26
22
  * The full, four digit year.
27
23
  *
@@ -36,8 +32,7 @@ export declare const dateOnlyShape: import("object-shape-tester").ShapeDefinitio
36
32
  /** The timezone that this date/time is meant for / originated from. */
37
33
  timezone: "UTC";
38
34
  }>;
39
- type DateOnlyPart = Overwrite<(typeof dateOnlyShape)['runTimeType'], WithTimezone>;
40
- export type DatePart = RequiredBy<Partial<FullDate>, keyof DateOnlyPart>;
35
+ export type DatePart = RequiredBy<Partial<FullDate>, keyof (typeof datePartShape)['runTimeType']>;
41
36
  export declare const fullDateShape: import("object-shape-tester").ShapeDefinition<import("object-shape-tester").ShapeAnd<[import("object-shape-tester").ShapeDefinition<{
42
37
  /**
43
38
  * The full, four digit year.
@@ -68,5 +63,6 @@ export declare const fullDateShape: import("object-shape-tester").ShapeDefinitio
68
63
  * A easily serializable (it fits into plain JSON) object that completely defines how a date is to
69
64
  * be represented with minimal data.
70
65
  */
71
- export type FullDate = Overwrite<(typeof fullDateShape)['runTimeType'], WithTimezone>;
72
- export {};
66
+ export type FullDate = Overwrite<(typeof fullDateShape)['runTimeType'], {
67
+ timezone: Timezone;
68
+ }>;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.fullDateShape = exports.dateOnlyShape = exports.timeOnlyShape = exports.FullDatePartEnum = void 0;
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
6
  var FullDatePartEnum;
@@ -8,7 +8,7 @@ var FullDatePartEnum;
8
8
  FullDatePartEnum["Date"] = "date";
9
9
  FullDatePartEnum["Time"] = "time";
10
10
  })(FullDatePartEnum = exports.FullDatePartEnum || (exports.FullDatePartEnum = {}));
11
- exports.timeOnlyShape = (0, object_shape_tester_1.defineShape)({
11
+ exports.timePartShape = (0, object_shape_tester_1.defineShape)({
12
12
  /** Hour of the day in 24 time: 0-23 */
13
13
  hour: 14,
14
14
  /** Minute of the hour: 0-59 */
@@ -20,7 +20,7 @@ exports.timeOnlyShape = (0, object_shape_tester_1.defineShape)({
20
20
  /** The timezone that this date/time is meant for / originated from. */
21
21
  timezone: timezones_1.utcTimezone,
22
22
  });
23
- exports.dateOnlyShape = (0, object_shape_tester_1.defineShape)({
23
+ exports.datePartShape = (0, object_shape_tester_1.defineShape)({
24
24
  /**
25
25
  * The full, four digit year.
26
26
  *
@@ -35,4 +35,4 @@ exports.dateOnlyShape = (0, object_shape_tester_1.defineShape)({
35
35
  /** The timezone that this date/time is meant for / originated from. */
36
36
  timezone: timezones_1.utcTimezone,
37
37
  });
38
- exports.fullDateShape = (0, object_shape_tester_1.defineShape)((0, object_shape_tester_1.and)(exports.dateOnlyShape, exports.timeOnlyShape));
38
+ exports.fullDateShape = (0, object_shape_tester_1.defineShape)((0, object_shape_tester_1.and)(exports.datePartShape, exports.timePartShape));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "date-vir",
3
- "version": "0.2.0",
3
+ "version": "0.2.2",
4
4
  "description": "Easy and explicit dates and times",
5
5
  "keywords": [
6
6
  "date",
@@ -42,7 +42,7 @@
42
42
  "@augment-vir/common": "^14.2.2",
43
43
  "@types/luxon": "^3.3.0",
44
44
  "luxon": "^3.3.0",
45
- "object-shape-tester": "^0.2.7",
45
+ "object-shape-tester": "^0.3.0",
46
46
  "type-fest": "^3.11.1"
47
47
  },
48
48
  "devDependencies": {
@@ -56,7 +56,7 @@
56
56
  "@web/test-runner-playwright": "^0.10.0",
57
57
  "@web/test-runner-visual-regression": "^0.8.0",
58
58
  "cspell": "^6.31.1",
59
- "element-vir": "^13.0.3",
59
+ "element-vir": "^13.0.4",
60
60
  "esbuild": "^0.17.19",
61
61
  "istanbul-smart-text-reporter": "^1.1.2",
62
62
  "markdown-code-example-inserter": "^0.3.1",