@eliasrrosa/tutorhub-public-assets 0.0.10 → 0.0.11

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 (29) hide show
  1. package/dist/domain/entities/ISO8601DateTime.js +1 -1
  2. package/dist/domain/entities/ISO8601Time.d.ts +20 -9
  3. package/dist/domain/entities/ISO8601Time.js +62 -25
  4. package/dist/domain/entities/ISO8601Time.test.js +105 -14
  5. package/dist/domain/entities/ISO8601TimeValidator.d.ts +7 -0
  6. package/dist/domain/entities/ISO8601TimeValidator.js +9 -3
  7. package/dist/domain/entities/ISO8601Timezone.d.ts +12 -0
  8. package/dist/domain/entities/ISO8601Timezone.js +13 -0
  9. package/dist/domain/entities/ISO8601Timezone.test.d.ts +1 -0
  10. package/dist/domain/entities/ISO8601Timezone.test.js +11 -0
  11. package/dist/domain/entities/ISO8601TimezoneConverter.d.ts +19 -0
  12. package/dist/domain/entities/ISO8601TimezoneConverter.js +28 -0
  13. package/dist/domain/entities/ISO8601TimezoneConverter.test.d.ts +1 -0
  14. package/dist/domain/entities/ISO8601TimezoneConverter.test.js +50 -0
  15. package/dist/domain/entities/ISO8601TimezoneParser.d.ts +17 -0
  16. package/dist/domain/entities/ISO8601TimezoneParser.js +47 -0
  17. package/dist/domain/entities/ISO8601TimezoneParser.test.d.ts +1 -0
  18. package/dist/domain/entities/ISO8601TimezoneParser.test.js +87 -0
  19. package/dist/domain/entities/ISO8601TimezoneSelector.d.ts +32 -0
  20. package/dist/domain/entities/ISO8601TimezoneSelector.js +61 -0
  21. package/dist/domain/entities/ISO8601TimezoneSelector.test.d.ts +1 -0
  22. package/dist/domain/entities/ISO8601TimezoneSelector.test.js +151 -0
  23. package/dist/domain/entities/ISO8601TimezoneValidator.d.ts +22 -0
  24. package/dist/domain/entities/ISO8601TimezoneValidator.js +37 -0
  25. package/dist/domain/entities/ISO8601TimezoneValidator.test.d.ts +1 -0
  26. package/dist/domain/entities/ISO8601TimezoneValidator.test.js +65 -0
  27. package/dist/domain/entities/Time.d.ts +26 -6
  28. package/dist/index.cjs +239 -29
  29. package/package.json +1 -1
package/dist/index.cjs CHANGED
@@ -184,9 +184,9 @@ var ISO8601DateTime = class _ISO8601DateTime {
184
184
  parseInt(this.date.getYear()),
185
185
  parseInt(this.date.getMonth()) - 1,
186
186
  parseInt(this.date.getDay()),
187
- parseInt(this.time.getHours()),
188
- parseInt(this.time.getMinutes()),
189
- parseInt(this.time.getSeconds())
187
+ parseInt(this.time.getHoursString()),
188
+ parseInt(this.time.getMinutesString()),
189
+ parseInt(this.time.getSecondsString())
190
190
  );
191
191
  };
