@eliasrrosa/tutorhub-public-assets 0.7.6 → 0.8.1

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.
Files changed (30) hide show
  1. package/dist/application/dtos/UserTimeSlotDTO.d.ts +11 -0
  2. package/dist/application/dtos/UserTimeSlotDTO.js +2 -0
  3. package/dist/application/helpers/filterAsync.d.ts +1 -0
  4. package/dist/application/helpers/filterAsync.js +18 -0
  5. package/dist/application/helpers/filterAsync.test.d.ts +1 -0
  6. package/dist/application/helpers/filterAsync.test.js +21 -0
  7. package/dist/application/helpers/tryCatch.d.ts +2 -0
  8. package/dist/application/helpers/tryCatch.js +31 -0
  9. package/dist/application/helpers/tryCatch.test.d.ts +1 -0
  10. package/dist/application/helpers/tryCatch.test.js +48 -0
  11. package/dist/domain/entities/Class.d.ts +0 -17
  12. package/dist/domain/entities/Class.js +0 -1
  13. package/dist/domain/entities/ISO8601DateTime.d.ts +36 -31
  14. package/dist/domain/entities/ISO8601DateTime.js +72 -34
  15. package/dist/domain/entities/ISO8601DateTime.test.js +51 -133
  16. package/dist/domain/entities/ISO8601DateValidator.d.ts +2 -1
  17. package/dist/domain/entities/ISO8601DateValidator.js +4 -3
  18. package/dist/domain/entities/ISO8601TimeValidator.d.ts +6 -3
  19. package/dist/domain/entities/ISO8601TimeValidator.js +6 -3
  20. package/dist/domain/entities/TimeUnitConverter.d.ts +20 -0
  21. package/dist/domain/entities/TimeUnitConverter.js +17 -0
  22. package/dist/domain/entities/TimeUnitConverter.test.d.ts +1 -0
  23. package/dist/domain/entities/TimeUnitConverter.test.js +110 -0
  24. package/dist/domain/types/ClassStatus copy.d.ts +4 -0
  25. package/dist/domain/types/ClassStatus copy.js +2 -0
  26. package/dist/index.cjs +98 -101
  27. package/dist/index.d.ts +3 -38
  28. package/dist/index.js +98 -99
  29. package/dist/infrastructure/restful/responses/GetFixedAvailableTimesResponseBody.d.ts +2 -2
  30. package/package.json +1 -1
