@bgord/tools 1.0.5 → 1.1.0

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 (60) hide show
  1. package/dist/age.vo.d.ts +4 -4
  2. package/dist/age.vo.js +6 -5
  3. package/dist/clock.vo.d.ts +2 -2
  4. package/dist/clock.vo.js +7 -7
  5. package/dist/date-calculator.service.d.ts +3 -3
  6. package/dist/date-calculator.service.js +3 -3
  7. package/dist/date-range.vo.d.ts +6 -6
  8. package/dist/date-range.vo.js +4 -4
  9. package/dist/day.vo.d.ts +3 -4
  10. package/dist/day.vo.js +10 -14
  11. package/dist/hour.vo.d.ts +2 -2
  12. package/dist/hour.vo.js +1 -1
  13. package/dist/index.d.ts +1 -1
  14. package/dist/index.js +1 -1
  15. package/dist/minute.vo.d.ts +2 -2
  16. package/dist/minute.vo.js +1 -1
  17. package/dist/month.vo.d.ts +3 -3
  18. package/dist/month.vo.js +6 -6
  19. package/dist/quarter.vo.d.ts +3 -3
  20. package/dist/quarter.vo.js +5 -5
  21. package/dist/rate-limiter.service.d.ts +2 -2
  22. package/dist/rate-limiter.service.js +3 -3
  23. package/dist/relative-date.vo.d.ts +5 -4
  24. package/dist/relative-date.vo.js +2 -2
  25. package/dist/stopwatch.service.d.ts +3 -3
  26. package/dist/stopwatch.service.js +4 -4
  27. package/dist/timestamp-value.vo.d.ts +6 -0
  28. package/dist/timestamp-value.vo.js +7 -0
  29. package/dist/timestamp.vo.d.ts +18 -6
  30. package/dist/timestamp.vo.js +43 -7
  31. package/dist/tsconfig.tsbuildinfo +1 -1
  32. package/dist/week.vo.d.ts +3 -3
  33. package/dist/week.vo.js +7 -7
  34. package/dist/weekday.vo.d.ts +2 -2
  35. package/dist/weekday.vo.js +1 -1
  36. package/dist/year.vo.d.ts +3 -3
  37. package/dist/year.vo.js +7 -7
  38. package/package.json +1 -1
  39. package/readme.md +1 -1
  40. package/src/age.vo.ts +8 -8
  41. package/src/clock.vo.ts +7 -7
  42. package/src/date-calculator.service.ts +6 -6
  43. package/src/date-range.vo.ts +11 -11
  44. package/src/day.vo.ts +15 -19
  45. package/src/hour.vo.ts +3 -3
  46. package/src/index.ts +1 -1
  47. package/src/minute.vo.ts +3 -3
  48. package/src/month.vo.ts +9 -9
  49. package/src/quarter.vo.ts +8 -8
  50. package/src/rate-limiter.service.ts +6 -6
  51. package/src/relative-date.vo.ts +7 -6
  52. package/src/stopwatch.service.ts +3 -3
  53. package/src/timestamp-value.vo.ts +11 -0
  54. package/src/timestamp.vo.ts +51 -8
  55. package/src/week.vo.ts +10 -10
  56. package/src/weekday.vo.ts +3 -3
  57. package/src/year.vo.ts +10 -10
  58. package/dist/time.service.d.ts +0 -8
  59. package/dist/time.service.js +0 -13
  60. package/src/time.service.ts +0 -15
@@ -1,11 +1,54 @@
1
- import { z } from "zod/v4";
1
+ import type { Duration } from "./duration.service";
2
+ import { TimestampValue, type TimestampValueType } from "./timestamp-value.vo";
2
3
 
