1 | ;
|
2 | // Copyright IBM Corp. and LoopBack contributors 2017,2019. All Rights Reserved.
|
3 | // Node module: @loopback/repository
|
4 | // This file is licensed under the MIT License.
|
5 | // License text available at https://opensource.org/licenses/MIT
|
6 | Object.defineProperty(exports, "__esModule", { value: true });
|
7 | exports.DateType = void 0;
|
8 | const tslib_1 = require("tslib");
|
9 | const util_1 = tslib_1.__importDefault(require("util"));
|
10 | /* eslint-disable @typescript-eslint/no-explicit-any */
|
11 | /**
|
12 | * Date type
|
13 | */
|
14 | class DateType {
|
15 | constructor() {
|
16 | this.name = 'date';
|
17 | }
|
18 | isInstance(value) {
|
19 | return value == null || value instanceof Date;
|
20 | }
|
21 | isCoercible(value) {
|
22 | // Please note new Date(...) allows the following
|
23 | /*
|
24 | > new Date('1')
|
25 | 2001-01-01T08:00:00.000Z
|
26 | > new Date('0')
|
27 | 2000-01-01T08:00:00.000Z
|
28 | > new Date(1)
|
29 | 1970-01-01T00:00:00.001Z
|
30 | > new Date(0)
|
31 | 1970-01-01T00:00:00.000Z
|
32 | > new Date(true)
|
33 | 1970-01-01T00:00:00.001Z
|
34 | > new Date(false)
|
35 | 1970-01-01T00:00:00.000Z
|
36 | */
|
37 | return value == null || !isNaN(new Date(value).getTime());
|
38 | }
|
39 | defaultValue() {
|
40 | return new Date();
|
41 | }
|
42 | coerce(value) {
|
43 | if (value == null)
|
44 | return value;
|
45 | if (value instanceof Date) {
|
46 | return value;
|
47 | }
|
48 | const d = new Date(value);
|
49 | if (isNaN(d.getTime())) {
|
50 | const msg = util_1.default.format('Invalid %s: %j', this.name, value);
|
51 | throw new TypeError(msg);
|
52 | }
|
53 | return d;
|
54 | }
|
55 | serialize(value) {
|
56 | if (value == null)
|
57 | return value;
|
58 | return value.toJSON();
|
59 | }
|
60 | }
|
61 | exports.DateType = DateType;
|
62 | //# sourceMappingURL=date.js.map |
\ | No newline at end of file |