@bgord/tools 0.17.0 → 0.17.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/month.vo.js CHANGED
@@ -1,4 +1,3 @@
1
- // src/month.vo.ts
2
1
  import { DateRange } from "./date-range.vo";
3
2
  import { MonthIsoId } from "./month-iso-id.vo";
4
3
  import { Timestamp } from "./timestamp.vo";
@@ -1,19 +1,17 @@
1
- import { type TimestampType } from "./timestamp.vo";
2
- type RateLimiterOptionsType = {
3
- ms: TimestampType;
4
- };
1
+ import { Duration } from "./duration.service";
2
+ import type { TimestampType } from "./timestamp.vo";
5
3
  type RateLimiterResultSuccessType = {
6
4
  allowed: true;
7
5
  };
8
6
  type RateLimiterResultErrorType = {
9
7
  allowed: false;
10
- remainingMs: TimestampType;
8
+ remaining: Duration;
11
9
  };
12
10
  type RateLimiterResultType = RateLimiterResultSuccessType | RateLimiterResultErrorType;
13
11
  export declare class RateLimiter {
14
- private readonly options;
12
+ private readonly duration;
15
13
  private lastInvocationTimestampMs;
16
- constructor(options: RateLimiterOptionsType);
14
+ constructor(duration: Duration);
17
15
  verify(currentTimestampMs: TimestampType): RateLimiterResultType;
18
16
  }
19
17
  export {};
