@fgv/ts-extras 5.1.0-6 → 5.1.0-8

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.
@@ -60,8 +60,41 @@ export const isoDate = new Conversion.BaseConverter((from) => {
60
60
  else if (from instanceof Date) {
61
61
  return succeed(from);
62
62
  }
63
+ else if (from instanceof DateTime) {
64
+ if (from.isValid) {
65
+ return succeed(from.toJSDate());
66
+ }
67
+ return fail(`Invalid date: ${from.invalidExplanation}`);
68
+ }
63
69
  return fail(`Cannot convert ${JSON.stringify(from)} to Date`);
64
70
  });
71
+ /**
72
+ * A `Converter` which converts an iso formatted string, a number or a `Date` object to
73
+ * a `DateTime` object.
74
+ * @public
75
+ */
76
+ export const isoDateTime = new Conversion.BaseConverter((from) => {
77
+ if (typeof from === 'string') {
78
+ const dt = DateTime.fromISO(from);
79
+ if (dt.isValid) {
80
+ return succeed(dt);
81
+ }
82
+ return fail(`Invalid date: ${dt.invalidExplanation}`);
83
+ }
84
+ else if (typeof from === 'number') {
85
+ return succeed(DateTime.fromMillis(from));
86
+ }
87
+ else if (from instanceof Date) {
88
+ return succeed(DateTime.fromJSDate(from));
89
+ }
90
+ else if (from instanceof DateTime) {
91
+ if (from.isValid) {
92
+ return succeed(from);
93
+ }
94
+ return fail(`Invalid date: ${from.invalidExplanation}`);
95
+ }
96
+ return fail(`Cannot convert ${JSON.stringify(from)} to DateTime`);
97
+ });
65
98
  /**
66
99
  * A helper function to create a `Converter` which converts `unknown` to {@link Experimental.ExtendedArray | ExtendedArray<T>}.
67
100
  * @remarks
@@ -1,5 +1,6 @@
1
1
  import { Conversion } from '@fgv/ts-utils';
2
2
  import { Converter } from '@fgv/ts-utils';
3
+ import { DateTime } from 'luxon';
3
4
  import { FileTree } from '@fgv/ts-json-base';
4
5
  import { Hash as Hash_2 } from '@fgv/ts-utils';
5
6
  import { JsonValue } from '@fgv/ts-json-base';
@@ -210,7 +211,8 @@ declare namespace Converters {
210
211
  extendedArrayOf,
211
212
  rangeTypeOf,
212
213
  rangeOf,
213
- isoDate
214
+ isoDate,
215
+ isoDateTime
214
216
  }
215
217
  }
216
218
  export { Converters }
@@ -1399,6 +1401,13 @@ declare function isKeyStoreFile(json: unknown): boolean;
1399
1401
  */
1400
1402
  declare const isoDate: Converter<Date, unknown>;
1401
1403
 
1404
+ /**
1405
+ * A `Converter` which converts an iso formatted string, a number or a `Date` object to
1406
+ * a `DateTime` object.
1407
+ * @public
1408
+ */
1409
+ declare const isoDateTime: Converter<DateTime, unknown>;
1410
+
1402
1411
  /**
1403
1412
  * Represents a variable reference extracted from a Mustache template.
1404
1413
  * @public
@@ -1,4 +1,5 @@
1
1
  import { Conversion, Converter, Result } from '@fgv/ts-utils';
2
+ import { DateTime } from 'luxon';
2
3
  import { ExtendedArray, RangeOf, RangeOfProperties } from '../experimental';
3
4
  /**
4
5
  * Helper function to create a `StringConverter` which converts
@@ -17,6 +18,12 @@ export declare function templateString(defaultContext?: unknown): Conversion.Str
17
18
  * @public
18
19
  */
19
20
  export declare const isoDate: Converter<Date, unknown>;