192
192
  this.getBrazilianIsoString = () => {
@@ -226,18 +226,24 @@ var ISO8601TimeValidator = class {
226
226
  constructor() {
227
227
  this.upTo23 = "([0-1][0-9]|[2][0-3])";
228
228
  this.upTo59 = "[0-5][0-9]";
229
+ this.upTo11 = "(0[0-9]|1[0-2])";
230
+ this.upTo13 = "(0[0-9]|1[0-4])";
231
+ this.numberedTimezonePatternString = `(\\+(${this.upTo11}:${this.upTo59}|12:00)|\\-(${this.upTo13}:${this.upTo59}|14:00))`;
232
+ this.numberedTimezonePattern = new RegExp(`^${this.numberedTimezonePatternString}$`);
233
+ this.zTimezonePatternString = "Z";
234
+ this.zAndMillisecondsPatternString = `.[0-9]{3}Z`;
229
235
  this.hhMMssPattern = new RegExp(
230
236
  `^${this.upTo23}:${this.upTo59}:${this.upTo59}$`
231
237
  );
232
238
  this.hhMMPattern = new RegExp(`^${this.upTo23}:${this.upTo59}$`);
233
239
  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}))$`
240
+ `^${this.upTo23}:${this.upTo59}:${this.upTo59}${this.numberedTimezonePatternString}$`
235
241
  );
236
242
  this.hhMMssZPattern = new RegExp(
237
- `^${this.upTo23}:${this.upTo59}:${this.upTo59}Z$`
243
+ `^${this.upTo23}:${this.upTo59}:${this.upTo59}${this.zTimezonePatternString}$`
238
244
  );
239
245
  this.hhMMssSSSZPattern = new RegExp(
240
- `^${this.upTo23}:${this.upTo59}:${this.upTo59}.[0-9]{3}Z$`
246
+ `^${this.upTo23}:${this.upTo59}:${this.upTo59}${this.zAndMillisecondsPatternString}$`
241
247
  );
242
248
  this.isHHmmSSPlusNumberedTimezone = (time) => {
243
249
  return this.hhMMssPlusNumberedTimezonePattern.test(time);
@@ -260,50 +266,249 @@ var ISO8601TimeValidator = class {
260
266
  }
261
267
  };
262
268
 
269
+ // src/domain/entities/ISO8601TimezoneValidator.ts
270
+ var ISO8601TimezoneValidator = class {
271
+ constructor() {
272
+ this.upTo59 = "[0-5][0-9]";
273
+ this.upTo11 = "(0[0-9]|1[0-1])";
274
+ this.upTo13 = "(0[0-9]|1[0-4])";
275
+ this.zTimezonePatternString = "Z";
276
+ this.zTimezonePattern = new RegExp(`${this.zTimezonePatternString}$`);
277
+ this.zAndMillisecondsPatternString = `.[0-9]{3}Z`;
278
+ this.zAndMillisecondsPattern = new RegExp(
279
+ `${this.zAndMillisecondsPatternString}$`
280
+ );
281
+ this.numberedTimezonePatternString = `(\\+(${this.upTo11}:${this.upTo59}|12:00)|\\-(${this.upTo13}:${this.upTo59}|14:00))`;
282
+ this.numberedTimezonePattern = new RegExp(
283
+ `${this.numberedTimezonePatternString}$`
284
+ );
285
+ this.formatIsValid = (timezone) => {
286
+ if (typeof timezone == "number") {
287
+ return this.timezoneNumberIsValid(timezone);
288
+ }
289
+ return this.timezoneIsNumbered(timezone) || this.timezoneIsZ(timezone) || this.timezoneIsZandMilliseconds(timezone);
290
+ };
291
+ }
292
+ timezoneIsNumbered(timezone) {
293
+ return this.numberedTimezonePattern.test(timezone);
294
+ }
295
+ timezoneIsZ(timezone) {
296
+ return this.zTimezonePattern.test(timezone);
297
+ }
298
+ timezoneIsZandMilliseconds(timezone) {
299
+ return this.zAndMillisecondsPattern.test(timezone);
300
+ }
301
+ timezoneNumberIsValid(timezone) {
302
+ return timezone >= -840 && timezone <= 720;
303
+ }
304
+ };
305
+
306
+ // src/domain/entities/ISO8601TimezoneSelector.ts
307
+ var ISO8601TimezoneSelector = class {
308
+ constructor() {
309
+ this.timezoneValidator = new ISO8601TimezoneValidator();
310
+ this.getTimezoneString = (timezone) => {
311
+ const timezoneFormatIsValid = this.timezoneValidator.formatIsValid(timezone);
312
+ if (!timezoneFormatIsValid) {
313
+ return "Z";
314
+ }
315
+ if (typeof timezone == "string" && (this.timezoneValidator.timezoneIsZ(timezone) || this.timezoneValidator.timezoneIsZandMilliseconds(timezone))) {
316
+ return "Z";
317
+ }
318
+ if (typeof timezone == "string" && this.timezoneValidator.timezoneIsNumbered(timezone)) {
319
+ const match = timezone.match(
320
+ this.timezoneValidator.numberedTimezonePattern
321
+ );
322
+ if (!match) return "Z";
323
+ return match[0];
324
+ }
325
+ return "Z";
326
+ };
327
+ this.getHoursString = (timezone) => {
328
+ var _a;
329
+ if (this.timezoneValidator.timezoneIsNumbered(timezone)) {
330
+ const matches = timezone.match(
331
+ this.timezoneValidator.numberedTimezonePatternString
332
+ );
333
+ if (matches == null || matches.length < 1) return void 0;
334
+ return (_a = matches[0].split(":")) == null ? void 0 : _a[0].split("").slice(1).join("");
335
+ }
336
+ return void 0;
337
+ };
338
+ this.getMinutesString = (timezone) => {
339
+ var _a;
340
+ if (this.timezoneValidator.timezoneIsNumbered(timezone)) {
341
+ const matches = timezone.match(
342
+ this.timezoneValidator.numberedTimezonePatternString
343
+ );
344
+ if (matches == null || matches.length < 1) return void 0;
345
+ return (_a = matches[0].split(":")) == null ? void 0 : _a[1];
346
+ }
347
+ return void 0;
348
+ };
349
+ this.getTimezoneSign = (timezone) => {
350
+ if (this.timezoneValidator.timezoneIsNumbered(timezone)) {
351
+ const matches = timezone.match(
352
+ this.timezoneValidator.numberedTimezonePatternString
353
+ );
354
+ if (matches == null || matches.length < 1) return void 0;
355
+ if (matches[0].includes("+")) return "+";
356
+ if (matches[0].includes("-")) return "-";
357
+ }
358
+ return void 0;
359
+ };
360
+ }
361
+ };
362
+
363
+ // src/domain/entities/ISO8601TimezoneParser.ts
364
+ var ISO8601TimezoneParser = class {
365
+ constructor() {
366
+ this.timezoneValidator = new ISO8601TimezoneValidator();
367
+ this.timezoneSelector = new ISO8601TimezoneSelector();
368
+ this.toMinutes = (timezone) => {
369
+ if (this.timezoneValidator.timezoneIsZ(timezone) || this.timezoneValidator.timezoneIsZandMilliseconds(timezone)) {
370
+ return 0;
371
+ }
372
+ if (this.timezoneValidator.timezoneIsNumbered(timezone)) {
373
+ const minutesString = this.timezoneSelector.getMinutesString(timezone);
374
+ const hoursString = this.timezoneSelector.getHoursString(timezone);
375
+ if (minutesString == void 0 || hoursString == void 0) {
376
+ return 0;
377
+ }
378
+ const minutes = parseInt(minutesString);
379
+ const hours = parseInt(hoursString);
380
+ const sign = this.timezoneSelector.getTimezoneSign(timezone);
381
+ return sign == void 0 ? 0 : sign == "+" ? minutes + hours * 60 : -(minutes + hours * 60);
382
+ }
383
+ return 0;
384
+ };
385
+ this.toString = (timezoneInMinutes) => {
386
+ if (this.timezoneValidator.timezoneNumberIsValid(timezoneInMinutes)) {
387
+ if (timezoneInMinutes == 0) return "Z";
388
+ const hours = Math.floor(Math.abs(timezoneInMinutes / 60));
389
+ const minutes = Math.abs(Math.abs(timezoneInMinutes) - hours * 60);
390
+ const hoursString = hours.toString().padStart(2, "0");
391
+ const minutesString = minutes.toString().padStart(2, "0");
392
+ const symbol = timezoneInMinutes > 0 ? "+" : "-";
393
+ return `${symbol}${hoursString}:${minutesString}`;
394
+ }
395
+ return "Z";
396
+ };
397
+ }
398
+ };
399
+
400
+ // src/domain/entities/ISO8601Timezone.ts
401
+ var ISO8601Timezone = class {
402
+ constructor(originalTimezoneString) {
403
+ this.selector = new ISO8601TimezoneSelector();
404
+ this.parser = new ISO8601TimezoneParser();
405
+ this.originalTimezoneString = originalTimezoneString || "Z";
406
+ }
407
+ };
408
+
409
+ // src/domain/entities/ISO8601TimezoneConverter.ts
410
+ var ISO8601TimezoneConverter = class {
411
+ constructor() {
412
+ this.timezoneParser = new ISO8601TimezoneParser();
413
+ this.timezoneValidator = new ISO8601TimezoneValidator();
414
+ this.convertTo = (opts) => {
415
+ if (!this.timezoneValidator.formatIsValid(opts.targetTimezone)) {
416
+ throw new Error("Timezone format is invalid, could not convert.");
417
+ }
418
+ const originalTimezoneInMinutes = opts.time.getTimezone().parser.toMinutes(opts.time.originalTimeString);
419
+ const targetTimezoneInMinutes = typeof opts.targetTimezone == "number" ? opts.targetTimezone : this.timezoneParser.toMinutes(opts.targetTimezone);
420
+ const timezoneOffsetMinutes = targetTimezoneInMinutes - originalTimezoneInMinutes;
421
+ const hoursOffset = Math.floor(timezoneOffsetMinutes / 60);
422
+ const minutesOffset = timezoneOffsetMinutes - hoursOffset * 60;
423
+ const targetTime = opts.time.addHours(hoursOffset).addMinutes(minutesOffset).addTimezone(opts.targetTimezone);
424
+ return targetTime;
425
+ };
426
+ }
427
+ };
428
+
263
429
  // src/domain/entities/ISO8601Time.ts
264
430
  var ISO8601Time = class _ISO8601Time {
265
431
  /**
266
432
  * Throws if format is invalid.
267
- * Accepted formats: HH:MM:SS and HH:MM.
433
+ * Accepted formats: HH:MM:SS, HH:MM, HH:MM:SS(+|-)HH:MM, HH:MM:SS.sssZ and HH:MM:SSZ.
268
434
  */
269
435
  constructor(value) {
270
- this.validator = new ISO8601TimeValidator();
271
- this.getNow = _ISO8601Time.getNow;
272
- this.value = value ? value : this.getNow();
436
+ this.timeValidator = new ISO8601TimeValidator();
437
+ this.converter = new ISO8601TimezoneConverter();
438
+ this.parser = new ISO8601TimezoneParser();
439
+ this.getNowTimeString = _ISO8601Time.getNow;
440
+ this.getTimezone = () => {
441
+ return new ISO8601Timezone(this.originalTimeString);
442
+ };
443
+ this.addHours = (hours) => {
444
+ let targetHour = parseInt(this.getHoursString()) + hours;
445
+ while (targetHour >= 24) {
446
+ targetHour = targetHour - 24;
447
+ }
448
+ while (targetHour < 0) {
449
+ targetHour = 24 + targetHour;
450
+ }
451
+ const targetHourString = targetHour >= 0 ? targetHour.toString().padStart(2, "0") : `-${Math.abs(targetHour).toString().padStart(2, "0")}`;
452
+ const targetTimeString = `${targetHourString}:${this.getMinutesString()}:${this.getSecondsString()}${this.getTimezone().selector.getTimezoneString(
453
+ this.originalTimeString
454
+ )}`;
455
+ return new _ISO8601Time(targetTimeString);
456
+ };
457
+ this.addMinutes = (minutes) => {
458
+ const minutesToAddFromZeroMinutes = minutes + parseInt(this.getMinutesString());
459
+ const hoursToAdd = Math.floor(minutesToAddFromZeroMinutes / 60);
460
+ const targetMinutes = minutesToAddFromZeroMinutes - hoursToAdd * 60;
461
+ const timeWithAddedHours = new _ISO8601Time(
462
+ this.originalTimeString
463
+ ).addHours(hoursToAdd);
464
+ const targetMinutesString = targetMinutes.toString().padStart(2, "0");
465
+ return new _ISO8601Time(
466
+ `${timeWithAddedHours.getHoursString()}:${targetMinutesString}:${timeWithAddedHours.getSecondsString()}${timeWithAddedHours.getTimezone().selector.getTimezoneString(timeWithAddedHours.originalTimeString)}`
467
+ );
468
+ };
469
+ this.addTimezone = (timezone) => {
470
+ const newTimezone = typeof timezone == "number" ? this.parser.toString(timezone) : timezone;
471
+ return new _ISO8601Time(
472
+ `${this.getHoursString()}:${this.getMinutesString()}:${this.getSecondsString()}${newTimezone}`
473
+ );
474
+ };
475
+ this.originalTimeString = value ? value : this.getNowTimeString();
273
476
  this.validateFormatOrThrow();
274
477
  }
275
478
  validateFormatOrThrow() {
276
- if (!this.validator.formatIsValid(this.value))
479
+ if (!this.timeValidator.formatIsValid(this.originalTimeString))
277
480
  throw new Error("Time format must be ISO8601.");
278
481
  }
279
- getTime(opts) {
482
+ getTimeString(opts) {
280
483
  this.validateFormatOrThrow();
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;
484
+ if ((opts == null ? void 0 : opts.format) == "HH:MM") {
485
+ return `${this.getHoursString()}:${this.getMinutesString()}`;
286
486
  }
287
- if ((opts == null ? void 0 : opts.format) == "HH:MM" && this.validator.isHHmmSS(this.value)) {
288
- return `${this.getHours()}:${this.getMinutes()}`;
487
+ if ((opts == null ? void 0 : opts.format) == "HH:MM:SS") {
488
+ return `${this.getHoursString()}:${this.getMinutesString()}:${this.getSecondsString()}`;
289
489
  }
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()}`;
490
+ if ((opts == null ? void 0 : opts.format) == "HH:MM:SS(+|-)HH:MM") {
491
+ const timezone = new ISO8601Timezone(this.originalTimeString);
492
+ return `${this.getHoursString()}:${this.getMinutesString()}:${this.getSecondsString()}${timezone.selector.getTimezoneString(
493
+ this.originalTimeString
494
+ )}`;
292
495
  }
