@bgord/tools 0.17.0 → 0.17.1
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 +0 -1
- package/dist/rate-limiter.service.d.ts +5 -7
- package/dist/rate-limiter.service.js +6 -7
- package/dist/relative-date.vo.d.ts +2 -2
- package/dist/relative-date.vo.js +5 -5
- package/dist/stopwatch.service.d.ts +2 -3
- package/dist/stopwatch.service.js +2 -1
- package/package.json +1 -1
- package/src/month.vo.ts +0 -1
- package/src/rate-limiter.service.ts +6 -7
- package/src/relative-date.vo.ts +5 -5
- package/src/stopwatch.service.ts +3 -2
package/dist/month.vo.js
CHANGED
|
@@ -1,19 +1,17 @@
|
|
|
1
|
-
import {
|
|
2
|
-
type
|
|
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
|
-
|
|
8
|
+
remaining: Duration;
|
|
11
9
|
};
|
|
12
10
|
type RateLimiterResultType = RateLimiterResultSuccessType | RateLimiterResultErrorType;
|
|
13
11
|
export declare class RateLimiter {
|
|
14
|
-
private readonly
|
|
12
|
+
private readonly duration;
|
|
15
13
|
private lastInvocationTimestampMs;
|
|
16
|
-
constructor(
|
|
14
|
+
constructor(duration: Duration);
|
|
17
15
|
verify(currentTimestampMs: TimestampType): RateLimiterResultType;
|
|
18
16
|
}
|
|
19
17
|
export {};
|
|
@@ -1,22 +1,21 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Duration } from "./duration.service";
|
|
2
2
|
export class RateLimiter {
|
|
3
|
-
|
|
3
|
+
duration;
|
|
4
4
|
lastInvocationTimestampMs = null;
|
|
5
|
-
constructor(
|
|
6
|
-
this.
|
|
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.
|
|
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
|
-
|
|
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(
|
|
9
|
-
static falsy(
|
|
8
|
+
static truthy(timestamp: TimestampType): RelativeDateType;
|
|
9
|
+
static falsy(timestamp: Falsy<TimestampType>): RelativeDateType | null;
|
|
10
10
|
private static _format;
|
|
11
11
|
}
|
|
12
12
|
export {};
|
package/dist/relative-date.vo.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { DateFormatters } from "./date-formatter.service";
|
|
2
2
|
export class RelativeDate {
|
|
3
|
-
static truthy(
|
|
4
|
-
return RelativeDate._format(
|
|
3
|
+
static truthy(timestamp) {
|
|
4
|
+
return RelativeDate._format(timestamp);
|
|
5
5
|
}
|
|
6
|
-
static falsy(
|
|
7
|
-
if (!
|
|
6
|
+
static falsy(timestamp) {
|
|
7
|
+
if (!timestamp)
|
|
8
8
|
return null;
|
|
9
|
-
return RelativeDate._format(
|
|
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
|
|
19
|
+
return Duration.Ms(Timestamp.parse(Date.now() - this.startMs));
|
|
19
20
|
}
|
|
20
21
|
}
|
package/package.json
CHANGED
package/src/month.vo.ts
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
import {
|
|
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;
|
|
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
|
|
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.
|
|
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,
|
|
29
|
+
return { allowed: false, remaining: Duration.Ms(remainingDelta) };
|
|
31
30
|
}
|
|
32
31
|
}
|
package/src/relative-date.vo.ts
CHANGED
|
@@ -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(
|
|
9
|
-
return RelativeDate._format(
|
|
8
|
+
static truthy(timestamp: TimestampType): RelativeDateType {
|
|
9
|
+
return RelativeDate._format(timestamp);
|
|
10
10
|
}
|
|
11
11
|
|
|
12
|
-
static falsy(
|
|
13
|
-
if (!
|
|
14
|
-
return RelativeDate._format(
|
|
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 {
|
package/src/stopwatch.service.ts
CHANGED
|
@@ -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 =
|
|
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
|
|
23
|
+
return Duration.Ms(Timestamp.parse(Date.now() - this.startMs));
|
|
23
24
|
}
|
|
24
25
|
}
|