@lucaapp/service-utils 1.29.0 → 1.30.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/lib/money/index.d.ts +1 -0
- package/dist/lib/money/index.js +17 -0
- package/dist/lib/money/money.d.ts +29 -0
- package/dist/lib/money/money.js +89 -0
- package/package.json +1 -1
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './money';
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./money"), exports);
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
interface Money {
|
|
2
|
+
getAmount(): number;
|
|
3
|
+
add(addend?: Money): Money;
|
|
4
|
+
subtract(subtrahend?: Money): Money;
|
|
5
|
+
multiply(multiplier: number): Money;
|
|
6
|
+
divide(divisor: number): Money;
|
|
7
|
+
equalsTo(comparator: Money): boolean;
|
|
8
|
+
lessThan(comparator: Money): boolean;
|
|
9
|
+
lessThanOrEqual(comparator: Money): boolean;
|
|
10
|
+
greaterThan(comparator: Money): boolean;
|
|
11
|
+
greaterThanOrEqual(comparator: Money): boolean;
|
|
12
|
+
isZero(): boolean;
|
|
13
|
+
isPositive(): boolean;
|
|
14
|
+
isNegative(): boolean;
|
|
15
|
+
}
|
|
16
|
+
declare class Money implements Money {
|
|
17
|
+
private readonly value;
|
|
18
|
+
static fromAmount(value: number): Money;
|
|
19
|
+
static fromFloat(value: number): Money;
|
|
20
|
+
static zero(): Money;
|
|
21
|
+
private constructor();
|
|
22
|
+
clone(): Money;
|
|
23
|
+
getEuroAmount(): number;
|
|
24
|
+
getEuroString(): string;
|
|
25
|
+
percentage(percentage: number): Money;
|
|
26
|
+
min(comparator: Money): Money;
|
|
27
|
+
max(comparator: Money): Money;
|
|
28
|
+
}
|
|
29
|
+
export { Money };
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Money = void 0;
|
|
4
|
+
const CENTS_PER_EURO = 100;
|
|
5
|
+
const EPSILON = 0.00000000001;
|
|
6
|
+
const roundHalfUp = (value) => {
|
|
7
|
+
const rounded = Math.sign(value) * Math.round(Math.abs(value) + EPSILON);
|
|
8
|
+
return rounded === 0 ? 0 : rounded;
|
|
9
|
+
};
|
|
10
|
+
const euroToAmount = (amountInEuros) => roundHalfUp(amountInEuros * CENTS_PER_EURO);
|
|
11
|
+
const amountToEuro = (value) => value / CENTS_PER_EURO;
|
|
12
|
+
class Money {
|
|
13
|
+
static fromAmount(value) {
|
|
14
|
+
if (!Number.isInteger(value)) {
|
|
15
|
+
throw new TypeError('Integer required.');
|
|
16
|
+
}
|
|
17
|
+
return new Money(value);
|
|
18
|
+
}
|
|
19
|
+
static fromFloat(value) {
|
|
20
|
+
return new Money(euroToAmount(value));
|
|
21
|
+
}
|
|
22
|
+
static zero() {
|
|
23
|
+
return Money.fromAmount(0);
|
|
24
|
+
}
|
|
25
|
+
constructor(amount) {
|
|
26
|
+
this.value = amount;
|
|
27
|
+
}
|
|
28
|
+
clone() {
|
|
29
|
+
return Money.fromAmount(this.value);
|
|
30
|
+
}
|
|
31
|
+
getAmount() {
|
|
32
|
+
return this.value;
|
|
33
|
+
}
|
|
34
|
+
getEuroAmount() {
|
|
35
|
+
return amountToEuro(this.value);
|
|
36
|
+
}
|
|
37
|
+
getEuroString() {
|
|
38
|
+
return this.getEuroAmount().toFixed(2);
|
|
39
|
+
}
|
|
40
|
+
add(addend) {
|
|
41
|
+
return Money.fromAmount(this.value + (addend?.value || 0));
|
|
42
|
+
}
|
|
43
|
+
subtract(subtrahend) {
|
|
44
|
+
return Money.fromAmount(this.value - (subtrahend?.value || 0));
|
|
45
|
+
}
|
|
46
|
+
multiply(multiplier) {
|
|
47
|
+
return Money.fromAmount(roundHalfUp(this.value * multiplier));
|
|
48
|
+
}
|
|
49
|
+
divide(divisor) {
|
|
50
|
+
return Money.fromAmount(roundHalfUp(this.value / divisor));
|
|
51
|
+
}
|
|
52
|
+
percentage(percentage) {
|
|
53
|
+
if (Number.isNaN(percentage) || percentage < 0 || percentage > 100) {
|
|
54
|
+
throw new TypeError('Expected number between 0 and 100');
|
|
55
|
+
}
|
|
56
|
+
return Money.fromAmount(roundHalfUp((this.value / 100) * percentage));
|
|
57
|
+
}
|
|
58
|
+
equalsTo(comparator) {
|
|
59
|
+
return this.value === comparator.value;
|
|
60
|
+
}
|
|
61
|
+
lessThan(comparator) {
|
|
62
|
+
return this.value < comparator.value;
|
|
63
|
+
}
|
|
64
|
+
lessThanOrEqual(comparator) {
|
|
65
|
+
return this.value <= comparator.value;
|
|
66
|
+
}
|
|
67
|
+
greaterThan(comparator) {
|
|
68
|
+
return this.value > comparator.value;
|
|
69
|
+
}
|
|
70
|
+
greaterThanOrEqual(comparator) {
|
|
71
|
+
return this.value >= comparator.value;
|
|
72
|
+
}
|
|
73
|
+
isZero() {
|
|
74
|
+
return this.value === 0;
|
|
75
|
+
}
|
|
76
|
+
isPositive() {
|
|
77
|
+
return this.value > 0;
|
|
78
|
+
}
|
|
79
|
+
isNegative() {
|
|
80
|
+
return this.value < 0;
|
|
81
|
+
}
|
|
82
|
+
min(comparator) {
|
|
83
|
+
return this.value < comparator.value ? this.clone() : comparator.clone();
|
|
84
|
+
}
|
|
85
|
+
max(comparator) {
|
|
86
|
+
return this.value > comparator.value ? this.clone() : comparator.clone();
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
exports.Money = Money;
|