@eliasrrosa/tutorhub-public-assets 0.0.7 → 0.0.9

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.
@@ -2,7 +2,7 @@ import { IISO8601TimeValidator } from "./ISO8601TimeValidator";
2
2
  import { ITime } from "./Time";
3
3
  /**
4
4
  * Interface for interacting with ISO8601 time formats.
5
- * Accepted formats: HH:MM:SS and HH:MM.
5
+ * Accepted formats: HH:MM:SS, HH:MM, HH:MM:SSZ, HH:MM:SS.sssZ and HH:MM:SS(+|-)HH:MM.
6
6
  *
7
7
  * @throws if instantiated with a different format
8
8
  * @throws if any method is called with a different format
@@ -10,13 +10,16 @@ import { ITime } from "./Time";
10
10
  export declare class ISO8601Time implements ITime {
11
11
  readonly value: string;
12
12
  readonly validator: IISO8601TimeValidator;
13
+ readonly timezone?: string;
13
14
  /**
14
15
  * Throws if format is invalid.
15
16
  * Accepted formats: HH:MM:SS and HH:MM.
16
17
  */
17
18
  constructor(value?: string);
18
19
  private validateFormatOrThrow;
19
- getTime(): string;
20
+ getTime(opts?: {
21
+ format?: "HH:MM" | "HH:MM:SS";
22
+ }): string;
20
23
  getMinutes(): string;
21
24
  getHours(): string;
22
25
  getSeconds(): string;
@@ -4,7 +4,7 @@ exports.ISO8601Time = void 0;
4
4
  const ISO8601TimeValidator_1 = require("./ISO8601TimeValidator");