@@ -0,0 +1,11 @@
1
+ export interface IUserTimeSlotDTO {
2
+ id: string;
3
+ userId: string;
4
+ groupId: string;
5
+ dayOfTheWeek: number;
6
+ startDateTime: Date;
7
+ endDateTime: Date;
8
+ isAvailableTime: boolean;
9
+ courseId?: string;
10
+ rescheduleRequestIds?: string[];
11
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1 @@
1
+ export declare function filterAsync<T>(arr: T[], predicate: (item: T) => Promise<boolean>): Promise<T[]>;
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.filterAsync = filterAsync;
13
+ function filterAsync(arr, predicate) {
14
+ return __awaiter(this, void 0, void 0, function* () {
15
+ const results = yield Promise.all(arr.map(predicate));
16
+ return arr.filter((_v, index) => results[index]);
17
+ });
18
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ const filterAsync_1 = require("./filterAsync");
13
+ describe("filterAsync", () => {
14
+ it("Should filter accepting async operations.", () => __awaiter(void 0, void 0, void 0, function* () {
15
+ const numbers = [1, 2, 3];
16
+ const filteredNumbers = yield (0, filterAsync_1.filterAsync)(numbers, (number) => __awaiter(void 0, void 0, void 0, function* () {
17
+ return number > 1;
18
+ }));
19
+ expect(filteredNumbers).toEqual([2, 3]);
20
+ }));
21
+ });
@@ -0,0 +1,2 @@
1
+ export declare function tryCatch<T, E>(callback: () => T, fallback: (error: unknown) => E): T | E;
2
+ export declare function tryCatchAsync<T, E>(callback: () => Promise<T>, fallback: (error: unknown) => Promise<E>): Promise<T | E>;
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.tryCatch = tryCatch;
13
+ exports.tryCatchAsync = tryCatchAsync;
14
+ function tryCatch(callback, fallback) {
15
+ try {
16
+ return callback();
17
+ }
18
+ catch (error) {
19
+ return fallback(error);
20
+ }
21
+ }
22
+ function tryCatchAsync(callback, fallback) {
23
+ return __awaiter(this, void 0, void 0, function* () {
24
+ try {
25
+ return yield callback();
26
+ }
27
+ catch (error) {
28
+ return yield fallback(error);
29
+ }
30
+ });
31
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,48 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ const tryCatch_1 = require("./tryCatch");
13
+ describe("tryCatch", () => {
14
+ it("Should return try return value if success.", () => {
15
+ const a = (0, tryCatch_1.tryCatch)(() => {
16
+ return "abc";
17
+ }, () => {
18
+ return new Error("Trycatch failed.");
19
+ });
20
+ expect(a).toEqual("abc");
21
+ });
22
+ it("Should return catch return value if error.", () => {
23
+ const a = (0, tryCatch_1.tryCatch)(() => {
24
+ throw new Error("Error!");
25
+ }, () => {
26
+ return new Error("Trycatch failed.");
27
+ });
28
+ expect(a).toBeInstanceOf(Error);
29
+ expect(a.message).toEqual("Trycatch failed.");
30
+ });
31
+ });
32
+ describe("tryCatchAsync", () => {
33
+ it("Should return try return value if success.", () => __awaiter(void 0, void 0, void 0, function* () {
34
+ const a = yield (0, tryCatch_1.tryCatchAsync)(() => __awaiter(void 0, void 0, void 0, function* () {
35
+ return "abc";
36
+ }), () => __awaiter(void 0, void 0, void 0, function* () { return new Error(); }));
37
+ expect(a).toEqual("abc");
38
+ }));
39
+ it("Should return catch return value if error.", () => __awaiter(void 0, void 0, void 0, function* () {
40
+ const a = yield (0, tryCatch_1.tryCatchAsync)(() => __awaiter(void 0, void 0, void 0, function* () {
41
+ throw new Error("Error!");
42
+ }), () => __awaiter(void 0, void 0, void 0, function* () {
43
+ return new Error("Trycatch failed.");
44
+ }));
45
+ expect(a).toBeInstanceOf(Error);
46
+ expect(a.message).toEqual("Trycatch failed.");
47
+ }));
48
+ });
@@ -1,17 +0,0 @@
1
- import { IClassStatus } from "./ClassStatus";
2
- export interface IClass {
3
- id: string;
4
- title: string;
5
- startDateTime: Date;
6
- endDateTime: Date;
7
- courseId: string;
8
- ownerId: string;
9
- status: IClassStatus;
10
- groupId?: string;
11
- description?: string;
12
- createdAt?: Date;
13
- updatedAt?: Date;
14
- paymentId?: string;
15
- meetingLink?: string;
16
- contractId?: string;
17
- }
@@ -1,2 +1 @@
1
1
  "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
@@ -1,39 +1,44 @@
1
- import { IDate } from "./Date";
2
- import { ITime } from "./Time";
3
1
  export interface IDateTime {
4
- getJSDateTime: () => Date;
2
+ getJsDateTime: () => Date;
5
3
  /**
6
- * Returns DateTime string with added Brazilian timezone (-03:00) without converting the date or time.
7
- * This function is analogous to ISO8601DateTime.getIsoString(-180)
8
- * @returns an ISO8601 string with Brazilian timezone
9
- */
10
- getBrazilianIsoString: () => string;
11
- /**
12
- * Returns DateTime string with added timezone (eg. +03:00, -01:00, .000Z) without converting the date or time.
13
- * @param timezoneOffsetMinutes defaults to 0.
14
- * Can be derived from the negative return value of Date.getTimezoneOffset()
15
- * @param newDate overrides the date for the output string, but does not change state.
16
- * @param newTime overrides the time for the output string, but does not change state.
17
- * @returns an ISO8601 string with the added timezone.
4
+ * @returns the same date but time is 00:00:00.000
18
5
  */
6
+ toMidnight: () => IDateTime;
19
7
  getIsoString: (opts?: {
20
- timezoneOffsetMinutes?: number;
21
- newDate?: IDate;
22
- newTime?: ITime;
8
+ timezone?: string | number;
23
9
  }) => string;
10
+ /**
11
+ * @returns a time string without timezone like "13:30:00"
12
+ */
13
+ getTimeString: () => string;
14
+ getHoursString: () => string;
15
+ getMinutesString: () => string;
16
+ getSecondsString: () => string;
17
+ getMilisecondsString: () => string;
24
18
  }
25
19
  export declare class ISO8601DateTime implements IDateTime {
26
- private date;
27
- private time;
28
- constructor(opts: {
29
- date: IDate;
30
- time: ITime;
31
- });
32
- getJSDateTime: () => Date;
33
- getBrazilianIsoString: () => string;
34
- getIsoString(opts?: {
35
- timezoneOffsetMinutes?: number;
36
- newDate?: IDate;
37
- newTime?: ITime;
38
- }): string;
20
+ private readonly timeFormatValidator;
21
+ private readonly dateFormatValidator;
22
+ private readonly dateString;
23
+ private readonly timeUnitConverter;
24
+ dateTimePattern: RegExp;
25
+ onlyDatePattern: RegExp;
26
+ constructor(
27
+ /**
28
+ * Accepts ISO8601 date time format with timezone.
29
+ * Eg:. `2025-01-01T00:00:00Z`, `2025-01-01T00:00:00.000Z`,
30
+ * `2025-01-01T00:00:00-03:00`, `2025-01-01T00:00:00+04:00`
31
+ */
32
+ dateString: string);
33
+ private normalizeDateString;
34
+ getTimeString: () => string;
35
+ getHoursString: () => string;
36
+ getMinutesString: () => string;
37
+ getSecondsString: () => string;
38
+ getMilisecondsString: () => string;
39
+ getJsDateTime: () => Date;
40
+ getIsoString: (opts?: {
41
+ timezone?: string | number;
42
+ }) => string;
43
+ toMidnight: () => IDateTime;
39
44
  }
@@ -1,44 +1,82 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ISO8601DateTime = void 0;
4
+ const ISO8601DateValidator_1 = require("./ISO8601DateValidator");
5
+ const ISO8601TimeValidator_1 = require("./ISO8601TimeValidator");
6
+ const ISO8601TimezoneParser_1 = require("./ISO8601TimezoneParser");
7
+ const ISO8601TimezoneValidator_1 = require("./ISO8601TimezoneValidator");
8
+ const TimeUnitConverter_1 = require("./TimeUnitConverter");
4
9
  class ISO8601DateTime {
5
- constructor(opts) {
6
- this.getJSDateTime = () => {
7
- return new Date(parseInt(this.date.getYear()), parseInt(this.date.getMonth()) - 1, parseInt(this.date.getDay()), parseInt(this.time.getHoursString()), parseInt(this.time.getMinutesString()), parseInt(this.time.getSecondsString()));
10
+ constructor(
11
+ /**
12
+ * Accepts ISO8601 date time format with timezone.
13
+ * Eg:. `2025-01-01T00:00:00Z`, `2025-01-01T00:00:00.000Z`,
14
+ * `2025-01-01T00:00:00-03:00`, `2025-01-01T00:00:00+04:00`
15
+ */
16
+ dateString) {
17
+ this.timeFormatValidator = new ISO8601TimeValidator_1.ISO8601TimeValidator();
18
+ this.dateFormatValidator = new ISO8601DateValidator_1.ISO8601DateValidator();
19
+ this.timeUnitConverter = new TimeUnitConverter_1.TimeUnitConverter();
20
+ this.dateTimePattern = new RegExp(`^${this.dateFormatValidator.YYYYmmDDpatternString}T(${this.timeFormatValidator.hhMMssPlusNumberedTimezonePatternString}|${this.timeFormatValidator.hhMMssSSSZPatternString}|${this.timeFormatValidator.hhMMssZPatternString})$`);
21
+ this.onlyDatePattern = new RegExp(`^${this.dateFormatValidator.YYYYmmDDpatternString}$`);
22
+ this.getTimeString = () => {
23
+ return `${this.getHoursString()}:${this.getMinutesString()}:${this.getSecondsString()}`;
8
24
  };
9
- this.getBrazilianIsoString = () => {
10
- return this.getIsoString({
11
- timezoneOffsetMinutes: -180,
12
- });
25
+ this.getHoursString = () => {
26
+ return this.dateString.split("T")[1].slice(0, 8).split(":")[0];
13
27
  };
14
- this.date = opts.date;
15
- this.time = opts.time;
28
+ this.getMinutesString = () => {
29
+ return this.dateString.split("T")[1].slice(0, 8).split(":")[1];
30
+ };
31
+ this.getSecondsString = () => {
32
+ return this.dateString.split("T")[1].slice(0, 8).split(":")[2];
33
+ };
34
+ this.getMilisecondsString = () => {
35
+ return this.dateString.split("T")[1].slice(0, 8).split(":")[3];
36
+ };
37
+ this.getJsDateTime = () => {
38
+ return new Date(this.dateString);
39
+ };
40
+ this.getIsoString = (opts) => {
41
+ if (!opts || !(opts === null || opts === void 0 ? void 0 : opts.timezone))
42
+ return this.getJsDateTime().toISOString();
43
+ const parser = new ISO8601TimezoneParser_1.ISO8601TimezoneParser();
44
+ const validator = new ISO8601TimezoneValidator_1.ISO8601TimezoneValidator();
45
+ const timezoneInMinutes = typeof opts.timezone == "string"
46
+ ? parser.toMinutes(opts.timezone)
47
+ : opts.timezone;
48
+ const isValid = validator.formatIsValid(timezoneInMinutes);
49
+ if (!isValid)
50
+ throw new Error("Invalid timezone format.");
51
+ const timezoneInMs = this.timeUnitConverter.minutesToMs(timezoneInMinutes);
52
+ const newDateUtcString = new Date(this.getJsDateTime().getTime() + timezoneInMs).toISOString();
53
+ const newDateUtcStringWithoutTimezone = newDateUtcString.split(".")[0];
54
+ const timezoneString = parser.toString(timezoneInMinutes);
55
+ const newDateWithTimezoneString = `${newDateUtcStringWithoutTimezone}${timezoneString}`;
56
+ return newDateWithTimezoneString;
57
+ };
58
+ this.toMidnight = () => {
59
+ const currentTimeString = this.getTimeString();
60
+ const matches = new RegExp(currentTimeString).exec(this.dateString);
61
+ if (!matches || matches.length < 1)
62
+ throw new Error("Error getting current time string.");
63
+ const datePlusT = this.dateString.slice(0, matches.index);
64
+ const matchedString = matches[0];
65
+ const timezone = this.dateString.slice(matchedString.length + matches.index);
66
+ return new ISO8601DateTime(`${datePlusT}00:00:00${timezone}`);
67
+ };
68
+ const normalizedDateString = this.normalizeDateString(dateString);
69
+ if (!normalizedDateString)
70
+ throw new Error("Date time format invalid.");
71
+ this.dateString = normalizedDateString;
16
72
  }
17
- getIsoString(opts) {
18
- const padLeft = (n) => String(n).padStart(2, "0");
19
- const newDateTime = new ISO8601DateTime({
20
- date: (opts === null || opts === void 0 ? void 0 : opts.newDate) || this.date,
21
- time: (opts === null || opts === void 0 ? void 0 : opts.newTime) || this.time,
22
- });
23
- const jsDate = newDateTime.getJSDateTime();
24
- const year = jsDate.getFullYear();
25
- const month = padLeft(jsDate.getMonth() + 1);
26
- const day = padLeft(jsDate.getDate());
27
- const hour = padLeft(jsDate.getHours());
28
- const minute = padLeft(jsDate.getMinutes());
29
- const second = padLeft(jsDate.getSeconds());
30
- const timezoneOffsetMinutes = (opts === null || opts === void 0 ? void 0 : opts.timezoneOffsetMinutes)
31
- ? opts.timezoneOffsetMinutes
32
- : 0;
33
- const sign = timezoneOffsetMinutes == 0
34
- ? undefined
35
- : timezoneOffsetMinutes < 0
36
- ? "-"
37
- : "+";
38
- const offsetHours = padLeft(Math.floor(Math.abs(timezoneOffsetMinutes) / 60));
39
- const offsetMinutes = padLeft(Math.abs(timezoneOffsetMinutes) % 60);
40
- const timezone = sign ? `${sign}${offsetHours}:${offsetMinutes}` : ".000Z";
41
- return `${year}-${month}-${day}T${hour}:${minute}:${second}${timezone}`;
73
+ normalizeDateString(dateString) {
74
+ if (this.onlyDatePattern.test(dateString)) {
75
+ return `${dateString}T00:00:00Z`;
76
+ }
77
+ if (this.dateTimePattern.test(dateString)) {
78
+ return dateString;
79
+ }
42
80
  }
43
81
  }
44
82
  exports.ISO8601DateTime = ISO8601DateTime;
@@ -1,146 +1,64 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- const ISO8601Date_1 = require("./ISO8601Date");
4
3
  const ISO8601DateTime_1 = require("./ISO8601DateTime");
5
- const ISO8601Time_1 = require("./ISO8601Time");
6
4
  describe("ISO8601DateTime", () => {
7
- describe("ISO8601DateTime getJSDateTime()", () => {
8
- it("Should return a JS Date object.", () => {
9
- const dateTime = new ISO8601DateTime_1.ISO8601DateTime({
10
- date: new ISO8601Date_1.ISO8601Date("2025-01-01"),
11
- time: new ISO8601Time_1.ISO8601Time("10:00:00"),
12
- });
13
- const jsDateTime = dateTime.getJSDateTime();
14
- expect(jsDateTime).toBeInstanceOf(Date);
5
+ describe("ISO8601DateTime constructor", () => {
6
+ it("Should throw given non ISO8601 format", () => {
7
+ const getDateTime = () => new ISO8601DateTime_1.ISO8601DateTime("abc");
8
+ expect(getDateTime).toThrow();
15
9
  });
16
- it("Should return the same year, month and day when returning UTC date.", () => {
17
- const dateTime = new ISO8601DateTime_1.ISO8601DateTime({
18
- date: new ISO8601Date_1.ISO8601Date("2025-01-02"),
19
- time: new ISO8601Time_1.ISO8601Time("00:00:00"),
20
- });
21
- const jsDateTime = dateTime.getJSDateTime();
22
- expect(jsDateTime.getUTCDate()).toBe(2);
23
- expect(jsDateTime.getUTCMonth()).toBe(0);
24
- expect(jsDateTime.getUTCFullYear()).toBe(2025);
25
- });
26
- it("Should return locale date string of same date and time as constructed.", () => {
27
- const dateTime = new ISO8601DateTime_1.ISO8601DateTime({
28
- date: new ISO8601Date_1.ISO8601Date("2025-01-02"),
29
- time: new ISO8601Time_1.ISO8601Time("01:02:03"),
30
- });
31
- const jsDateTime = dateTime.getJSDateTime();
32
- expect(jsDateTime.toLocaleDateString("pt-BR")).toBe("02/01/2025");
33
- });
34
- it("Should return ISO string converted to UTC.", () => {
35
- const dateTime = new ISO8601DateTime_1.ISO8601DateTime({
36
- date: new ISO8601Date_1.ISO8601Date("2025-01-02"),
37
- time: new ISO8601Time_1.ISO8601Time("01:02:03"),
38
- });
39
- const jsDateTime = dateTime.getJSDateTime();
40
- expect(jsDateTime.toISOString()).toBe("2025-01-02T04:02:03.000Z");
41
- });
42
- });
43
- describe("ISO8601DateTime.getBrazilianIsoString()", () => {
44
- it("Should return ISO string converted to Brazilian time.", () => {
45
- const dateTime = new ISO8601DateTime_1.ISO8601DateTime({
46
- date: new ISO8601Date_1.ISO8601Date("2025-01-02"),
47
- time: new ISO8601Time_1.ISO8601Time("01:02:03"),
48
- });
49
- const brazilianIsoString = dateTime.getBrazilianIsoString();
50
- expect(brazilianIsoString).toBe("2025-01-02T01:02:03-03:00");
10
+ it("Should not throw given non ISO8601 format but with no time", () => {
11
+ const getDateTime = () => new ISO8601DateTime_1.ISO8601DateTime("2025-01-01");
12
+ expect(getDateTime).not.toThrow();
51
13
  });
52
14
  });
53
15
  describe("ISO8601DateTime.getIsoString()", () => {
54
- it("Should return ISO UTC string if no arguments.", () => {
55
- const dateTime = new ISO8601DateTime_1.ISO8601DateTime({
56
- date: new ISO8601Date_1.ISO8601Date("2025-01-02"),
57
- time: new ISO8601Time_1.ISO8601Time("03:01:02"),
58
- });
59
- const utcIsoString = dateTime.getIsoString();
60
- expect(utcIsoString).toBe("2025-01-02T03:01:02.000Z");
61
- });
62
- it("Should return ISO string +01:00 for London timezone.", () => {
63
- const dateTime = new ISO8601DateTime_1.ISO8601DateTime({
64
- date: new ISO8601Date_1.ISO8601Date("2025-01-02"),
65
- time: new ISO8601Time_1.ISO8601Time("03:01:02"),
66
- });
67
- const isoString = dateTime.getIsoString({ timezoneOffsetMinutes: 60 });
68
- expect(isoString).toBe("2025-01-02T03:01:02+01:00");
69
- });
70
- it("Should return ISO string -05:00 for Chicago DST timezone.", () => {
71
- const dateTime = new ISO8601DateTime_1.ISO8601DateTime({
72
- date: new ISO8601Date_1.ISO8601Date("2025-01-02"),
73
- time: new ISO8601Time_1.ISO8601Time("03:01:02"),
74
- });
75
- const isoString = dateTime.getIsoString({
76
- timezoneOffsetMinutes: -300,
77
- });
78
- expect(isoString).toBe("2025-01-02T03:01:02-05:00");
16
+ it("Should convert to brazilian timezone properly.", () => {
17
+ expect(new ISO8601DateTime_1.ISO8601DateTime("2025-01-01T03:00:00Z").getIsoString({
18
+ timezone: "-03:00",
19
+ })).toEqual("2025-01-01T00:00:00-03:00");
20
+ expect(new ISO8601DateTime_1.ISO8601DateTime("2025-01-01T03:00:00Z").getIsoString({
21
+ timezone: -180,
22
+ })).toEqual("2025-01-01T00:00:00-03:00");
23
+ expect(new ISO8601DateTime_1.ISO8601DateTime("2025-01-01T03:00:00Z").getIsoString({
24
+ timezone: "-04:00",
25
+ })).toEqual("2024-12-31T23:00:00-04:00");
26
+ expect(new ISO8601DateTime_1.ISO8601DateTime("2025-01-01T00:00:00-03:00").getIsoString({
27
+ timezone: "Z",
28
+ })).toEqual("2025-01-01T03:00:00Z");
29
+ expect(new ISO8601DateTime_1.ISO8601DateTime("2025-01-01T00:00:00-03:00").getIsoString({
30
+ timezone: "-03:00",
31
+ })).toEqual("2025-01-01T00:00:00-03:00");
32
+ expect(new ISO8601DateTime_1.ISO8601DateTime("2025-01-01T00:00:00-03:00").getIsoString({
33
+ timezone: undefined,
34
+ })).toEqual("2025-01-01T03:00:00.000Z");
35
+ expect(new ISO8601DateTime_1.ISO8601DateTime("2025-01-01T00:00:00-03:00").getIsoString({
36
+ timezone: "-02:00",
37
+ })).toEqual("2025-01-01T01:00:00-02:00");
38
+ expect(new ISO8601DateTime_1.ISO8601DateTime("2025-01-01T00:00:00-03:00").getIsoString({
39
+ timezone: "+03:00",
40
+ })).toEqual("2025-01-01T06:00:00+03:00");
41
+ expect(new ISO8601DateTime_1.ISO8601DateTime("2025-01-01").getIsoString({
42
+ timezone: "+03:00",
43
+ })).toEqual("2025-01-01T03:00:00+03:00");
44
+ expect(new ISO8601DateTime_1.ISO8601DateTime("2025-01-01").getIsoString({
45
+ timezone: undefined,
46
+ })).toEqual("2025-01-01T00:00:00.000Z");
79
47
  });
80
- it("Should return ISO string -03:00 for Brazilian timezone.", () => {
81
- const dateTime = new ISO8601DateTime_1.ISO8601DateTime({
82
- date: new ISO8601Date_1.ISO8601Date("2025-01-02"),
83
- time: new ISO8601Time_1.ISO8601Time("03:01:02"),
84
- });
85
- const isoString = dateTime.getIsoString({
86
- timezoneOffsetMinutes: -180,
87
- });
88
- expect(isoString).toBe("2025-01-02T03:01:02-03:00");
89
- });
90
- it("Should return ISO string -03:00 for Brazilian timezone derived from Date.", () => {
91
- const dateTime = new ISO8601DateTime_1.ISO8601DateTime({
92
- date: new ISO8601Date_1.ISO8601Date("2025-01-02"),
93
- time: new ISO8601Time_1.ISO8601Time("03:01:02"),
94
- });
95
- const isoString = dateTime.getIsoString({
96
- timezoneOffsetMinutes: -new Date().getTimezoneOffset(),
97
- });
98
- expect(isoString).toBe("2025-01-02T03:01:02-03:00");
99
- });
100
- it("Should accept newDate and properly output it.", () => {
101
- const dateTime = new ISO8601DateTime_1.ISO8601DateTime({
102
- date: new ISO8601Date_1.ISO8601Date("2025-01-02"),
103
- time: new ISO8601Time_1.ISO8601Time("03:01:02"),
104
- });
105
- const isoString = dateTime.getIsoString({
106
- timezoneOffsetMinutes: -new Date().getTimezoneOffset(),
107
- newDate: new ISO8601Date_1.ISO8601Date("2025-01-03"),
108
- });
109
- expect(isoString).toBe("2025-01-03T03:01:02-03:00");
110
- });
111
- it("Should accept newTime and properly output it.", () => {
112
- const dateTime = new ISO8601DateTime_1.ISO8601DateTime({
113
- date: new ISO8601Date_1.ISO8601Date("2025-01-02"),
114
- time: new ISO8601Time_1.ISO8601Time("03:01:02"),
115
- });
116
- const isoString = dateTime.getIsoString({
117
- timezoneOffsetMinutes: -new Date().getTimezoneOffset(),
118
- newTime: new ISO8601Time_1.ISO8601Time("03:01:03")
119
- });
120
- expect(isoString).toBe("2025-01-02T03:01:03-03:00");
121
- });
122
- it("Should accept newTime and newDate and properly output them.", () => {
123
- const dateTime = new ISO8601DateTime_1.ISO8601DateTime({
124
- date: new ISO8601Date_1.ISO8601Date("2025-01-02"),
125
- time: new ISO8601Time_1.ISO8601Time("03:01:02"),
126
- });
127
- const isoString = dateTime.getIsoString({
128
- timezoneOffsetMinutes: -new Date().getTimezoneOffset(),
129
- newTime: new ISO8601Time_1.ISO8601Time("03:01:03"),
130
- newDate: new ISO8601Date_1.ISO8601Date("2025-01-03")
131
- });
132
- expect(isoString).toBe("2025-01-03T03:01:03-03:00");
48
+ });
49
+ describe("ISO8601DateTime.toMidnight()", () => {
50
+ it("Should return midnight of the same day.", () => {
51
+ expect(new ISO8601DateTime_1.ISO8601DateTime("2025-01-01T03:00:00Z").toMidnight().getJsDateTime()).toEqual(new Date("2025-01-01T00:00:00Z"));
52
+ expect(new ISO8601DateTime_1.ISO8601DateTime("2025-01-01T03:00:00-03:00")
53
+ .toMidnight()
54
+ .getJsDateTime()).toEqual(new Date("2025-01-01T00:00:00-03:00"));
133
55
  });
134
- it("Should accept newTime and newDate and properly output them, even when timezone is UTC.", () => {
135
- const dateTime = new ISO8601DateTime_1.ISO8601DateTime({
136
- date: new ISO8601Date_1.ISO8601Date("2025-01-02"),
137
- time: new ISO8601Time_1.ISO8601Time("03:01:02"),
138
- });
139
- const isoString = dateTime.getIsoString({
140
- newTime: new ISO8601Time_1.ISO8601Time("03:01:03"),
141
- newDate: new ISO8601Date_1.ISO8601Date("2025-01-03")
142
- });
143
- expect(isoString).toBe("2025-01-03T03:01:03.000Z");
56
+ });
57
+ describe("ISO8601DateTime.toMidnight().getIsoString()", () => {
58
+ it("Must take UTC ISO string and return it midnight", () => {
59
+ expect(new ISO8601DateTime_1.ISO8601DateTime("2025-01-01T03:00:00Z")
60
+ .toMidnight()
61
+ .getIsoString()).toEqual("2025-01-01T00:00:00.000Z");
144
62
  });
145
63
  });
146
64
  });
@@ -5,7 +5,8 @@ import { IDateValidator } from "./DateValidator";
5
5
  * @throws on get methods if format is wrong
6
6
  */
7
7
  export declare class ISO8601DateValidator implements IDateValidator {
8
- private YYYYmmDD;
8
+ YYYYmmDDpatternString: string;
9
+ YYYYmmDDpattern: RegExp;
9
10
  /**
10
11
  * Accepted format: YYYY-MM-DD
11
12
  */
@@ -8,16 +8,17 @@ exports.ISO8601DateValidator = void 0;
8
8
  */
9
9
  class ISO8601DateValidator {
10
10
  constructor() {
11
- this.YYYYmmDD = new RegExp("^[0-9]{4}-([0][1-9]|[1][0-2])-([0][1-9]|[1-2][0-9]|[3][0-1])");
11
+ this.YYYYmmDDpatternString = "[0-9]{4}-([0][1-9]|[1][0-2])-([0][1-9]|[1-2][0-9]|[3][0-1])";
12
+ this.YYYYmmDDpattern = new RegExp(`^${this.YYYYmmDDpatternString}`);
12
13
  }
13
14
  /**
14
15
  * Accepted format: YYYY-MM-DD
15
16
  */
16
17
  formatIsValid(date) {
17
- return this.YYYYmmDD.test(date);
18
+ return this.YYYYmmDDpattern.test(date);
18
19
  }
19
20
  match(date) {
20
- const matches = date.match(this.YYYYmmDD);
21
+ const matches = date.match(this.YYYYmmDDpattern);
21
22
  if (!matches || matches.length < 1) {
22
23
  return undefined;
23
24
  }
@@ -20,9 +20,12 @@ export declare class ISO8601TimeValidator implements IISO8601TimeValidator {
20
20
  private zAndMillisecondsPatternString;
21
21
  private hhMMssPattern;
22
22
  private hhMMPattern;
23
- private hhMMssPlusNumberedTimezonePattern;
24
- private hhMMssZPattern;
25
- private hhMMssSSSZPattern;
23
+ hhMMssPlusNumberedTimezonePatternString: string;
24
+ hhMMssPlusNumberedTimezonePattern: RegExp;
25
+ hhMMssZPatternString: string;
26
+ hhMMssZPattern: RegExp;
27
+ hhMMssSSSZPatternString: string;
28
+ hhMMssSSSZPattern: RegExp;
26
29
  formatIsValid(time: string): boolean;
27
30
  isHHmmSS(time: string): boolean;
28
31
  isHHmm(time: string): boolean;
@@ -13,9 +13,12 @@ class ISO8601TimeValidator {
13
13
  this.zAndMillisecondsPatternString = `.[0-9]{3}Z`;
14
14
  this.hhMMssPattern = new RegExp(`^${this.upTo23}:${this.upTo59}:${this.upTo59}$`);
15
15
  this.hhMMPattern = new RegExp(`^${this.upTo23}:${this.upTo59}$`);
16
- this.hhMMssPlusNumberedTimezonePattern = new RegExp(`^${this.upTo23}:${this.upTo59}:${this.upTo59}${this.numberedTimezonePatternString}$`);
17
- this.hhMMssZPattern = new RegExp(`^${this.upTo23}:${this.upTo59}:${this.upTo59}${this.zTimezonePatternString}$`);
18
- this.hhMMssSSSZPattern = new RegExp(`^${this.upTo23}:${this.upTo59}:${this.upTo59}${this.zAndMillisecondsPatternString}$`);
16
+ this.hhMMssPlusNumberedTimezonePatternString = `${this.upTo23}:${this.upTo59}:${this.upTo59}${this.numberedTimezonePatternString}`;
17
+ this.hhMMssPlusNumberedTimezonePattern = new RegExp(`^${this.hhMMssPlusNumberedTimezonePatternString}$`);
18
+ this.hhMMssZPatternString = `${this.upTo23}:${this.upTo59}:${this.upTo59}${this.zTimezonePatternString}`;
19
+ this.hhMMssZPattern = new RegExp(`^${this.hhMMssZPatternString}$`);
20
+ this.hhMMssSSSZPatternString = `${this.upTo23}:${this.upTo59}:${this.upTo59}${this.zAndMillisecondsPatternString}`;
21
+ this.hhMMssSSSZPattern = new RegExp(`^${this.hhMMssSSSZPatternString}$`);
19
22
  this.isHHmmSSPlusNumberedTimezone = (time) => {
20
23
  return this.hhMMssPlusNumberedTimezonePattern.test(time);
21
24
  };