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