3
- export const TimestampError = { Invalid: "timestamp.invalid" } as const;
4
+ export class Timestamp {
5
+ constructor(private readonly value: TimestampValueType) {}
4
6
 
5
- export const Timestamp = z
6
- .number(TimestampError.Invalid)
7
- .int(TimestampError.Invalid)
8
- .gte(0, TimestampError.Invalid)
9
- .brand("Timestamp");
7
+ static fromValue(value: TimestampValueType): Timestamp {
8
+ return new Timestamp(value);
9
+ }
10
10
 
11
- export type TimestampType = z.infer<typeof Timestamp>;
11
+ static fromNumber(value: number): Timestamp {
12
+ return new Timestamp(TimestampValue.parse(value));
13
+ }
14
+
15
+ add(duration: Duration): Timestamp {
16
+ return Timestamp.fromNumber(this.value + duration.ms);
17
+ }
18
+
19
+ subtract(duration: Duration): Timestamp {
20
+ return Timestamp.fromNumber(this.value - duration.ms);
21
+ }
22
+
23
+ isBefore(another: Timestamp): boolean {
24
+ return this.value < another.value;
25
+ }
26
+
27
+ isBeforeOrEqual(another: Timestamp): boolean {
28
+ return this.value <= another.value;
29
+ }
30
+
31
+ isAfter(another: Timestamp): boolean {
32
+ return this.value > another.value;
33
+ }
34
+
35
+ isAfterOrEqual(another: Timestamp): boolean {
36
+ return this.value >= another.value;
37
+ }
38
+
39
+ equals(another: Timestamp): boolean {
40
+ return this.value === another.value;
41
+ }
42
+
43
+ get(): TimestampValueType {
44
+ return this.value;
45
+ }
46
+
47
+ toJSON(): TimestampValueType {
48
+ return this.value;
49
+ }
50
+
51
+ toString(): string {
52
+ return this.value.toString();
53
+ }
54
+ }
package/src/week.vo.ts CHANGED
@@ -1,17 +1,17 @@
1
1
  import { addWeeks, endOfISOWeek, getISOWeek, getISOWeekYear, setISOWeek, startOfISOWeek } from "date-fns";
2
2
  import { DateRange } from "./date-range.vo";
3
- import { Timestamp, type TimestampType } from "./timestamp.vo";
3
+ import { Timestamp } from "./timestamp.vo";
4
4
  import { WeekIsoId, type WeekIsoIdType } from "./week-iso-id.vo";
5
5
 
