@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.
- package/dist/age.vo.d.ts +5 -5
- package/dist/age.vo.js +5 -5
- package/dist/clock.vo.d.ts +2 -2
- package/dist/clock.vo.js +3 -3
- package/dist/date-calculator.service.d.ts +5 -5
- package/dist/date-calculator.service.js +6 -9
- package/dist/date-range.vo.d.ts +6 -6
- package/dist/date-range.vo.js +1 -1
- package/dist/day.vo.d.ts +5 -3
- package/dist/day.vo.js +8 -5
- package/dist/hour.vo.d.ts +4 -2
- package/dist/hour.vo.js +6 -2
- package/dist/minute.vo.d.ts +4 -2
- package/dist/minute.vo.js +6 -2
- package/dist/month.vo.d.ts +5 -3
- package/dist/month.vo.js +10 -7
- package/dist/quarter.vo.d.ts +5 -3
- package/dist/quarter.vo.js +9 -6
- package/dist/rate-limiter.service.d.ts +3 -3
- package/dist/rate-limiter.service.js +1 -3
- package/dist/relative-date.vo.d.ts +3 -3
- package/dist/relative-date.vo.js +1 -1
- package/dist/stopwatch.service.d.ts +3 -3
- package/dist/stopwatch.service.js +2 -3
- package/dist/timestamp.vo.d.ts +13 -12
- package/dist/timestamp.vo.js +10 -6
- package/dist/week.vo.d.ts +5 -3
- package/dist/week.vo.js +11 -8
- package/dist/weekday.vo.d.ts +4 -2
- package/dist/weekday.vo.js +6 -2
- package/dist/year.vo.d.ts +5 -3
- package/dist/year.vo.js +11 -8
- package/package.json +2 -2
- package/src/age.vo.ts +6 -6
- package/src/clock.vo.ts +4 -4
- package/src/date-calculator.service.ts +7 -11
- package/src/date-range.vo.ts +8 -8
- package/src/day.vo.ts +12 -7
- package/src/hour.vo.ts +8 -3
- package/src/minute.vo.ts +8 -3
- package/src/month.vo.ts +14 -9
- package/src/quarter.vo.ts +13 -8
- package/src/rate-limiter.service.ts +5 -7
- package/src/relative-date.vo.ts +5 -5
- package/src/stopwatch.service.ts +4 -4
- package/src/timestamp.vo.ts +20 -16
- package/src/week.vo.ts +15 -10
- package/src/weekday.vo.ts +8 -3
- package/src/year.vo.ts +15 -10
package/dist/age.vo.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
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
|
|
12
|
-
birthdate:
|
|
13
|
-
now:
|
|
11
|
+
static fromBirthdateTimestamp(params: {
|
|
12
|
+
birthdate: TimestampVO;
|
|
13
|
+
now: TimestampVO;
|
|
14
14
|
}): Age;
|
|
15
15
|
static fromBirthdate(candidate: {
|
|
16
16
|
birthdate: string;
|
|
17
|
-
now:
|
|
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 {
|
|
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
|
|
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.
|
|
18
|
+
return Age.fromValue(differenceInYears(params.now.ms, params.birthdate.ms));
|
|
19
19
|
}
|
|
20
20
|
static fromBirthdate(candidate) {
|
|
21
|
-
const birthdate =
|
|
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.
|
|
24
|
+
return Age.fromValue(differenceInYears(candidate.now.ms, birthdate.ms));
|
|
25
25
|
}
|
|
26
26
|
get() {
|
|
27
27
|
return this.value;
|
package/dist/clock.vo.d.ts
CHANGED
|
@@ -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 {
|
|
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
|
|
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
|
|
14
|
-
const hour = Hour.
|
|
15
|
-
const minute = Minute.
|
|
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
|
|
2
|
-
import {
|
|
1
|
+
import { Duration } from "./duration.service";
|
|
2
|
+
import { TimestampVO } from "./timestamp.vo";
|
|
3
3
|
type GetStartOfDayTsInTzConfigType = {
|
|
4
|
-
now:
|
|
5
|
-
|
|
4
|
+
now: TimestampVO;
|
|
5
|
+
timeZoneOffset: Duration;
|
|
6
6
|
};
|
|
7
7
|
export declare class DateCalculator {
|
|
8
|
-
static getStartOfDayTsInTz(config: GetStartOfDayTsInTzConfigType):
|
|
8
|
+
static getStartOfDayTsInTz(config: GetStartOfDayTsInTzConfigType): TimestampVO;
|
|
9
9
|
}
|
|
10
10
|
export {};
|
|
@@ -1,15 +1,12 @@
|
|
|
1
1
|
import { Duration } from "./duration.service";
|
|
2
|
-
import {
|
|
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
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
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
|
}
|
package/dist/date-range.vo.d.ts
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
import type {
|
|
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:
|
|
9
|
-
getStart():
|
|
10
|
-
getEnd():
|
|
11
|
-
toRange(): [
|
|
12
|
-
contains(timestamp:
|
|
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;
|
package/dist/date-range.vo.js
CHANGED
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 {
|
|
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:
|
|
6
|
-
static
|
|
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 {
|
|
5
|
+
import { TimestampVO } from "./timestamp.vo";
|
|
6
6
|
export class Day extends DateRange {
|
|
7
7
|
static fromTimestamp(timestamp) {
|
|
8
|
-
const date = new Date(timestamp.
|
|
9
|
-
const startUtc =
|
|
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 =
|
|
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.
|
|
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
|
|
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
|
|
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
|
|
11
|
-
return new Hour(new Date(timestamp.
|
|
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;
|
package/dist/minute.vo.d.ts
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
import { type MinuteSchemaType } from "./minute-schema.vo";
|
|
2
|
-
import
|
|
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
|
|
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
|
|
10
|
-
return new Minute(new Date(timestamp.
|
|
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;
|
package/dist/month.vo.d.ts
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import { DateRange } from "./date-range.vo";
|
|
2
2
|
import { type MonthIsoIdType } from "./month-iso-id.vo";
|
|
3
|
-
import {
|
|
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:
|
|
6
|
-
static
|
|
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 {
|
|
4
|
+
import { TimestampVO } from "./timestamp.vo";
|
|
5
5
|
export class Month extends DateRange {
|
|
6
6
|
static fromTimestamp(timestamp) {
|
|
7
|
-
const start =
|
|
8
|
-
const end =
|
|
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(
|
|
20
|
+
return Month.fromTimestamp(TimestampVO.fromNumber(reference));
|
|
18
21
|
}
|
|
19
22
|
toIsoId() {
|
|
20
|
-
return MonthIsoId.parse(format(this.getStart().
|
|
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().
|
|
30
|
-
return Month.fromTimestamp(
|
|
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();
|
package/dist/quarter.vo.d.ts
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import { DateRange } from "./date-range.vo";
|
|
2
2
|
import { type QuarterIsoIdType } from "./quarter-iso-id.vo";
|
|
3
|
-
import {
|
|
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:
|
|
6
|
-
static
|
|
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;
|
package/dist/quarter.vo.js
CHANGED
|
@@ -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 {
|
|
4
|
+
import { TimestampVO } from "./timestamp.vo";
|
|
5
5
|
export class Quarter extends DateRange {
|
|
6
6
|
static fromTimestamp(timestamp) {
|
|
7
|
-
const start =
|
|
8
|
-
const end =
|
|
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(
|
|
20
|
+
return Quarter.fromTimestamp(TimestampVO.fromNumber(reference));
|
|
18
21
|
}
|
|
19
22
|
toIsoId() {
|
|
20
|
-
const year = getYear(this.getStart().
|
|
21
|
-
const quarter = getQuarter(this.getStart().
|
|
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 {
|
|
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:
|
|
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
|
-
|
|
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 {
|
|
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:
|
|
10
|
-
static falsy(timestamp: Falsy<
|
|
9
|
+
static truthy(timestamp: TimestampVO): RelativeDateType;
|
|
10
|
+
static falsy(timestamp: Falsy<TimestampVO>): RelativeDateType | null;
|
|
11
11
|
private static _format;
|
|
12
12
|
}
|
|
13
13
|
export {};
|
package/dist/relative-date.vo.js
CHANGED
|
@@ -9,6 +9,6 @@ export class RelativeDate {
|
|
|
9
9
|
return RelativeDate._format(timestamp);
|
|
10
10
|
}
|
|
11
11
|
static _format(timestamp) {
|
|
12
|
-
return { raw: timestamp.
|
|
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 {
|
|
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:
|
|
10
|
+
constructor(start: TimestampVO);
|
|
11
11
|
stop(): StopwatchResultType;
|
|
12
12
|
}
|
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import {
|
|
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
|
|
18
|
+
return TimestampVO.fromNumber(Date.now()).difference(this.start);
|
|
20
19
|
}
|
|
21
20
|
}
|
package/dist/timestamp.vo.d.ts
CHANGED
|
@@ -1,18 +1,19 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { Duration } from "./duration.service";
|
|
2
2
|
import { type TimestampValueType } from "./timestamp-value.vo";
|
|
3
|
-
export declare class
|
|
3
|
+
export declare class TimestampVO {
|
|
4
4
|
private readonly value;
|
|
5
5
|
constructor(value: TimestampValueType);
|
|
6
|
-
static fromValue(value: TimestampValueType):
|
|
7
|
-
static fromNumber(value: number):
|
|
8
|
-
add(duration: Duration):
|
|
9
|
-
subtract(duration: Duration):
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
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
|
}
|
package/dist/timestamp.vo.js
CHANGED
|
@@ -1,20 +1,24 @@
|
|
|
1
|
+
import { Duration } from "./duration.service";
|
|
1
2
|
import { TimestampValue } from "./timestamp-value.vo";
|
|
2
|
-
export class
|
|
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
|
|
9
|
+
return new TimestampVO(value);
|
|
9
10
|
}
|
|
10
11
|
static fromNumber(value) {
|
|
11
|
-
return new
|
|
12
|
+
return new TimestampVO(TimestampValue.parse(value));
|
|
12
13
|
}
|
|
13
14
|
add(duration) {
|
|
14
|
-
return
|
|
15
|
+
return TimestampVO.fromNumber(this.value + duration.ms);
|
|
15
16
|
}
|
|
16
17
|
subtract(duration) {
|
|
17
|
-
return
|
|
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 {
|
|
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:
|
|
6
|
-
static
|
|
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 {
|
|
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 =
|
|
8
|
-
const end =
|
|
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(
|
|
21
|
+
return Week.fromTimestamp(TimestampVO.fromNumber(reference));
|
|
19
22
|
}
|
|
20
23
|
toIsoId() {
|
|
21
|
-
const year = getISOWeekYear(this.getStart().
|
|
22
|
-
const week = getISOWeek(this.getStart().
|
|
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().
|
|
33
|
-
return Week.fromTimestamp(
|
|
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();
|
package/dist/weekday.vo.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import
|
|
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
|
|
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;
|