@bgord/tools 1.1.2 → 1.1.4

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 (48) hide show
  1. package/dist/age.vo.d.ts +4 -4
  2. package/dist/age.vo.js +2 -2
  3. package/dist/clock.vo.d.ts +2 -2
  4. package/dist/date-calculator.service.d.ts +3 -3
  5. package/dist/date-calculator.service.js +2 -2
  6. package/dist/date-range.vo.d.ts +6 -6
  7. package/dist/day.vo.d.ts +3 -3
  8. package/dist/day.vo.js +4 -4
  9. package/dist/duration.service.d.ts +1 -0
  10. package/dist/duration.service.js +3 -0
  11. package/dist/hour.vo.d.ts +2 -2
  12. package/dist/hour.vo.js +2 -2
  13. package/dist/minute.vo.d.ts +2 -2
  14. package/dist/minute.vo.js +2 -2
  15. package/dist/month.vo.d.ts +3 -3
  16. package/dist/month.vo.js +6 -6
  17. package/dist/quarter.vo.d.ts +3 -3
  18. package/dist/quarter.vo.js +5 -5
  19. package/dist/rate-limiter.service.d.ts +2 -2
  20. package/dist/relative-date.vo.d.ts +3 -3
  21. package/dist/stopwatch.service.d.ts +2 -2
  22. package/dist/stopwatch.service.js +2 -2
  23. package/dist/timestamp.vo.d.ts +11 -11
  24. package/dist/timestamp.vo.js +5 -5
  25. package/dist/week.vo.d.ts +3 -3
  26. package/dist/week.vo.js +6 -6
  27. package/dist/weekday.vo.d.ts +2 -2
  28. package/dist/weekday.vo.js +2 -2
  29. package/dist/year.vo.d.ts +3 -3
  30. package/dist/year.vo.js +6 -6
  31. package/package.json +3 -3
  32. package/src/age.vo.ts +4 -4
  33. package/src/clock.vo.ts +2 -2
  34. package/src/date-calculator.service.ts +4 -4
  35. package/src/date-range.vo.ts +7 -7
  36. package/src/day.vo.ts +6 -6
  37. package/src/duration.service.ts +4 -0
  38. package/src/hour.vo.ts +3 -3
  39. package/src/minute.vo.ts +3 -3
  40. package/src/month.vo.ts +8 -8
  41. package/src/quarter.vo.ts +7 -7
  42. package/src/rate-limiter.service.ts +3 -3
  43. package/src/relative-date.vo.ts +4 -4
  44. package/src/stopwatch.service.ts +3 -3
  45. package/src/timestamp.vo.ts +15 -15
  46. package/src/week.vo.ts +8 -8
  47. package/src/weekday.vo.ts +3 -3
  48. package/src/year.vo.ts +8 -8
package/dist/age.vo.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { TimestampVO } from "./timestamp.vo";
1
+ import { Timestamp } from "./timestamp.vo";
2
2
  export declare const AgeError: {
3
3
  readonly FutureBirthdate: "age.future.birthdate";
4
4
  };