6
6
  export class Week extends DateRange {
7
- static fromTimestamp(timestamp: TimestampType): Week {
8
- const start = Timestamp.parse(startOfISOWeek(timestamp).getTime());
9
- const end = Timestamp.parse(endOfISOWeek(timestamp).getTime());
7
+ static fromTimestamp(timestamp: Timestamp): Week {
8
+ const start = Timestamp.fromNumber(startOfISOWeek(timestamp.get()).getTime());
9
+ const end = Timestamp.fromNumber(endOfISOWeek(timestamp.get()).getTime());
10
10
 
11
11
  return new Week(start, end);
12
12
  }
13
13
 
14
- static fromNow(now: TimestampType): Week {
14
+ static fromNow(now: Timestamp): Week {
15
15
  return Week.fromTimestamp(now);
16
16
  }
17
17
 
@@ -21,12 +21,12 @@ export class Week extends DateRange {
21
21
  // ISO-8601 rule: Jan 4 is always in week 01 of the ISO week-year.
22
22
  const reference = setISOWeek(Date.UTC(year, 0, 4), week).getTime();
23
23
 
24
- return Week.fromTimestamp(Timestamp.parse(reference));
24
+ return Week.fromTimestamp(Timestamp.fromNumber(reference));
25
25
  }
26
26
 
27
27
  toIsoId(): WeekIsoIdType {
28
- const year = getISOWeekYear(this.getStart());
29
- const week = getISOWeek(this.getStart());
28
+ const year = getISOWeekYear(this.getStart().get());
29
+ const week = getISOWeek(this.getStart().get());
30
30
 
31
31
  return WeekIsoId.parse(`${year}-W${String(week).padStart(2, "0")}`);
32
32
  }
@@ -40,9 +40,9 @@ export class Week extends DateRange {
40
40
  }
41
41
 
42
42
  shift(count: number): Week {
43
- const shifted = addWeeks(this.getStart(), count).getTime();
43
+ const shifted = addWeeks(this.getStart().get(), count).getTime();
44
44
 
45
- return Week.fromTimestamp(Timestamp.parse(shifted));
45
+ return Week.fromTimestamp(Timestamp.fromNumber(shifted));
46
46
  }
47
47
 
48
48
  toString(): string {
package/src/weekday.vo.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { TimestampType } from "./timestamp.vo";
1
+ import type { Timestamp } from "./timestamp.vo";
2
2
 
3
3
  export type WeekdayFormatter = (value: Weekday["value"]) => string;
4
4
 
@@ -52,8 +52,8 @@ export class Weekday {
52
52
  this.formatter = formatter ?? WeekdayFormatters.FULL;
53
53
  }
54
54
 
55
- static fromUtcTimestamp(timestamp: TimestampType, formatter?: WeekdayFormatter): Weekday {
56
- const dayZeroBased = new Date(timestamp).getUTCDay(); // 0..6
55
+ static fromUtcTimestamp(timestamp: Timestamp, formatter?: WeekdayFormatter): Weekday {
56
+ const dayZeroBased = new Date(timestamp.get()).getUTCDay(); // 0..6
57
57
  return new Weekday(dayZeroBased, formatter);
58
58
  }
59
59
 
package/src/year.vo.ts CHANGED
@@ -1,17 +1,17 @@
1
1
  import { addYears, endOfYear, getYear, startOfYear } from "date-fns";
2
2
  import { DateRange } from "./date-range.vo";
3
- import { Timestamp, type TimestampType } from "./timestamp.vo";
3
+ import { Timestamp } from "./timestamp.vo";
4
4
  import { YearIsoId, type YearIsoIdType } from "./year-iso-id.vo";
5
5
 
6
6
  export class Year extends DateRange {
7
- static fromTimestamp(timestamp: TimestampType): Year {
8
- const start = Timestamp.parse(startOfYear(timestamp).getTime());
9
- const end = Timestamp.parse(endOfYear(timestamp).getTime());
7
+ static fromTimestamp(timestamp: Timestamp): Year {
8
+ const start = Timestamp.fromNumber(startOfYear(timestamp.get()).getTime());
9
+ const end = Timestamp.fromNumber(endOfYear(timestamp.get()).getTime());
10
10
 
11
11
  return new Year(start, end);
12
12
  }
13
13
 
14
- static fromNow(now: TimestampType): Year {
14
+ static fromNow(now: Timestamp): Year {
15
15
  return Year.fromTimestamp(now);
16
16
  }
17
17
 
@@ -22,15 +22,15 @@ export class Year extends DateRange {
22
22
  static fromIsoId(isoId: YearIsoIdType): Year {
23
23
  const reference = Date.UTC(Number(isoId));
24
24
 
25
- return Year.fromTimestamp(Timestamp.parse(reference));
25
+ return Year.fromTimestamp(Timestamp.fromNumber(reference));
26
26
  }
27
27
 
28
28
  toIsoId(): YearIsoIdType {
29
- return YearIsoId.parse(String(getYear(this.getStart())));
29
+ return YearIsoId.parse(String(getYear(this.getStart().get())));
30
30
  }
31
31
 
32
32
  isLeapYear(): boolean {
33
- const year = getYear(this.getStart());
33
+ const year = getYear(this.getStart().get());
34
34
 
35
35
  return (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0;
36
36
  }
@@ -44,9 +44,9 @@ export class Year extends DateRange {
44
44
  }
45
45
 
46
46
  shift(count: number): Year {
47
- const shifted = addYears(this.getStart(), count).getTime();
47
+ const shifted = addYears(this.getStart().get(), count).getTime();
48
48
 
49
- return Year.fromTimestamp(Timestamp.parse(shifted));
49
+ return Year.fromTimestamp(Timestamp.fromNumber(shifted));
50
50
  }
51
51
 
52
52
  toString(): string {
@@ -1,8 +0,0 @@
1
- import type { Duration } from "./duration.service";
2
- import { type TimestampType } from "./timestamp.vo";
3
- export declare const Time: {
4
- Now(now: TimestampType): {
5
- Add(duration: Duration): TimestampType;
6
- Minus(duration: Duration): TimestampType;
7
- };
8
- };
@@ -1,13 +0,0 @@
1
- import { Timestamp } from "./timestamp.vo";
2
- export const Time = {
3
- Now(now) {
4
- return {
5
- Add(duration) {
6
- return Timestamp.parse(now + duration.ms);
7
- },
8
- Minus(duration) {
9
- return Timestamp.parse(now - duration.ms);
10
- },
11
- };
12
- },
13
- };
@@ -1,15 +0,0 @@
1
- import type { Duration } from "./duration.service";
2
- import { Timestamp, type TimestampType } from "./timestamp.vo";
3
-
4
- export const Time = {
5
- Now(now: TimestampType) {
6
- return {
7
- Add(duration: Duration): TimestampType {
8
- return Timestamp.parse(now + duration.ms);
9
- },
10
- Minus(duration: Duration): TimestampType {
11
- return Timestamp.parse(now - duration.ms);
12
- },
13
- };
14
- },
15
- };