5
5
  /**
6
6
  * Interface for interacting with ISO8601 time formats.
7
- * Accepted formats: HH:MM:SS and HH:MM.
7
+ * Accepted formats: HH:MM:SS, HH:MM, HH:MM:SSZ, HH:MM:SS.sssZ and HH:MM:SS(+|-)HH:MM.
8
8
  *
9
9
  * @throws if instantiated with a different format
10
10
  * @throws if any method is called with a different format
@@ -24,12 +24,23 @@ class ISO8601Time {
24
24
  if (!this.validator.formatIsValid(this.value))
25
25
  throw new Error("Time format must be ISO8601.");
26
26
  }
27
- getTime() {
27
+ getTime(opts) {
28
28
  this.validateFormatOrThrow();
29
- if (this.validator.isHHmm(this.value)) {
30
- return `${this.value}:00`;
29
+ if ((opts === null || opts === void 0 ? void 0 : opts.format) == "HH:MM" && this.validator.isHHmm(this.value)) {
30
+ return this.value;
31
31
  }
32
- return this.value;
32
+ if (((opts === null || opts === void 0 ? void 0 : opts.format) == "HH:MM:SS" || !(opts === null || opts === void 0 ? void 0 : opts.format)) &&
33
+ this.validator.isHHmmSS(this.value)) {
34
+ return this.value;
35
+ }
36
+ if ((opts === null || opts === void 0 ? void 0 : opts.format) == "HH:MM" && this.validator.isHHmmSS(this.value)) {
37
+ return `${this.getHours()}:${this.getMinutes()}`;
38
+ }
39
+ if (((opts === null || opts === void 0 ? void 0 : opts.format) == "HH:MM:SS" || !(opts === null || opts === void 0 ? void 0 : opts.format)) &&
40
+ this.validator.isHHmm(this.value)) {
41
+ return `${this.getHours()}:${this.getMinutes()}:${this.getSeconds()}`;
42
+ }
43
+ throw new Error("Time format is invalid.");
33
44
  }
34
45
  getMinutes() {
35
46
  this.validateFormatOrThrow();
@@ -20,6 +20,46 @@ describe("ISO8601Time", () => {
20
20
  const time = new ISO8601Time_1.ISO8601Time();
21
21
  expect(time.value).toBe("23:00:00");
22
22
  });
23
+ it("Should throw given irregular time format", () => {
24
+ const time = () => new ISO8601Time_1.ISO8601Time("000:00:10");
25
+ expect(time).toThrow();
26
+ });
27
+ it("Should not throw given HH:MM format", () => {
28
+ const time = () => new ISO8601Time_1.ISO8601Time("10:00");
29
+ expect(time).not.toThrow();
30
+ });
31
+ it("Should not throw given HH:MM:SS format", () => {
32
+ const time = () => new ISO8601Time_1.ISO8601Time("10:00:00");
33
+ expect(time).not.toThrow();
34
+ });
35
+ it("Should throw given invalid HH:MM:SS format", () => {
36
+ const time = () => new ISO8601Time_1.ISO8601Time("25:00:00");
37
+ expect(time).toThrow();
38
+ });
39
+ it("Should throw given invalid HH:MM format", () => {
40
+ const time = () => new ISO8601Time_1.ISO8601Time("12:60");
41
+ expect(time).toThrow();
42
+ });
43
+ it("Should throw given invalid negative timezone", () => {
44
+ const time = () => new ISO8601Time_1.ISO8601Time("12:00:00-15:00");
45
+ expect(time).toThrow();
46
+ });
47
+ it("Should throw given invalid positive timezone", () => {
48
+ const time = () => new ISO8601Time_1.ISO8601Time("12:00:00+13:00");
49
+ expect(time).toThrow();
50
+ });
51
+ it("Should not throw given HH:MM:SS(+|-)HH:MM format", () => {
52
+ const time = () => new ISO8601Time_1.ISO8601Time("12:00:00-03:00");
53
+ expect(time).not.toThrow();
54
+ });
55
+ it("Should not throw given HH:MM:SS.sssZ format", () => {
56
+ const time = () => new ISO8601Time_1.ISO8601Time("12:00:00.000Z");
57
+ expect(time).not.toThrow();
58
+ });
59
+ it("Should not throw given HH:MM:SSZ format", () => {
60
+ const time = () => new ISO8601Time_1.ISO8601Time("12:00:00Z");
61
+ expect(time).not.toThrow();
62
+ });
23
63
  });
24
64
  describe("ISO8601Time.getNow()", () => {
25
65
  it("Should get now time.", () => {
@@ -59,5 +99,25 @@ describe("ISO8601Time", () => {
59
99
  const time2 = new ISO8601Time_1.ISO8601Time("00:01:02");
60
100
  expect(time2.getTime()).toBe("00:01:02");
61
101
  });
102
+ it("Should return HH:MM if format argument is passed", () => {
103
+ const time = new ISO8601Time_1.ISO8601Time("00:01");
104
+ expect(time.getTime({ format: "HH:MM" })).toBe("00:01");
105
+ });
106
+ it("Should return HH:MM if format argument is passed and time format is HH:MM:SS", () => {
107
+ const time = new ISO8601Time_1.ISO8601Time("00:01:10");
108
+ expect(time.getTime({ format: "HH:MM" })).toBe("00:01");
109
+ });
110
+ it("Should return HH:MM:SS by default", () => {
111
+ const time = new ISO8601Time_1.ISO8601Time("00:01:10");
112
+ expect(time.getTime()).toBe("00:01:10");
113
+ });
114
+ it("Should return HH:MM:SS by default even if format is HH:MM", () => {
115
+ const time = new ISO8601Time_1.ISO8601Time("00:01");
116
+ expect(time.getTime()).toBe("00:01:00");
117
+ });
118
+ it("Should return HH:MM:SS given format argument", () => {
119
+ const time = new ISO8601Time_1.ISO8601Time("00:01");
120
+ expect(time.getTime({ format: "HH:MM:SS" })).toBe("00:01:00");
121
+ });
62
122
  });
63
123
  });
@@ -2,11 +2,24 @@ import { ITimeValidator } from "./TimeValidator";
2
2
  export interface IISO8601TimeValidator extends ITimeValidator {
3
3
  isHHmmSS: (time: string) => boolean;
4
4
  isHHmm: (time: string) => boolean;
5
+ isHHmmSSPlusNumberedTimezone: (time: string) => boolean;
6
+ isHHmmSSsssZ: (time: string) => boolean;
7
+ isHHmmSSZ: (time: string) => boolean;
8
+ /** Accepted formats: `HH:MM:SS`, `HH:MM`, `HH:MM:SSZ`, `HH:MM:SS.sssZ` and `HH:MM:SS(+|-)HH:MM`. */
9
+ formatIsValid: (time: string) => boolean;
5
10
  }