21
+ /**
22
+ * A `Converter` which converts an iso formatted string, a number or a `Date` object to
23
+ * a `DateTime` object.
24
+ * @public
25
+ */
26
+ export declare const isoDateTime: Converter<DateTime, unknown>;
20
27
  /**
21
28
  * A helper function to create a `Converter` which converts `unknown` to {@link Experimental.ExtendedArray | ExtendedArray<T>}.
22
29
  * @remarks
@@ -24,7 +24,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
24
24
  return (mod && mod.__esModule) ? mod : { "default": mod };
25
25
  };
26
26
  Object.defineProperty(exports, "__esModule", { value: true });
27
- exports.isoDate = void 0;
27
+ exports.isoDateTime = exports.isoDate = void 0;
28
28
  exports.templateString = templateString;
29
29
  exports.extendedArrayOf = extendedArrayOf;
30
30
  exports.rangeTypeOf = rangeTypeOf;
@@ -70,8 +70,41 @@ exports.isoDate = new ts_utils_1.Conversion.BaseConverter((from) => {
70
70
  else if (from instanceof Date) {
71
71
  return (0, ts_utils_1.succeed)(from);
72
72
  }
73
+ else if (from instanceof luxon_1.DateTime) {
74
+ if (from.isValid) {
75
+ return (0, ts_utils_1.succeed)(from.toJSDate());
76
+ }
77
+ return (0, ts_utils_1.fail)(`Invalid date: ${from.invalidExplanation}`);
78
+ }
73
79
  return (0, ts_utils_1.fail)(`Cannot convert ${JSON.stringify(from)} to Date`);
74
80
  });
81
+ /**
82
+ * A `Converter` which converts an iso formatted string, a number or a `Date` object to
83
+ * a `DateTime` object.
84
+ * @public
85
+ */
86
+ exports.isoDateTime = new ts_utils_1.Conversion.BaseConverter((from) => {
87
+ if (typeof from === 'string') {
88
+ const dt = luxon_1.DateTime.fromISO(from);
89
+ if (dt.isValid) {
90
+ return (0, ts_utils_1.succeed)(dt);
91
+ }
92
+ return (0, ts_utils_1.fail)(`Invalid date: ${dt.invalidExplanation}`);
93
+ }
94
+ else if (typeof from === 'number') {
95
+ return (0, ts_utils_1.succeed)(luxon_1.DateTime.fromMillis(from));
96
+ }
97
+ else if (from instanceof Date) {
98
+ return (0, ts_utils_1.succeed)(luxon_1.DateTime.fromJSDate(from));
99
+ }
100
+ else if (from instanceof luxon_1.DateTime) {
101
+ if (from.isValid) {
102
+ return (0, ts_utils_1.succeed)(from);
103
+ }
104
+ return (0, ts_utils_1.fail)(`Invalid date: ${from.invalidExplanation}`);
105
+ }
106
+ return (0, ts_utils_1.fail)(`Cannot convert ${JSON.stringify(from)} to DateTime`);
107
+ });
75
108
  /**
76
109
  * A helper function to create a `Converter` which converts `unknown` to {@link Experimental.ExtendedArray | ExtendedArray<T>}.
77
110
  * @remarks
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fgv/ts-extras",
3
- "version": "5.1.0-6",
3
+ "version": "5.1.0-8",
4
4
  "description": "Assorted Typescript Utilities",
5
5
  "main": "lib/index.js",
6
6
  "types": "dist/ts-extras.d.ts",
@@ -86,10 +86,10 @@
86
86
  "@types/js-yaml": "~4.0.9",
87
87
  "typedoc": "~0.28.16",
88
88
  "typedoc-plugin-markdown": "~4.9.0",
89
- "@fgv/heft-dual-rig": "5.1.0-6",
90
- "@fgv/typedoc-compact-theme": "5.1.0-6",
91
- "@fgv/ts-utils-jest": "5.1.0-6",
92
- "@fgv/ts-utils": "5.1.0-6"
89
+ "@fgv/typedoc-compact-theme": "5.1.0-8",
90
+ "@fgv/ts-utils-jest": "5.1.0-8",
91
+ "@fgv/heft-dual-rig": "5.1.0-8",
92
+ "@fgv/ts-utils": "5.1.0-8"
93
93
  },
94
94
  "dependencies": {
95
95
  "luxon": "^3.7.2",
@@ -97,10 +97,10 @@
97
97
  "papaparse": "^5.4.1",
98
98
  "fflate": "~0.8.2",
99
99
  "js-yaml": "~4.1.1",
100
- "@fgv/ts-json-base": "5.1.0-6"
100
+ "@fgv/ts-json-base": "5.1.0-8"
101
101
  },
102
102
  "peerDependencies": {
103
- "@fgv/ts-utils": "5.1.0-6"
103
+ "@fgv/ts-utils": "5.1.0-8"
104
104
  },
105
105
  "repository": {
106
106
  "type": "git",