@bgord/tools 0.12.1 → 0.12.3

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.
@@ -0,0 +1,11 @@
1
+ import { TimestampType } from "./timestamp.vo";
2
+ export declare class DateRange {
3
+ private readonly start;
4
+ private readonly end;
5
+ constructor(start: TimestampType, end: TimestampType);
6
+ getStart(): TimestampType;
7
+ getEnd(): TimestampType;
8
+ toRange(): [TimestampType, TimestampType];
9
+ contains(timestamp: TimestampType): boolean;
10
+ equals(other: DateRange): boolean;
11
+ }
@@ -0,0 +1,25 @@
1
+ export class DateRange {
2
+ start;
3
+ end;
4
+ constructor(start, end) {
5
+ this.start = start;
6
+ this.end = end;
7
+ if (start > end)
8
+ throw new Error("Invalid date range");
9
+ }
10
+ getStart() {
11
+ return this.start;
12
+ }
13
+ getEnd() {
14
+ return this.end;
15
+ }
16
+ toRange() {
17
+ return [this.start, this.end];
18
+ }
19
+ contains(timestamp) {
20
+ return timestamp >= this.start && timestamp <= this.end;
21
+ }
22
+ equals(other) {
23
+ return this.start === other.start && this.end === other.end;
24
+ }
25
+ }
package/dist/index.d.ts CHANGED
@@ -3,6 +3,7 @@ export * from "./build-version.vo";
3
3
  export * from "./clock.vo";
4
4
  export * from "./date-calculator.service";
5
5
  export * from "./date-formatter.service";
6
+ export * from "./date-range.vo";
6
7
  export * from "./dates-of-the-week.vo";
7
8
  export * from "./dll.service";
8
9
  export * from "./email-mask.service";
@@ -46,4 +47,5 @@ export * from "./timestamp.vo";
46
47
  export * from "./timezone.vo";
47
48
  export * from "./ts-utils";
48
49
  export * from "./visually-unambiguous-characters-generator.service";
50
+ export * from "./week.vo";
49
51
  export * from "./z-score.service";
package/dist/index.js CHANGED
@@ -3,6 +3,7 @@ export * from "./build-version.vo";
3
3
  export * from "./clock.vo";
4
4
  export * from "./date-calculator.service";
5
5
  export * from "./date-formatter.service";
6
+ export * from "./date-range.vo";
6
7
  export * from "./dates-of-the-week.vo";
7
8
  export * from "./dll.service";
8
9
  export * from "./email-mask.service";
@@ -46,4 +47,5 @@ export * from "./timestamp.vo";
46
47
  export * from "./timezone.vo";
47
48
  export * from "./ts-utils";
48
49
  export * from "./visually-unambiguous-characters-generator.service";
50
+ export * from "./week.vo";
49
51
  export * from "./z-score.service";
@@ -1,3 +1,3 @@
1
1
  import { z } from "zod/v4";
2
- export declare const Timestamp: z.core.$ZodBranded<z.ZodDefault<z.ZodNumber>, "Timestamp">;
2
+ export declare const Timestamp: z.core.$ZodBranded<z.ZodNumber, "Timestamp">;
3
3
  export type TimestampType = z.infer<typeof Timestamp>;
@@ -1,7 +1,2 @@
1
1
  import { z } from "zod/v4";
