@bgord/tools 1.2.7 → 1.2.9
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/distance.vo.d.ts +3 -3
- package/dist/distance.vo.js +3 -3
- package/dist/duration.service.d.ts +1 -0
- package/dist/duration.service.js +5 -3
- package/dist/height.vo.d.ts +4 -4
- package/dist/height.vo.js +5 -4
- package/dist/index.d.ts +5 -2
- package/dist/index.js +5 -2
- package/dist/linear-regression.service.d.ts +4 -4
- package/dist/linear-regression.service.js +2 -2
- package/dist/mean.service.d.ts +2 -2
- package/dist/mean.service.js +2 -2
- package/dist/min-max-scaler.service.d.ts +2 -2
- package/dist/min-max-scaler.service.js +2 -2
- package/dist/money.vo.d.ts +3 -3
- package/dist/money.vo.js +4 -3
- package/dist/pagination.service.js +2 -2
- package/dist/percentage.service.d.ts +2 -2
- package/dist/percentage.service.js +2 -2
- package/dist/population-standard-deviation.service.d.ts +2 -2
- package/dist/population-standard-deviation.service.js +2 -2
- package/dist/random.service.js +2 -2
- package/dist/rounding-decimal.strategy.d.ts +12 -0
- package/dist/{rounding.adapter.js → rounding-decimal.strategy.js} +1 -16
- package/dist/rounding-down.strategy.d.ts +4 -0
- package/dist/rounding-down.strategy.js +5 -0
- package/dist/rounding-to-nearest.strategy.d.ts +4 -0
- package/dist/rounding-to-nearest.strategy.js +5 -0
- package/dist/rounding-up.strategy.d.ts +4 -0
- package/dist/rounding-up.strategy.js +5 -0
- package/dist/{rounding.port.d.ts → rounding.strategy.d.ts} +1 -1
- package/dist/size.vo.js +4 -3
- package/dist/time-zone-offset-value.vo.d.ts +6 -1
- package/dist/time-zone-offset-value.vo.js +14 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/dist/weight.vo.d.ts +3 -3
- package/dist/weight.vo.js +4 -4
- package/dist/z-score.service.d.ts +2 -2
- package/dist/z-score.service.js +2 -2
- package/package.json +1 -1
- package/readme.md +5 -2
- package/src/distance.vo.ts +10 -4
- package/src/duration.service.ts +7 -4
- package/src/height.vo.ts +11 -7
- package/src/index.ts +5 -2
- package/src/linear-regression.service.ts +7 -7
- package/src/mean.service.ts +4 -4
- package/src/min-max-scaler.service.ts +5 -5
- package/src/money.vo.ts +7 -6
- package/src/pagination.service.ts +2 -2
- package/src/percentage.service.ts +7 -3
- package/src/population-standard-deviation.service.ts +3 -3
- package/src/random.service.ts +2 -2
- package/src/{rounding.adapter.ts → rounding-decimal.strategy.ts} +2 -20
- package/src/rounding-down.strategy.ts +7 -0
- package/src/rounding-to-nearest.strategy.ts +7 -0
- package/src/rounding-up.strategy.ts +7 -0
- package/src/rounding.strategy.ts +3 -0
- package/src/size.vo.ts +4 -3
- package/src/time-zone-offset-value.vo.ts +15 -1
- package/src/weight.vo.ts +8 -5
- package/src/z-score.service.ts +3 -3
- package/dist/rounding.adapter.d.ts +0 -21
- package/src/rounding.port.ts +0 -3
- /package/dist/{rounding.port.js → rounding.strategy.js} +0 -0
package/dist/distance.vo.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { type DistanceValueType } from "./distance-value.vo";
|
|
2
|
-
import type {
|
|
2
|
+
import type { RoundingStrategy } from "./rounding.strategy";
|
|
3
3
|
export declare const DistanceError: {
|
|
4
4
|
SubtractResultLessThanZero: string;
|
|
5
5
|
};
|
|
@@ -9,8 +9,8 @@ export declare class Distance {
|
|
|
9
9
|
private constructor();
|
|
10
10
|
static fromMeters(candidate: number): Distance;
|
|
11
11
|
static fromMetersSafe(candidate: DistanceValueType): Distance;
|
|
12
|
-
static fromKilometers(candidate: number, rounding?:
|
|
13
|
-
static fromMiles(candidate: number, rounding?:
|
|
12
|
+
static fromKilometers(candidate: number, rounding?: RoundingStrategy): Distance;
|
|
13
|
+
static fromMiles(candidate: number, rounding?: RoundingStrategy): Distance;
|
|
14
14
|
get(): DistanceValueType;
|
|
15
15
|
add(distance: Distance): Distance;
|
|
16
16
|
subtract(money: Distance): Distance;
|
package/dist/distance.vo.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { DistanceValue } from "./distance-value.vo";
|
|
2
|
-
import {
|
|
2
|
+
import { RoundingToNearestStrategy } from "./rounding-to-nearest.strategy";
|
|
3
3
|
export const DistanceError = { SubtractResultLessThanZero: "distance.subtract.result.less.than.zero" };
|
|
4
4
|
export class Distance {
|
|
5
5
|
value;
|
|
@@ -13,10 +13,10 @@ export class Distance {
|
|
|
13
13
|
static fromMetersSafe(candidate) {
|
|
14
14
|
return new Distance(candidate);
|
|
15
15
|
}
|
|
16
|
-
static fromKilometers(candidate, rounding = new
|
|
16
|
+
static fromKilometers(candidate, rounding = new RoundingToNearestStrategy()) {
|
|
17
17
|
return new Distance(DistanceValue.parse(rounding.round(candidate * 1000)));
|
|
18
18
|
}
|
|
19
|
-
static fromMiles(candidate, rounding = new
|
|
19
|
+
static fromMiles(candidate, rounding = new RoundingToNearestStrategy()) {
|
|
20
20
|
return new Distance(DistanceValue.parse(rounding.round(candidate * 1_609.344)));
|
|
21
21
|
}
|
|
22
22
|
get() {
|
|
@@ -3,6 +3,7 @@ import type { MultiplicationFactorType } from "./multiplication-factor.vo";
|
|
|
3
3
|
export declare class Duration {
|
|
4
4
|
private static readonly rounding;
|
|
5
5
|
private readonly internal;
|
|
6
|
+
static readonly MIN: Duration;
|
|
6
7
|
private static readonly MS_IN_SECOND;
|
|
7
8
|
private static readonly MS_IN_MINUTE;
|
|
8
9
|
private static readonly MS_IN_HOUR;
|
package/dist/duration.service.js
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import { DurationMs } from "./duration-ms.vo";
|
|
2
|
-
import {
|
|
2
|
+
import { RoundingDecimalStrategy } from "./rounding-decimal.strategy";
|
|
3
|
+
import { RoundingToNearestStrategy } from "./rounding-to-nearest.strategy";
|
|
3
4
|
export class Duration {
|
|
4
|
-
static rounding = new
|
|
5
|
+
static rounding = new RoundingDecimalStrategy(2);
|
|
5
6
|
internal;
|
|
7
|
+
static MIN = Duration.Ms(1);
|
|
6
8
|
static MS_IN_SECOND = 1_000;
|
|
7
9
|
static MS_IN_MINUTE = 60 * Duration.MS_IN_SECOND;
|
|
8
10
|
static MS_IN_HOUR = 60 * Duration.MS_IN_MINUTE;
|
|
@@ -63,7 +65,7 @@ export class Duration {
|
|
|
63
65
|
return Duration.Ms(this.internal - another.internal);
|
|
64
66
|
}
|
|
65
67
|
times(factor) {
|
|
66
|
-
const rounding = new
|
|
68
|
+
const rounding = new RoundingToNearestStrategy();
|
|
67
69
|
return Duration.Ms(rounding.round(this.internal * factor));
|
|
68
70
|
}
|
|
69
71
|
toAbsolute() {
|
package/dist/height.vo.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { type HeightMillimetersType } from "./height-milimiters.vo";
|
|
2
|
-
import type {
|
|
2
|
+
import type { RoundingStrategy } from "./rounding.strategy";
|
|
3
3
|
export declare class Height {
|
|
4
4
|
private readonly millimeters;
|
|
5
5
|
private static readonly ZERO;
|
|
@@ -7,12 +7,12 @@ export declare class Height {
|
|
|
7
7
|
private constructor();
|
|
8
8
|
static fromMillimeters(millimeters: number): Height;
|
|
9
9
|
static fromMillimetersSafe(millimeters: HeightMillimetersType): Height;
|
|
10
|
-
static fromCentimeters(centimeters: number, rounding?:
|
|
10
|
+
static fromCentimeters(centimeters: number, rounding?: RoundingStrategy): Height;
|
|
11
11
|
static zero(): Height;
|
|
12
12
|
get(): number;
|
|
13
13
|
toMillimeters(): number;
|
|
14
|
-
toCentimeters(rounding?:
|
|
15
|
-
format(rounding?:
|
|
14
|
+
toCentimeters(rounding?: RoundingStrategy): number;
|
|
15
|
+
format(rounding?: RoundingStrategy): string;
|
|
16
16
|
toString(): string;
|
|
17
17
|
equals(another: Height): boolean;
|
|
18
18
|
greaterThan(another: Height): boolean;
|
package/dist/height.vo.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { HeightMillimeters } from "./height-milimiters.vo";
|
|
2
|
-
import {
|
|
2
|
+
import { RoundingDecimalStrategy } from "./rounding-decimal.strategy";
|
|
3
|
+
import { RoundingToNearestStrategy } from "./rounding-to-nearest.strategy";
|
|
3
4
|
export class Height {
|
|
4
5
|
millimeters;
|
|
5
6
|
static ZERO = HeightMillimeters.parse(0);
|
|
@@ -13,7 +14,7 @@ export class Height {
|
|
|
13
14
|
static fromMillimetersSafe(millimeters) {
|
|
14
15
|
return new Height(millimeters);
|
|
15
16
|
}
|
|
16
|
-
static fromCentimeters(centimeters, rounding = new
|
|
17
|
+
static fromCentimeters(centimeters, rounding = new RoundingToNearestStrategy()) {
|
|
17
18
|
const millimeters = rounding.round(centimeters * Height.MILLIMETERS_PER_CENTIMETER);
|
|
18
19
|
return new Height(HeightMillimeters.parse(millimeters));
|
|
19
20
|
}
|
|
@@ -33,12 +34,12 @@ export class Height {
|
|
|
33
34
|
return centimeters;
|
|
34
35
|
}
|
|
35
36
|
format(rounding) {
|
|
36
|
-
const chosen = rounding ?? new
|
|
37
|
+
const chosen = rounding ?? new RoundingDecimalStrategy(1);
|
|
37
38
|
const value = this.toCentimeters(chosen);
|
|
38
39
|
return `${value} cm`;
|
|
39
40
|
}
|
|
40
41
|
toString() {
|
|
41
|
-
return this.format(new
|
|
42
|
+
return this.format(new RoundingDecimalStrategy(1));
|
|
42
43
|
}
|
|
43
44
|
equals(another) {
|
|
44
45
|
return this.millimeters === another.millimeters;
|
package/dist/index.d.ts
CHANGED
|
@@ -74,8 +74,11 @@ export * from "./reordering.service";
|
|
|
74
74
|
export * from "./reordering-item-position-value.vo";
|
|
75
75
|
export * from "./revision.vo";
|
|
76
76
|
export * from "./revision-value.vo";
|
|
77
|
-
export * from "./rounding.
|
|
78
|
-
export * from "./rounding.
|
|
77
|
+
export * from "./rounding.strategy";
|
|
78
|
+
export * from "./rounding-decimal.strategy";
|
|
79
|
+
export * from "./rounding-down.strategy";
|
|
80
|
+
export * from "./rounding-to-nearest.strategy";
|
|
81
|
+
export * from "./rounding-up.strategy";
|
|
79
82
|
export * from "./size.vo";
|
|
80
83
|
export * from "./size-bytes.vo";
|
|
81
84
|
export * from "./slug.service";
|
package/dist/index.js
CHANGED
|
@@ -74,8 +74,11 @@ export * from "./reordering.service";
|
|
|
74
74
|
export * from "./reordering-item-position-value.vo";
|
|
75
75
|
export * from "./revision.vo";
|
|
76
76
|
export * from "./revision-value.vo";
|
|
77
|
-
export * from "./rounding.
|
|
78
|
-
export * from "./rounding.
|
|
77
|
+
export * from "./rounding.strategy";
|
|
78
|
+
export * from "./rounding-decimal.strategy";
|
|
79
|
+
export * from "./rounding-down.strategy";
|
|
80
|
+
export * from "./rounding-to-nearest.strategy";
|
|
81
|
+
export * from "./rounding-up.strategy";
|
|
79
82
|
export * from "./size.vo";
|
|
80
83
|
export * from "./size-bytes.vo";
|
|
81
84
|
export * from "./slug.service";
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { RoundingStrategy } from "./rounding.strategy";
|
|
2
2
|
export type LinearRegressionPairType = {
|
|
3
3
|
x: number;
|
|
4
4
|
y: number;
|
|
@@ -20,8 +20,8 @@ export declare class LinearRegression {
|
|
|
20
20
|
private static readonly DEFAULT_ROUNDING;
|
|
21
21
|
private readonly params;
|
|
22
22
|
private readonly rounding;
|
|
23
|
-
constructor(params: LinearRegressionParamsType, rounding?:
|
|
24
|
-
static fromPairs(pairs: LinearRegressionPairType[], rounding?:
|
|
25
|
-
predict(x: LinearRegressionPairType["x"], rounding?:
|
|
23
|
+
constructor(params: LinearRegressionParamsType, rounding?: RoundingStrategy);
|
|
24
|
+
static fromPairs(pairs: LinearRegressionPairType[], rounding?: RoundingStrategy): LinearRegression;
|
|
25
|
+
predict(x: LinearRegressionPairType["x"], rounding?: RoundingStrategy): LinearRegressionPredictionType;
|
|
26
26
|
inspect(): LinearRegression["params"];
|
|
27
27
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { RoundingToNearestStrategy } from "./rounding-to-nearest.strategy";
|
|
2
2
|
export const LinearRegressionError = {
|
|
3
3
|
MinPairs: "linear.regression.min.pairs",
|
|
4
4
|
SumXTooBig: "linear.regression.sum.x.too.big",
|
|
@@ -8,7 +8,7 @@ export const LinearRegressionError = {
|
|
|
8
8
|
ModelCreation: "linear.regression.model.creation",
|
|
9
9
|
};
|
|
10
10
|
export class LinearRegression {
|
|
11
|
-
static DEFAULT_ROUNDING = new
|
|
11
|
+
static DEFAULT_ROUNDING = new RoundingToNearestStrategy();
|
|
12
12
|
params;
|
|
13
13
|
rounding;
|
|
14
14
|
constructor(params, rounding) {
|
package/dist/mean.service.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { RoundingStrategy } from "./rounding.strategy";
|
|
2
2
|
export declare const MeanError: {
|
|
3
3
|
NotEnoughValues: string;
|
|
4
4
|
};
|
|
5
5
|
export declare class Mean {
|
|
6
6
|
private static readonly DEFAULT_ROUNDING;
|
|
7
|
-
static calculate(values: number[], rounding?:
|
|
7
|
+
static calculate(values: number[], rounding?: RoundingStrategy): number;
|
|
8
8
|
}
|
package/dist/mean.service.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { RoundingDecimalStrategy } from "./rounding-decimal.strategy";
|
|
2
2
|
import { Sum } from "./sum.service";
|
|
3
3
|
export const MeanError = { NotEnoughValues: "mean.not.enough.values" };
|
|
4
4
|
export class Mean {
|
|
5
|
-
static DEFAULT_ROUNDING = new
|
|
5
|
+
static DEFAULT_ROUNDING = new RoundingDecimalStrategy(2);
|
|
6
6
|
static calculate(values, rounding) {
|
|
7
7
|
if (values.length === 0)
|
|
8
8
|
throw new Error(MeanError.NotEnoughValues);
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { RoundingStrategy } from "./rounding.strategy";
|
|
2
2
|
type MinMaxScalerValueType = number;
|
|
3
3
|
type MinMaxScalerConfigType = {
|
|
4
4
|
min: MinMaxScalerValueType;
|
|
@@ -7,7 +7,7 @@ type MinMaxScalerConfigType = {
|
|
|
7
7
|
lower: MinMaxScalerValueType;
|
|
8
8
|
upper: MinMaxScalerValueType;
|
|
9
9
|
};
|
|
10
|
-
rounding?:
|
|
10
|
+
rounding?: RoundingStrategy;
|
|
11
11
|
};
|
|
12
12
|
type ScaleResult = {
|
|
13
13
|
original: MinMaxScalerValueType;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { RoundingDecimalStrategy } from "./rounding-decimal.strategy";
|
|
2
2
|
export const MinMaxScalerError = {
|
|
3
3
|
InvalidMinMax: "min.max.scaler.invalid.min.max",
|
|
4
4
|
InvalidBound: "min.max.scaler.invalid.bound",
|
|
@@ -7,7 +7,7 @@ export const MinMaxScalerError = {
|
|
|
7
7
|
EmptyArray: "min.max.scaler.empty.array",
|
|
8
8
|
};
|
|
9
9
|
export class MinMaxScaler {
|
|
10
|
-
static DEFAULT_ROUNDING = new
|
|
10
|
+
static DEFAULT_ROUNDING = new RoundingDecimalStrategy(2);
|
|
11
11
|
min;
|
|
12
12
|
max;
|
|
13
13
|
lower;
|
package/dist/money.vo.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { DivisionFactorType } from "./division-factor.vo";
|
|
2
2
|
import { type MoneyAmountType } from "./money-amount.vo";
|
|
3
3
|
import type { MultiplicationFactorType } from "./multiplication-factor.vo";
|
|
4
|
-
import type {
|
|
4
|
+
import type { RoundingStrategy } from "./rounding.strategy";
|
|
5
5
|
export declare const MoneyError: {
|
|
6
6
|
SubtractResultLessThanZero: string;
|
|
7
7
|
};
|
|
@@ -10,8 +10,8 @@ export declare class Money {
|
|
|
10
10
|
private readonly rounding;
|
|
11
11
|
private static readonly ZERO;
|
|
12
12
|
private constructor();
|
|
13
|
-
static fromAmount(candidate: number, rounding?:
|
|
14
|
-
static fromAmountSafe(candidate: MoneyAmountType, rounding?:
|
|
13
|
+
static fromAmount(candidate: number, rounding?: RoundingStrategy): Money;
|
|
14
|
+
static fromAmountSafe(candidate: MoneyAmountType, rounding?: RoundingStrategy): Money;
|
|
15
15
|
static zero(): Money;
|
|
16
16
|
getAmount(): MoneyAmountType;
|
|
17
17
|
add(money: Money): Money;
|
package/dist/money.vo.js
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import { MoneyAmount } from "./money-amount.vo";
|
|
2
|
-
import {
|
|
2
|
+
import { RoundingDownStrategy } from "./rounding-down.strategy";
|
|
3
|
+
import { RoundingToNearestStrategy } from "./rounding-to-nearest.strategy";
|
|
3
4
|
export const MoneyError = { SubtractResultLessThanZero: "money.subtract.result.less.than.zero" };
|
|
4
5
|
export class Money {
|
|
5
6
|
amount;
|
|
6
7
|
rounding;
|
|
7
8
|
static ZERO = MoneyAmount.parse(0);
|
|
8
|
-
constructor(amount, rounding = new
|
|
9
|
+
constructor(amount, rounding = new RoundingToNearestStrategy()) {
|
|
9
10
|
this.amount = amount;
|
|
10
11
|
this.rounding = rounding;
|
|
11
12
|
}
|
|
@@ -49,7 +50,7 @@ export class Money {
|
|
|
49
50
|
return this.amount === Money.ZERO;
|
|
50
51
|
}
|
|
51
52
|
format() {
|
|
52
|
-
const whole = new
|
|
53
|
+
const whole = new RoundingDownStrategy().round(this.amount / 100);
|
|
53
54
|
const fraction = this.amount % 100;
|
|
54
55
|
return `${whole}.${fraction.toString().padStart(2, "0")}`;
|
|
55
56
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Page } from "./pagination-page.vo";
|
|
2
2
|
import { Skip } from "./pagination-skip.vo";
|
|
3
3
|
import { Take } from "./pagination-take.vo";
|
|
4
|
-
import {
|
|
4
|
+
import { RoundingUpStrategy } from "./rounding-up.strategy";
|
|
5
5
|
export class Pagination {
|
|
6
6
|
static parse(values, _take) {
|
|
7
7
|
const page = Page.parse(values.page);
|
|
@@ -24,7 +24,7 @@ export class Pagination {
|
|
|
24
24
|
return Pagination.getLastPage(config) <= config.pagination.page;
|
|
25
25
|
}
|
|
26
26
|
static getLastPage(config) {
|
|
27
|
-
return Page.parse(new
|
|
27
|
+
return Page.parse(new RoundingUpStrategy().round(config.total / config.pagination.values.take));
|
|
28
28
|
}
|
|
29
29
|
static empty = {
|
|
30
30
|
result: [],
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { RoundingStrategy } from "./rounding.strategy";
|
|
2
2
|
export declare const PercentageError: {
|
|
3
3
|
InvalidDenominator: string;
|
|
4
4
|
};
|
|
5
5
|
export declare class Percentage {
|
|
6
|
-
static of(numerator: number, denominator: number, rounding?:
|
|
6
|
+
static of(numerator: number, denominator: number, rounding?: RoundingStrategy): number;
|
|
7
7
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { RoundingToNearestStrategy } from "./rounding-to-nearest.strategy";
|
|
2
2
|
export const PercentageError = { InvalidDenominator: "percentage.invalid.denominator" };
|
|
3
3
|
export class Percentage {
|
|
4
|
-
static of(numerator, denominator, rounding = new
|
|
4
|
+
static of(numerator, denominator, rounding = new RoundingToNearestStrategy()) {
|
|
5
5
|
if (denominator === 0)
|
|
6
6
|
throw new Error(PercentageError.InvalidDenominator);
|
|
7
7
|
if (numerator === 0)
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { RoundingStrategy } from "./rounding.strategy";
|
|
2
2
|
export declare const PopulationStandardDeviationError: {
|
|
3
3
|
NotEnoughValues: string;
|
|
4
4
|
};
|
|
5
5
|
export declare class PopulationStandardDeviation {
|
|
6
|
-
static calculate(values: number[], rounding?:
|
|
6
|
+
static calculate(values: number[], rounding?: RoundingStrategy): number;
|
|
7
7
|
}
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import { Mean } from "./mean.service";
|
|
2
|
-
import {
|
|
2
|
+
import { RoundingDecimalStrategy } from "./rounding-decimal.strategy";
|
|
3
3
|
import { Sum } from "./sum.service";
|
|
4
4
|
export const PopulationStandardDeviationError = {
|
|
5
5
|
NotEnoughValues: "population.standard.deviation.not.enough.values",
|
|
6
6
|
};
|
|
7
7
|
export class PopulationStandardDeviation {
|
|
8
|
-
static calculate(values, rounding = new
|
|
8
|
+
static calculate(values, rounding = new RoundingDecimalStrategy(2)) {
|
|
9
9
|
if (values.length < 2)
|
|
10
10
|
throw new Error(PopulationStandardDeviationError.NotEnoughValues);
|
|
11
11
|
const mean = Mean.calculate(values);
|
package/dist/random.service.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { RoundingDownStrategy } from "./rounding-down.strategy";
|
|
2
2
|
export const RandomError = { MinMax: "random.min.max" };
|
|
3
3
|
export class Random {
|
|
4
4
|
static DEFAULT_MIN = 0;
|
|
@@ -10,6 +10,6 @@ export class Random {
|
|
|
10
10
|
throw new Error(RandomError.MinMax);
|
|
11
11
|
if (min > max)
|
|
12
12
|
throw new Error(RandomError.MinMax);
|
|
13
|
-
return new
|
|
13
|
+
return new RoundingDownStrategy().round(Math.random() * (max - min + 1)) + min;
|
|
14
14
|
}
|
|
15
15
|
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { z } from "zod/v4";
|
|
2
|
+
import type { RoundingStrategy } from "./rounding.strategy";
|
|
3
|
+
export declare const RoundingDecimalError: {
|
|
4
|
+
Type: string;
|
|
5
|
+
Invalid: string;
|
|
6
|
+
};
|
|
7
|
+
export declare const RoundingDecimal: z.core.$ZodBranded<z.ZodNumber, "RoundingDecimal">;
|
|
8
|
+
export declare class RoundingDecimalStrategy implements RoundingStrategy {
|
|
9
|
+
private readonly decimals;
|
|
10
|
+
constructor(candidate: number);
|
|
11
|
+
round(value: number): number;
|
|
12
|
+
}
|
|
@@ -1,19 +1,4 @@
|
|
|
1
1
|
import { z } from "zod/v4";
|
|
2
|
-
export class RoundToNearest {
|
|
3
|
-
round(value) {
|
|
4
|
-
return Math.round(value);
|
|
5
|
-
}
|
|
6
|
-
}
|
|
7
|
-
export class RoundUp {
|
|
8
|
-
round(value) {
|
|
9
|
-
return Math.ceil(value);
|
|
10
|
-
}
|
|
11
|
-
}
|
|
12
|
-
export class RoundDown {
|
|
13
|
-
round(value) {
|
|
14
|
-
return Math.floor(value);
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
2
|
export const RoundingDecimalError = { Type: "rounding.decimal.type", Invalid: "rounding.decimal.invalid" };
|
|
18
3
|
export const RoundingDecimal = z
|
|
19
4
|
.number(RoundingDecimalError.Type)
|
|
@@ -21,7 +6,7 @@ export const RoundingDecimal = z
|
|
|
21
6
|
.min(1, RoundingDecimalError.Invalid)
|
|
22
7
|
.max(100, RoundingDecimalError.Invalid)
|
|
23
8
|
.brand("RoundingDecimal");
|
|
24
|
-
export class
|
|
9
|
+
export class RoundingDecimalStrategy {
|
|
25
10
|
decimals;
|
|
26
11
|
constructor(candidate) {
|
|
27
12
|
this.decimals = RoundingDecimal.parse(candidate);
|
package/dist/size.vo.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { RoundingDecimalStrategy } from "./rounding-decimal.strategy";
|
|
2
|
+
import { RoundingUpStrategy } from "./rounding-up.strategy";
|
|
2
3
|
import { SizeBytes } from "./size-bytes.vo";
|
|
3
4
|
var SizeUnitEnum;
|
|
4
5
|
(function (SizeUnitEnum) {
|
|
@@ -13,8 +14,8 @@ export class Size {
|
|
|
13
14
|
static KB_MULTIPLIER = 1024;
|
|
14
15
|
static MB_MULTIPLIER = 1024 * Size.KB_MULTIPLIER;
|
|
15
16
|
static GB_MULTIPLIER = 1024 * Size.MB_MULTIPLIER;
|
|
16
|
-
static CONVERT_ROUND = new
|
|
17
|
-
static FORMAT_ROUND = new
|
|
17
|
+
static CONVERT_ROUND = new RoundingUpStrategy();
|
|
18
|
+
static FORMAT_ROUND = new RoundingDecimalStrategy(2);
|
|
18
19
|
constructor(config) {
|
|
19
20
|
this.unit = config.unit;
|
|
20
21
|
this.bytes = this.calculateBytes(config.value, config.unit);
|
|
@@ -1,3 +1,8 @@
|
|
|
1
1
|
import { z } from "zod/v4";
|
|
2
|
-
export declare const
|
|
2
|
+
export declare const TimeZoneOffsetValueError: {
|
|
3
|
+
Type: string;
|
|
4
|
+
Min: string;
|
|
5
|
+
Max: string;
|
|
6
|
+
};
|
|
7
|
+
export declare const TimeZoneOffsetValue: z.core.$ZodBranded<z.ZodDefault<z.ZodCoercedNumber<unknown>>, "TimeZoneOffsetValue">;
|
|
3
8
|
export type TimeZoneOffsetValueType = z.infer<typeof TimeZoneOffsetValue>;
|
|
@@ -1,2 +1,15 @@
|
|
|
1
1
|
import { z } from "zod/v4";
|
|
2
|
-
export const
|
|
2
|
+
export const TimeZoneOffsetValueError = {
|
|
3
|
+
Type: "time.zone.offset.value.type",
|
|
4
|
+
Min: "time.zone.offset.value.min",
|
|
5
|
+
Max: "time.zone.offset.value.max",
|
|
6
|
+
};
|
|
7
|
+
export const TimeZoneOffsetValue = z.coerce
|
|
8
|
+
.number({ error: TimeZoneOffsetValueError.Type })
|
|
9
|
+
.int({ error: TimeZoneOffsetValueError.Type })
|
|
10
|
+
// UTC+14 (Kiribati)
|
|
11
|
+
.min(-840, { error: TimeZoneOffsetValueError.Min })
|
|
12
|
+
// UTC-12 (Baker Island)
|
|
13
|
+
.max(720, { error: TimeZoneOffsetValueError.Max })
|
|
14
|
+
.default(0) // Default to UTC if missing or invalid
|
|
15
|
+
.brand("TimeZoneOffsetValue");
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"root":["../src/age-years.vo.ts","../src/age.vo.ts","../src/api-key.vo.ts","../src/basename.vo.ts","../src/clock-format.service.ts","../src/clock.vo.ts","../src/date-calculator.service.ts","../src/date-formatter.service.ts","../src/date-range.vo.ts","../src/day-iso-id.vo.ts","../src/day.vo.ts","../src/directory-path-absolute.vo.ts","../src/directory-path-relative.vo.ts","../src/distance-value.vo.ts","../src/distance.vo.ts","../src/division-factor.vo.ts","../src/dll.service.ts","../src/duration-ms.vo.ts","../src/duration.service.ts","../src/email-mask.service.ts","../src/email.vo.ts","../src/etags.vo.ts","../src/extension.vo.ts","../src/feature-flag-value.vo.ts","../src/feature-flag.vo.ts","../src/file-path-absolute-schema.vo.ts","../src/file-path-relative-schema.vo.ts","../src/file-path.vo.ts","../src/filename-affix.vo.ts","../src/filename-from-string.vo.ts","../src/filename.vo.ts","../src/height-milimiters.vo.ts","../src/height.vo.ts","../src/hour-format.service.ts","../src/hour-schema.vo.ts","../src/hour.vo.ts","../src/iban-mask.service.ts","../src/iban-schema.vo.ts","../src/iban.vo.ts","../src/image.vo.ts","../src/index.ts","../src/language.vo.ts","../src/linear-regression.service.ts","../src/mean.service.ts","../src/mime-types.vo.ts","../src/mime-value.vo.ts","../src/mime.vo.ts","../src/min-max-scaler.service.ts","../src/minute-schema.vo.ts","../src/minute.vo.ts","../src/money-amount.vo.ts","../src/money.vo.ts","../src/month-iso-id.vo.ts","../src/month.vo.ts","../src/multiplication-factor.vo.ts","../src/noop.service.ts","../src/notification-template.vo.ts","../src/object-key.vo.ts","../src/outlier-detector.service.ts","../src/package-version-schema.vo.ts","../src/package-version.vo.ts","../src/pagination-page.vo.ts","../src/pagination-skip.vo.ts","../src/pagination-take.vo.ts","../src/pagination.service.ts","../src/percentage.service.ts","../src/population-standard-deviation.service.ts","../src/quarter-iso-id.vo.ts","../src/quarter.vo.ts","../src/random.service.ts","../src/rate-limiter.service.ts","../src/relative-date.vo.ts","../src/reordering-item-position-value.vo.ts","../src/reordering.service.ts","../src/revision-value.vo.ts","../src/revision.vo.ts","../src/rounding.
|
|
1
|
+
{"root":["../src/age-years.vo.ts","../src/age.vo.ts","../src/api-key.vo.ts","../src/basename.vo.ts","../src/clock-format.service.ts","../src/clock.vo.ts","../src/date-calculator.service.ts","../src/date-formatter.service.ts","../src/date-range.vo.ts","../src/day-iso-id.vo.ts","../src/day.vo.ts","../src/directory-path-absolute.vo.ts","../src/directory-path-relative.vo.ts","../src/distance-value.vo.ts","../src/distance.vo.ts","../src/division-factor.vo.ts","../src/dll.service.ts","../src/duration-ms.vo.ts","../src/duration.service.ts","../src/email-mask.service.ts","../src/email.vo.ts","../src/etags.vo.ts","../src/extension.vo.ts","../src/feature-flag-value.vo.ts","../src/feature-flag.vo.ts","../src/file-path-absolute-schema.vo.ts","../src/file-path-relative-schema.vo.ts","../src/file-path.vo.ts","../src/filename-affix.vo.ts","../src/filename-from-string.vo.ts","../src/filename.vo.ts","../src/height-milimiters.vo.ts","../src/height.vo.ts","../src/hour-format.service.ts","../src/hour-schema.vo.ts","../src/hour.vo.ts","../src/iban-mask.service.ts","../src/iban-schema.vo.ts","../src/iban.vo.ts","../src/image.vo.ts","../src/index.ts","../src/language.vo.ts","../src/linear-regression.service.ts","../src/mean.service.ts","../src/mime-types.vo.ts","../src/mime-value.vo.ts","../src/mime.vo.ts","../src/min-max-scaler.service.ts","../src/minute-schema.vo.ts","../src/minute.vo.ts","../src/money-amount.vo.ts","../src/money.vo.ts","../src/month-iso-id.vo.ts","../src/month.vo.ts","../src/multiplication-factor.vo.ts","../src/noop.service.ts","../src/notification-template.vo.ts","../src/object-key.vo.ts","../src/outlier-detector.service.ts","../src/package-version-schema.vo.ts","../src/package-version.vo.ts","../src/pagination-page.vo.ts","../src/pagination-skip.vo.ts","../src/pagination-take.vo.ts","../src/pagination.service.ts","../src/percentage.service.ts","../src/population-standard-deviation.service.ts","../src/quarter-iso-id.vo.ts","../src/quarter.vo.ts","../src/random.service.ts","../src/rate-limiter.service.ts","../src/relative-date.vo.ts","../src/reordering-item-position-value.vo.ts","../src/reordering.service.ts","../src/revision-value.vo.ts","../src/revision.vo.ts","../src/rounding-decimal.strategy.ts","../src/rounding-down.strategy.ts","../src/rounding-to-nearest.strategy.ts","../src/rounding-up.strategy.ts","../src/rounding.strategy.ts","../src/size-bytes.vo.ts","../src/size.vo.ts","../src/slug.service.ts","../src/sum.service.ts","../src/thousands-separator.service.ts","../src/time-zone-offset-value.vo.ts","../src/timestamp-value.vo.ts","../src/timestamp.vo.ts","../src/timezone.vo.ts","../src/ts-utils.ts","../src/url-with-slash.vo.ts","../src/url-without-slash.vo.ts","../src/visually-unambiguous-characters-generator.service.ts","../src/week-iso-id.vo.ts","../src/week.vo.ts","../src/weekday.vo.ts","../src/weight-grams.vo.ts","../src/weight.vo.ts","../src/year-iso-id.vo.ts","../src/year.vo.ts","../src/z-score.service.ts"],"version":"5.9.3"}
|
package/dist/weight.vo.d.ts
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import type { DivisionFactorType } from "./division-factor.vo";
|
|
2
2
|
import type { MultiplicationFactorType } from "./multiplication-factor.vo";
|
|
3
|
-
import type {
|
|
3
|
+
import type { RoundingStrategy } from "./rounding.strategy";
|
|
4
4
|
export declare class Weight {
|
|
5
5
|
private readonly grams;
|
|
6
6
|
private readonly rounding;
|
|
7
7
|
private static readonly GRAMS_PER_KILOGRAM;
|
|
8
8
|
private constructor();
|
|
9
|
-
static fromKilograms(kilograms: number, rounding?:
|
|
10
|
-
static fromGrams(grams: number, rounding?:
|
|
9
|
+
static fromKilograms(kilograms: number, rounding?: RoundingStrategy): Weight;
|
|
10
|
+
static fromGrams(grams: number, rounding?: RoundingStrategy): Weight;
|
|
11
11
|
static zero(): Weight;
|
|
12
12
|
get(): number;
|
|
13
13
|
toKilograms(): number;
|
package/dist/weight.vo.js
CHANGED
|
@@ -1,18 +1,18 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { RoundingToNearestStrategy } from "./rounding-to-nearest.strategy";
|
|
2
2
|
import { WeightGrams } from "./weight-grams.vo";
|
|
3
3
|
export class Weight {
|
|
4
4
|
grams;
|
|
5
5
|
rounding;
|
|
6
6
|
static GRAMS_PER_KILOGRAM = 1_000;
|
|
7
|
-
constructor(grams, rounding = new
|
|
7
|
+
constructor(grams, rounding = new RoundingToNearestStrategy()) {
|
|
8
8
|
this.grams = grams;
|
|
9
9
|
this.rounding = rounding;
|
|
10
10
|
}
|
|
11
|
-
static fromKilograms(kilograms, rounding = new
|
|
11
|
+
static fromKilograms(kilograms, rounding = new RoundingToNearestStrategy()) {
|
|
12
12
|
const grams = rounding.round(kilograms * Weight.GRAMS_PER_KILOGRAM);
|
|
13
13
|
return new Weight(WeightGrams.parse(grams), rounding);
|
|
14
14
|
}
|
|
15
|
-
static fromGrams(grams, rounding = new
|
|
15
|
+
static fromGrams(grams, rounding = new RoundingToNearestStrategy()) {
|
|
16
16
|
return new Weight(WeightGrams.parse(grams), rounding);
|
|
17
17
|
}
|
|
18
18
|
static zero() {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { RoundingStrategy } from "./rounding.strategy";
|
|
2
2
|
export declare const ZScoreError: {
|
|
3
3
|
NotEnoughValues: string;
|
|
4
4
|
};
|
|
@@ -6,6 +6,6 @@ export declare class ZScore {
|
|
|
6
6
|
private readonly rounding;
|
|
7
7
|
private readonly mean;
|
|
8
8
|
private readonly standardDeviation;
|
|
9
|
-
constructor(values: number[], rounding?:
|
|
9
|
+
constructor(values: number[], rounding?: RoundingStrategy);
|
|
10
10
|
calculate(value: number): number;
|
|
11
11
|
}
|
package/dist/z-score.service.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { Mean } from "./mean.service";
|
|
2
2
|
import { PopulationStandardDeviation } from "./population-standard-deviation.service";
|
|
3
|
-
import {
|
|
3
|
+
import { RoundingDecimalStrategy } from "./rounding-decimal.strategy";
|
|
4
4
|
export const ZScoreError = { NotEnoughValues: "z.score.not.enough.values" };
|
|
5
5
|
export class ZScore {
|
|
6
6
|
rounding;
|
|
7
7
|
mean;
|
|
8
8
|
standardDeviation;
|
|
9
|
-
constructor(values, rounding = new
|
|
9
|
+
constructor(values, rounding = new RoundingDecimalStrategy(2)) {
|
|
10
10
|
this.rounding = rounding;
|
|
11
11
|
if (values.length < 2)
|
|
12
12
|
throw new Error(ZScoreError.NotEnoughValues);
|
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -99,8 +99,11 @@ src/
|
|
|
99
99
|
├── reordering.service.ts
|
|
100
100
|
├── revision-value.vo.ts
|
|
101
101
|
├── revision.vo.ts
|
|
102
|
-
├── rounding.
|
|
103
|
-
├── rounding.
|
|
102
|
+
├── rounding-decimal.strategy.ts
|
|
103
|
+
├── rounding-down.strategy.ts
|
|
104
|
+
├── rounding-to-nearest.strategy.ts
|
|
105
|
+
├── rounding-up.strategy.ts
|
|
106
|
+
├── rounding.strategy.ts
|
|
104
107
|
├── size-bytes.vo.ts
|
|
105
108
|
├── size.vo.ts
|
|
106
109
|
├── slug.service.ts
|
package/src/distance.vo.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { DistanceValue, type DistanceValueType } from "./distance-value.vo";
|
|
2
|
-
import {
|
|
3
|
-
import
|
|
2
|
+
import type { RoundingStrategy } from "./rounding.strategy";
|
|
3
|
+
import { RoundingToNearestStrategy } from "./rounding-to-nearest.strategy";
|
|
4
4
|
|
|
5
5
|
export const DistanceError = { SubtractResultLessThanZero: "distance.subtract.result.less.than.zero" };
|
|
6
6
|
|
|
@@ -17,11 +17,17 @@ export class Distance {
|
|
|
17
17
|
return new Distance(candidate);
|
|
18
18
|
}
|
|
19
19
|
|
|
20
|
-
static fromKilometers(
|
|
20
|
+
static fromKilometers(
|
|
21
|
+
candidate: number,
|
|
22
|
+
rounding: RoundingStrategy = new RoundingToNearestStrategy(),
|
|
23
|
+
): Distance {
|
|
21
24
|
return new Distance(DistanceValue.parse(rounding.round(candidate * 1000)));
|
|
22
25
|
}
|
|
23
26
|
|
|
24
|
-
static fromMiles(
|
|
27
|
+
static fromMiles(
|
|
28
|
+
candidate: number,
|
|
29
|
+
rounding: RoundingStrategy = new RoundingToNearestStrategy(),
|
|
30
|
+
): Distance {
|
|
25
31
|
return new Distance(DistanceValue.parse(rounding.round(candidate * 1_609.344)));
|
|
26
32
|
}
|
|
27
33
|
|
package/src/duration.service.ts
CHANGED
|
@@ -1,12 +1,15 @@
|
|
|
1
1
|
import { DurationMs, type DurationMsType } from "./duration-ms.vo";
|
|
2
2
|
import type { MultiplicationFactorType } from "./multiplication-factor.vo";
|
|
3
|
-
import {
|
|
4
|
-
import
|
|
3
|
+
import type { RoundingStrategy } from "./rounding.strategy";
|
|
4
|
+
import { RoundingDecimalStrategy } from "./rounding-decimal.strategy";
|
|
5
|
+
import { RoundingToNearestStrategy } from "./rounding-to-nearest.strategy";
|
|
5
6
|
|
|
6
7
|
export class Duration {
|
|
7
|
-
private static readonly rounding:
|
|
8
|
+
private static readonly rounding: RoundingStrategy = new RoundingDecimalStrategy(2);
|
|
8
9
|
private readonly internal: DurationMsType;
|
|
9
10
|
|
|
11
|
+
static readonly MIN = Duration.Ms(1);
|
|
12
|
+
|
|
10
13
|
private static readonly MS_IN_SECOND = 1_000;
|
|
11
14
|
private static readonly MS_IN_MINUTE = 60 * Duration.MS_IN_SECOND;
|
|
12
15
|
private static readonly MS_IN_HOUR = 60 * Duration.MS_IN_MINUTE;
|
|
@@ -75,7 +78,7 @@ export class Duration {
|
|
|
75
78
|
}
|
|
76
79
|
|
|
77
80
|
times(factor: MultiplicationFactorType): Duration {
|
|
78
|
-
const rounding = new
|
|
81
|
+
const rounding = new RoundingToNearestStrategy();
|
|
79
82
|
|
|
80
83
|
return Duration.Ms(rounding.round(this.internal * factor));
|
|
81
84
|
}
|
package/src/height.vo.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { HeightMillimeters, type HeightMillimetersType } from "./height-milimiters.vo";
|
|
2
|
-
import {
|
|
3
|
-
import
|
|
2
|
+
import type { RoundingStrategy } from "./rounding.strategy";
|
|
3
|
+
import { RoundingDecimalStrategy } from "./rounding-decimal.strategy";
|
|
4
|
+
import { RoundingToNearestStrategy } from "./rounding-to-nearest.strategy";
|
|
4
5
|
|
|
5
6
|
export class Height {
|
|
6
7
|
private static readonly ZERO = HeightMillimeters.parse(0);
|
|
@@ -17,7 +18,10 @@ export class Height {
|
|
|
17
18
|
return new Height(millimeters);
|
|
18
19
|
}
|
|
19
20
|
|
|
20
|
-
static fromCentimeters(
|
|
21
|
+
static fromCentimeters(
|
|
22
|
+
centimeters: number,
|
|
23
|
+
rounding: RoundingStrategy = new RoundingToNearestStrategy(),
|
|
24
|
+
): Height {
|
|
21
25
|
const millimeters = rounding.round(centimeters * Height.MILLIMETERS_PER_CENTIMETER);
|
|
22
26
|
|
|
23
27
|
return new Height(HeightMillimeters.parse(millimeters));
|
|
@@ -35,22 +39,22 @@ export class Height {
|
|
|
35
39
|
return this.millimeters;
|
|
36
40
|
}
|
|
37
41
|
|
|
38
|
-
toCentimeters(rounding?:
|
|
42
|
+
toCentimeters(rounding?: RoundingStrategy): number {
|
|
39
43
|
const centimeters = this.millimeters / Height.MILLIMETERS_PER_CENTIMETER;
|
|
40
44
|
|
|
41
45
|
if (rounding) return rounding.round(centimeters);
|
|
42
46
|
return centimeters;
|
|
43
47
|
}
|
|
44
48
|
|
|
45
|
-
format(rounding?:
|
|
46
|
-
const chosen = rounding ?? new
|
|
49
|
+
format(rounding?: RoundingStrategy): string {
|
|
50
|
+
const chosen = rounding ?? new RoundingDecimalStrategy(1);
|
|
47
51
|
const value = this.toCentimeters(chosen);
|
|
48
52
|
|
|
49
53
|
return `${value} cm`;
|
|
50
54
|
}
|
|
51
55
|
|
|
52
56
|
toString(): string {
|
|
53
|
-
return this.format(new
|
|
57
|
+
return this.format(new RoundingDecimalStrategy(1));
|
|
54
58
|
}
|
|
55
59
|
|
|
56
60
|
equals(another: Height): boolean {
|
package/src/index.ts
CHANGED
|
@@ -74,8 +74,11 @@ export * from "./reordering.service";
|
|
|
74
74
|
export * from "./reordering-item-position-value.vo";
|
|
75
75
|
export * from "./revision.vo";
|
|
76
76
|
export * from "./revision-value.vo";
|
|
77
|
-
export * from "./rounding.
|
|
78
|
-
export * from "./rounding.
|
|
77
|
+
export * from "./rounding.strategy";
|
|
78
|
+
export * from "./rounding-decimal.strategy";
|
|
79
|
+
export * from "./rounding-down.strategy";
|
|
80
|
+
export * from "./rounding-to-nearest.strategy";
|
|
81
|
+
export * from "./rounding-up.strategy";
|
|
79
82
|
export * from "./size.vo";
|
|
80
83
|
export * from "./size-bytes.vo";
|
|
81
84
|
export * from "./slug.service";
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import
|
|
1
|
+
import type { RoundingStrategy } from "./rounding.strategy";
|
|
2
|
+
import { RoundingToNearestStrategy } from "./rounding-to-nearest.strategy";
|
|
3
3
|
|
|
4
4
|
export type LinearRegressionPairType = { x: number; y: number };
|
|
5
5
|
export type LinearRegressionParamsType = { a: number; b: number };
|
|
@@ -15,17 +15,17 @@ export const LinearRegressionError = {
|
|
|
15
15
|
};
|
|
16
16
|
|
|
17
17
|
export class LinearRegression {
|
|
18
|
-
private static readonly DEFAULT_ROUNDING:
|
|
18
|
+
private static readonly DEFAULT_ROUNDING: RoundingStrategy = new RoundingToNearestStrategy();
|
|
19
19
|
|
|
20
20
|
private readonly params: LinearRegressionParamsType;
|
|
21
|
-
private readonly rounding:
|
|
21
|
+
private readonly rounding: RoundingStrategy;
|
|
22
22
|
|
|
23
|
-
constructor(params: LinearRegressionParamsType, rounding?:
|
|
23
|
+
constructor(params: LinearRegressionParamsType, rounding?: RoundingStrategy) {
|
|
24
24
|
this.params = params;
|
|
25
25
|
this.rounding = rounding ?? LinearRegression.DEFAULT_ROUNDING;
|
|
26
26
|
}
|
|
27
27
|
|
|
28
|
-
static fromPairs(pairs: LinearRegressionPairType[], rounding?:
|
|
28
|
+
static fromPairs(pairs: LinearRegressionPairType[], rounding?: RoundingStrategy): LinearRegression {
|
|
29
29
|
const count = pairs.length;
|
|
30
30
|
|
|
31
31
|
if (count < 2) throw new Error(LinearRegressionError.MinPairs);
|
|
@@ -58,7 +58,7 @@ export class LinearRegression {
|
|
|
58
58
|
return new LinearRegression({ a, b }, rounding);
|
|
59
59
|
}
|
|
60
60
|
|
|
61
|
-
predict(x: LinearRegressionPairType["x"], rounding?:
|
|
61
|
+
predict(x: LinearRegressionPairType["x"], rounding?: RoundingStrategy): LinearRegressionPredictionType {
|
|
62
62
|
const chosen = rounding ?? this.rounding;
|
|
63
63
|
const prediction = this.params.b * x + this.params.a;
|
|
64
64
|
|
package/src/mean.service.ts
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import
|
|
1
|
+
import type { RoundingStrategy } from "./rounding.strategy";
|
|
2
|
+
import { RoundingDecimalStrategy } from "./rounding-decimal.strategy";
|
|
3
3
|
import { Sum } from "./sum.service";
|
|
4
4
|
|
|
5
5
|
export const MeanError = { NotEnoughValues: "mean.not.enough.values" };
|
|
6
6
|
|
|
7
7
|
export class Mean {
|
|
8
|
-
private static readonly DEFAULT_ROUNDING:
|
|
8
|
+
private static readonly DEFAULT_ROUNDING: RoundingStrategy = new RoundingDecimalStrategy(2);
|
|
9
9
|
|
|
10
|
-
static calculate(values: number[], rounding?:
|
|
10
|
+
static calculate(values: number[], rounding?: RoundingStrategy): number {
|
|
11
11
|
if (values.length === 0) throw new Error(MeanError.NotEnoughValues);
|
|
12
12
|
|
|
13
13
|
const sum = Sum.of(values);
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import
|
|
1
|
+
import type { RoundingStrategy } from "./rounding.strategy";
|
|
2
|
+
import { RoundingDecimalStrategy } from "./rounding-decimal.strategy";
|
|
3
3
|
|
|
4
4
|
type MinMaxScalerValueType = number;
|
|
5
5
|
|
|
@@ -7,7 +7,7 @@ type MinMaxScalerConfigType = {
|
|
|
7
7
|
min: MinMaxScalerValueType;
|
|
8
8
|
max: MinMaxScalerValueType;
|
|
9
9
|
bound?: { lower: MinMaxScalerValueType; upper: MinMaxScalerValueType };
|
|
10
|
-
rounding?:
|
|
10
|
+
rounding?: RoundingStrategy;
|
|
11
11
|
};
|
|
12
12
|
|
|
13
13
|
type ScaleResult = {
|
|
@@ -33,13 +33,13 @@ export const MinMaxScalerError = {
|
|
|
33
33
|
};
|
|
34
34
|
|
|
35
35
|
export class MinMaxScaler {
|
|
36
|
-
private static readonly DEFAULT_ROUNDING:
|
|
36
|
+
private static readonly DEFAULT_ROUNDING: RoundingStrategy = new RoundingDecimalStrategy(2);
|
|
37
37
|
|
|
38
38
|
private readonly min: MinMaxScalerValueType;
|
|
39
39
|
private readonly max: MinMaxScalerValueType;
|
|
40
40
|
private readonly lower: MinMaxScalerValueType;
|
|
41
41
|
private readonly upper: MinMaxScalerValueType;
|
|
42
|
-
private readonly rounding:
|
|
42
|
+
private readonly rounding: RoundingStrategy;
|
|
43
43
|
|
|
44
44
|
constructor(config: MinMaxScalerConfigType) {
|
|
45
45
|
const lower = config.bound?.lower ?? 0;
|
package/src/money.vo.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import type { DivisionFactorType } from "./division-factor.vo";
|
|
2
2
|
import { MoneyAmount, type MoneyAmountType } from "./money-amount.vo";
|
|
3
3
|
import type { MultiplicationFactorType } from "./multiplication-factor.vo";
|
|
4
|
-
import {
|
|
5
|
-
import
|
|
4
|
+
import type { RoundingStrategy } from "./rounding.strategy";
|
|
5
|
+
import { RoundingDownStrategy } from "./rounding-down.strategy";
|
|
6
|
+
import { RoundingToNearestStrategy } from "./rounding-to-nearest.strategy";
|
|
6
7
|
|
|
7
8
|
export const MoneyError = { SubtractResultLessThanZero: "money.subtract.result.less.than.zero" };
|
|
8
9
|
|
|
@@ -11,14 +12,14 @@ export class Money {
|
|
|
11
12
|
|
|
12
13
|
private constructor(
|
|
13
14
|
private readonly amount: MoneyAmountType,
|
|
14
|
-
private readonly rounding:
|
|
15
|
+
private readonly rounding: RoundingStrategy = new RoundingToNearestStrategy(),
|
|
15
16
|
) {}
|
|
16
17
|
|
|
17
|
-
static fromAmount(candidate: number, rounding?:
|
|
18
|
+
static fromAmount(candidate: number, rounding?: RoundingStrategy): Money {
|
|
18
19
|
return new Money(MoneyAmount.parse(candidate), rounding);
|
|
19
20
|
}
|
|
20
21
|
|
|
21
|
-
static fromAmountSafe(candidate: MoneyAmountType, rounding?:
|
|
22
|
+
static fromAmountSafe(candidate: MoneyAmountType, rounding?: RoundingStrategy): Money {
|
|
22
23
|
return new Money(candidate, rounding);
|
|
23
24
|
}
|
|
24
25
|
|
|
@@ -66,7 +67,7 @@ export class Money {
|
|
|
66
67
|
}
|
|
67
68
|
|
|
68
69
|
format(): string {
|
|
69
|
-
const whole = new
|
|
70
|
+
const whole = new RoundingDownStrategy().round(this.amount / 100);
|
|
70
71
|
const fraction = this.amount % 100;
|
|
71
72
|
|
|
72
73
|
return `${whole}.${fraction.toString().padStart(2, "0")}`;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Page, type PageType } from "./pagination-page.vo";
|
|
2
2
|
import { Skip, type SkipType } from "./pagination-skip.vo";
|
|
3
3
|
import { Take, type TakeType } from "./pagination-take.vo";
|
|
4
|
-
import {
|
|
4
|
+
import { RoundingUpStrategy } from "./rounding-up.strategy";
|
|
5
5
|
|
|
6
6
|
export type PaginationType = { values: { take: TakeType; skip: SkipType }; page: PageType };
|
|
7
7
|
export type PaginationValuesType = Record<string, unknown>;
|
|
@@ -40,7 +40,7 @@ export class Pagination {
|
|
|
40
40
|
}
|
|
41
41
|
|
|
42
42
|
private static getLastPage(config: PaginationExhaustedConfig): PageType {
|
|
43
|
-
return Page.parse(new
|
|
43
|
+
return Page.parse(new RoundingUpStrategy().round(config.total / config.pagination.values.take));
|
|
44
44
|
}
|
|
45
45
|
|
|
46
46
|
static empty = {
|
|
@@ -1,10 +1,14 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import
|
|
1
|
+
import type { RoundingStrategy } from "./rounding.strategy";
|
|
2
|
+
import { RoundingToNearestStrategy } from "./rounding-to-nearest.strategy";
|
|
3
3
|
|
|
4
4
|
export const PercentageError = { InvalidDenominator: "percentage.invalid.denominator" };
|
|
5
5
|
|
|
6
6
|
export class Percentage {
|
|
7
|
-
static of(
|
|
7
|
+
static of(
|
|
8
|
+
numerator: number,
|
|
9
|
+
denominator: number,
|
|
10
|
+
rounding: RoundingStrategy = new RoundingToNearestStrategy(),
|
|
11
|
+
): number {
|
|
8
12
|
if (denominator === 0) throw new Error(PercentageError.InvalidDenominator);
|
|
9
13
|
if (numerator === 0) return 0;
|
|
10
14
|
return rounding.round((numerator / denominator) * 100);
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Mean } from "./mean.service";
|
|
2
|
-
import {
|
|
3
|
-
import
|
|
2
|
+
import type { RoundingStrategy } from "./rounding.strategy";
|
|
3
|
+
import { RoundingDecimalStrategy } from "./rounding-decimal.strategy";
|
|
4
4
|
import { Sum } from "./sum.service";
|
|
5
5
|
|
|
6
6
|
export const PopulationStandardDeviationError = {
|
|
@@ -8,7 +8,7 @@ export const PopulationStandardDeviationError = {
|
|
|
8
8
|
};
|
|
9
9
|
|
|
10
10
|
export class PopulationStandardDeviation {
|
|
11
|
-
static calculate(values: number[], rounding:
|
|
11
|
+
static calculate(values: number[], rounding: RoundingStrategy = new RoundingDecimalStrategy(2)): number {
|
|
12
12
|
if (values.length < 2) throw new Error(PopulationStandardDeviationError.NotEnoughValues);
|
|
13
13
|
|
|
14
14
|
const mean = Mean.calculate(values);
|
package/src/random.service.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { RoundingDownStrategy } from "./rounding-down.strategy";
|
|
2
2
|
|
|
3
3
|
type RandomGenerateConfigType = { min: number; max: number };
|
|
4
4
|
|
|
@@ -15,6 +15,6 @@ export class Random {
|
|
|
15
15
|
if (min === max) throw new Error(RandomError.MinMax);
|
|
16
16
|
if (min > max) throw new Error(RandomError.MinMax);
|
|
17
17
|
|
|
18
|
-
return new
|
|
18
|
+
return new RoundingDownStrategy().round(Math.random() * (max - min + 1)) + min;
|
|
19
19
|
}
|
|
20
20
|
}
|
|
@@ -1,23 +1,5 @@
|
|
|
1
1
|
import { z } from "zod/v4";
|
|
2
|
-
import type {
|
|
3
|
-
|
|
4
|
-
export class RoundToNearest implements RoundingPort {
|
|
5
|
-
round(value: number): number {
|
|
6
|
-
return Math.round(value);
|
|
7
|
-
}
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
export class RoundUp implements RoundingPort {
|
|
11
|
-
round(value: number): number {
|
|
12
|
-
return Math.ceil(value);
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
export class RoundDown implements RoundingPort {
|
|
17
|
-
round(value: number): number {
|
|
18
|
-
return Math.floor(value);
|
|
19
|
-
}
|
|
20
|
-
}
|
|
2
|
+
import type { RoundingStrategy } from "./rounding.strategy";
|
|
21
3
|
|
|
22
4
|
export const RoundingDecimalError = { Type: "rounding.decimal.type", Invalid: "rounding.decimal.invalid" };
|
|
23
5
|
|
|
@@ -28,7 +10,7 @@ export const RoundingDecimal = z
|
|
|
28
10
|
.max(100, RoundingDecimalError.Invalid)
|
|
29
11
|
.brand("RoundingDecimal");
|
|
30
12
|
|
|
31
|
-
export class
|
|
13
|
+
export class RoundingDecimalStrategy implements RoundingStrategy {
|
|
32
14
|
private readonly decimals: number;
|
|
33
15
|
|
|
34
16
|
constructor(candidate: number) {
|
package/src/size.vo.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { RoundingDecimalStrategy } from "./rounding-decimal.strategy";
|
|
2
|
+
import { RoundingUpStrategy } from "./rounding-up.strategy";
|
|
2
3
|
import { SizeBytes, type SizeBytesType } from "./size-bytes.vo";
|
|
3
4
|
|
|
4
5
|
enum SizeUnitEnum {
|
|
@@ -18,8 +19,8 @@ export class Size {
|
|
|
18
19
|
private static readonly MB_MULTIPLIER = 1024 * Size.KB_MULTIPLIER;
|
|
19
20
|
private static readonly GB_MULTIPLIER = 1024 * Size.MB_MULTIPLIER;
|
|
20
21
|
|
|
21
|
-
private static readonly CONVERT_ROUND = new
|
|
22
|
-
private static readonly FORMAT_ROUND = new
|
|
22
|
+
private static readonly CONVERT_ROUND = new RoundingUpStrategy();
|
|
23
|
+
private static readonly FORMAT_ROUND = new RoundingDecimalStrategy(2);
|
|
23
24
|
|
|
24
25
|
private constructor(config: SizeConfigType) {
|
|
25
26
|
this.unit = config.unit;
|
|
@@ -1,5 +1,19 @@
|
|
|
1
1
|
import { z } from "zod/v4";
|
|
2
2
|
|
|
3
|
-
export const
|
|
3
|
+
export const TimeZoneOffsetValueError = {
|
|
4
|
+
Type: "time.zone.offset.value.type",
|
|
5
|
+
Min: "time.zone.offset.value.min",
|
|
6
|
+
Max: "time.zone.offset.value.max",
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
export const TimeZoneOffsetValue = z.coerce
|
|
10
|
+
.number({ error: TimeZoneOffsetValueError.Type })
|
|
11
|
+
.int({ error: TimeZoneOffsetValueError.Type })
|
|
12
|
+
// UTC+14 (Kiribati)
|
|
13
|
+
.min(-840, { error: TimeZoneOffsetValueError.Min })
|
|
14
|
+
// UTC-12 (Baker Island)
|
|
15
|
+
.max(720, { error: TimeZoneOffsetValueError.Max })
|
|
16
|
+
.default(0) // Default to UTC if missing or invalid
|
|
17
|
+
.brand("TimeZoneOffsetValue");
|
|
4
18
|
|
|
5
19
|
export type TimeZoneOffsetValueType = z.infer<typeof TimeZoneOffsetValue>;
|
package/src/weight.vo.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { DivisionFactorType } from "./division-factor.vo";
|
|
2
2
|
import type { MultiplicationFactorType } from "./multiplication-factor.vo";
|
|
3
|
-
import {
|
|
4
|
-
import
|
|
3
|
+
import type { RoundingStrategy } from "./rounding.strategy";
|
|
4
|
+
import { RoundingToNearestStrategy } from "./rounding-to-nearest.strategy";
|
|
5
5
|
import { WeightGrams } from "./weight-grams.vo";
|
|
6
6
|
|
|
7
7
|
export class Weight {
|
|
@@ -9,16 +9,19 @@ export class Weight {
|
|
|
9
9
|
|
|
10
10
|
private constructor(
|
|
11
11
|
private readonly grams: number,
|
|
12
|
-
private readonly rounding:
|
|
12
|
+
private readonly rounding: RoundingStrategy = new RoundingToNearestStrategy(),
|
|
13
13
|
) {}
|
|
14
14
|
|
|
15
|
-
static fromKilograms(
|
|
15
|
+
static fromKilograms(
|
|
16
|
+
kilograms: number,
|
|
17
|
+
rounding: RoundingStrategy = new RoundingToNearestStrategy(),
|
|
18
|
+
): Weight {
|
|
16
19
|
const grams = rounding.round(kilograms * Weight.GRAMS_PER_KILOGRAM);
|
|
17
20
|
|
|
18
21
|
return new Weight(WeightGrams.parse(grams), rounding);
|
|
19
22
|
}
|
|
20
23
|
|
|
21
|
-
static fromGrams(grams: number, rounding:
|
|
24
|
+
static fromGrams(grams: number, rounding: RoundingStrategy = new RoundingToNearestStrategy()): Weight {
|
|
22
25
|
return new Weight(WeightGrams.parse(grams), rounding);
|
|
23
26
|
}
|
|
24
27
|
|
package/src/z-score.service.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Mean } from "./mean.service";
|
|
2
2
|
import { PopulationStandardDeviation } from "./population-standard-deviation.service";
|
|
3
|
-
import {
|
|
4
|
-
import
|
|
3
|
+
import type { RoundingStrategy } from "./rounding.strategy";
|
|
4
|
+
import { RoundingDecimalStrategy } from "./rounding-decimal.strategy";
|
|
5
5
|
|
|
6
6
|
export const ZScoreError = { NotEnoughValues: "z.score.not.enough.values" };
|
|
7
7
|
|
|
@@ -11,7 +11,7 @@ export class ZScore {
|
|
|
11
11
|
|
|
12
12
|
constructor(
|
|
13
13
|
values: number[],
|
|
14
|
-
private readonly rounding:
|
|
14
|
+
private readonly rounding: RoundingStrategy = new RoundingDecimalStrategy(2),
|
|
15
15
|
) {
|
|
16
16
|
if (values.length < 2) throw new Error(ZScoreError.NotEnoughValues);
|
|
17
17
|
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import { z } from "zod/v4";
|
|
2
|
-
import type { RoundingPort } from "./rounding.port";
|
|
3
|
-
export declare class RoundToNearest implements RoundingPort {
|
|
4
|
-
round(value: number): number;
|
|
5
|
-
}
|
|
6
|
-
export declare class RoundUp implements RoundingPort {
|
|
7
|
-
round(value: number): number;
|
|
8
|
-
}
|
|
9
|
-
export declare class RoundDown implements RoundingPort {
|
|
10
|
-
round(value: number): number;
|
|
11
|
-
}
|
|
12
|
-
export declare const RoundingDecimalError: {
|
|
13
|
-
Type: string;
|
|
14
|
-
Invalid: string;
|
|
15
|
-
};
|
|
16
|
-
export declare const RoundingDecimal: z.core.$ZodBranded<z.ZodNumber, "RoundingDecimal">;
|
|
17
|
-
export declare class RoundToDecimal implements RoundingPort {
|
|
18
|
-
private readonly decimals;
|
|
19
|
-
constructor(candidate: number);
|
|
20
|
-
round(value: number): number;
|
|
21
|
-
}
|
package/src/rounding.port.ts
DELETED
|
File without changes
|