@bgord/tools 1.1.0 → 1.1.2

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 (49) hide show
  1. package/dist/age.vo.d.ts +5 -5
  2. package/dist/age.vo.js +5 -5
  3. package/dist/clock.vo.d.ts +2 -2
  4. package/dist/clock.vo.js +3 -3
  5. package/dist/date-calculator.service.d.ts +5 -5
  6. package/dist/date-calculator.service.js +6 -9
  7. package/dist/date-range.vo.d.ts +6 -6
  8. package/dist/date-range.vo.js +1 -1
  9. package/dist/day.vo.d.ts +5 -3
  10. package/dist/day.vo.js +8 -5
  11. package/dist/hour.vo.d.ts +4 -2
  12. package/dist/hour.vo.js +6 -2
  13. package/dist/minute.vo.d.ts +4 -2
  14. package/dist/minute.vo.js +6 -2
  15. package/dist/month.vo.d.ts +5 -3
  16. package/dist/month.vo.js +10 -7
  17. package/dist/quarter.vo.d.ts +5 -3
  18. package/dist/quarter.vo.js +9 -6
  19. package/dist/rate-limiter.service.d.ts +3 -3
  20. package/dist/rate-limiter.service.js +1 -3
  21. package/dist/relative-date.vo.d.ts +3 -3
  22. package/dist/relative-date.vo.js +1 -1
  23. package/dist/stopwatch.service.d.ts +3 -3
  24. package/dist/stopwatch.service.js +2 -3
  25. package/dist/timestamp.vo.d.ts +13 -12
  26. package/dist/timestamp.vo.js +10 -6
  27. package/dist/week.vo.d.ts +5 -3
  28. package/dist/week.vo.js +11 -8
  29. package/dist/weekday.vo.d.ts +4 -2
  30. package/dist/weekday.vo.js +6 -2
  31. package/dist/year.vo.d.ts +5 -3
  32. package/dist/year.vo.js +11 -8
  33. package/package.json +2 -2
  34. package/src/age.vo.ts +6 -6
  35. package/src/clock.vo.ts +4 -4
  36. package/src/date-calculator.service.ts +7 -11
  37. package/src/date-range.vo.ts +8 -8
  38. package/src/day.vo.ts +12 -7
  39. package/src/hour.vo.ts +8 -3
  40. package/src/minute.vo.ts +8 -3
  41. package/src/month.vo.ts +14 -9
  42. package/src/quarter.vo.ts +13 -8
  43. package/src/rate-limiter.service.ts +5 -7
  44. package/src/relative-date.vo.ts +5 -5
  45. package/src/stopwatch.service.ts +4 -4
  46. package/src/timestamp.vo.ts +20 -16
  47. package/src/week.vo.ts +15 -10
  48. package/src/weekday.vo.ts +8 -3
  49. package/src/year.vo.ts +15 -10
package/dist/age.vo.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { Timestamp } from "./timestamp.vo";
1
+ import { TimestampVO } from "./timestamp.vo";
2
2
  export declare const AgeError: {
3
3
  readonly FutureBirthdate: "age.future.birthdate";
4
4
  };