293
- throw new Error("Time format is invalid.");
496
+ return `${this.getHoursString()}:${this.getMinutesString()}:${this.getSecondsString()}`;
294
497
  }
295
- getMinutes() {
498
+ getMinutesString() {
296
499
  this.validateFormatOrThrow();
297
- return this.value.split(":")[1];
500
+ return this.originalTimeString.split(":")[1];
298
501
  }
299
- getHours() {
502
+ getHoursString() {
300
503
  this.validateFormatOrThrow();
301
- return this.value.split(":")[0];
504
+ return this.originalTimeString.split(":")[0];
302
505
  }
303
- getSeconds() {
506
+ getSecondsString() {
304
507
  this.validateFormatOrThrow();
305
- const seconds = this.value.split(":")[2];
306
- if (seconds) return seconds;
508
+ const seconds = this.originalTimeString.split(":")[2];
509
+ const secondsFirstDigit = seconds == null ? void 0 : seconds.split("")[0];
510
+ const secondsSecondDigit = seconds == null ? void 0 : seconds.split("")[1];
511
+ if (seconds) return `${secondsFirstDigit}${secondsSecondDigit}`;
307
512
  return "00";
308
513
  }
309
514
  static getNow() {
@@ -313,6 +518,11 @@ var ISO8601Time = class _ISO8601Time {
313
518
  const seconds = nowDate.getSeconds().toString().padStart(2, "0");
314
519
  return `${hours}:${minutes}:${seconds}`;
315
520
  }
521
+ getTimezoneString() {
522
+ return this.getTimezone().selector.getTimezoneString(
523
+ this.originalTimeString
524
+ );
525
+ }
316
526
  };
317
527
 
318
528
  // src/domain/entities/UserUnavailableTime.ts
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@eliasrrosa/tutorhub-public-assets",
3
- "version": "0.0.10",
3
+ "version": "0.0.11",
4
4
  "description": "Assets, mainly interfaces, to be shared among different Tutorhub apps.",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",