@@ -1,22 +1,21 @@
1
- import { Timestamp } from "./timestamp.vo";
1
+ import { Duration } from "./duration.service";
2
2
  export class RateLimiter {
3
- options;
3
+ duration;
4
4
  lastInvocationTimestampMs = null;
5
- constructor(options) {
6
- this.options = options;
5
+ constructor(duration) {
6
+ this.duration = duration;
7
7
  }
8
8
  verify(currentTimestampMs) {
9
9
  if (this.lastInvocationTimestampMs == null) {
10
10
  this.lastInvocationTimestampMs = currentTimestampMs;
11
11
  return { allowed: true };
12
12
  }
13
- const nextAllowedTimestampMs = this.lastInvocationTimestampMs + this.options.ms;
13
+ const nextAllowedTimestampMs = this.lastInvocationTimestampMs + this.duration.ms;
14
14
  if (nextAllowedTimestampMs <= currentTimestampMs) {
15
15
  this.lastInvocationTimestampMs = currentTimestampMs;
16
16
  return { allowed: true };
17
17
  }
18
18
  const remainingDelta = nextAllowedTimestampMs - currentTimestampMs;
19
- const remainingMs = Timestamp.parse(remainingDelta);
20
- return { allowed: false, remainingMs };
19
+ return { allowed: false, remaining: Duration.Ms(remainingDelta) };
21
20
  }
22
21
  }
@@ -5,8 +5,8 @@ type RelativeDateType = {
5
5
  relative: string;
6
6
  };
7
7
  export declare class RelativeDate {
8
- static truthy(timestampMs: TimestampType): RelativeDateType;
9
- static falsy(timestampMs: Falsy<TimestampType>): RelativeDateType | null;
8
+ static truthy(timestamp: TimestampType): RelativeDateType;
9
+ static falsy(timestamp: Falsy<TimestampType>): RelativeDateType | null;
10
10
  private static _format;
11
11
  }
12
12
  export {};
@@ -1,12 +1,12 @@
1
1
  import { DateFormatters } from "./date-formatter.service";
2
2
  export class RelativeDate {
3
- static truthy(timestampMs) {
4
- return RelativeDate._format(timestampMs);
3
+ static truthy(timestamp) {
4
+ return RelativeDate._format(timestamp);
5
5
  }
6
- static falsy(timestampMs) {
7
- if (!timestampMs)
6
+ static falsy(timestamp) {
7
+ if (!timestamp)
8
8
  return null;
9
- return RelativeDate._format(timestampMs);
9
+ return RelativeDate._format(timestamp);
10
10
  }
11
11
  static _format(timestampMs) {
12
12
  return { raw: timestampMs, relative: DateFormatters.relative(timestampMs) };
@@ -1,8 +1,7 @@
1
+ import { Duration } from "./duration.service";
1
2
  import { type TimestampType } from "./timestamp.vo";
2
3
  export declare const StopwatchStateError: "stopwatch.already.stopped";
3
- export type StopwatchResultType = {
4
- durationMs: TimestampType;
5
- };
4
+ export type StopwatchResultType = Duration;
6
5
  export declare class Stopwatch {
7
6
  private readonly startMs;
8
7
  private state;
@@ -1,3 +1,4 @@
1
+ import { Duration } from "./duration.service";
1
2
  import { Timestamp } from "./timestamp.vo";
2
3
  var StopwatchState;
3
4
  (function (StopwatchState) {
@@ -15,6 +16,6 @@ export class Stopwatch {
15
16
  if (this.state === StopwatchState.stopped)
16
17
  throw new Error(StopwatchStateError);
17
18
  this.state = StopwatchState.stopped;
18
- return { durationMs: Timestamp.parse(Date.now() - this.startMs) };
19
+ return Duration.Ms(Timestamp.parse(Date.now() - this.startMs));
19
20
  }
20
21
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bgord/tools",
3
- "version": "0.17.0",
3
+ "version": "0.17.2",
4
4
  "type": "module",
5
5
  "license": "MIT",
6
6
  "author": "Bartosz Gordon",
@@ -27,19 +27,19 @@
27
27
  "@types/bun": "1.2.23",
28
28
  "@types/mime-types": "3.0.1",
29
29
  "cspell": "9.2.1",
30
- "knip": "5.64.1",
30
+ "knip": "5.64.2",
31
31
  "lefthook": "1.13.6",
32
32
  "only-allow": "1.2.1",
33
33
  "shellcheck": "4.1.0",
34
34
  "typescript": "5.9.3",
35
- "zod": "4.1.11"
35
+ "zod": "4.1.12"
36
36
  },
37
37
  "dependencies": {
38
38
  "date-fns": "4.1.0",
39
39
  "mime-types": "3.0.1"
40
40
  },
41
41
  "peerDependencies": {
42
- "zod": "4.1.11"
42
+ "zod": "4.1.12"
43
43
  },
44
44
  "sideEffects": false
45
45
  }
package/src/month.vo.ts CHANGED
@@ -1,4 +1,3 @@
1
- // src/month.vo.ts
2
1
  import { DateRange } from "./date-range.vo";
3
2
  import { MonthIsoId, type MonthIsoIdType } from "./month-iso-id.vo";
4
3
  import { Timestamp, type TimestampType } from "./timestamp.vo";
@@ -1,14 +1,14 @@
1
- import { Timestamp, type TimestampType } from "./timestamp.vo";
1
+ import { Duration } from "./duration.service";
2
+ import type { TimestampType } from "./timestamp.vo";
2
3
 
3
- type RateLimiterOptionsType = { ms: TimestampType };
4
4
  type RateLimiterResultSuccessType = { allowed: true };
5
- type RateLimiterResultErrorType = { allowed: false; remainingMs: TimestampType };
5
+ type RateLimiterResultErrorType = { allowed: false; remaining: Duration };
6
6
  type RateLimiterResultType = RateLimiterResultSuccessType | RateLimiterResultErrorType;
7
7
 
8
8
  export class RateLimiter {
9
9
  private lastInvocationTimestampMs: TimestampType | null = null;
10
10
 
11
- constructor(private readonly options: RateLimiterOptionsType) {}
11
+ constructor(private readonly duration: Duration) {}
12
12
 
13
13
  verify(currentTimestampMs: TimestampType): RateLimiterResultType {
14
14
  if (this.lastInvocationTimestampMs == null) {
@@ -17,7 +17,7 @@ export class RateLimiter {
17
17
  return { allowed: true };
18
18
  }
19
19
 
20
- const nextAllowedTimestampMs = this.lastInvocationTimestampMs + this.options.ms;
20
+ const nextAllowedTimestampMs = this.lastInvocationTimestampMs + this.duration.ms;
21
21
 
22
22
  if (nextAllowedTimestampMs <= currentTimestampMs) {
23
23
  this.lastInvocationTimestampMs = currentTimestampMs;
@@ -25,8 +25,7 @@ export class RateLimiter {
25
25
  }
26
26
 
27
27
  const remainingDelta = nextAllowedTimestampMs - currentTimestampMs;
28
- const remainingMs = Timestamp.parse(remainingDelta);
29
28
 
30
- return { allowed: false, remainingMs };
29
+ return { allowed: false, remaining: Duration.Ms(remainingDelta) };
31
30
  }
32
31
  }
@@ -5,13 +5,13 @@ import type { Falsy } from "./ts-utils";
5
5
  type RelativeDateType = { raw: TimestampType; relative: string };
6
6
 
7
7
  export class RelativeDate {
8
- static truthy(timestampMs: TimestampType): RelativeDateType {
9
- return RelativeDate._format(timestampMs);
8
+ static truthy(timestamp: TimestampType): RelativeDateType {
9
+ return RelativeDate._format(timestamp);
10
10
  }
11
11
 
12
- static falsy(timestampMs: Falsy<TimestampType>): RelativeDateType | null {
13
- if (!timestampMs) return null;
14
- return RelativeDate._format(timestampMs);
12
+ static falsy(timestamp: Falsy<TimestampType>): RelativeDateType | null {
13
+ if (!timestamp) return null;
14
+ return RelativeDate._format(timestamp);
15
15
  }
16
16
 
17
17
  private static _format(timestampMs: TimestampType): RelativeDateType {
@@ -1,3 +1,4 @@
1
+ import { Duration } from "./duration.service";
1
2
  import { Timestamp, type TimestampType } from "./timestamp.vo";
2
3
 
3
4
  enum StopwatchState {
@@ -7,7 +8,7 @@ enum StopwatchState {
7
8
 
8
9
  export const StopwatchStateError = "stopwatch.already.stopped" as const;
9
10
 
10
- export type StopwatchResultType = { durationMs: TimestampType };
11
+ export type StopwatchResultType = Duration;
11
12
 
12
13
  export class Stopwatch {
13
14
  private state: StopwatchState = StopwatchState.started;
@@ -19,6 +20,6 @@ export class Stopwatch {
19
20
 
20
21
  this.state = StopwatchState.stopped;
21
22
 
22
- return { durationMs: Timestamp.parse(Date.now() - this.startMs) };
23
+ return Duration.Ms(Timestamp.parse(Date.now() - this.startMs));
23
24
  }
24
25
  }