@@ -8,13 +8,13 @@ export declare class Age {
8
8
  static readonly MAX: number;
9
9
  private constructor();
10
10
  static fromValue(candidate: number): Age;
11
- static fromBirthdateEpochMs(params: {
12
- birthdate: Timestamp;
13
- now: Timestamp;
11
+ static fromBirthdateTimestamp(params: {
12
+ birthdate: TimestampVO;
13
+ now: TimestampVO;
14
14
  }): Age;
15
15
  static fromBirthdate(candidate: {
16
16
  birthdate: string;
17
- now: Timestamp;
17
+ now: TimestampVO;
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 { Timestamp } from "./timestamp.vo";
3
+ import { TimestampVO } from "./timestamp.vo";
4
4
  export const AgeError = { FutureBirthdate: "age.future.birthdate" };
5
5
  export class Age {
6
6
  value;
@@ -12,16 +12,16 @@ export class Age {
12
12
  static fromValue(candidate) {
13
13
  return new Age(AgeYears.parse(candidate));
14
14
  }
15
- static fromBirthdateEpochMs(params) {
15
+ static fromBirthdateTimestamp(params) {
16
16
  if (params.birthdate.isAfter(params.now))
17
17
  throw new Error(AgeError.FutureBirthdate);
18
- return Age.fromValue(differenceInYears(params.now.get(), params.birthdate.get()));
18
+ return Age.fromValue(differenceInYears(params.now.ms, params.birthdate.ms));
19
19
  }
20
20
  static fromBirthdate(candidate) {
21
- const birthdate = Timestamp.fromNumber(new Date(candidate.birthdate).getTime());
21
+ const birthdate = TimestampVO.fromNumber(new Date(candidate.birthdate).getTime());
22
22
  if (birthdate.isAfter(candidate.now))
23
23
  throw new Error(AgeError.FutureBirthdate);
24
- return Age.fromValue(differenceInYears(candidate.now.get(), birthdate.get()));
24
+ return Age.fromValue(differenceInYears(candidate.now.ms, birthdate.ms));
25
25
  }
26
26
  get() {
27
27
  return this.value;
@@ -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 { Timestamp } from "./timestamp.vo";
6
+ import type { TimestampVO } 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 fromEpochMs(timestamp: Timestamp, formatter?: ClockFormatter): Clock;
12
+ static fromTimestamp(timestamp: TimestampVO, formatter?: ClockFormatter): Clock;
13
13
  get(): {
14
14
  hour: HourSchemaType;
15
15
  minute: MinuteSchemaType;
package/dist/clock.vo.js CHANGED
@@ -10,9 +10,9 @@ export class Clock {
10
10
  this.minute = minute;
11
11
  this.formatter = formatter ?? ClockFormatters.TWENTY_FOUR_HOURS;
12
12
  }
13
- static fromEpochMs(timestamp, formatter) {
14
- const hour = Hour.fromEpochMs(timestamp);
15
- const minute = Minute.fromEpochMs(timestamp);
13
+ static fromTimestamp(timestamp, formatter) {
14
+ const hour = Hour.fromTimestamp(timestamp);
15
+ const minute = Minute.fromTimestamp(timestamp);
16
16
  return new Clock(hour, minute, formatter);
17
17
  }
18
18
  get() {
@@ -1,10 +1,10 @@
1
- import type { TimeZoneOffsetValueType } from "./time-zone-offset-value.vo";
2
- import { Timestamp } from "./timestamp.vo";
1
+ import { Duration } from "./duration.service";
2
+ import { TimestampVO } from "./timestamp.vo";
3
3
  type GetStartOfDayTsInTzConfigType = {
4
- now: Timestamp;
5
- timeZoneOffsetMs: TimeZoneOffsetValueType;
4
+ now: TimestampVO;
5
+ timeZoneOffset: Duration;
6
6
  };
7
7
  export declare class DateCalculator {
8
- static getStartOfDayTsInTz(config: GetStartOfDayTsInTzConfigType): Timestamp;
8
+ static getStartOfDayTsInTz(config: GetStartOfDayTsInTzConfigType): TimestampVO;
9
9
  }
10
10
  export {};
@@ -1,15 +1,12 @@
1
1
  import { Duration } from "./duration.service";
2
- import { Timestamp } from "./timestamp.vo";
2
+ import { TimestampVO } from "./timestamp.vo";
3
3
  export class DateCalculator {
4
4
  static getStartOfDayTsInTz(config) {
5
5
  const dayMs = Duration.Days(1).ms;
6
- // UTC midnight for the UTC date of `now`
7
- const utcMidnight = Math.floor(config.now.get() / dayMs) * dayMs;
8
- // Candidate start of the local day (in UTC), anchored to the same UTC date
9
- let start = utcMidnight + config.timeZoneOffsetMs;
10
- // If the candidate is in the future relative to `now`, it means local midnight was "yesterday" in UTC.
11
- if (start > config.now.get())
12
- start -= dayMs;
13
- return Timestamp.fromNumber(start);
6
+ const utcMidnightOfNow = TimestampVO.fromNumber(Math.floor(config.now.ms / dayMs) * dayMs);
7
+ let startOfDayInTz = utcMidnightOfNow.add(config.timeZoneOffset);
8
+ if (startOfDayInTz.isAfter(config.now))
9
+ startOfDayInTz = startOfDayInTz.subtract(Duration.Days(1));
10
+ return startOfDayInTz;
14
11
  }
15
12
  }
@@ -1,15 +1,15 @@
1
- import type { Timestamp } from "./timestamp.vo";
1
+ import type { TimestampVO } 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: Timestamp, end: Timestamp);
9
- getStart(): Timestamp;
10
- getEnd(): Timestamp;
11
- toRange(): [Timestamp, Timestamp];
12
- contains(timestamp: Timestamp): boolean;
8
+ constructor(start: TimestampVO, end: TimestampVO);
9
+ getStart(): TimestampVO;
10
+ getEnd(): TimestampVO;
11
+ toRange(): [TimestampVO, TimestampVO];
12
+ contains(timestamp: TimestampVO): boolean;
13
13
  equals(other: DateRange): boolean;
14
14
  toJSON(): {
15
15
  start: number;
@@ -24,6 +24,6 @@ export class DateRange {
24
24
  return this.start.equals(other.start) && this.end.equals(other.end);
25
25
  }
26
26
  toJSON() {
27
- return { start: this.getStart().get(), end: this.getEnd().get() };
27
+ return { start: this.getStart().ms, end: this.getEnd().ms };
28
28
  }
29
29
  }
package/dist/day.vo.d.ts CHANGED
@@ -1,9 +1,11 @@
1
1
  import { DateRange } from "./date-range.vo";
2
2
  import { type DayIsoIdType } from "./day-iso-id.vo";
3
- import { Timestamp } from "./timestamp.vo";
3
+ import { TimestampVO } from "./timestamp.vo";
4
+ import type { TimestampValueType } from "./timestamp-value.vo";
4
5
  export declare class Day extends DateRange {
5
- static fromTimestamp(timestamp: Timestamp): Day;
6
- static fromNow(now: Timestamp): Day;
6
+ static fromTimestamp(timestamp: TimestampVO): Day;
7
+ static fromTimestampValue(timestamp: TimestampValueType): Day;
8
+ static fromNow(now: TimestampVO): Day;
7
9
  static fromIsoId(isoId: DayIsoIdType): Day;
8
10
  toIsoId(): DayIsoIdType;
9
11
  previous(): Day;
package/dist/day.vo.js CHANGED
@@ -2,26 +2,29 @@ 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 { Timestamp } from "./timestamp.vo";
5
+ import { TimestampVO } from "./timestamp.vo";
6
6
  export class Day extends DateRange {
7
7
  static fromTimestamp(timestamp) {
8
- const date = new Date(timestamp.get());
9
- const startUtc = Timestamp.fromNumber(Date.UTC(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate()));
8
+ const date = new Date(timestamp.ms);
9
+ const startUtc = TimestampVO.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
+ static fromTimestampValue(timestamp) {
14
+ return Day.fromTimestamp(TimestampVO.fromValue(timestamp));
15
+ }
13
16
  static fromNow(now) {
14
17
  return Day.fromTimestamp(now);
15
18
  }
16
19
  static fromIsoId(isoId) {
17
20
  const [year, month, day] = DayIsoId.parse(isoId).split("-").map(Number);
18
- const startUtc = Timestamp.fromNumber(Date.UTC(year, month - 1, day));
21
+ const startUtc = TimestampVO.fromNumber(Date.UTC(year, month - 1, day));
19
22
  const endUtc = startUtc.add(Duration.Days(1)).subtract(Duration.Ms(1));
20
23
  return new Day(startUtc, endUtc);
21
24
  }
22
25
  toIsoId() {
23
26
  const midday = this.getStart().add(Duration.Hours(12));
24
- return DayIsoId.parse(formatISO(midday.get(), { representation: "date" }));
27
+ return DayIsoId.parse(formatISO(midday.ms, { representation: "date" }));
25
28
  }
26
29
  previous() {
27
30
  return this.shift(-1);
package/dist/hour.vo.d.ts CHANGED
@@ -1,12 +1,14 @@
1
1
  import { type HourFormatter } from "./hour-format.service";
2
2
  import { type HourSchemaType } from "./hour-schema.vo";
3
- import type { Timestamp } from "./timestamp.vo";
3
+ import { TimestampVO } from "./timestamp.vo";
4
+ import type { TimestampValueType } from "./timestamp-value.vo";
4
5
  export declare class Hour {
5
6
  private readonly value;
6
7
  static readonly ZERO: Hour;
7
8
  static readonly MAX: Hour;
8
9
  constructor(candidate: number);
9
- static fromEpochMs(timestamp: Timestamp): Hour;
10
+ static fromTimestamp(timestamp: TimestampVO): Hour;
11
+ static fromTimestampValue(timestamp: TimestampValueType): Hour;
10
12
  get(): HourSchemaType;
11
13
  format(formatter: HourFormatter): string;
12
14
  equals(another: Hour): boolean;
package/dist/hour.vo.js CHANGED
@@ -1,5 +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
4
  export class Hour {
4
5
  value;
5
6
  static ZERO = new Hour(0);
@@ -7,8 +8,11 @@ export class Hour {
7
8
  constructor(candidate) {
8
9
  this.value = HourSchema.parse(candidate);
9
10
  }
10
- static fromEpochMs(timestamp) {
11
- return new Hour(new Date(timestamp.get()).getUTCHours());
11
+ static fromTimestamp(timestamp) {
12
+ return new Hour(new Date(timestamp.ms).getUTCHours());
13
+ }
14
+ static fromTimestampValue(timestamp) {
15
+ return Hour.fromTimestamp(TimestampVO.fromValue(timestamp));
12
16
  }
13
17
  get() {
14
18
  return this.value;
@@ -1,11 +1,13 @@
1
1
  import { type MinuteSchemaType } from "./minute-schema.vo";
2
- import type { Timestamp } from "./timestamp.vo";
2
+ import { TimestampVO } from "./timestamp.vo";
3
+ import type { TimestampValueType } from "./timestamp-value.vo";
3
4
  export declare class Minute {
4
5
  private readonly value;
5
6
  static readonly ZERO: Minute;
6
7
  static readonly MAX: Minute;
7
8
  constructor(candidate: number);
8
- static fromEpochMs(timestamp: Timestamp): Minute;
9
+ static fromTimestamp(timestamp: TimestampVO): Minute;
10
+ static fromTimestampValue(timestamp: TimestampValueType): Minute;
9
11
  get(): MinuteSchemaType;
10
12
  equals(another: Minute): boolean;
11
13
  isAfter(another: Minute): boolean;
package/dist/minute.vo.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import { MinuteSchema } from "./minute-schema.vo";
2
+ import { TimestampVO } from "./timestamp.vo";
2
3
  export class Minute {
3
4
  value;
4
5
  static ZERO = new Minute(0);
@@ -6,8 +7,11 @@ export class Minute {
6
7
  constructor(candidate) {
7
8
  this.value = MinuteSchema.parse(candidate);
8
9
  }
9
- static fromEpochMs(timestamp) {
10
- return new Minute(new Date(timestamp.get()).getUTCMinutes());
10
+ static fromTimestamp(timestamp) {
11
+ return new Minute(new Date(timestamp.ms).getUTCMinutes());
12
+ }
13
+ static fromTimestampValue(timestamp) {
14
+ return Minute.fromTimestamp(TimestampVO.fromValue(timestamp));
11
15
  }
12
16
  get() {
13
17
  return this.value;
@@ -1,9 +1,11 @@
1
1
  import { DateRange } from "./date-range.vo";
2
2
  import { type MonthIsoIdType } from "./month-iso-id.vo";
3
- import { Timestamp } from "./timestamp.vo";
3
+ import { TimestampVO } from "./timestamp.vo";
4
+ import type { TimestampValueType } from "./timestamp-value.vo";
4
5
  export declare class Month extends DateRange {
5
- static fromTimestamp(timestamp: Timestamp): Month;
6
- static fromNow(now: Timestamp): Month;
6
+ static fromTimestamp(timestamp: TimestampVO): Month;
7
+ static fromTimestampValue(timestamp: TimestampValueType): Month;
8
+ static fromNow(now: TimestampVO): Month;
7
9
  static fromIsoId(iso: MonthIsoIdType): Month;
8
10
  toIsoId(): MonthIsoIdType;
9
11
  previous(): Month;
package/dist/month.vo.js CHANGED
@@ -1,23 +1,26 @@
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 { Timestamp } from "./timestamp.vo";
4
+ import { TimestampVO } from "./timestamp.vo";
5
5
  export class Month extends DateRange {
6
6
  static fromTimestamp(timestamp) {
7
- const start = Timestamp.fromNumber(startOfMonth(timestamp.get()).getTime());
8
- const end = Timestamp.fromNumber(endOfMonth(timestamp.get()).getTime());
7
+ const start = TimestampVO.fromNumber(startOfMonth(timestamp.ms).getTime());
8
+ const end = TimestampVO.fromNumber(endOfMonth(timestamp.ms).getTime());
9
9
  return new Month(start, end);
10
10
  }
11
+ static fromTimestampValue(timestamp) {
12
+ return Month.fromTimestamp(TimestampVO.fromValue(timestamp));
13
+ }
11
14
  static fromNow(now) {
12
15
  return Month.fromTimestamp(now);
13
16
  }
14
17
  static fromIsoId(iso) {
15
18
  const [year, month] = MonthIsoId.parse(iso).split("-").map(Number);
16
19
  const reference = setMonth(Date.UTC(year), month - 1).getTime();
17
- return Month.fromTimestamp(Timestamp.fromNumber(reference));
20
+ return Month.fromTimestamp(TimestampVO.fromNumber(reference));
18
21
  }
19
22
  toIsoId() {
20
- return MonthIsoId.parse(format(this.getStart().get(), "yyyy-MM"));
23
+ return MonthIsoId.parse(format(this.getStart().ms, "yyyy-MM"));
21
24
  }
22
25
  previous() {
23
26
  return this.shift(-1);
@@ -26,8 +29,8 @@ export class Month extends DateRange {
26
29
  return this.shift(1);
27
30
  }
28
31
  shift(count) {
29
- const shifted = setMonth(this.getStart().get(), getMonth(this.getStart().get()) + count).getTime();
30
- return Month.fromTimestamp(Timestamp.fromNumber(shifted));
32
+ const shifted = setMonth(this.getStart().ms, getMonth(this.getStart().ms) + count).getTime();
33
+ return Month.fromTimestamp(TimestampVO.fromNumber(shifted));
31
34
  }
32
35
  toString() {
33
36
  return this.toIsoId();
@@ -1,9 +1,11 @@
1
1
  import { DateRange } from "./date-range.vo";
2
2
  import { type QuarterIsoIdType } from "./quarter-iso-id.vo";
3
- import { Timestamp } from "./timestamp.vo";
3
+ import { TimestampVO } from "./timestamp.vo";
4
+ import type { TimestampValueType } from "./timestamp-value.vo";
4
5
  export declare class Quarter extends DateRange {
5
- static fromTimestamp(timestamp: Timestamp): Quarter;
6
- static fromNow(now: Timestamp): Quarter;
6
+ static fromTimestamp(timestamp: TimestampVO): Quarter;
7
+ static fromTimestampValue(timestamp: TimestampValueType): Quarter;
8
+ static fromNow(now: TimestampVO): Quarter;
7
9
  static fromIsoId(isoId: QuarterIsoIdType): Quarter;
8
10
  toIsoId(): QuarterIsoIdType;
9
11
  toString(): string;
@@ -1,24 +1,27 @@
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 { Timestamp } from "./timestamp.vo";
4
+ import { TimestampVO } from "./timestamp.vo";
5
5
  export class Quarter extends DateRange {
6
6
  static fromTimestamp(timestamp) {
7
- const start = Timestamp.fromNumber(startOfQuarter(timestamp.get()).getTime());
8
- const end = Timestamp.fromNumber(endOfQuarter(timestamp.get()).getTime());
7
+ const start = TimestampVO.fromNumber(startOfQuarter(timestamp.ms).getTime());
8
+ const end = TimestampVO.fromNumber(endOfQuarter(timestamp.ms).getTime());
9
9
  return new Quarter(start, end);
10
10
  }
11
+ static fromTimestampValue(timestamp) {
12
+ return Quarter.fromTimestamp(TimestampVO.fromValue(timestamp));
13
+ }
11
14
  static fromNow(now) {
12
15
  return Quarter.fromTimestamp(now);
13
16
  }
14
17
  static fromIsoId(isoId) {
15
18
  const [year, quarter] = QuarterIsoId.parse(isoId).split("-Q").map(Number);
16
19
  const reference = setQuarter(Date.UTC(year), quarter).getTime();
17
- return Quarter.fromTimestamp(Timestamp.fromNumber(reference));
20
+ return Quarter.fromTimestamp(TimestampVO.fromNumber(reference));
18
21
  }
19
22
  toIsoId() {
20
- const year = getYear(this.getStart().get());
21
- const quarter = getQuarter(this.getStart().get());
23
+ const year = getYear(this.getStart().ms);
24
+ const quarter = getQuarter(this.getStart().ms);
22
25
  return QuarterIsoId.parse(`${year}-Q${quarter}`);
23
26
  }
24
27
  toString() {
@@ -1,5 +1,5 @@
1
- import { Duration } from "./duration.service";
2
- import type { Timestamp } from "./timestamp.vo";
1
+ import type { Duration } from "./duration.service";
2
+ import type { TimestampVO } 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: Timestamp): RateLimiterResultType;
15
+ verify(now: TimestampVO): RateLimiterResultType;
16
16
  }
17
17
  export {};
@@ -1,4 +1,3 @@
1
- import { Duration } from "./duration.service";
2
1
  export class RateLimiter {
3
2
  duration;
4
3
  lastInvocation = null;
@@ -15,7 +14,6 @@ export class RateLimiter {
15
14
  this.lastInvocation = now;
16
15
  return { allowed: true };
17
16
  }
18
- const remainingDelta = nextAllowedTimestamp.get() - now.get();
19
- return { allowed: false, remaining: Duration.Ms(remainingDelta) };
17
+ return { allowed: false, remaining: nextAllowedTimestamp.difference(now) };
20
18
  }
21
19
  }
@@ -1,4 +1,4 @@
1
- import type { Timestamp } from "./timestamp.vo";
1
+ import type { TimestampVO } 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: Timestamp): RelativeDateType;
10
- static falsy(timestamp: Falsy<Timestamp>): RelativeDateType | null;
9
+ static truthy(timestamp: TimestampVO): RelativeDateType;
10
+ static falsy(timestamp: Falsy<TimestampVO>): RelativeDateType | null;
11
11
  private static _format;
12
12
  }
13
13
  export {};
@@ -9,6 +9,6 @@ export class RelativeDate {
9
9
  return RelativeDate._format(timestamp);
10
10
  }
11
11
  static _format(timestamp) {
12
- return { raw: timestamp.get(), relative: DateFormatters.relative(timestamp.get()) };
12
+ return { raw: timestamp.ms, relative: DateFormatters.relative(timestamp.ms) };
13
13
  }
14
14
  }
@@ -1,5 +1,5 @@
1
- import { Duration } from "./duration.service";
2
- import { Timestamp } from "./timestamp.vo";
1
+ import type { Duration } from "./duration.service";
2
+ import { TimestampVO } 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: Timestamp);
10
+ constructor(start: TimestampVO);
11
11
  stop(): StopwatchResultType;
12
12
  }
@@ -1,5 +1,4 @@
1
- import { Duration } from "./duration.service";
2
- import { Timestamp } from "./timestamp.vo";
1
+ import { TimestampVO } from "./timestamp.vo";
3
2
  export const StopwatchError = { AlreadyStopped: "stopwatch.already.stopped" };
4
3
  var StopwatchState;
5
4
  (function (StopwatchState) {
@@ -16,6 +15,6 @@ export class Stopwatch {
16
15
  if (this.state === StopwatchState.stopped)
17
16
  throw new Error(StopwatchError.AlreadyStopped);
18
17
  this.state = StopwatchState.stopped;
19
- return Duration.Ms(Timestamp.fromNumber(Date.now() - this.start.get()).get());
18
+ return TimestampVO.fromNumber(Date.now()).difference(this.start);
20
19
  }
21
20
  }
@@ -1,18 +1,19 @@
1
- import type { Duration } from "./duration.service";
1
+ import { Duration } from "./duration.service";
2
2
  import { type TimestampValueType } from "./timestamp-value.vo";
3
- export declare class Timestamp {
3
+ export declare class TimestampVO {
4
4
  private readonly value;
5
5
  constructor(value: TimestampValueType);
6
- static fromValue(value: TimestampValueType): Timestamp;
7
- static fromNumber(value: number): Timestamp;
8
- add(duration: Duration): Timestamp;
9
- subtract(duration: Duration): Timestamp;
10
- isBefore(another: Timestamp): boolean;
11
- isBeforeOrEqual(another: Timestamp): boolean;
12
- isAfter(another: Timestamp): boolean;
13
- isAfterOrEqual(another: Timestamp): boolean;
14
- equals(another: Timestamp): boolean;
15
- get(): 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;
16
+ get ms(): TimestampValueType;
16
17
  toJSON(): TimestampValueType;
17
18
  toString(): string;
18
19
  }
@@ -1,20 +1,24 @@
1
+ import { Duration } from "./duration.service";
1
2
  import { TimestampValue } from "./timestamp-value.vo";
2
- export class Timestamp {
3
+ export class TimestampVO {
3
4
  value;
4
5
  constructor(value) {
5
6
  this.value = value;
6
7
  }
7
8
  static fromValue(value) {
8
- return new Timestamp(value);
9
+ return new TimestampVO(value);
9
10
  }
10
11
  static fromNumber(value) {
11
- return new Timestamp(TimestampValue.parse(value));
12
+ return new TimestampVO(TimestampValue.parse(value));
12
13
  }
13
14
  add(duration) {
14
- return Timestamp.fromNumber(this.value + duration.ms);
15
+ return TimestampVO.fromNumber(this.value + duration.ms);
15
16
  }
16
17
  subtract(duration) {
17
- return Timestamp.fromNumber(this.value - duration.ms);
18
+ return TimestampVO.fromNumber(this.value - duration.ms);
19
+ }
20
+ difference(another) {
21
+ return Duration.Ms(this.value - another.value);
18
22
  }
19
23
  isBefore(another) {
20
24
  return this.value < another.value;
@@ -31,7 +35,7 @@ export class Timestamp {
31
35
  equals(another) {
32
36
  return this.value === another.value;
33
37
  }
34
- get() {
38
+ get ms() {
35
39
  return this.value;
36
40
  }
37
41
  toJSON() {
package/dist/week.vo.d.ts CHANGED
@@ -1,9 +1,11 @@
1
1
  import { DateRange } from "./date-range.vo";
2
- import { Timestamp } from "./timestamp.vo";
2
+ import { TimestampVO } from "./timestamp.vo";
3
+ import type { TimestampValueType } from "./timestamp-value.vo";
3
4
  import { type WeekIsoIdType } from "./week-iso-id.vo";
4
5
  export declare class Week extends DateRange {
5
- static fromTimestamp(timestamp: Timestamp): Week;
6
- static fromNow(now: Timestamp): Week;
6
+ static fromTimestamp(timestamp: TimestampVO): Week;
7
+ static fromTimestampValue(timestamp: TimestampValueType): Week;
8
+ static fromNow(now: TimestampVO): Week;
7
9
  static fromIsoId(isoId: WeekIsoIdType): Week;
8
10
  toIsoId(): WeekIsoIdType;
9
11
  previous(): Week;
package/dist/week.vo.js CHANGED
@@ -1,13 +1,16 @@
1
1
  import { addWeeks, endOfISOWeek, getISOWeek, getISOWeekYear, setISOWeek, startOfISOWeek } from "date-fns";
2
2
  import { DateRange } from "./date-range.vo";
3
- import { Timestamp } from "./timestamp.vo";
3
+ import { TimestampVO } 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 = Timestamp.fromNumber(startOfISOWeek(timestamp.get()).getTime());
8
- const end = Timestamp.fromNumber(endOfISOWeek(timestamp.get()).getTime());
7
+ const start = TimestampVO.fromNumber(startOfISOWeek(timestamp.ms).getTime());
8
+ const end = TimestampVO.fromNumber(endOfISOWeek(timestamp.ms).getTime());
9
9
  return new Week(start, end);
10
10
  }
11
+ static fromTimestampValue(timestamp) {
12
+ return Week.fromTimestamp(TimestampVO.fromValue(timestamp));
13
+ }
11
14
  static fromNow(now) {
12
15
  return Week.fromTimestamp(now);
13
16
  }
@@ -15,11 +18,11 @@ export class Week extends DateRange {
15
18
  const [year, week] = WeekIsoId.parse(isoId).split("-W").map(Number);
16
19
  // ISO-8601 rule: Jan 4 is always in week 01 of the ISO week-year.
17
20
  const reference = setISOWeek(Date.UTC(year, 0, 4), week).getTime();
18
- return Week.fromTimestamp(Timestamp.fromNumber(reference));
21
+ return Week.fromTimestamp(TimestampVO.fromNumber(reference));
19
22
  }
20
23
  toIsoId() {
21
- const year = getISOWeekYear(this.getStart().get());
22
- const week = getISOWeek(this.getStart().get());
24
+ const year = getISOWeekYear(this.getStart().ms);
25
+ const week = getISOWeek(this.getStart().ms);
23
26
  return WeekIsoId.parse(`${year}-W${String(week).padStart(2, "0")}`);
24
27
  }
25
28
  previous() {
@@ -29,8 +32,8 @@ export class Week extends DateRange {
29
32
  return this.shift(1);
30
33
  }
31
34
  shift(count) {
32
- const shifted = addWeeks(this.getStart().get(), count).getTime();
33
- return Week.fromTimestamp(Timestamp.fromNumber(shifted));
35
+ const shifted = addWeeks(this.getStart().ms, count).getTime();
36
+ return Week.fromTimestamp(TimestampVO.fromNumber(shifted));
34
37
  }
35
38
  toString() {
36
39
  return this.toIsoId();
@@ -1,4 +1,5 @@
1
- import type { Timestamp } from "./timestamp.vo";
1
+ import { TimestampVO } from "./timestamp.vo";
2
+ import type { TimestampValueType } from "./timestamp-value.vo";
2
3
  export type WeekdayFormatter = (value: Weekday["value"]) => string;
3
4
  export declare enum WeekdayFormatterEnum {
4
5
  FULL = "FULL",// "Sunday"
@@ -19,7 +20,8 @@ export declare class Weekday {
19
20
  static readonly FRIDAY: Weekday;
20
21
  static readonly SATURDAY: Weekday;
21
22
  constructor(candidate: number, formatter?: WeekdayFormatter);
22
- static fromUtcTimestamp(timestamp: Timestamp, formatter?: WeekdayFormatter): Weekday;
23
+ static fromTimestamp(timestamp: TimestampVO, formatter?: WeekdayFormatter): Weekday;
24
+ static fromTimestampValue(timestamp: TimestampValueType): Weekday;
23
25
  get(): number;
24
26
  format(): string;
25
27
  toString(): string;