@@ -9,12 +9,12 @@ export declare class Age {
9
9
  private constructor();
10
10
  static fromValue(candidate: number): Age;
11
11
  static fromBirthdateTimestamp(params: {
12
- birthdate: TimestampVO;
13
- now: TimestampVO;
12
+ birthdate: Timestamp;
13
+ now: Timestamp;
14
14
  }): Age;
15
15
  static fromBirthdate(candidate: {
16
16
  birthdate: string;
17
- now: TimestampVO;
17
+ now: Timestamp;
18
18
  }): Age;
19
19
  get(): number;
20
20
  equals(other: Age): boolean;
package/dist/age.vo.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { differenceInYears } from "date-fns";
2
2
  import { AgeYears, AgeYearsConstraints } from "./age-years.vo";
3
- import { TimestampVO } from "./timestamp.vo";
3
+ import { Timestamp } from "./timestamp.vo";
4
4
  export const AgeError = { FutureBirthdate: "age.future.birthdate" };
5
5
  export class Age {
6
6
  value;
@@ -18,7 +18,7 @@ export class Age {
18
18
  return Age.fromValue(differenceInYears(params.now.ms, params.birthdate.ms));
19
19
  }
20
20
  static fromBirthdate(candidate) {
21
- const birthdate = TimestampVO.fromNumber(new Date(candidate.birthdate).getTime());
21
+ const birthdate = Timestamp.fromNumber(new Date(candidate.birthdate).getTime());
22
22
  if (birthdate.isAfter(candidate.now))
23
23
  throw new Error(AgeError.FutureBirthdate);
24
24
  return Age.fromValue(differenceInYears(candidate.now.ms, birthdate.ms));
@@ -3,13 +3,13 @@ import { Hour } from "./hour.vo";
3
3
  import type { HourSchemaType } from "./hour-schema.vo";
4
4
  import { Minute } from "./minute.vo";
5
5
  import type { MinuteSchemaType } from "./minute-schema.vo";
6
- import type { TimestampVO } from "./timestamp.vo";
6
+ import type { Timestamp } from "./timestamp.vo";
7
7
  export declare class Clock {
8
8
  private readonly hour;
9
9
  private readonly minute;
10
10
  private readonly formatter;
11
11
  constructor(hour: Hour, minute: Minute, formatter?: ClockFormatter);
12
- static fromTimestamp(timestamp: TimestampVO, formatter?: ClockFormatter): Clock;
12
+ static fromTimestamp(timestamp: Timestamp, formatter?: ClockFormatter): Clock;
13
13
  get(): {
14
14
  hour: HourSchemaType;
15
15
  minute: MinuteSchemaType;
@@ -1,10 +1,10 @@
1
1
  import { Duration } from "./duration.service";
2
- import { TimestampVO } from "./timestamp.vo";
2
+ import { Timestamp } from "./timestamp.vo";
3
3
  type GetStartOfDayTsInTzConfigType = {
4
- now: TimestampVO;
4
+ now: Timestamp;
5
5
  timeZoneOffset: Duration;
6
6
  };
7
7
  export declare class DateCalculator {
8
- static getStartOfDayTsInTz(config: GetStartOfDayTsInTzConfigType): TimestampVO;
8
+ static getStartOfDayTsInTz(config: GetStartOfDayTsInTzConfigType): Timestamp;
9
9
  }
10
10
  export {};
@@ -1,9 +1,9 @@
1
1
  import { Duration } from "./duration.service";
2
- import { TimestampVO } from "./timestamp.vo";
2
+ import { Timestamp } from "./timestamp.vo";
3
3
  export class DateCalculator {
4
4
  static getStartOfDayTsInTz(config) {
5
5
  const dayMs = Duration.Days(1).ms;
6
- const utcMidnightOfNow = TimestampVO.fromNumber(Math.floor(config.now.ms / dayMs) * dayMs);
6
+ const utcMidnightOfNow = Timestamp.fromNumber(Math.floor(config.now.ms / dayMs) * dayMs);
7
7
  let startOfDayInTz = utcMidnightOfNow.add(config.timeZoneOffset);
8
8
  if (startOfDayInTz.isAfter(config.now))
9
9
  startOfDayInTz = startOfDayInTz.subtract(Duration.Days(1));
@@ -1,15 +1,15 @@
1
- import type { TimestampVO } from "./timestamp.vo";
1
+ import type { Timestamp } from "./timestamp.vo";
2
2
  export declare const DateRangeError: {
3
3
  readonly Invalid: "date.range.invalid";
4
4
  };
5
5
  export declare class DateRange {
6
6
  private readonly start;
7
7
  private readonly end;
8
- constructor(start: TimestampVO, end: TimestampVO);
9
- getStart(): TimestampVO;
10
- getEnd(): TimestampVO;
11
- toRange(): [TimestampVO, TimestampVO];
12
- contains(timestamp: TimestampVO): boolean;
8
+ constructor(start: Timestamp, end: Timestamp);
9
+ getStart(): Timestamp;
10
+ getEnd(): Timestamp;
11
+ toRange(): [Timestamp, Timestamp];
12
+ contains(timestamp: Timestamp): boolean;
13
13
  equals(other: DateRange): boolean;
14
14
  toJSON(): {
15
15
  start: number;
package/dist/day.vo.d.ts CHANGED
@@ -1,11 +1,11 @@
1
1
  import { DateRange } from "./date-range.vo";
2
2
  import { type DayIsoIdType } from "./day-iso-id.vo";
3
- import { TimestampVO } from "./timestamp.vo";
3
+ import { Timestamp } from "./timestamp.vo";
4
4
  import type { TimestampValueType } from "./timestamp-value.vo";
5
5
  export declare class Day extends DateRange {
6
- static fromTimestamp(timestamp: TimestampVO): Day;
6
+ static fromTimestamp(timestamp: Timestamp): Day;
7
7
  static fromTimestampValue(timestamp: TimestampValueType): Day;
8
- static fromNow(now: TimestampVO): Day;
8
+ static fromNow(now: Timestamp): Day;
9
9
  static fromIsoId(isoId: DayIsoIdType): Day;
10
10
  toIsoId(): DayIsoIdType;
11
11
  previous(): Day;
package/dist/day.vo.js CHANGED
@@ -2,23 +2,23 @@ import { formatISO } from "date-fns";
2
2
  import { DateRange } from "./date-range.vo";
3
3
  import { DayIsoId } from "./day-iso-id.vo";
4
4
  import { Duration } from "./duration.service";
5
- import { TimestampVO } from "./timestamp.vo";
5
+ import { Timestamp } from "./timestamp.vo";
6
6
  export class Day extends DateRange {
7
7
  static fromTimestamp(timestamp) {
8
8
  const date = new Date(timestamp.ms);
9
- const startUtc = TimestampVO.fromNumber(Date.UTC(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate()));
9
+ const startUtc = Timestamp.fromNumber(Date.UTC(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate()));
10
10
  const endUtc = startUtc.add(Duration.Days(1)).subtract(Duration.Ms(1));
11
11
  return new Day(startUtc, endUtc);
12
12
  }
13
13
  static fromTimestampValue(timestamp) {
14
- return Day.fromTimestamp(TimestampVO.fromValue(timestamp));
14
+ return Day.fromTimestamp(Timestamp.fromValue(timestamp));
15
15
  }
16
16
  static fromNow(now) {
17
17
  return Day.fromTimestamp(now);
18
18
  }
19
19
  static fromIsoId(isoId) {
20
20
  const [year, month, day] = DayIsoId.parse(isoId).split("-").map(Number);
21
- const startUtc = TimestampVO.fromNumber(Date.UTC(year, month - 1, day));
21
+ const startUtc = Timestamp.fromNumber(Date.UTC(year, month - 1, day));
22
22
  const endUtc = startUtc.add(Duration.Days(1)).subtract(Duration.Ms(1));
23
23
  return new Day(startUtc, endUtc);
24
24
  }
@@ -25,4 +25,5 @@ export declare class Duration {
25
25
  equals(other: Duration): boolean;
26
26
  add(another: Duration): Duration;
27
27
  subtract(another: Duration): Duration;
28
+ toAbolute(): Duration;
28
29
  }
@@ -62,4 +62,7 @@ export class Duration {
62
62
  subtract(another) {
63
63
  return Duration.Ms(this.internal - another.internal);
64
64
  }
65
+ toAbolute() {
66
+ return Duration.Ms(Math.abs(this.internal));
67
+ }
65
68
  }
package/dist/hour.vo.d.ts CHANGED
@@ -1,13 +1,13 @@
1
1
  import { type HourFormatter } from "./hour-format.service";
2
2
  import { type HourSchemaType } from "./hour-schema.vo";
3
- import { TimestampVO } from "./timestamp.vo";
3
+ import { Timestamp } from "./timestamp.vo";
4
4
  import type { TimestampValueType } from "./timestamp-value.vo";
5
5
  export declare class Hour {
6
6
  private readonly value;
7
7
  static readonly ZERO: Hour;
8
8
  static readonly MAX: Hour;
9
9
  constructor(candidate: number);
10
- static fromTimestamp(timestamp: TimestampVO): Hour;
10
+ static fromTimestamp(timestamp: Timestamp): Hour;
11
11
  static fromTimestampValue(timestamp: TimestampValueType): Hour;
12
12
  get(): HourSchemaType;
13
13
  format(formatter: HourFormatter): string;
package/dist/hour.vo.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { HourFormatters } from "./hour-format.service";
2
2
  import { HourSchema } from "./hour-schema.vo";
3
- import { TimestampVO } from "./timestamp.vo";
3
+ import { Timestamp } from "./timestamp.vo";
4
4
  export class Hour {
5
5
  value;
6
6
  static ZERO = new Hour(0);
@@ -12,7 +12,7 @@ export class Hour {
12
12
  return new Hour(new Date(timestamp.ms).getUTCHours());
13
13
  }
14
14
  static fromTimestampValue(timestamp) {
15
- return Hour.fromTimestamp(TimestampVO.fromValue(timestamp));
15
+ return Hour.fromTimestamp(Timestamp.fromValue(timestamp));
16
16
  }
17
17
  get() {
18
18
  return this.value;
@@ -1,12 +1,12 @@
1
1
  import { type MinuteSchemaType } from "./minute-schema.vo";
2
- import { TimestampVO } from "./timestamp.vo";
2
+ import { Timestamp } from "./timestamp.vo";
3
3
  import type { TimestampValueType } from "./timestamp-value.vo";
4
4
  export declare class Minute {
5
5
  private readonly value;
6
6
  static readonly ZERO: Minute;
7
7
  static readonly MAX: Minute;
8
8
  constructor(candidate: number);
9
- static fromTimestamp(timestamp: TimestampVO): Minute;
9
+ static fromTimestamp(timestamp: Timestamp): Minute;
10
10
  static fromTimestampValue(timestamp: TimestampValueType): Minute;
11
11
  get(): MinuteSchemaType;
12
12
  equals(another: Minute): boolean;
package/dist/minute.vo.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { MinuteSchema } from "./minute-schema.vo";
2
- import { TimestampVO } from "./timestamp.vo";
2
+ import { Timestamp } from "./timestamp.vo";
3
3
  export class Minute {
4
4
  value;
5
5
  static ZERO = new Minute(0);
@@ -11,7 +11,7 @@ export class Minute {
11
11
  return new Minute(new Date(timestamp.ms).getUTCMinutes());
12
12
  }
13
13
  static fromTimestampValue(timestamp) {
14
- return Minute.fromTimestamp(TimestampVO.fromValue(timestamp));
14
+ return Minute.fromTimestamp(Timestamp.fromValue(timestamp));
15
15
  }
16
16
  get() {
17
17
  return this.value;
@@ -1,11 +1,11 @@
1
1
  import { DateRange } from "./date-range.vo";
2
2
  import { type MonthIsoIdType } from "./month-iso-id.vo";
3
- import { TimestampVO } from "./timestamp.vo";
3
+ import { Timestamp } from "./timestamp.vo";
4
4
  import type { TimestampValueType } from "./timestamp-value.vo";
5
5
  export declare class Month extends DateRange {
6
- static fromTimestamp(timestamp: TimestampVO): Month;
6
+ static fromTimestamp(timestamp: Timestamp): Month;
7
7
  static fromTimestampValue(timestamp: TimestampValueType): Month;
8
- static fromNow(now: TimestampVO): Month;
8
+ static fromNow(now: Timestamp): Month;
9
9
  static fromIsoId(iso: MonthIsoIdType): Month;
10
10
  toIsoId(): MonthIsoIdType;
11
11
  previous(): Month;
package/dist/month.vo.js CHANGED
@@ -1,15 +1,15 @@
1
1
  import { endOfMonth, format, getMonth, setMonth, startOfMonth } from "date-fns";
2
2
  import { DateRange } from "./date-range.vo";
3
3
  import { MonthIsoId } from "./month-iso-id.vo";
4
- import { TimestampVO } from "./timestamp.vo";
4
+ import { Timestamp } from "./timestamp.vo";
5
5
  export class Month extends DateRange {
6
6
  static fromTimestamp(timestamp) {
7
- const start = TimestampVO.fromNumber(startOfMonth(timestamp.ms).getTime());
8
- const end = TimestampVO.fromNumber(endOfMonth(timestamp.ms).getTime());
7
+ const start = Timestamp.fromNumber(startOfMonth(timestamp.ms).getTime());
8
+ const end = Timestamp.fromNumber(endOfMonth(timestamp.ms).getTime());
9
9
  return new Month(start, end);
10
10
  }
11
11
  static fromTimestampValue(timestamp) {
12
- return Month.fromTimestamp(TimestampVO.fromValue(timestamp));
12
+ return Month.fromTimestamp(Timestamp.fromValue(timestamp));
13
13
  }
14
14
  static fromNow(now) {
15
15
  return Month.fromTimestamp(now);
@@ -17,7 +17,7 @@ export class Month extends DateRange {
17
17
  static fromIsoId(iso) {
18
18
  const [year, month] = MonthIsoId.parse(iso).split("-").map(Number);
19
19
  const reference = setMonth(Date.UTC(year), month - 1).getTime();
20
- return Month.fromTimestamp(TimestampVO.fromNumber(reference));
20
+ return Month.fromTimestamp(Timestamp.fromNumber(reference));
21
21
  }
22
22
  toIsoId() {
23
23
  return MonthIsoId.parse(format(this.getStart().ms, "yyyy-MM"));
@@ -30,7 +30,7 @@ export class Month extends DateRange {
30
30
  }
31
31
  shift(count) {
32
32
  const shifted = setMonth(this.getStart().ms, getMonth(this.getStart().ms) + count).getTime();
33
- return Month.fromTimestamp(TimestampVO.fromNumber(shifted));
33
+ return Month.fromTimestamp(Timestamp.fromNumber(shifted));
34
34
  }
35
35
  toString() {
36
36
  return this.toIsoId();
@@ -1,11 +1,11 @@
1
1
  import { DateRange } from "./date-range.vo";
2
2
  import { type QuarterIsoIdType } from "./quarter-iso-id.vo";
3
- import { TimestampVO } from "./timestamp.vo";
3
+ import { Timestamp } from "./timestamp.vo";
4
4
  import type { TimestampValueType } from "./timestamp-value.vo";
5
5
  export declare class Quarter extends DateRange {
6
- static fromTimestamp(timestamp: TimestampVO): Quarter;
6
+ static fromTimestamp(timestamp: Timestamp): Quarter;
7
7
  static fromTimestampValue(timestamp: TimestampValueType): Quarter;
8
- static fromNow(now: TimestampVO): Quarter;
8
+ static fromNow(now: Timestamp): Quarter;
9
9
  static fromIsoId(isoId: QuarterIsoIdType): Quarter;
10
10
  toIsoId(): QuarterIsoIdType;
11
11
  toString(): string;
@@ -1,15 +1,15 @@
1
1
  import { endOfQuarter, getQuarter, getYear, setQuarter, startOfQuarter } from "date-fns";
2
2
  import { DateRange } from "./date-range.vo";
3
3
  import { QuarterIsoId } from "./quarter-iso-id.vo";
4
- import { TimestampVO } from "./timestamp.vo";
4
+ import { Timestamp } from "./timestamp.vo";
5
5
  export class Quarter extends DateRange {
6
6
  static fromTimestamp(timestamp) {
7
- const start = TimestampVO.fromNumber(startOfQuarter(timestamp.ms).getTime());
8
- const end = TimestampVO.fromNumber(endOfQuarter(timestamp.ms).getTime());
7
+ const start = Timestamp.fromNumber(startOfQuarter(timestamp.ms).getTime());
8
+ const end = Timestamp.fromNumber(endOfQuarter(timestamp.ms).getTime());
9
9
  return new Quarter(start, end);
10
10
  }
11
11
  static fromTimestampValue(timestamp) {
12
- return Quarter.fromTimestamp(TimestampVO.fromValue(timestamp));
12
+ return Quarter.fromTimestamp(Timestamp.fromValue(timestamp));
13
13
  }
14
14
  static fromNow(now) {
15
15
  return Quarter.fromTimestamp(now);
@@ -17,7 +17,7 @@ export class Quarter extends DateRange {
17
17
  static fromIsoId(isoId) {
18
18
  const [year, quarter] = QuarterIsoId.parse(isoId).split("-Q").map(Number);
19
19
  const reference = setQuarter(Date.UTC(year), quarter).getTime();
20
- return Quarter.fromTimestamp(TimestampVO.fromNumber(reference));
20
+ return Quarter.fromTimestamp(Timestamp.fromNumber(reference));
21
21
  }
22
22
  toIsoId() {
23
23
  const year = getYear(this.getStart().ms);
@@ -1,5 +1,5 @@
1
1
  import type { Duration } from "./duration.service";
2
- import type { TimestampVO } from "./timestamp.vo";
2
+ import type { Timestamp } from "./timestamp.vo";
3
3
  type RateLimiterResultSuccessType = {
4
4
  allowed: true;
5
5
  };
@@ -12,6 +12,6 @@ export declare class RateLimiter {
12
12
  private readonly duration;
13
13
  private lastInvocation;
14
14
  constructor(duration: Duration);
15
- verify(now: TimestampVO): RateLimiterResultType;
15
+ verify(now: Timestamp): RateLimiterResultType;
16
16
  }
17
17
  export {};
@@ -1,4 +1,4 @@
1
- import type { TimestampVO } from "./timestamp.vo";
1
+ import type { Timestamp } from "./timestamp.vo";
2
2
  import type { TimestampValueType } from "./timestamp-value.vo";
3
3
  import type { Falsy } from "./ts-utils";
4
4
  type RelativeDateType = {
@@ -6,8 +6,8 @@ type RelativeDateType = {
6
6
  relative: string;
7
7
  };
8
8
  export declare class RelativeDate {
9
- static truthy(timestamp: TimestampVO): RelativeDateType;
10
- static falsy(timestamp: Falsy<TimestampVO>): RelativeDateType | null;
9
+ static truthy(timestamp: Timestamp): RelativeDateType;
10
+ static falsy(timestamp: Falsy<Timestamp>): RelativeDateType | null;
11
11
  private static _format;
12
12
  }
13
13
  export {};
@@ -1,5 +1,5 @@
1
1
  import type { Duration } from "./duration.service";
2
- import { TimestampVO } from "./timestamp.vo";
2
+ import { Timestamp } from "./timestamp.vo";
3
3
  export declare const StopwatchError: {
4
4
  readonly AlreadyStopped: "stopwatch.already.stopped";
5
5
  };
@@ -7,6 +7,6 @@ export type StopwatchResultType = Duration;
7
7
  export declare class Stopwatch {
8
8
  private readonly start;
9
9
  private state;
10
- constructor(start: TimestampVO);
10
+ constructor(start: Timestamp);
11
11
  stop(): StopwatchResultType;
12
12
  }
@@ -1,4 +1,4 @@
1
- import { TimestampVO } from "./timestamp.vo";
1
+ import { Timestamp } from "./timestamp.vo";
2
2
  export const StopwatchError = { AlreadyStopped: "stopwatch.already.stopped" };
3
3
  var StopwatchState;
4
4
  (function (StopwatchState) {
@@ -15,6 +15,6 @@ export class Stopwatch {
15
15
  if (this.state === StopwatchState.stopped)
16
16
  throw new Error(StopwatchError.AlreadyStopped);
17
17
  this.state = StopwatchState.stopped;
18
- return TimestampVO.fromNumber(Date.now()).difference(this.start);
18
+ return Timestamp.fromNumber(Date.now()).difference(this.start);
19
19
  }
20
20
  }
@@ -1,18 +1,18 @@
1
1
  import { Duration } from "./duration.service";
2
2
  import { type TimestampValueType } from "./timestamp-value.vo";
3
- export declare class TimestampVO {
3
+ export declare class Timestamp {
4
4
  private readonly value;
5
5
  constructor(value: TimestampValueType);
6
- static fromValue(value: TimestampValueType): TimestampVO;
7
- static fromNumber(value: number): TimestampVO;
8
- add(duration: Duration): TimestampVO;
9
- subtract(duration: Duration): TimestampVO;
10
- difference(another: TimestampVO): Duration;
11
- isBefore(another: TimestampVO): boolean;
12
- isBeforeOrEqual(another: TimestampVO): boolean;
13
- isAfter(another: TimestampVO): boolean;
14
- isAfterOrEqual(another: TimestampVO): boolean;
15
- equals(another: TimestampVO): boolean;
6
+ static fromValue(value: TimestampValueType): Timestamp;
7
+ static fromNumber(value: number): Timestamp;
8
+ add(duration: Duration): Timestamp;
9
+ subtract(duration: Duration): Timestamp;
10
+ difference(another: Timestamp): Duration;
11
+ isBefore(another: Timestamp): boolean;
12
+ isBeforeOrEqual(another: Timestamp): boolean;
13
+ isAfter(another: Timestamp): boolean;
14
+ isAfterOrEqual(another: Timestamp): boolean;
15
+ equals(another: Timestamp): boolean;
16
16
  get ms(): TimestampValueType;
17
17
  toJSON(): TimestampValueType;
18
18
  toString(): string;
@@ -1,21 +1,21 @@
1
1
  import { Duration } from "./duration.service";
2
2
  import { TimestampValue } from "./timestamp-value.vo";
3
- export class TimestampVO {
3
+ export class Timestamp {
4
4
  value;
5
5
  constructor(value) {
6
6
  this.value = value;
7
7
  }
8
8
  static fromValue(value) {
9
- return new TimestampVO(value);
9
+ return new Timestamp(value);
10
10
  }
11
11
  static fromNumber(value) {
12
- return new TimestampVO(TimestampValue.parse(value));
12
+ return new Timestamp(TimestampValue.parse(value));
13
13
  }
14
14
  add(duration) {
15
- return TimestampVO.fromNumber(this.value + duration.ms);
15
+ return Timestamp.fromNumber(this.value + duration.ms);
16
16
  }
17
17
  subtract(duration) {
18
- return TimestampVO.fromNumber(this.value - duration.ms);
18
+ return Timestamp.fromNumber(this.value - duration.ms);
19
19
  }
20
20
  difference(another) {
21
21
  return Duration.Ms(this.value - another.value);
package/dist/week.vo.d.ts CHANGED
@@ -1,11 +1,11 @@
1
1
  import { DateRange } from "./date-range.vo";
2
- import { TimestampVO } from "./timestamp.vo";
2
+ import { Timestamp } from "./timestamp.vo";
3
3
  import type { TimestampValueType } from "./timestamp-value.vo";
4
4
  import { type WeekIsoIdType } from "./week-iso-id.vo";
5
5
  export declare class Week extends DateRange {
6
- static fromTimestamp(timestamp: TimestampVO): Week;
6
+ static fromTimestamp(timestamp: Timestamp): Week;
7
7
  static fromTimestampValue(timestamp: TimestampValueType): Week;
8
- static fromNow(now: TimestampVO): Week;
8
+ static fromNow(now: Timestamp): Week;
9
9
  static fromIsoId(isoId: WeekIsoIdType): Week;
10
10
  toIsoId(): WeekIsoIdType;
11
11
  previous(): Week;
package/dist/week.vo.js CHANGED
@@ -1,15 +1,15 @@
1
1
  import { addWeeks, endOfISOWeek, getISOWeek, getISOWeekYear, setISOWeek, startOfISOWeek } from "date-fns";
2
2
  import { DateRange } from "./date-range.vo";
3
- import { TimestampVO } from "./timestamp.vo";
3
+ import { Timestamp } from "./timestamp.vo";
4
4
  import { WeekIsoId } from "./week-iso-id.vo";
5
5
  export class Week extends DateRange {
6
6
  static fromTimestamp(timestamp) {
7
- const start = TimestampVO.fromNumber(startOfISOWeek(timestamp.ms).getTime());
8
- const end = TimestampVO.fromNumber(endOfISOWeek(timestamp.ms).getTime());
7
+ const start = Timestamp.fromNumber(startOfISOWeek(timestamp.ms).getTime());
8
+ const end = Timestamp.fromNumber(endOfISOWeek(timestamp.ms).getTime());
9
9
  return new Week(start, end);
10
10
  }
11
11
  static fromTimestampValue(timestamp) {
12
- return Week.fromTimestamp(TimestampVO.fromValue(timestamp));
12
+ return Week.fromTimestamp(Timestamp.fromValue(timestamp));
13
13
  }
14
14
  static fromNow(now) {
15
15
  return Week.fromTimestamp(now);
@@ -18,7 +18,7 @@ export class Week extends DateRange {
18
18
  const [year, week] = WeekIsoId.parse(isoId).split("-W").map(Number);
19
19
  // ISO-8601 rule: Jan 4 is always in week 01 of the ISO week-year.
20
20
  const reference = setISOWeek(Date.UTC(year, 0, 4), week).getTime();
21
- return Week.fromTimestamp(TimestampVO.fromNumber(reference));
21
+ return Week.fromTimestamp(Timestamp.fromNumber(reference));
22
22
  }
23
23
  toIsoId() {
24
24
  const year = getISOWeekYear(this.getStart().ms);
@@ -33,7 +33,7 @@ export class Week extends DateRange {
33
33
  }
34
34
  shift(count) {
35
35
  const shifted = addWeeks(this.getStart().ms, count).getTime();
36
- return Week.fromTimestamp(TimestampVO.fromNumber(shifted));
36
+ return Week.fromTimestamp(Timestamp.fromNumber(shifted));
37
37
  }
38
38
  toString() {
39
39
  return this.toIsoId();
@@ -1,4 +1,4 @@
1
- import { TimestampVO } from "./timestamp.vo";
1
+ import { Timestamp } from "./timestamp.vo";
2
2
  import type { TimestampValueType } from "./timestamp-value.vo";
3
3
  export type WeekdayFormatter = (value: Weekday["value"]) => string;
4
4
  export declare enum WeekdayFormatterEnum {
@@ -20,7 +20,7 @@ export declare class Weekday {
20
20
  static readonly FRIDAY: Weekday;
21
21
  static readonly SATURDAY: Weekday;
22
22
  constructor(candidate: number, formatter?: WeekdayFormatter);
23
- static fromTimestamp(timestamp: TimestampVO, formatter?: WeekdayFormatter): Weekday;
23
+ static fromTimestamp(timestamp: Timestamp, formatter?: WeekdayFormatter): Weekday;
24
24
  static fromTimestampValue(timestamp: TimestampValueType): Weekday;
25
25
  get(): number;
26
26
  format(): string;
@@ -1,4 +1,4 @@
1
- import { TimestampVO } from "./timestamp.vo";
1
+ import { Timestamp } from "./timestamp.vo";
2
2
  export var WeekdayFormatterEnum;
3
3
  (function (WeekdayFormatterEnum) {
4
4
  WeekdayFormatterEnum["FULL"] = "FULL";
@@ -46,7 +46,7 @@ export class Weekday {
46
46
  return new Weekday(dayZeroBased, formatter);
47
47
  }
48
48
  static fromTimestampValue(timestamp) {
49
- return Weekday.fromTimestamp(TimestampVO.fromValue(timestamp));
49
+ return Weekday.fromTimestamp(Timestamp.fromValue(timestamp));
50
50
  }
51
51
  get() {
52
52
  return this.value;
package/dist/year.vo.d.ts CHANGED
@@ -1,11 +1,11 @@
1
1
  import { DateRange } from "./date-range.vo";
2
- import { TimestampVO } from "./timestamp.vo";
2
+ import { Timestamp } from "./timestamp.vo";
3
3
  import type { TimestampValueType } from "./timestamp-value.vo";
4
4
  import { type YearIsoIdType } from "./year-iso-id.vo";
5
5
  export declare class Year extends DateRange {
6
- static fromTimestamp(timestamp: TimestampVO): Year;
6
+ static fromTimestamp(timestamp: Timestamp): Year;
7
7
  static fromTimestampValue(timestamp: TimestampValueType): Year;
8
- static fromNow(now: TimestampVO): Year;
8
+ static fromNow(now: Timestamp): Year;
9
9
  static fromNumber(candidate: number): Year;
10
10
  static fromIsoId(isoId: YearIsoIdType): Year;
11
11
  toIsoId(): YearIsoIdType;
package/dist/year.vo.js CHANGED
@@ -1,15 +1,15 @@
1
1
  import { addYears, endOfYear, getYear, startOfYear } from "date-fns";
2
2
  import { DateRange } from "./date-range.vo";
3
- import { TimestampVO } from "./timestamp.vo";
3
+ import { Timestamp } from "./timestamp.vo";
4
4
  import { YearIsoId } from "./year-iso-id.vo";
5
5
  export class Year extends DateRange {
6
6
  static fromTimestamp(timestamp) {
7
- const start = TimestampVO.fromNumber(startOfYear(timestamp.ms).getTime());
8
- const end = TimestampVO.fromNumber(endOfYear(timestamp.ms).getTime());
7
+ const start = Timestamp.fromNumber(startOfYear(timestamp.ms).getTime());
8
+ const end = Timestamp.fromNumber(endOfYear(timestamp.ms).getTime());
9
9
  return new Year(start, end);
10
10
  }
11
11
  static fromTimestampValue(timestamp) {
12
- return Year.fromTimestamp(TimestampVO.fromValue(timestamp));
12
+ return Year.fromTimestamp(Timestamp.fromValue(timestamp));
13
13
  }
14
14
  static fromNow(now) {
15
15
  return Year.fromTimestamp(now);
@@ -19,7 +19,7 @@ export class Year extends DateRange {
19
19
  }
20
20
  static fromIsoId(isoId) {
21
21
  const reference = Date.UTC(Number(isoId));
22
- return Year.fromTimestamp(TimestampVO.fromNumber(reference));
22
+ return Year.fromTimestamp(Timestamp.fromNumber(reference));
23
23
  }
24
24
  toIsoId() {
25
25
  return YearIsoId.parse(String(getYear(this.getStart().ms)));
@@ -36,7 +36,7 @@ export class Year extends DateRange {
36
36
  }
37
37
  shift(count) {
38
38
  const shifted = addYears(this.getStart().ms, count).getTime();
39
- return Year.fromTimestamp(TimestampVO.fromNumber(shifted));
39
+ return Year.fromTimestamp(Timestamp.fromNumber(shifted));
40
40
  }
41
41
  toString() {
42
42
  return this.toIsoId();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bgord/tools",
3
- "version": "1.1.2",
3
+ "version": "1.1.4",
4
4
  "type": "module",
5
5
  "license": "MIT",
6
6
  "author": "Bartosz Gordon",
@@ -21,13 +21,13 @@
21
21
  "preinstall": "bunx only-allow bun"
22
22
  },
23
23
  "devDependencies": {
24
- "@biomejs/biome": "2.3.2",
24
+ "@biomejs/biome": "2.3.3",
25
25
  "@commitlint/cli": "20.1.0",
26
26
  "@commitlint/config-conventional": "20.0.0",
27
27
  "@types/bun": "1.3.1",
28
28
  "@types/mime-types": "3.0.1",
29
29
  "cspell": "9.2.2",
30
- "knip": "5.67.0",
30
+ "knip": "5.67.1",
31
31
  "lefthook": "2.0.2",
32
32
  "only-allow": "1.2.1",
33
33
  "shellcheck": "4.1.0",
package/src/age.vo.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { differenceInYears } from "date-fns";
2
2
  import { AgeYears, AgeYearsConstraints, type AgeYearsType } from "./age-years.vo";
3
- import { TimestampVO } from "./timestamp.vo";
3
+ import { Timestamp } from "./timestamp.vo";
4
4
 
5
5
  export const AgeError = { FutureBirthdate: "age.future.birthdate" } as const;
6
6
 
@@ -14,13 +14,13 @@ export class Age {
14
14
  return new Age(AgeYears.parse(candidate));
15
15
  }
16
16
 
17
- static fromBirthdateTimestamp(params: { birthdate: TimestampVO; now: TimestampVO }): Age {
17
+ static fromBirthdateTimestamp(params: { birthdate: Timestamp; now: Timestamp }): Age {
18
18
  if (params.birthdate.isAfter(params.now)) throw new Error(AgeError.FutureBirthdate);
19
19
  return Age.fromValue(differenceInYears(params.now.ms, params.birthdate.ms));
20
20
  }
21
21
 
22
- static fromBirthdate(candidate: { birthdate: string; now: TimestampVO }): Age {
23
- const birthdate = TimestampVO.fromNumber(new Date(candidate.birthdate).getTime());
22
+ static fromBirthdate(candidate: { birthdate: string; now: Timestamp }): Age {
23
+ const birthdate = Timestamp.fromNumber(new Date(candidate.birthdate).getTime());
24
24
 
25
25
  if (birthdate.isAfter(candidate.now)) throw new Error(AgeError.FutureBirthdate);
26
26
  return Age.fromValue(differenceInYears(candidate.now.ms, birthdate.ms));
package/src/clock.vo.ts CHANGED
@@ -3,7 +3,7 @@ import { Hour } from "./hour.vo";
3
3
  import type { HourSchemaType } from "./hour-schema.vo";
4
4
  import { Minute } from "./minute.vo";
5
5
  import type { MinuteSchemaType } from "./minute-schema.vo";
6
- import type { TimestampVO } from "./timestamp.vo";
6
+ import type { Timestamp } from "./timestamp.vo";
7
7
 
8
8
  export class Clock {
9
9
  private readonly formatter: ClockFormatter;
@@ -16,7 +16,7 @@ export class Clock {
16
16
  this.formatter = formatter ?? ClockFormatters.TWENTY_FOUR_HOURS;
17
17
  }
18
18
 
19
- static fromTimestamp(timestamp: TimestampVO, formatter?: ClockFormatter): Clock {
19
+ static fromTimestamp(timestamp: Timestamp, formatter?: ClockFormatter): Clock {
20
20
  const hour = Hour.fromTimestamp(timestamp);
21
21
  const minute = Minute.fromTimestamp(timestamp);
22
22
 
@@ -1,13 +1,13 @@
1
1
  import { Duration } from "./duration.service";
2
- import { TimestampVO } from "./timestamp.vo";
2
+ import { Timestamp } from "./timestamp.vo";
3
3
 
4
- type GetStartOfDayTsInTzConfigType = { now: TimestampVO; timeZoneOffset: Duration };
4
+ type GetStartOfDayTsInTzConfigType = { now: Timestamp; timeZoneOffset: Duration };
5
5
 
6
6
  export class DateCalculator {
7
- static getStartOfDayTsInTz(config: GetStartOfDayTsInTzConfigType): TimestampVO {
7
+ static getStartOfDayTsInTz(config: GetStartOfDayTsInTzConfigType): Timestamp {
8
8
  const dayMs = Duration.Days(1).ms;
9
9
 
10
- const utcMidnightOfNow = TimestampVO.fromNumber(Math.floor(config.now.ms / dayMs) * dayMs);
10
+ const utcMidnightOfNow = Timestamp.fromNumber(Math.floor(config.now.ms / dayMs) * dayMs);
11
11
 
12
12
  let startOfDayInTz = utcMidnightOfNow.add(config.timeZoneOffset);
13
13
 
@@ -1,28 +1,28 @@
1
- import type { TimestampVO } from "./timestamp.vo";
1
+ import type { Timestamp } from "./timestamp.vo";
2
2
 
3
3
  export const DateRangeError = { Invalid: "date.range.invalid" } as const;
4
4
 
5
5
  export class DateRange {
6
6
  constructor(
7
- private readonly start: TimestampVO,
8
- private readonly end: TimestampVO,
7
+ private readonly start: Timestamp,
8
+ private readonly end: Timestamp,
9
9
  ) {
10
10
  if (start.isAfter(end)) throw new Error(DateRangeError.Invalid);
11
11
  }
12
12
 
13
- getStart(): TimestampVO {
13
+ getStart(): Timestamp {
14
14
  return this.start;
15
15
  }
16
16
 
17
- getEnd(): TimestampVO {
17
+ getEnd(): Timestamp {
18
18
  return this.end;
19
19
  }
20
20
 
21
- toRange(): [TimestampVO, TimestampVO] {
21
+ toRange(): [Timestamp, Timestamp] {
22
22
  return [this.start, this.end];
23
23
  }
24
24
 
25
- contains(timestamp: TimestampVO): boolean {
25
+ contains(timestamp: Timestamp): boolean {
26
26
  return timestamp.isAfterOrEqual(this.start) && timestamp.isBeforeOrEqual(this.end);
27
27
  }
28
28
 
package/src/day.vo.ts CHANGED
@@ -2,14 +2,14 @@ import { formatISO } from "date-fns";
2
2
  import { DateRange } from "./date-range.vo";
3
3
  import { DayIsoId, type DayIsoIdType } from "./day-iso-id.vo";
4
4
  import { Duration } from "./duration.service";
5
- import { TimestampVO } from "./timestamp.vo";
5
+ import { Timestamp } from "./timestamp.vo";
6
6
  import type { TimestampValueType } from "./timestamp-value.vo";
7
7
 
8
8
  export class Day extends DateRange {
9
- static fromTimestamp(timestamp: TimestampVO): Day {
9
+ static fromTimestamp(timestamp: Timestamp): Day {
10
10
  const date = new Date(timestamp.ms);
11
11
 
12
- const startUtc = TimestampVO.fromNumber(
12
+ const startUtc = Timestamp.fromNumber(
13
13
  Date.UTC(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate()),
14
14
  );
15
15
  const endUtc = startUtc.add(Duration.Days(1)).subtract(Duration.Ms(1));
@@ -18,17 +18,17 @@ export class Day extends DateRange {
18
18
  }
19
19
 
20
20
  static fromTimestampValue(timestamp: TimestampValueType): Day {
21
- return Day.fromTimestamp(TimestampVO.fromValue(timestamp));
21
+ return Day.fromTimestamp(Timestamp.fromValue(timestamp));
22
22
  }
23
23
 
24
- static fromNow(now: TimestampVO): Day {
24
+ static fromNow(now: Timestamp): Day {
25
25
  return Day.fromTimestamp(now);
26
26
  }
27
27
 
28
28
  static fromIsoId(isoId: DayIsoIdType): Day {
29
29
  const [year, month, day] = DayIsoId.parse(isoId).split("-").map(Number);
30
30
 
31
- const startUtc = TimestampVO.fromNumber(Date.UTC(year, month - 1, day));
31
+ const startUtc = Timestamp.fromNumber(Date.UTC(year, month - 1, day));
32
32
  const endUtc = startUtc.add(Duration.Days(1)).subtract(Duration.Ms(1));
33
33
 
34
34
  return new Day(startUtc, endUtc);
@@ -72,4 +72,8 @@ export class Duration {
72
72
  subtract(another: Duration): Duration {
73
73
  return Duration.Ms(this.internal - another.internal);
74
74
  }
75
+
76
+ toAbolute(): Duration {
77
+ return Duration.Ms(Math.abs(this.internal));
78
+ }
75
79
  }
package/src/hour.vo.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { type HourFormatter, HourFormatters } from "./hour-format.service";
2
2
  import { HourSchema, type HourSchemaType } from "./hour-schema.vo";
3
- import { TimestampVO } from "./timestamp.vo";
3
+ import { Timestamp } from "./timestamp.vo";
4
4
  import type { TimestampValueType } from "./timestamp-value.vo";
5
5
 
6
6
  export class Hour {
@@ -13,12 +13,12 @@ export class Hour {
13
13
  this.value = HourSchema.parse(candidate);
14
14
  }
15
15
 
16
- static fromTimestamp(timestamp: TimestampVO): Hour {
16
+ static fromTimestamp(timestamp: Timestamp): Hour {
17
17
  return new Hour(new Date(timestamp.ms).getUTCHours());
18
18
  }
19
19
 
20
20
  static fromTimestampValue(timestamp: TimestampValueType): Hour {
21
- return Hour.fromTimestamp(TimestampVO.fromValue(timestamp));
21
+ return Hour.fromTimestamp(Timestamp.fromValue(timestamp));
22
22
  }
23
23
 
24
24
  get(): HourSchemaType {
package/src/minute.vo.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { MinuteSchema, type MinuteSchemaType } from "./minute-schema.vo";
2
- import { TimestampVO } from "./timestamp.vo";
2
+ import { Timestamp } from "./timestamp.vo";
3
3
  import type { TimestampValueType } from "./timestamp-value.vo";
4
4
 
5
5
  export class Minute {
@@ -12,12 +12,12 @@ export class Minute {
12
12
  this.value = MinuteSchema.parse(candidate);
13
13
  }
14
14
 
15
- static fromTimestamp(timestamp: TimestampVO): Minute {
15
+ static fromTimestamp(timestamp: Timestamp): Minute {
16
16
  return new Minute(new Date(timestamp.ms).getUTCMinutes());
17
17
  }
18
18
 
19
19
  static fromTimestampValue(timestamp: TimestampValueType): Minute {
20
- return Minute.fromTimestamp(TimestampVO.fromValue(timestamp));
20
+ return Minute.fromTimestamp(Timestamp.fromValue(timestamp));
21
21
  }
22
22
 
23
23
  get(): MinuteSchemaType {
package/src/month.vo.ts CHANGED
@@ -1,22 +1,22 @@
1
1
  import { endOfMonth, format, getMonth, setMonth, startOfMonth } from "date-fns";
2
2
  import { DateRange } from "./date-range.vo";
3
3
  import { MonthIsoId, type MonthIsoIdType } from "./month-iso-id.vo";
4
- import { TimestampVO } from "./timestamp.vo";
4
+ import { Timestamp } from "./timestamp.vo";
5
5
  import type { TimestampValueType } from "./timestamp-value.vo";
6
6
 
7
7
  export class Month extends DateRange {
8
- static fromTimestamp(timestamp: TimestampVO): Month {
9
- const start = TimestampVO.fromNumber(startOfMonth(timestamp.ms).getTime());
10
- const end = TimestampVO.fromNumber(endOfMonth(timestamp.ms).getTime());
8
+ static fromTimestamp(timestamp: Timestamp): Month {
9
+ const start = Timestamp.fromNumber(startOfMonth(timestamp.ms).getTime());
10
+ const end = Timestamp.fromNumber(endOfMonth(timestamp.ms).getTime());
11
11
 
12
12
  return new Month(start, end);
13
13
  }
14
14
 
15
15
  static fromTimestampValue(timestamp: TimestampValueType): Month {
16
- return Month.fromTimestamp(TimestampVO.fromValue(timestamp));
16
+ return Month.fromTimestamp(Timestamp.fromValue(timestamp));
17
17
  }
18
18
 
19
- static fromNow(now: TimestampVO): Month {
19
+ static fromNow(now: Timestamp): Month {
20
20
  return Month.fromTimestamp(now);
21
21
  }
22
22
 
@@ -25,7 +25,7 @@ export class Month extends DateRange {
25
25
 
26
26
  const reference = setMonth(Date.UTC(year), month - 1).getTime();
27
27
 
28
- return Month.fromTimestamp(TimestampVO.fromNumber(reference));
28
+ return Month.fromTimestamp(Timestamp.fromNumber(reference));
29
29
  }
30
30
 
31
31
  toIsoId(): MonthIsoIdType {
@@ -43,7 +43,7 @@ export class Month extends DateRange {
43
43
  shift(count: number): Month {
44
44
  const shifted = setMonth(this.getStart().ms, getMonth(this.getStart().ms) + count).getTime();
45
45
 
46
- return Month.fromTimestamp(TimestampVO.fromNumber(shifted));
46
+ return Month.fromTimestamp(Timestamp.fromNumber(shifted));
47
47
  }
48
48
 
49
49
  toString(): string {
package/src/quarter.vo.ts CHANGED
@@ -1,22 +1,22 @@
1
1
  import { endOfQuarter, getQuarter, getYear, setQuarter, startOfQuarter } from "date-fns";
2
2
  import { DateRange } from "./date-range.vo";
3
3
  import { QuarterIsoId, type QuarterIsoIdType } from "./quarter-iso-id.vo";
4
- import { TimestampVO } from "./timestamp.vo";
4
+ import { Timestamp } from "./timestamp.vo";
5
5
  import type { TimestampValueType } from "./timestamp-value.vo";
6
6
 
7
7
  export class Quarter extends DateRange {
8
- static fromTimestamp(timestamp: TimestampVO): Quarter {
9
- const start = TimestampVO.fromNumber(startOfQuarter(timestamp.ms).getTime());
10
- const end = TimestampVO.fromNumber(endOfQuarter(timestamp.ms).getTime());
8
+ static fromTimestamp(timestamp: Timestamp): Quarter {
9
+ const start = Timestamp.fromNumber(startOfQuarter(timestamp.ms).getTime());
10
+ const end = Timestamp.fromNumber(endOfQuarter(timestamp.ms).getTime());
11
11
 
12
12
  return new Quarter(start, end);
13
13
  }
14
14
 
15
15
  static fromTimestampValue(timestamp: TimestampValueType): Quarter {
16
- return Quarter.fromTimestamp(TimestampVO.fromValue(timestamp));
16
+ return Quarter.fromTimestamp(Timestamp.fromValue(timestamp));
17
17
  }
18
18
 
19
- static fromNow(now: TimestampVO): Quarter {
19
+ static fromNow(now: Timestamp): Quarter {
20
20
  return Quarter.fromTimestamp(now);
21
21
  }
22
22
 
@@ -25,7 +25,7 @@ export class Quarter extends DateRange {
25
25
 
26
26
  const reference = setQuarter(Date.UTC(year), quarter).getTime();
27
27
 
28
- return Quarter.fromTimestamp(TimestampVO.fromNumber(reference));
28
+ return Quarter.fromTimestamp(Timestamp.fromNumber(reference));
29
29
  }
30
30
 
31
31
  toIsoId(): QuarterIsoIdType {
@@ -1,16 +1,16 @@
1
1
  import type { Duration } from "./duration.service";
2
- import type { TimestampVO } from "./timestamp.vo";
2
+ import type { Timestamp } from "./timestamp.vo";
3
3
 
4
4
  type RateLimiterResultSuccessType = { allowed: true };
5
5
  type RateLimiterResultErrorType = { allowed: false; remaining: Duration };
6
6
  type RateLimiterResultType = RateLimiterResultSuccessType | RateLimiterResultErrorType;
7
7
 
8
8
  export class RateLimiter {
9
- private lastInvocation: TimestampVO | null = null;
9
+ private lastInvocation: Timestamp | null = null;
10
10
 
11
11
  constructor(private readonly duration: Duration) {}
12
12
 
13
- verify(now: TimestampVO): RateLimiterResultType {
13
+ verify(now: Timestamp): RateLimiterResultType {
14
14
  if (this.lastInvocation == null) {
15
15
  this.lastInvocation = now;
16
16
 
@@ -1,21 +1,21 @@
1
1
  import { DateFormatters } from "./date-formatter.service";
2
- import type { TimestampVO } from "./timestamp.vo";
2
+ import type { Timestamp } from "./timestamp.vo";
3
3
  import type { TimestampValueType } from "./timestamp-value.vo";
4
4
  import type { Falsy } from "./ts-utils";
5
5
 
6
6
  type RelativeDateType = { raw: TimestampValueType; relative: string };
7
7
 
8
8
  export class RelativeDate {
9
- static truthy(timestamp: TimestampVO): RelativeDateType {
9
+ static truthy(timestamp: Timestamp): RelativeDateType {
10
10
  return RelativeDate._format(timestamp);
11
11
  }
12
12
 
13
- static falsy(timestamp: Falsy<TimestampVO>): RelativeDateType | null {
13
+ static falsy(timestamp: Falsy<Timestamp>): RelativeDateType | null {
14
14
  if (!timestamp) return null;
15
15
  return RelativeDate._format(timestamp);
16
16
  }
17
17
 
18
- private static _format(timestamp: TimestampVO): RelativeDateType {
18
+ private static _format(timestamp: Timestamp): RelativeDateType {
19
19
  return { raw: timestamp.ms, relative: DateFormatters.relative(timestamp.ms) };
20
20
  }
21
21
  }
@@ -1,5 +1,5 @@
1
1
  import type { Duration } from "./duration.service";
2
- import { TimestampVO } from "./timestamp.vo";
2
+ import { Timestamp } from "./timestamp.vo";
3
3
 
4
4
  export const StopwatchError = { AlreadyStopped: "stopwatch.already.stopped" } as const;
5
5
 
@@ -13,13 +13,13 @@ export type StopwatchResultType = Duration;
13
13
  export class Stopwatch {
14
14
  private state: StopwatchState = StopwatchState.started;
15
15
 
16
- constructor(private readonly start: TimestampVO) {}
16
+ constructor(private readonly start: Timestamp) {}
17
17
 
18
18
  stop(): StopwatchResultType {
19
19
  if (this.state === StopwatchState.stopped) throw new Error(StopwatchError.AlreadyStopped);
20
20
 
21
21
  this.state = StopwatchState.stopped;
22
22
 
23
- return TimestampVO.fromNumber(Date.now()).difference(this.start);
23
+ return Timestamp.fromNumber(Date.now()).difference(this.start);
24
24
  }
25
25
  }
@@ -1,46 +1,46 @@
1
1
  import { Duration } from "./duration.service";
2
2
  import { TimestampValue, type TimestampValueType } from "./timestamp-value.vo";
3
3
 
4
- export class TimestampVO {
4
+ export class Timestamp {
5
5
  constructor(private readonly value: TimestampValueType) {}
6
6
 
7
- static fromValue(value: TimestampValueType): TimestampVO {
8
- return new TimestampVO(value);
7
+ static fromValue(value: TimestampValueType): Timestamp {
8
+ return new Timestamp(value);
9
9
  }
10
10
 
11
- static fromNumber(value: number): TimestampVO {
12
- return new TimestampVO(TimestampValue.parse(value));
11
+ static fromNumber(value: number): Timestamp {
12
+ return new Timestamp(TimestampValue.parse(value));
13
13
  }
14
14
 
15
- add(duration: Duration): TimestampVO {
16
- return TimestampVO.fromNumber(this.value + duration.ms);
15
+ add(duration: Duration): Timestamp {
16
+ return Timestamp.fromNumber(this.value + duration.ms);
17
17
  }
18
18
 
19
- subtract(duration: Duration): TimestampVO {
20
- return TimestampVO.fromNumber(this.value - duration.ms);
19
+ subtract(duration: Duration): Timestamp {
20
+ return Timestamp.fromNumber(this.value - duration.ms);
21
21
  }
22
22
 
23
- difference(another: TimestampVO): Duration {
23
+ difference(another: Timestamp): Duration {
24
24
  return Duration.Ms(this.value - another.value);
25
25
  }
26
26
 
27
- isBefore(another: TimestampVO): boolean {
27
+ isBefore(another: Timestamp): boolean {
28
28
  return this.value < another.value;
29
29
  }
30
30
 
31
- isBeforeOrEqual(another: TimestampVO): boolean {
31
+ isBeforeOrEqual(another: Timestamp): boolean {
32
32
  return this.value <= another.value;
33
33
  }
34
34
 
35
- isAfter(another: TimestampVO): boolean {
35
+ isAfter(another: Timestamp): boolean {
36
36
  return this.value > another.value;
37
37
  }
38
38
 
39
- isAfterOrEqual(another: TimestampVO): boolean {
39
+ isAfterOrEqual(another: Timestamp): boolean {
40
40
  return this.value >= another.value;
41
41
  }
42
42
 
43
- equals(another: TimestampVO): boolean {
43
+ equals(another: Timestamp): boolean {
44
44
  return this.value === another.value;
45
45
  }
46
46
 
package/src/week.vo.ts CHANGED
@@ -1,22 +1,22 @@
1
1
  import { addWeeks, endOfISOWeek, getISOWeek, getISOWeekYear, setISOWeek, startOfISOWeek } from "date-fns";
2
2
  import { DateRange } from "./date-range.vo";
3
- import { TimestampVO } from "./timestamp.vo";
3
+ import { Timestamp } from "./timestamp.vo";
4
4
  import type { TimestampValueType } from "./timestamp-value.vo";
5
5
  import { WeekIsoId, type WeekIsoIdType } from "./week-iso-id.vo";
6
6
 
7
7
  export class Week extends DateRange {
8
- static fromTimestamp(timestamp: TimestampVO): Week {
9
- const start = TimestampVO.fromNumber(startOfISOWeek(timestamp.ms).getTime());
10
- const end = TimestampVO.fromNumber(endOfISOWeek(timestamp.ms).getTime());
8
+ static fromTimestamp(timestamp: Timestamp): Week {
9
+ const start = Timestamp.fromNumber(startOfISOWeek(timestamp.ms).getTime());
10
+ const end = Timestamp.fromNumber(endOfISOWeek(timestamp.ms).getTime());
11
11
 
12
12
  return new Week(start, end);
13
13
  }
14
14
 
15
15
  static fromTimestampValue(timestamp: TimestampValueType): Week {
16
- return Week.fromTimestamp(TimestampVO.fromValue(timestamp));
16
+ return Week.fromTimestamp(Timestamp.fromValue(timestamp));
17
17
  }
18
18
 
19
- static fromNow(now: TimestampVO): Week {
19
+ static fromNow(now: Timestamp): Week {
20
20
  return Week.fromTimestamp(now);
21
21
  }
22
22
 
@@ -26,7 +26,7 @@ export class Week extends DateRange {
26
26
  // ISO-8601 rule: Jan 4 is always in week 01 of the ISO week-year.
27
27
  const reference = setISOWeek(Date.UTC(year, 0, 4), week).getTime();
28
28
 
29
- return Week.fromTimestamp(TimestampVO.fromNumber(reference));
29
+ return Week.fromTimestamp(Timestamp.fromNumber(reference));
30
30
  }
31
31
 
32
32
  toIsoId(): WeekIsoIdType {
@@ -47,7 +47,7 @@ export class Week extends DateRange {
47
47
  shift(count: number): Week {
48
48
  const shifted = addWeeks(this.getStart().ms, count).getTime();
49
49
 
50
- return Week.fromTimestamp(TimestampVO.fromNumber(shifted));
50
+ return Week.fromTimestamp(Timestamp.fromNumber(shifted));
51
51
  }
52
52
 
53
53
  toString(): string {
package/src/weekday.vo.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { TimestampVO } from "./timestamp.vo";
1
+ import { Timestamp } from "./timestamp.vo";
2
2
  import type { TimestampValueType } from "./timestamp-value.vo";
3
3
 
4
4
  export type WeekdayFormatter = (value: Weekday["value"]) => string;
@@ -53,13 +53,13 @@ export class Weekday {
53
53
  this.formatter = formatter ?? WeekdayFormatters.FULL;
54
54
  }
55
55
 
56
- static fromTimestamp(timestamp: TimestampVO, formatter?: WeekdayFormatter): Weekday {
56
+ static fromTimestamp(timestamp: Timestamp, formatter?: WeekdayFormatter): Weekday {
57
57
  const dayZeroBased = new Date(timestamp.ms).getUTCDay(); // 0..6
58
58
  return new Weekday(dayZeroBased, formatter);
59
59
  }
60
60
 
61
61
  static fromTimestampValue(timestamp: TimestampValueType): Weekday {
62
- return Weekday.fromTimestamp(TimestampVO.fromValue(timestamp));
62
+ return Weekday.fromTimestamp(Timestamp.fromValue(timestamp));
63
63
  }
64
64
 
65
65
  get(): number {
package/src/year.vo.ts CHANGED
@@ -1,22 +1,22 @@
1
1
  import { addYears, endOfYear, getYear, startOfYear } from "date-fns";
2
2
  import { DateRange } from "./date-range.vo";
3
- import { TimestampVO } from "./timestamp.vo";
3
+ import { Timestamp } from "./timestamp.vo";
4
4
  import type { TimestampValueType } from "./timestamp-value.vo";
5
5
  import { YearIsoId, type YearIsoIdType } from "./year-iso-id.vo";
6
6
 
7
7
  export class Year extends DateRange {
8
- static fromTimestamp(timestamp: TimestampVO): Year {
9
- const start = TimestampVO.fromNumber(startOfYear(timestamp.ms).getTime());
10
- const end = TimestampVO.fromNumber(endOfYear(timestamp.ms).getTime());
8
+ static fromTimestamp(timestamp: Timestamp): Year {
9
+ const start = Timestamp.fromNumber(startOfYear(timestamp.ms).getTime());
10
+ const end = Timestamp.fromNumber(endOfYear(timestamp.ms).getTime());
11
11
 
12
12
  return new Year(start, end);
13
13
  }
14
14
 
15
15
  static fromTimestampValue(timestamp: TimestampValueType): Year {
16
- return Year.fromTimestamp(TimestampVO.fromValue(timestamp));
16
+ return Year.fromTimestamp(Timestamp.fromValue(timestamp));
17
17
  }
18
18
 
19
- static fromNow(now: TimestampVO): Year {
19
+ static fromNow(now: Timestamp): Year {
20
20
  return Year.fromTimestamp(now);
21
21
  }
22
22
 
@@ -27,7 +27,7 @@ export class Year extends DateRange {
27
27
  static fromIsoId(isoId: YearIsoIdType): Year {
28
28
  const reference = Date.UTC(Number(isoId));
29
29
 
30
- return Year.fromTimestamp(TimestampVO.fromNumber(reference));
30
+ return Year.fromTimestamp(Timestamp.fromNumber(reference));
31
31
  }
32
32
 
33
33
  toIsoId(): YearIsoIdType {
@@ -51,7 +51,7 @@ export class Year extends DateRange {
51
51
  shift(count: number): Year {
52
52
  const shifted = addYears(this.getStart().ms, count).getTime();
53
53
 
54
- return Year.fromTimestamp(TimestampVO.fromNumber(shifted));
54
+ return Year.fromTimestamp(Timestamp.fromNumber(shifted));
55
55
  }
56
56
 
57
57
  toString(): string {