From ba5c20efbdbff94ae0bc6596237730e5aa27cde5 Mon Sep 17 00:00:00 2001 From: mShan0 <96149598+mShan0@users.noreply.github.com> Date: Tue, 26 Jul 2022 16:05:50 -0700 Subject: [PATCH] refactor: replace `@js-joda/core` with `temporal` in Date --- package-lock.json | 16 ++++++++++++++++ package.json | 1 + src/data-types/date.ts | 25 ++++++++++++++----------- 3 files changed, 31 insertions(+), 11 deletions(-) diff --git a/package-lock.json b/package-lock.json index 4def8c522..d6de2db85 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2861,6 +2861,22 @@ "resolved": "https://registry.npmjs.org/@js-joda/core/-/core-5.2.0.tgz", "integrity": "sha512-0OriPYIaMLB3XiLQMe0BXKVIqeriTn3H7JMOzTsHEtt7Zqq+TetCu97KnAhU3ckiQZKBxfZshft+H1OC4D1lXw==" }, + "@js-temporal/polyfill": { + "version": "0.4.2", + "resolved": "https://registry.npmjs.org/@js-temporal/polyfill/-/polyfill-0.4.2.tgz", + "integrity": "sha512-c85vRxyqnJaXKyf4tvYij8jwiVIZhNLYDI9C4LLuOwVEHf4HUqGg07BBn70Le71W193QT/vmKg3jPUyQxJRHKQ==", + "requires": { + "jsbi": "^4.1.0", + "tslib": "^2.3.1" + }, + "dependencies": { + "tslib": { + "version": "2.4.0", + "resolved": "https://registry.npmjs.org/tslib/-/tslib-2.4.0.tgz", + "integrity": "sha512-d6xOpEDfsi2CZVlPQzGeux8XMwLT9hssAsaPYExaQMuYskwb+x1x7J371tWlbBdWHroy99KnVB6qIkUbs5X3UQ==" + } + } + }, "@nicolo-ribaudo/chokidar-2": { "version": "2.1.8-no-fsevents.3", "resolved": "https://registry.npmjs.org/@nicolo-ribaudo/chokidar-2/-/chokidar-2-2.1.8-no-fsevents.3.tgz", diff --git a/package.json b/package.json index c5f4a6bd8..8d0d56c51 100644 --- a/package.json +++ b/package.json @@ -43,6 +43,7 @@ "@azure/identity": "^2.0.4", "@azure/keyvault-keys": "^4.4.0", "@js-joda/core": "^5.2.0", + "@js-temporal/polyfill": "^0.4.2", "@types/es-aggregate-error": "^1.0.2", "bl": "^5.0.0", "es-aggregate-error": "^1.0.8", diff --git a/src/data-types/date.ts b/src/data-types/date.ts index 6cf7a547f..c5d1aa5cf 100644 --- a/src/data-types/date.ts +++ b/src/data-types/date.ts @@ -1,12 +1,11 @@ import { DataType } from '../data-type'; -import { ChronoUnit, LocalDate } from '@js-joda/core'; +import { Temporal } from '@js-temporal/polyfill'; // globalDate is to be used for JavaScript's global 'Date' object to avoid name clashing with the 'Date' constant below -const globalDate = global.Date; -const EPOCH_DATE = LocalDate.ofYearDay(1, 1); +const EPOCH_DATE = new Temporal.PlainDate(1, 1, 1); const NULL_LENGTH = Buffer.from([0x00]); const DATA_LENGTH = Buffer.from([0x03]); - +console.log(EPOCH_DATE.toString()); const Date: DataType = { id: 0x28, type: 'DATEN', @@ -37,27 +36,31 @@ const Date: DataType = { let date; if (options.useUTC) { - date = LocalDate.of(value.getUTCFullYear(), value.getUTCMonth() + 1, value.getUTCDate()); + date = new Temporal.PlainDate(value.getUTCFullYear(), value.getUTCMonth() + 1, value.getUTCDate()); } else { - date = LocalDate.of(value.getFullYear(), value.getMonth() + 1, value.getDate()); + date = new Temporal.PlainDate(value.getFullYear(), value.getMonth() + 1, value.getDate()); } - - const days = EPOCH_DATE.until(date, ChronoUnit.DAYS); + const days = EPOCH_DATE.until(date).days; const buffer = Buffer.alloc(3); buffer.writeUIntLE(days, 0, 3); yield buffer; }, // TODO: value is techincally of type 'unknown'. - validate: function(value): null | Date { + validate: function(value): null | Temporal.PlainDate { if (value == null) { return null; } - if (!(value instanceof globalDate)) { - value = new globalDate(globalDate.parse(value)); + try { + if (!(value instanceof Temporal.PlainDate)) { + value = Temporal.PlainDate.from(value); + } + } catch { + throw new TypeError('Invalid date.'); } + if (isNaN(value)) { throw new TypeError('Invalid date.'); }