6
- export declare class ISO8601TimeValidator implements ITimeValidator {
11
+ export declare class ISO8601TimeValidator implements IISO8601TimeValidator {
12
+ private upTo23;
13
+ private upTo59;
7
14
  private hhMMssPattern;
8
15
  private hhMMPattern;
16
+ private hhMMssPlusNumberedTimezonePattern;
17
+ private hhMMssZPattern;
18
+ private hhMMssSSSZPattern;
9
19
  formatIsValid(time: string): boolean;
10
20
  isHHmmSS(time: string): boolean;
11
21
  isHHmm(time: string): boolean;
22
+ isHHmmSSPlusNumberedTimezone: (time: string) => boolean;
23
+ isHHmmSSZ: (time: string) => boolean;
24
+ isHHmmSSsssZ: (time: string) => boolean;
12
25
  }
@@ -3,11 +3,29 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ISO8601TimeValidator = void 0;
4
4
  class ISO8601TimeValidator {
5
5
  constructor() {
6
- this.hhMMssPattern = new RegExp("^([0-1][0-9]|[2][0-3]):[0-5][0-9]:[0-5][0-9]$");
7
- this.hhMMPattern = new RegExp("^([0-1][0-9]|[2][0-3]):[0-5][0-9]$");
6
+ this.upTo23 = "([0-1][0-9]|[2][0-3])";
7
+ this.upTo59 = "[0-5][0-9]";
8
+ this.hhMMssPattern = new RegExp(`^${this.upTo23}:${this.upTo59}:${this.upTo59}$`);
9
+ this.hhMMPattern = new RegExp(`^${this.upTo23}:${this.upTo59}$`);
10
+ this.hhMMssPlusNumberedTimezonePattern = new RegExp(`^${this.upTo23}:${this.upTo59}:${this.upTo59}(\\+((0[0-9]|1[0-2]):${this.upTo59})|\\-((0[0-9]|1[0-4]):${this.upTo59}))$`);
11
+ this.hhMMssZPattern = new RegExp(`^${this.upTo23}:${this.upTo59}:${this.upTo59}Z$`);
12
+ this.hhMMssSSSZPattern = new RegExp(`^${this.upTo23}:${this.upTo59}:${this.upTo59}.[0-9]{3}Z$`);
13
+ this.isHHmmSSPlusNumberedTimezone = (time) => {
14
+ return this.hhMMssPlusNumberedTimezonePattern.test(time);
15
+ };
16
+ this.isHHmmSSZ = (time) => {
17
+ return this.hhMMssZPattern.test(time);
18
+ };
19
+ this.isHHmmSSsssZ = (time) => {
20
+ return this.hhMMssSSSZPattern.test(time);
21
+ };
8
22
  }
9
23
  formatIsValid(time) {
10
- return this.isHHmm(time) || this.isHHmmSS(time);
24
+ return (this.isHHmm(time) ||
25
+ this.isHHmmSS(time) ||
26
+ this.isHHmmSSPlusNumberedTimezone(time) ||
27
+ this.isHHmmSSZ(time) ||
28
+ this.isHHmmSSsssZ(time));
11
29
  }
12
30
  isHHmmSS(time) {
13
31
  return this.hhMMssPattern.test(time);
@@ -4,12 +4,24 @@ const ISO8601TimeValidator_1 = require("./ISO8601TimeValidator");
4
4
  describe("ISO8601TimeValidator", () => {
5
5
  const validator = new ISO8601TimeValidator_1.ISO8601TimeValidator();
6
6
  describe("ISO8601TimeValidator.formatIsValid()", () => {
7
- it("Should not accept ISO8601 timestamp with offset.", () => {
7
+ it("Should accept ISO8601 timestamp with offset.", () => {
8
8
  const time = "00:00:00-03:00";
9
- expect(validator.formatIsValid(time)).toBe(false);
9
+ expect(validator.formatIsValid(time)).toBe(true);
10
10
  });
11
- it("Should not accept ISO8601 format with timestamp.", () => {
11
+ it("Should accept ISO8601 format with another offset.", () => {
12
12
  const time = "00:00:00-02:00";
13
+ expect(validator.formatIsValid(time)).toBe(true);
14
+ });
15
+ it("Should accept HH:MM:SSZ format.", () => {
16
+ const time = "00:00:00Z";
17
+ expect(validator.formatIsValid(time)).toBe(true);
18
+ });
19
+ it("Should accept HH:MM:SS.sssZ format.", () => {
20
+ const time = "00:00:00.000Z";
21
+ expect(validator.formatIsValid(time)).toBe(true);
22
+ });
23
+ it("Should not accept HH:MM:SS.sssZ format with non-numeric digits.", () => {
24
+ const time = "00:00:00.00dZ";
13
25
  expect(validator.formatIsValid(time)).toBe(false);
14
26
  });
15
27
  it("Should not accept non-ISO8601 formats.", () => {
@@ -6,4 +6,5 @@ export interface ITime {
6
6
  getMinutes: () => string;
7
7
  getSeconds: () => string;
8
8
  getNow: () => string;
9
+ readonly timezone?: string;
9
10
  }
package/dist/index.cjs CHANGED
@@ -224,15 +224,33 @@ var ISO8601DateTime = class _ISO8601DateTime {
224
224
  // src/domain/entities/ISO8601TimeValidator.ts
225
225
  var ISO8601TimeValidator = class {
226
226
  constructor() {
227
+ this.upTo23 = "([0-1][0-9]|[2][0-3])";
228
+ this.upTo59 = "[0-5][0-9]";
227
229
  this.hhMMssPattern = new RegExp(
228
- "^([0-1][0-9]|[2][0-3]):[0-5][0-9]:[0-5][0-9]$"
230
+ `^${this.upTo23}:${this.upTo59}:${this.upTo59}$`
229
231
  );
230
- this.hhMMPattern = new RegExp(
231
- "^([0-1][0-9]|[2][0-3]):[0-5][0-9]$"
232
+ this.hhMMPattern = new RegExp(`^${this.upTo23}:${this.upTo59}$`);
233
+ this.hhMMssPlusNumberedTimezonePattern = new RegExp(
234
+ `^${this.upTo23}:${this.upTo59}:${this.upTo59}(\\+((0[0-9]|1[0-2]):${this.upTo59})|\\-((0[0-9]|1[0-4]):${this.upTo59}))$`
232
235
  );
236
+ this.hhMMssZPattern = new RegExp(
237
+ `^${this.upTo23}:${this.upTo59}:${this.upTo59}Z$`
238
+ );
239
+ this.hhMMssSSSZPattern = new RegExp(
240
+ `^${this.upTo23}:${this.upTo59}:${this.upTo59}.[0-9]{3}Z$`
241
+ );
242
+ this.isHHmmSSPlusNumberedTimezone = (time) => {
243
+ return this.hhMMssPlusNumberedTimezonePattern.test(time);
244
+ };
245
+ this.isHHmmSSZ = (time) => {
246
+ return this.hhMMssZPattern.test(time);
247
+ };
248
+ this.isHHmmSSsssZ = (time) => {
249
+ return this.hhMMssSSSZPattern.test(time);
250
+ };
233
251
  }
234
252
  formatIsValid(time) {
235
- return this.isHHmm(time) || this.isHHmmSS(time);
253
+ return this.isHHmm(time) || this.isHHmmSS(time) || this.isHHmmSSPlusNumberedTimezone(time) || this.isHHmmSSZ(time) || this.isHHmmSSsssZ(time);
236
254
  }
237
255
  isHHmmSS(time) {
238
256
  return this.hhMMssPattern.test(time);
@@ -246,7 +264,7 @@ var ISO8601TimeValidator = class {
246
264
  var ISO8601Time = class _ISO8601Time {
247
265
  /**
248
266
  * Throws if format is invalid.
249
- * Accepted formats: HH:MM:SS and HH:MM.
267
+ * Accepted formats: HH:MM:SS and HH:MM.
250
268
  */
251
269
  constructor(value) {
252
270
  this.validator = new ISO8601TimeValidator();
@@ -258,12 +276,21 @@ var ISO8601Time = class _ISO8601Time {
258
276
  if (!this.validator.formatIsValid(this.value))
259
277
  throw new Error("Time format must be ISO8601.");
260
278
  }
261
- getTime() {
279
+ getTime(opts) {
262
280
  this.validateFormatOrThrow();
263
- if (this.validator.isHHmm(this.value)) {
264
- return `${this.value}:00`;
281
+ if ((opts == null ? void 0 : opts.format) == "HH:MM" && this.validator.isHHmm(this.value)) {
282
+ return this.value;
283
+ }
284
+ if (((opts == null ? void 0 : opts.format) == "HH:MM:SS" || !(opts == null ? void 0 : opts.format)) && this.validator.isHHmmSS(this.value)) {
285
+ return this.value;
286
+ }
287
+ if ((opts == null ? void 0 : opts.format) == "HH:MM" && this.validator.isHHmmSS(this.value)) {
288
+ return `${this.getHours()}:${this.getMinutes()}`;
289
+ }
290
+ if (((opts == null ? void 0 : opts.format) == "HH:MM:SS" || !(opts == null ? void 0 : opts.format)) && this.validator.isHHmm(this.value)) {
291
+ return `${this.getHours()}:${this.getMinutes()}:${this.getSeconds()}`;
265
292
  }
266
- return this.value;
293
+ throw new Error("Time format is invalid.");
267
294
  }
268
295
  getMinutes() {
269
296
  this.validateFormatOrThrow();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@eliasrrosa/tutorhub-public-assets",
3
- "version": "0.0.7",
3
+ "version": "0.0.9",
4
4
  "description": "Assets, mainly interfaces, to be shared among different Tutorhub apps.",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",