2
- export const Timestamp = z
3
- .number()
4
- .int()
5
- .gte(0)
6
- .default(() => Date.now())
7
- .brand("Timestamp");
2
+ export const Timestamp = z.number().int().gte(0).brand("Timestamp");
@@ -1 +1 @@
1
- {"root":["../src/api-key.vo.ts","../src/build-version.vo.ts","../src/clock.vo.ts","../src/date-calculator.service.ts","../src/date-formatter.service.ts","../src/dates-of-the-week.vo.ts","../src/dll.service.ts","../src/email-mask.service.ts","../src/etags.vo.ts","../src/feature-flag.vo.ts","../src/filter.vo.ts","../src/hour.vo.ts","../src/iban-mask.service.ts","../src/iban.vo.ts","../src/image.vo.ts","../src/index.ts","../src/language.vo.ts","../src/leap-year-checker.service.ts","../src/mean.service.ts","../src/mime-types.vo.ts","../src/mime.vo.ts","../src/min-max-scaler.service.ts","../src/minute.vo.ts","../src/money.vo.ts","../src/noop.service.ts","../src/outlier-detector.service.ts","../src/package-version.vo.ts","../src/pagination.service.ts","../src/percentage.service.ts","../src/population-standard-deviation.service.ts","../src/random.service.ts","../src/rate-limiter.service.ts","../src/relative-date.vo.ts","../src/reordering.service.ts","../src/revision.vo.ts","../src/rounding.service.ts","../src/simple-linear-regression.service.ts","../src/size.vo.ts","../src/stepper.service.ts","../src/stopwatch.service.ts","../src/streak-calculator.service.ts","../src/sum.service.ts","../src/thousands-separator.service.ts","../src/time-zone-offset-value.vo.ts","../src/time.service.ts","../src/timestamp.vo.ts","../src/timezone.vo.ts","../src/ts-utils.ts","../src/visually-unambiguous-characters-generator.service.ts","../src/z-score.service.ts"],"version":"5.8.3"}
1
+ {"root":["../src/api-key.vo.ts","../src/build-version.vo.ts","../src/clock.vo.ts","../src/date-calculator.service.ts","../src/date-formatter.service.ts","../src/date-range.vo.ts","../src/dates-of-the-week.vo.ts","../src/dll.service.ts","../src/email-mask.service.ts","../src/etags.vo.ts","../src/feature-flag.vo.ts","../src/filter.vo.ts","../src/hour.vo.ts","../src/iban-mask.service.ts","../src/iban.vo.ts","../src/image.vo.ts","../src/index.ts","../src/language.vo.ts","../src/leap-year-checker.service.ts","../src/mean.service.ts","../src/mime-types.vo.ts","../src/mime.vo.ts","../src/min-max-scaler.service.ts","../src/minute.vo.ts","../src/money.vo.ts","../src/noop.service.ts","../src/outlier-detector.service.ts","../src/package-version.vo.ts","../src/pagination.service.ts","../src/percentage.service.ts","../src/population-standard-deviation.service.ts","../src/random.service.ts","../src/rate-limiter.service.ts","../src/relative-date.vo.ts","../src/reordering.service.ts","../src/revision.vo.ts","../src/rounding.service.ts","../src/simple-linear-regression.service.ts","../src/size.vo.ts","../src/stepper.service.ts","../src/stopwatch.service.ts","../src/streak-calculator.service.ts","../src/sum.service.ts","../src/thousands-separator.service.ts","../src/time-zone-offset-value.vo.ts","../src/time.service.ts","../src/timestamp.vo.ts","../src/timezone.vo.ts","../src/ts-utils.ts","../src/visually-unambiguous-characters-generator.service.ts","../src/week.vo.ts","../src/z-score.service.ts"],"version":"5.8.3"}
@@ -0,0 +1,9 @@
1
+ import { DateRange } from "./date-range.vo";
2
+ import { TimestampType } from "./timestamp.vo";
3
+ export declare class Week extends DateRange {
4
+ constructor(start: TimestampType, end: TimestampType);
5
+ toIsoId(): string;
6
+ static fromTimestamp(timestamp: TimestampType): Week;
7
+ static fromNow(now?: TimestampType): Week;
8
+ static fromIsoId(isoId: string): Week;
9
+ }
@@ -0,0 +1,29 @@
1
+ import { endOfISOWeek, getISOWeek, getISOWeekYear, setISOWeek, startOfISOWeek } from "date-fns";
2
+ import { DateRange } from "./date-range.vo";
3
+ import { Timestamp } from "./timestamp.vo";
4
+ export class Week extends DateRange {
5
+ constructor(start, end) {
6
+ super(start, end);
7
+ }
8
+ toIsoId() {
9
+ const year = getISOWeekYear(this.getStart());
10
+ const week = getISOWeek(this.getStart()).toString().padStart(2, "0");
11
+ return `${year}-W${week}`;
12
+ }
13
+ static fromTimestamp(timestamp) {
14
+ const start = Timestamp.parse(startOfISOWeek(timestamp).getTime());
15
+ const end = Timestamp.parse(endOfISOWeek(timestamp).getTime());
16
+ return new Week(start, end);
17
+ }
18
+ static fromNow(now = Timestamp.parse(Date.now())) {
19
+ return Week.fromTimestamp(now);
20
+ }
21
+ static fromIsoId(isoId) {
22
+ const [yearPart, weekPart] = isoId.split("-W");
23
+ const year = Number(yearPart);
24
+ const week = Number(weekPart);
25
+ // ISO-8601 rule: Jan 4 is always in week 01 of the ISO week-year.
26
+ const reference = setISOWeek(new Date(Date.UTC(year, 0, 4)), week);
27
+ return Week.fromTimestamp(Timestamp.parse(reference.getTime()));
28
+ }
29
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bgord/tools",
3
- "version": "0.12.1",
3
+ "version": "0.12.3",
4
4
  "type": "module",
5
5
  "license": "MIT",
6
6
  "author": "Bartosz Gordon",
package/readme.md CHANGED
@@ -29,6 +29,7 @@ src/
29
29
  ├── clock.vo.ts
30
30
  ├── date-calculator.service.ts
31
31
  ├── date-formatter.service.ts
32
+ ├── date-range.vo.ts
32
33
  ├── dates-of-the-week.vo.ts
33
34
  ├── dll.service.ts
34
35
  ├── email-mask.service.ts
@@ -72,6 +73,7 @@ src/
72
73
  ├── timezone.vo.ts
73
74
  ├── ts-utils.ts
74
75
  ├── visually-unambiguous-characters-generator.service.ts
76
+ ├── week.vo.ts
75
77
  └── z-score.service.ts
76
78
  ```
77
79
 
@@ -0,0 +1,30 @@
1
+ import { TimestampType } from "./timestamp.vo";
2
+
3
+ export class DateRange {
4
+ constructor(
5
+ private readonly start: TimestampType,
6
+ private readonly end: TimestampType,
7
+ ) {
8
+ if (start > end) throw new Error("Invalid date range");
9
+ }
10
+
11
+ getStart(): TimestampType {
12
+ return this.start;
13
+ }
14
+
15
+ getEnd(): TimestampType {
16
+ return this.end;
17
+ }
18
+
19
+ toRange(): [TimestampType, TimestampType] {
20
+ return [this.start, this.end];
21
+ }
22
+
23
+ contains(timestamp: TimestampType): boolean {
24
+ return timestamp >= this.start && timestamp <= this.end;
25
+ }
26
+
27
+ equals(other: DateRange): boolean {
28
+ return this.start === other.start && this.end === other.end;
29
+ }
30
+ }
package/src/index.ts CHANGED
@@ -3,6 +3,7 @@ export * from "./build-version.vo";
3
3
  export * from "./clock.vo";
4
4
  export * from "./date-calculator.service";
5
5
  export * from "./date-formatter.service";
6
+ export * from "./date-range.vo";
6
7
  export * from "./dates-of-the-week.vo";
7
8
  export * from "./dll.service";
8
9
  export * from "./email-mask.service";
@@ -46,4 +47,5 @@ export * from "./timestamp.vo";
46
47
  export * from "./timezone.vo";
47
48
  export * from "./ts-utils";
48
49
  export * from "./visually-unambiguous-characters-generator.service";
50
+ export * from "./week.vo";
49
51
  export * from "./z-score.service";
@@ -1,10 +1,5 @@
1
1
  import { z } from "zod/v4";
2
2
 
3
- export const Timestamp = z
4
- .number()
5
- .int()
6
- .gte(0)
7
- .default(() => Date.now())
8
- .brand("Timestamp");
3
+ export const Timestamp = z.number().int().gte(0).brand("Timestamp");
9
4
 
10
5
  export type TimestampType = z.infer<typeof Timestamp>;
package/src/week.vo.ts ADDED
@@ -0,0 +1,39 @@
1
+ import { endOfISOWeek, getISOWeek, getISOWeekYear, setISOWeek, startOfISOWeek } from "date-fns";
2
+ import { DateRange } from "./date-range.vo";
3
+ import { Timestamp, TimestampType } from "./timestamp.vo";
4
+
5
+ export class Week extends DateRange {
6
+ constructor(start: TimestampType, end: TimestampType) {
7
+ super(start, end);
8
+ }
9
+
10
+ toIsoId(): string {
11
+ const year = getISOWeekYear(this.getStart());
12
+ const week = getISOWeek(this.getStart()).toString().padStart(2, "0");
13
+
14
+ return `${year}-W${week}`;
15
+ }
16
+
17
+ static fromTimestamp(timestamp: TimestampType): Week {
18
+ const start = Timestamp.parse(startOfISOWeek(timestamp).getTime());
19
+ const end = Timestamp.parse(endOfISOWeek(timestamp).getTime());
20
+
21
+ return new Week(start, end);
22
+ }
23
+
24
+ static fromNow(now: TimestampType = Timestamp.parse(Date.now())): Week {
25
+ return Week.fromTimestamp(now);
26
+ }
27
+
28
+ static fromIsoId(isoId: string): Week {
29
+ const [yearPart, weekPart] = isoId.split("-W");
30
+
31
+ const year = Number(yearPart);
32
+ const week = Number(weekPart);
33
+
34
+ // ISO-8601 rule: Jan 4 is always in week 01 of the ISO week-year.
35
+ const reference = setISOWeek(new Date(Date.UTC(year, 0, 4)), week);
36
+
37
+ return Week.fromTimestamp(Timestamp.parse(reference.getTime()));
38
+ }
39
+ }