@utiliread/primitives 1.0.0-70 → 1.0.0-72
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/cjs/aggregate-id.js +2 -2
- package/dist/cjs/consumption-type-id.js +2 -2
- package/dist/cjs/index.js +21 -21
- package/dist/cjs/measure-id.js +2 -2
- package/dist/cjs/number-formatter.js +81 -81
- package/dist/cjs/number-formatter.spec.js +97 -97
- package/dist/cjs/optional.js +2 -2
- package/dist/cjs/precision-number.js +35 -35
- package/dist/cjs/precision-number.spec.js +26 -26
- package/dist/cjs/register-convention.js +55 -55
- package/dist/cjs/register-convention.spec.js +38 -38
- package/dist/cjs/register-definition.js +2 -2
- package/dist/cjs/register-id.js +2 -2
- package/dist/cjs/register-library.js +50 -50
- package/dist/cjs/register-library.spec.js +36 -36
- package/dist/cjs/register-pack.js +2 -2
- package/dist/cjs/register-utils.js +7 -7
- package/dist/cjs/repository.js +25 -25
- package/dist/cjs/repository.js.map +1 -1
- package/dist/cjs/repository.spec.js +20 -20
- package/dist/cjs/step.js +40 -40
- package/dist/cjs/step.js.map +1 -1
- package/dist/cjs/step.spec.js +28 -28
- package/dist/cjs/time-or-rule.js +18 -18
- package/dist/cjs/time-or-rule.js.map +1 -1
- package/dist/cjs/unit-id.js +13 -13
- package/dist/esm/aggregate-id.d.ts +1 -1
- package/dist/esm/aggregate-id.js +1 -1
- package/dist/esm/consumption-type-id.d.ts +1 -1
- package/dist/esm/consumption-type-id.js +1 -1
- package/dist/esm/index.d.ts +16 -16
- package/dist/esm/index.js +9 -9
- package/dist/esm/measure-id.d.ts +1 -1
- package/dist/esm/measure-id.js +1 -1
- package/dist/esm/number-formatter.d.ts +17 -17
- package/dist/esm/number-formatter.js +78 -78
- package/dist/esm/number-formatter.spec.d.ts +1 -1
- package/dist/esm/number-formatter.spec.js +95 -95
- package/dist/esm/optional.d.ts +1 -1
- package/dist/esm/optional.js +1 -1
- package/dist/esm/precision-number.d.ts +11 -11
- package/dist/esm/precision-number.js +31 -31
- package/dist/esm/precision-number.spec.d.ts +1 -1
- package/dist/esm/precision-number.spec.js +24 -24
- package/dist/esm/register-convention.d.ts +14 -14
- package/dist/esm/register-convention.js +51 -51
- package/dist/esm/register-convention.spec.d.ts +1 -1
- package/dist/esm/register-convention.spec.js +36 -36
- package/dist/esm/register-definition.d.ts +13 -13
- package/dist/esm/register-definition.js +1 -1
- package/dist/esm/register-id.d.ts +1 -1
- package/dist/esm/register-id.js +1 -1
- package/dist/esm/register-library.d.ts +15 -15
- package/dist/esm/register-library.js +47 -47
- package/dist/esm/register-library.spec.d.ts +1 -1
- package/dist/esm/register-library.spec.js +34 -34
- package/dist/esm/register-pack.d.ts +4 -4
- package/dist/esm/register-pack.js +1 -1
- package/dist/esm/register-utils.d.ts +3 -3
- package/dist/esm/register-utils.js +3 -3
- package/dist/esm/repository.d.ts +9 -9
- package/dist/esm/repository.js +21 -21
- package/dist/esm/repository.spec.d.ts +1 -1
- package/dist/esm/repository.spec.js +18 -18
- package/dist/esm/step.d.ts +15 -15
- package/dist/esm/step.js +37 -37
- package/dist/esm/step.spec.d.ts +1 -1
- package/dist/esm/step.spec.js +26 -26
- package/dist/esm/time-or-rule.d.ts +7 -7
- package/dist/esm/time-or-rule.js +15 -15
- package/dist/esm/unit-id.d.ts +2 -2
- package/dist/esm/unit-id.js +10 -10
- package/locales/da/registers.json +2 -1
- package/locales/en/registers.json +2 -1
- package/package.json +3 -3
- package/src/register-id.ts +2 -1
package/dist/cjs/aggregate-id.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
//# sourceMappingURL=aggregate-id.js.map
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
//# sourceMappingURL=consumption-type-id.js.map
|
package/dist/cjs/index.js
CHANGED
|
@@ -1,22 +1,22 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.localeDecimalSeparator = exports.invariantDecimalSeparator = exports.TimeOrRule = exports.Step = exports.Repository = exports.RegisterLibrary = exports.RegisterConvention = exports.PrecisionNumber = exports.NumberFormatter = exports.DayOfWeek = void 0;
|
|
4
|
-
const register_library_1 = require("./register-library");
|
|
5
|
-
Object.defineProperty(exports, "RegisterLibrary", { enumerable: true, get: function () { return register_library_1.RegisterLibrary; } });
|
|
6
|
-
const number_formatter_1 = require("./number-formatter");
|
|
7
|
-
Object.defineProperty(exports, "NumberFormatter", { enumerable: true, get: function () { return number_formatter_1.NumberFormatter; } });
|
|
8
|
-
Object.defineProperty(exports, "invariantDecimalSeparator", { enumerable: true, get: function () { return number_formatter_1.invariantDecimalSeparator; } });
|
|
9
|
-
Object.defineProperty(exports, "localeDecimalSeparator", { enumerable: true, get: function () { return number_formatter_1.localeDecimalSeparator; } });
|
|
10
|
-
const register_convention_1 = require("./register-convention");
|
|
11
|
-
Object.defineProperty(exports, "RegisterConvention", { enumerable: true, get: function () { return register_convention_1.RegisterConvention; } });
|
|
12
|
-
const step_1 = require("./step");
|
|
13
|
-
Object.defineProperty(exports, "Step", { enumerable: true, get: function () { return step_1.Step; } });
|
|
14
|
-
Object.defineProperty(exports, "DayOfWeek", { enumerable: true, get: function () { return step_1.DayOfWeek; } });
|
|
15
|
-
const time_or_rule_1 = require("./time-or-rule");
|
|
16
|
-
Object.defineProperty(exports, "TimeOrRule", { enumerable: true, get: function () { return time_or_rule_1.TimeOrRule; } });
|
|
17
|
-
const precision_number_1 = require("./precision-number");
|
|
18
|
-
Object.defineProperty(exports, "PrecisionNumber", { enumerable: true, get: function () { return precision_number_1.PrecisionNumber; } });
|
|
19
|
-
require("./repository"); // We need this to get the Repository namespace exported
|
|
20
|
-
const repository_1 = require("./repository");
|
|
21
|
-
Object.defineProperty(exports, "Repository", { enumerable: true, get: function () { return repository_1.Repository; } });
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.localeDecimalSeparator = exports.invariantDecimalSeparator = exports.TimeOrRule = exports.Step = exports.Repository = exports.RegisterLibrary = exports.RegisterConvention = exports.PrecisionNumber = exports.NumberFormatter = exports.DayOfWeek = void 0;
|
|
4
|
+
const register_library_1 = require("./register-library");
|
|
5
|
+
Object.defineProperty(exports, "RegisterLibrary", { enumerable: true, get: function () { return register_library_1.RegisterLibrary; } });
|
|
6
|
+
const number_formatter_1 = require("./number-formatter");
|
|
7
|
+
Object.defineProperty(exports, "NumberFormatter", { enumerable: true, get: function () { return number_formatter_1.NumberFormatter; } });
|
|
8
|
+
Object.defineProperty(exports, "invariantDecimalSeparator", { enumerable: true, get: function () { return number_formatter_1.invariantDecimalSeparator; } });
|
|
9
|
+
Object.defineProperty(exports, "localeDecimalSeparator", { enumerable: true, get: function () { return number_formatter_1.localeDecimalSeparator; } });
|
|
10
|
+
const register_convention_1 = require("./register-convention");
|
|
11
|
+
Object.defineProperty(exports, "RegisterConvention", { enumerable: true, get: function () { return register_convention_1.RegisterConvention; } });
|
|
12
|
+
const step_1 = require("./step");
|
|
13
|
+
Object.defineProperty(exports, "Step", { enumerable: true, get: function () { return step_1.Step; } });
|
|
14
|
+
Object.defineProperty(exports, "DayOfWeek", { enumerable: true, get: function () { return step_1.DayOfWeek; } });
|
|
15
|
+
const time_or_rule_1 = require("./time-or-rule");
|
|
16
|
+
Object.defineProperty(exports, "TimeOrRule", { enumerable: true, get: function () { return time_or_rule_1.TimeOrRule; } });
|
|
17
|
+
const precision_number_1 = require("./precision-number");
|
|
18
|
+
Object.defineProperty(exports, "PrecisionNumber", { enumerable: true, get: function () { return precision_number_1.PrecisionNumber; } });
|
|
19
|
+
require("./repository"); // We need this to get the Repository namespace exported
|
|
20
|
+
const repository_1 = require("./repository");
|
|
21
|
+
Object.defineProperty(exports, "Repository", { enumerable: true, get: function () { return repository_1.Repository; } });
|
|
22
22
|
//# sourceMappingURL=index.js.map
|
package/dist/cjs/measure-id.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
//# sourceMappingURL=measure-id.js.map
|
|
@@ -1,82 +1,82 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.NumberFormatter = exports.localeDecimalSeparator = exports.invariantDecimalSeparator = void 0;
|
|
4
|
-
exports.invariantDecimalSeparator = (0.1).toString()[1];
|
|
5
|
-
exports.localeDecimalSeparator = (0.1).toLocaleString()[1];
|
|
6
|
-
class Formatter {
|
|
7
|
-
toPrecision(value, precision, maxDecimals) {
|
|
8
|
-
const absValue = Math.abs(value);
|
|
9
|
-
const noOfIntegerDigits = Math.round(absValue).toString().length;
|
|
10
|
-
// Adjust precision to maxDecimals
|
|
11
|
-
if (maxDecimals !== undefined) {
|
|
12
|
-
precision = Math.min(precision, noOfIntegerDigits + maxDecimals);
|
|
13
|
-
}
|
|
14
|
-
if (value === 0) {
|
|
15
|
-
// (0).toPrecision(1) == "0"
|
|
16
|
-
// (0).toPrecision(2) == "0.0"
|
|
17
|
-
// (0).toPrecision(3) == "0.00"
|
|
18
|
-
// However, we want:
|
|
19
|
-
// Formatter.toPrecision(0, 0) == "0"
|
|
20
|
-
// Formatter.toPrecision(0, 1) == "0.0"
|
|
21
|
-
// Formatter.toPrecision(0, 2) == "0.00"
|
|
22
|
-
return value.toPrecision(precision + 1);
|
|
23
|
-
}
|
|
24
|
-
if (absValue < 1) {
|
|
25
|
-
return value.toPrecision(precision);
|
|
26
|
-
}
|
|
27
|
-
return precision >= noOfIntegerDigits
|
|
28
|
-
? value.toFixed(precision - noOfIntegerDigits)
|
|
29
|
-
: value.toPrecision(precision);
|
|
30
|
-
}
|
|
31
|
-
toLocalePrecision(value, precision, maxDecimals) {
|
|
32
|
-
return this.toPrecision(value, precision, maxDecimals)
|
|
33
|
-
.replace(exports.invariantDecimalSeparator, exports.localeDecimalSeparator);
|
|
34
|
-
}
|
|
35
|
-
toFixed(value, precision, maxDecimals) {
|
|
36
|
-
const parts = this.getParts(value, precision, maxDecimals);
|
|
37
|
-
if (parts.noOfCoefficientDecimals < parts.exponent) {
|
|
38
|
-
const number = parts.invariantCoefficientString.replace(exports.invariantDecimalSeparator, "");
|
|
39
|
-
const zeros = parts.exponent - parts.noOfCoefficientDecimals;
|
|
40
|
-
return `${number}${"0".repeat(zeros)}`;
|
|
41
|
-
}
|
|
42
|
-
else {
|
|
43
|
-
return parts.invariantPrecisionString;
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
toLocaleFixed(value, precision, maxDecimals) {
|
|
47
|
-
return this.toFixed(value, precision, maxDecimals)
|
|
48
|
-
.replace(exports.invariantDecimalSeparator, exports.localeDecimalSeparator);
|
|
49
|
-
}
|
|
50
|
-
getParts(value, precision, maxDecimals) {
|
|
51
|
-
const precisionString = this.toPrecision(value, precision, maxDecimals);
|
|
52
|
-
if (precisionString.indexOf("e") >= 0) {
|
|
53
|
-
const match = /(.*)e([\+-]\d+)$/.exec(precisionString);
|
|
54
|
-
const coefficientString = match[1];
|
|
55
|
-
const exponent = parseInt(match[2]);
|
|
56
|
-
const decimalSeparatorIndex = coefficientString.indexOf(exports.invariantDecimalSeparator);
|
|
57
|
-
const noOfCoefficientDecimals = decimalSeparatorIndex >= 0
|
|
58
|
-
? coefficientString.length - decimalSeparatorIndex - 1
|
|
59
|
-
: 0;
|
|
60
|
-
return {
|
|
61
|
-
invariantPrecisionString: precisionString,
|
|
62
|
-
invariantCoefficientString: coefficientString,
|
|
63
|
-
exponent,
|
|
64
|
-
noOfCoefficientDecimals,
|
|
65
|
-
};
|
|
66
|
-
}
|
|
67
|
-
else {
|
|
68
|
-
const decimalSeparatorIndex = precisionString.indexOf(exports.invariantDecimalSeparator);
|
|
69
|
-
const noOfCoefficientDecimals = decimalSeparatorIndex >= 0
|
|
70
|
-
? precisionString.length - decimalSeparatorIndex - 1
|
|
71
|
-
: 0;
|
|
72
|
-
return {
|
|
73
|
-
invariantPrecisionString: precisionString,
|
|
74
|
-
invariantCoefficientString: precisionString,
|
|
75
|
-
exponent: 0,
|
|
76
|
-
noOfCoefficientDecimals,
|
|
77
|
-
};
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
exports.NumberFormatter = new Formatter();
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.NumberFormatter = exports.localeDecimalSeparator = exports.invariantDecimalSeparator = void 0;
|
|
4
|
+
exports.invariantDecimalSeparator = (0.1).toString()[1];
|
|
5
|
+
exports.localeDecimalSeparator = (0.1).toLocaleString()[1];
|
|
6
|
+
class Formatter {
|
|
7
|
+
toPrecision(value, precision, maxDecimals) {
|
|
8
|
+
const absValue = Math.abs(value);
|
|
9
|
+
const noOfIntegerDigits = Math.round(absValue).toString().length;
|
|
10
|
+
// Adjust precision to maxDecimals
|
|
11
|
+
if (maxDecimals !== undefined) {
|
|
12
|
+
precision = Math.min(precision, noOfIntegerDigits + maxDecimals);
|
|
13
|
+
}
|
|
14
|
+
if (value === 0) {
|
|
15
|
+
// (0).toPrecision(1) == "0"
|
|
16
|
+
// (0).toPrecision(2) == "0.0"
|
|
17
|
+
// (0).toPrecision(3) == "0.00"
|
|
18
|
+
// However, we want:
|
|
19
|
+
// Formatter.toPrecision(0, 0) == "0"
|
|
20
|
+
// Formatter.toPrecision(0, 1) == "0.0"
|
|
21
|
+
// Formatter.toPrecision(0, 2) == "0.00"
|
|
22
|
+
return value.toPrecision(precision + 1);
|
|
23
|
+
}
|
|
24
|
+
if (absValue < 1) {
|
|
25
|
+
return value.toPrecision(precision);
|
|
26
|
+
}
|
|
27
|
+
return precision >= noOfIntegerDigits
|
|
28
|
+
? value.toFixed(precision - noOfIntegerDigits)
|
|
29
|
+
: value.toPrecision(precision);
|
|
30
|
+
}
|
|
31
|
+
toLocalePrecision(value, precision, maxDecimals) {
|
|
32
|
+
return this.toPrecision(value, precision, maxDecimals)
|
|
33
|
+
.replace(exports.invariantDecimalSeparator, exports.localeDecimalSeparator);
|
|
34
|
+
}
|
|
35
|
+
toFixed(value, precision, maxDecimals) {
|
|
36
|
+
const parts = this.getParts(value, precision, maxDecimals);
|
|
37
|
+
if (parts.noOfCoefficientDecimals < parts.exponent) {
|
|
38
|
+
const number = parts.invariantCoefficientString.replace(exports.invariantDecimalSeparator, "");
|
|
39
|
+
const zeros = parts.exponent - parts.noOfCoefficientDecimals;
|
|
40
|
+
return `${number}${"0".repeat(zeros)}`;
|
|
41
|
+
}
|
|
42
|
+
else {
|
|
43
|
+
return parts.invariantPrecisionString;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
toLocaleFixed(value, precision, maxDecimals) {
|
|
47
|
+
return this.toFixed(value, precision, maxDecimals)
|
|
48
|
+
.replace(exports.invariantDecimalSeparator, exports.localeDecimalSeparator);
|
|
49
|
+
}
|
|
50
|
+
getParts(value, precision, maxDecimals) {
|
|
51
|
+
const precisionString = this.toPrecision(value, precision, maxDecimals);
|
|
52
|
+
if (precisionString.indexOf("e") >= 0) {
|
|
53
|
+
const match = /(.*)e([\+-]\d+)$/.exec(precisionString);
|
|
54
|
+
const coefficientString = match[1];
|
|
55
|
+
const exponent = parseInt(match[2]);
|
|
56
|
+
const decimalSeparatorIndex = coefficientString.indexOf(exports.invariantDecimalSeparator);
|
|
57
|
+
const noOfCoefficientDecimals = decimalSeparatorIndex >= 0
|
|
58
|
+
? coefficientString.length - decimalSeparatorIndex - 1
|
|
59
|
+
: 0;
|
|
60
|
+
return {
|
|
61
|
+
invariantPrecisionString: precisionString,
|
|
62
|
+
invariantCoefficientString: coefficientString,
|
|
63
|
+
exponent,
|
|
64
|
+
noOfCoefficientDecimals,
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
else {
|
|
68
|
+
const decimalSeparatorIndex = precisionString.indexOf(exports.invariantDecimalSeparator);
|
|
69
|
+
const noOfCoefficientDecimals = decimalSeparatorIndex >= 0
|
|
70
|
+
? precisionString.length - decimalSeparatorIndex - 1
|
|
71
|
+
: 0;
|
|
72
|
+
return {
|
|
73
|
+
invariantPrecisionString: precisionString,
|
|
74
|
+
invariantCoefficientString: precisionString,
|
|
75
|
+
exponent: 0,
|
|
76
|
+
noOfCoefficientDecimals,
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
exports.NumberFormatter = new Formatter();
|
|
82
82
|
//# sourceMappingURL=number-formatter.js.map
|
|
@@ -1,98 +1,98 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const number_formatter_1 = require("./number-formatter");
|
|
4
|
-
const chai_1 = require("chai");
|
|
5
|
-
describe("toParts and toPrecision", () => {
|
|
6
|
-
it("should format correctly", () => {
|
|
7
|
-
const data = [
|
|
8
|
-
[0, 0, { invariantPrecisionString: "0", invariantCoefficientString: "0", exponent: 0, noOfCoefficientDecimals: 0 }],
|
|
9
|
-
[0.0, 1, { invariantPrecisionString: "0.0", invariantCoefficientString: "0.0", exponent: 0, noOfCoefficientDecimals: 1 }],
|
|
10
|
-
[77.1274, 1, { invariantPrecisionString: "8e+1", invariantCoefficientString: "8", exponent: 1, noOfCoefficientDecimals: 0 }],
|
|
11
|
-
[77.1274, 2, { invariantPrecisionString: "77", invariantCoefficientString: "77", exponent: 0, noOfCoefficientDecimals: 0 }],
|
|
12
|
-
[77.1274, 4, { invariantPrecisionString: "77.13", invariantCoefficientString: "77.13", exponent: 0, noOfCoefficientDecimals: 2 }],
|
|
13
|
-
[77.1274, 5, { invariantPrecisionString: "77.127", invariantCoefficientString: "77.127", exponent: 0, noOfCoefficientDecimals: 3 }],
|
|
14
|
-
[77.1274, 6, { invariantPrecisionString: "77.1274", invariantCoefficientString: "77.1274", exponent: 0, noOfCoefficientDecimals: 4 }],
|
|
15
|
-
[77.1274, 7, { invariantPrecisionString: "77.12740", invariantCoefficientString: "77.12740", exponent: 0, noOfCoefficientDecimals: 5 }],
|
|
16
|
-
[77.1274, 15, { invariantPrecisionString: "77.1274000000000", invariantCoefficientString: "77.1274000000000", exponent: 0, noOfCoefficientDecimals: 13 }],
|
|
17
|
-
[77.1274, 16, { invariantPrecisionString: "77.12739999999999", invariantCoefficientString: "77.12739999999999", exponent: 0, noOfCoefficientDecimals: 14 }],
|
|
18
|
-
[77.1274, 17, { invariantPrecisionString: "77.127399999999994", invariantCoefficientString: "77.127399999999994", exponent: 0, noOfCoefficientDecimals: 15 }],
|
|
19
|
-
[77.1274, 18, { invariantPrecisionString: "77.1273999999999944", invariantCoefficientString: "77.1273999999999944", exponent: 0, noOfCoefficientDecimals: 16 }],
|
|
20
|
-
[77.1274, 19, { invariantPrecisionString: "77.12739999999999441", invariantCoefficientString: "77.12739999999999441", exponent: 0, noOfCoefficientDecimals: 17 }],
|
|
21
|
-
[77.1274, 20, { invariantPrecisionString: "77.127399999999994407", invariantCoefficientString: "77.127399999999994407", exponent: 0, noOfCoefficientDecimals: 18 }],
|
|
22
|
-
[77.1274, 21, { invariantPrecisionString: "77.1273999999999944066", invariantCoefficientString: "77.1273999999999944066", exponent: 0, noOfCoefficientDecimals: 19 }],
|
|
23
|
-
[0.00000123, 2, { invariantPrecisionString: "0.0000012", invariantCoefficientString: "0.0000012", exponent: 0, noOfCoefficientDecimals: 7 }],
|
|
24
|
-
[0.000000123, 2, { invariantPrecisionString: "1.2e-7", invariantCoefficientString: "1.2", exponent: -7, noOfCoefficientDecimals: 1 }],
|
|
25
|
-
[0.00000123, 21, { invariantPrecisionString: "0.00000123000000000000008198", invariantCoefficientString: "0.00000123000000000000008198", exponent: 0, noOfCoefficientDecimals: 26 }],
|
|
26
|
-
[123456, 3, { invariantPrecisionString: "1.23e+5", invariantCoefficientString: "1.23", exponent: 5, noOfCoefficientDecimals: 2 }],
|
|
27
|
-
[Number.MAX_VALUE, 3, { invariantPrecisionString: "1.80e+308", invariantCoefficientString: "1.80", exponent: 308, noOfCoefficientDecimals: 2 }],
|
|
28
|
-
[123456, 15, { invariantPrecisionString: "123456.000000000", invariantCoefficientString: "123456.000000000", exponent: 0, noOfCoefficientDecimals: 9 }],
|
|
29
|
-
[0, 5, { invariantPrecisionString: "0.00000", invariantCoefficientString: "0.00000", exponent: 0, noOfCoefficientDecimals: 5 }],
|
|
30
|
-
[0.000001, 1, { invariantPrecisionString: "0.000001", invariantCoefficientString: "0.000001", exponent: 0, noOfCoefficientDecimals: 6 }],
|
|
31
|
-
[0.000001, 10, { invariantPrecisionString: "0.000001000000000", invariantCoefficientString: "0.000001000000000", exponent: 0, noOfCoefficientDecimals: 15 }],
|
|
32
|
-
[0.0000001, 10, { invariantPrecisionString: "1.000000000e-7", invariantCoefficientString: "1.000000000", exponent: -7, noOfCoefficientDecimals: 9 }],
|
|
33
|
-
[0.00000000001, 1, { invariantPrecisionString: "1e-11", invariantCoefficientString: "1", exponent: -11, noOfCoefficientDecimals: 0 }],
|
|
34
|
-
[0.00000000001, 2, { invariantPrecisionString: "1.0e-11", invariantCoefficientString: "1.0", exponent: -11, noOfCoefficientDecimals: 1 }],
|
|
35
|
-
[55, 2, { invariantPrecisionString: "55", invariantCoefficientString: "55", exponent: 0, noOfCoefficientDecimals: 0 }],
|
|
36
|
-
[55, 1, { invariantPrecisionString: "6e+1", invariantCoefficientString: "6", exponent: 1, noOfCoefficientDecimals: 0 }],
|
|
37
|
-
[-55, 2, { invariantPrecisionString: "-55", invariantCoefficientString: "-55", exponent: 0, noOfCoefficientDecimals: 0 }],
|
|
38
|
-
[-55, 1, { invariantPrecisionString: "-6e+1", invariantCoefficientString: "-6", exponent: 1, noOfCoefficientDecimals: 0 }],
|
|
39
|
-
[9.59, 1, { invariantPrecisionString: "1e+1", invariantCoefficientString: "1", exponent: 1, noOfCoefficientDecimals: 0 }],
|
|
40
|
-
[9.59, 2, { invariantPrecisionString: "10", invariantCoefficientString: "10", exponent: 0, noOfCoefficientDecimals: 0 }],
|
|
41
|
-
[9.95, 2, { invariantPrecisionString: "10", invariantCoefficientString: "10", exponent: 0, noOfCoefficientDecimals: 0 }],
|
|
42
|
-
[9.96, 2, { invariantPrecisionString: "10", invariantCoefficientString: "10", exponent: 0, noOfCoefficientDecimals: 0 }],
|
|
43
|
-
[-555555555555555555555, 1, { invariantPrecisionString: "-6e+20", invariantCoefficientString: "-6", exponent: 20, noOfCoefficientDecimals: 0 }],
|
|
44
|
-
[-5555555555555555555555, 1, { invariantPrecisionString: "-6e+21", invariantCoefficientString: "-6", exponent: 21, noOfCoefficientDecimals: 0 }],
|
|
45
|
-
[18014398509482012, 21, { invariantPrecisionString: "18014398509482012.0000", invariantCoefficientString: "18014398509482012.0000", exponent: 0, noOfCoefficientDecimals: 4 }],
|
|
46
|
-
[180143985094820121234, 21, { invariantPrecisionString: "180143985094820134912", invariantCoefficientString: "180143985094820134912", exponent: 0, noOfCoefficientDecimals: 0 }],
|
|
47
|
-
[0.1, 21, { invariantPrecisionString: "0.100000000000000005551", invariantCoefficientString: "0.100000000000000005551", exponent: 0, noOfCoefficientDecimals: 21 }],
|
|
48
|
-
[Number.EPSILON, 2, { invariantPrecisionString: "2.2e-16", invariantCoefficientString: "2.2", exponent: -16, noOfCoefficientDecimals: 1 }],
|
|
49
|
-
[Number.EPSILON, 21, { invariantPrecisionString: "2.22044604925031308085e-16", invariantCoefficientString: "2.22044604925031308085", exponent: -16, noOfCoefficientDecimals: 20 }],
|
|
50
|
-
[Number.NaN, 3, { invariantPrecisionString: "NaN", invariantCoefficientString: "NaN", exponent: 0, noOfCoefficientDecimals: 0 }],
|
|
51
|
-
[Number.POSITIVE_INFINITY, 3, { invariantPrecisionString: "Infinity", invariantCoefficientString: "Infinity", exponent: 0, noOfCoefficientDecimals: 0 }],
|
|
52
|
-
[Number.NEGATIVE_INFINITY, 3, { invariantPrecisionString: "-Infinity", invariantCoefficientString: "-Infinity", exponent: 0, noOfCoefficientDecimals: 0 }],
|
|
53
|
-
[164735280, 7, { invariantPrecisionString: "1.647353e+8", invariantCoefficientString: "1.647353", exponent: 8, noOfCoefficientDecimals: 6 }],
|
|
54
|
-
];
|
|
55
|
-
for (const [value, precision, expectedParts] of data) {
|
|
56
|
-
(0, chai_1.expect)(number_formatter_1.NumberFormatter.toPrecision(value, precision)).to.equal(expectedParts.invariantPrecisionString);
|
|
57
|
-
const localePrecisionString = expectedParts.invariantPrecisionString.replace(number_formatter_1.invariantDecimalSeparator, number_formatter_1.localeDecimalSeparator);
|
|
58
|
-
(0, chai_1.expect)(number_formatter_1.NumberFormatter.toLocalePrecision(value, precision)).to.equal(localePrecisionString);
|
|
59
|
-
const parts = number_formatter_1.NumberFormatter.getParts(value, precision);
|
|
60
|
-
(0, chai_1.expect)(parts.invariantPrecisionString).to.equal(expectedParts.invariantPrecisionString);
|
|
61
|
-
(0, chai_1.expect)(parts.invariantCoefficientString).to.equal(expectedParts.invariantCoefficientString);
|
|
62
|
-
(0, chai_1.expect)(parts.exponent).to.equal(expectedParts.exponent);
|
|
63
|
-
(0, chai_1.expect)(parts.noOfCoefficientDecimals).to.equal(expectedParts.noOfCoefficientDecimals);
|
|
64
|
-
}
|
|
65
|
-
});
|
|
66
|
-
});
|
|
67
|
-
describe("toFixed", () => {
|
|
68
|
-
it("should format correctly", () => {
|
|
69
|
-
const data = [
|
|
70
|
-
[0, 0, "0"],
|
|
71
|
-
[0.0, 1, "0.0"],
|
|
72
|
-
[10, 1, "10"],
|
|
73
|
-
[10, 2, "10"],
|
|
74
|
-
[10, 3, "10.0"],
|
|
75
|
-
[10, 4, "10.00"],
|
|
76
|
-
[10.2, 1, "10"],
|
|
77
|
-
[10.2, 2, "10"],
|
|
78
|
-
[10.2, 3, "10.2"],
|
|
79
|
-
[10.2, 4, "10.20"],
|
|
80
|
-
[217887000000, 6, "217887000000"],
|
|
81
|
-
];
|
|
82
|
-
for (const [value, precision, invariantExpected] of data) {
|
|
83
|
-
(0, chai_1.expect)(number_formatter_1.NumberFormatter.toFixed(value, precision)).to.equal(invariantExpected);
|
|
84
|
-
const localeExpected = invariantExpected.replace(number_formatter_1.invariantDecimalSeparator, number_formatter_1.localeDecimalSeparator);
|
|
85
|
-
(0, chai_1.expect)(number_formatter_1.NumberFormatter.toLocaleFixed(value, precision)).to.equal(localeExpected);
|
|
86
|
-
}
|
|
87
|
-
});
|
|
88
|
-
});
|
|
89
|
-
describe("toLocaleFixed", () => {
|
|
90
|
-
it("should format correctly", () => {
|
|
91
|
-
(0, chai_1.expect)(number_formatter_1.NumberFormatter.toLocaleFixed(400, 2)).to.equal("400");
|
|
92
|
-
});
|
|
93
|
-
it("should format correctly with max decimals", () => {
|
|
94
|
-
(0, chai_1.expect)(number_formatter_1.NumberFormatter.toLocaleFixed(35.123456, 8, 2)).to.equal("35.12");
|
|
95
|
-
(0, chai_1.expect)(number_formatter_1.NumberFormatter.toLocaleFixed(35.129999, 8, 2)).to.equal("35.13");
|
|
96
|
-
});
|
|
97
|
-
});
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const number_formatter_1 = require("./number-formatter");
|
|
4
|
+
const chai_1 = require("chai");
|
|
5
|
+
describe("toParts and toPrecision", () => {
|
|
6
|
+
it("should format correctly", () => {
|
|
7
|
+
const data = [
|
|
8
|
+
[0, 0, { invariantPrecisionString: "0", invariantCoefficientString: "0", exponent: 0, noOfCoefficientDecimals: 0 }],
|
|
9
|
+
[0.0, 1, { invariantPrecisionString: "0.0", invariantCoefficientString: "0.0", exponent: 0, noOfCoefficientDecimals: 1 }],
|
|
10
|
+
[77.1274, 1, { invariantPrecisionString: "8e+1", invariantCoefficientString: "8", exponent: 1, noOfCoefficientDecimals: 0 }],
|
|
11
|
+
[77.1274, 2, { invariantPrecisionString: "77", invariantCoefficientString: "77", exponent: 0, noOfCoefficientDecimals: 0 }],
|
|
12
|
+
[77.1274, 4, { invariantPrecisionString: "77.13", invariantCoefficientString: "77.13", exponent: 0, noOfCoefficientDecimals: 2 }],
|
|
13
|
+
[77.1274, 5, { invariantPrecisionString: "77.127", invariantCoefficientString: "77.127", exponent: 0, noOfCoefficientDecimals: 3 }],
|
|
14
|
+
[77.1274, 6, { invariantPrecisionString: "77.1274", invariantCoefficientString: "77.1274", exponent: 0, noOfCoefficientDecimals: 4 }],
|
|
15
|
+
[77.1274, 7, { invariantPrecisionString: "77.12740", invariantCoefficientString: "77.12740", exponent: 0, noOfCoefficientDecimals: 5 }],
|
|
16
|
+
[77.1274, 15, { invariantPrecisionString: "77.1274000000000", invariantCoefficientString: "77.1274000000000", exponent: 0, noOfCoefficientDecimals: 13 }],
|
|
17
|
+
[77.1274, 16, { invariantPrecisionString: "77.12739999999999", invariantCoefficientString: "77.12739999999999", exponent: 0, noOfCoefficientDecimals: 14 }],
|
|
18
|
+
[77.1274, 17, { invariantPrecisionString: "77.127399999999994", invariantCoefficientString: "77.127399999999994", exponent: 0, noOfCoefficientDecimals: 15 }],
|
|
19
|
+
[77.1274, 18, { invariantPrecisionString: "77.1273999999999944", invariantCoefficientString: "77.1273999999999944", exponent: 0, noOfCoefficientDecimals: 16 }],
|
|
20
|
+
[77.1274, 19, { invariantPrecisionString: "77.12739999999999441", invariantCoefficientString: "77.12739999999999441", exponent: 0, noOfCoefficientDecimals: 17 }],
|
|
21
|
+
[77.1274, 20, { invariantPrecisionString: "77.127399999999994407", invariantCoefficientString: "77.127399999999994407", exponent: 0, noOfCoefficientDecimals: 18 }],
|
|
22
|
+
[77.1274, 21, { invariantPrecisionString: "77.1273999999999944066", invariantCoefficientString: "77.1273999999999944066", exponent: 0, noOfCoefficientDecimals: 19 }],
|
|
23
|
+
[0.00000123, 2, { invariantPrecisionString: "0.0000012", invariantCoefficientString: "0.0000012", exponent: 0, noOfCoefficientDecimals: 7 }],
|
|
24
|
+
[0.000000123, 2, { invariantPrecisionString: "1.2e-7", invariantCoefficientString: "1.2", exponent: -7, noOfCoefficientDecimals: 1 }],
|
|
25
|
+
[0.00000123, 21, { invariantPrecisionString: "0.00000123000000000000008198", invariantCoefficientString: "0.00000123000000000000008198", exponent: 0, noOfCoefficientDecimals: 26 }],
|
|
26
|
+
[123456, 3, { invariantPrecisionString: "1.23e+5", invariantCoefficientString: "1.23", exponent: 5, noOfCoefficientDecimals: 2 }],
|
|
27
|
+
[Number.MAX_VALUE, 3, { invariantPrecisionString: "1.80e+308", invariantCoefficientString: "1.80", exponent: 308, noOfCoefficientDecimals: 2 }],
|
|
28
|
+
[123456, 15, { invariantPrecisionString: "123456.000000000", invariantCoefficientString: "123456.000000000", exponent: 0, noOfCoefficientDecimals: 9 }],
|
|
29
|
+
[0, 5, { invariantPrecisionString: "0.00000", invariantCoefficientString: "0.00000", exponent: 0, noOfCoefficientDecimals: 5 }],
|
|
30
|
+
[0.000001, 1, { invariantPrecisionString: "0.000001", invariantCoefficientString: "0.000001", exponent: 0, noOfCoefficientDecimals: 6 }],
|
|
31
|
+
[0.000001, 10, { invariantPrecisionString: "0.000001000000000", invariantCoefficientString: "0.000001000000000", exponent: 0, noOfCoefficientDecimals: 15 }],
|
|
32
|
+
[0.0000001, 10, { invariantPrecisionString: "1.000000000e-7", invariantCoefficientString: "1.000000000", exponent: -7, noOfCoefficientDecimals: 9 }],
|
|
33
|
+
[0.00000000001, 1, { invariantPrecisionString: "1e-11", invariantCoefficientString: "1", exponent: -11, noOfCoefficientDecimals: 0 }],
|
|
34
|
+
[0.00000000001, 2, { invariantPrecisionString: "1.0e-11", invariantCoefficientString: "1.0", exponent: -11, noOfCoefficientDecimals: 1 }],
|
|
35
|
+
[55, 2, { invariantPrecisionString: "55", invariantCoefficientString: "55", exponent: 0, noOfCoefficientDecimals: 0 }],
|
|
36
|
+
[55, 1, { invariantPrecisionString: "6e+1", invariantCoefficientString: "6", exponent: 1, noOfCoefficientDecimals: 0 }],
|
|
37
|
+
[-55, 2, { invariantPrecisionString: "-55", invariantCoefficientString: "-55", exponent: 0, noOfCoefficientDecimals: 0 }],
|
|
38
|
+
[-55, 1, { invariantPrecisionString: "-6e+1", invariantCoefficientString: "-6", exponent: 1, noOfCoefficientDecimals: 0 }],
|
|
39
|
+
[9.59, 1, { invariantPrecisionString: "1e+1", invariantCoefficientString: "1", exponent: 1, noOfCoefficientDecimals: 0 }],
|
|
40
|
+
[9.59, 2, { invariantPrecisionString: "10", invariantCoefficientString: "10", exponent: 0, noOfCoefficientDecimals: 0 }],
|
|
41
|
+
[9.95, 2, { invariantPrecisionString: "10", invariantCoefficientString: "10", exponent: 0, noOfCoefficientDecimals: 0 }],
|
|
42
|
+
[9.96, 2, { invariantPrecisionString: "10", invariantCoefficientString: "10", exponent: 0, noOfCoefficientDecimals: 0 }],
|
|
43
|
+
[-555555555555555555555, 1, { invariantPrecisionString: "-6e+20", invariantCoefficientString: "-6", exponent: 20, noOfCoefficientDecimals: 0 }],
|
|
44
|
+
[-5555555555555555555555, 1, { invariantPrecisionString: "-6e+21", invariantCoefficientString: "-6", exponent: 21, noOfCoefficientDecimals: 0 }],
|
|
45
|
+
[18014398509482012, 21, { invariantPrecisionString: "18014398509482012.0000", invariantCoefficientString: "18014398509482012.0000", exponent: 0, noOfCoefficientDecimals: 4 }],
|
|
46
|
+
[180143985094820121234, 21, { invariantPrecisionString: "180143985094820134912", invariantCoefficientString: "180143985094820134912", exponent: 0, noOfCoefficientDecimals: 0 }],
|
|
47
|
+
[0.1, 21, { invariantPrecisionString: "0.100000000000000005551", invariantCoefficientString: "0.100000000000000005551", exponent: 0, noOfCoefficientDecimals: 21 }],
|
|
48
|
+
[Number.EPSILON, 2, { invariantPrecisionString: "2.2e-16", invariantCoefficientString: "2.2", exponent: -16, noOfCoefficientDecimals: 1 }],
|
|
49
|
+
[Number.EPSILON, 21, { invariantPrecisionString: "2.22044604925031308085e-16", invariantCoefficientString: "2.22044604925031308085", exponent: -16, noOfCoefficientDecimals: 20 }],
|
|
50
|
+
[Number.NaN, 3, { invariantPrecisionString: "NaN", invariantCoefficientString: "NaN", exponent: 0, noOfCoefficientDecimals: 0 }],
|
|
51
|
+
[Number.POSITIVE_INFINITY, 3, { invariantPrecisionString: "Infinity", invariantCoefficientString: "Infinity", exponent: 0, noOfCoefficientDecimals: 0 }],
|
|
52
|
+
[Number.NEGATIVE_INFINITY, 3, { invariantPrecisionString: "-Infinity", invariantCoefficientString: "-Infinity", exponent: 0, noOfCoefficientDecimals: 0 }],
|
|
53
|
+
[164735280, 7, { invariantPrecisionString: "1.647353e+8", invariantCoefficientString: "1.647353", exponent: 8, noOfCoefficientDecimals: 6 }],
|
|
54
|
+
];
|
|
55
|
+
for (const [value, precision, expectedParts] of data) {
|
|
56
|
+
(0, chai_1.expect)(number_formatter_1.NumberFormatter.toPrecision(value, precision)).to.equal(expectedParts.invariantPrecisionString);
|
|
57
|
+
const localePrecisionString = expectedParts.invariantPrecisionString.replace(number_formatter_1.invariantDecimalSeparator, number_formatter_1.localeDecimalSeparator);
|
|
58
|
+
(0, chai_1.expect)(number_formatter_1.NumberFormatter.toLocalePrecision(value, precision)).to.equal(localePrecisionString);
|
|
59
|
+
const parts = number_formatter_1.NumberFormatter.getParts(value, precision);
|
|
60
|
+
(0, chai_1.expect)(parts.invariantPrecisionString).to.equal(expectedParts.invariantPrecisionString);
|
|
61
|
+
(0, chai_1.expect)(parts.invariantCoefficientString).to.equal(expectedParts.invariantCoefficientString);
|
|
62
|
+
(0, chai_1.expect)(parts.exponent).to.equal(expectedParts.exponent);
|
|
63
|
+
(0, chai_1.expect)(parts.noOfCoefficientDecimals).to.equal(expectedParts.noOfCoefficientDecimals);
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
});
|
|
67
|
+
describe("toFixed", () => {
|
|
68
|
+
it("should format correctly", () => {
|
|
69
|
+
const data = [
|
|
70
|
+
[0, 0, "0"],
|
|
71
|
+
[0.0, 1, "0.0"],
|
|
72
|
+
[10, 1, "10"],
|
|
73
|
+
[10, 2, "10"],
|
|
74
|
+
[10, 3, "10.0"],
|
|
75
|
+
[10, 4, "10.00"],
|
|
76
|
+
[10.2, 1, "10"],
|
|
77
|
+
[10.2, 2, "10"],
|
|
78
|
+
[10.2, 3, "10.2"],
|
|
79
|
+
[10.2, 4, "10.20"],
|
|
80
|
+
[217887000000, 6, "217887000000"],
|
|
81
|
+
];
|
|
82
|
+
for (const [value, precision, invariantExpected] of data) {
|
|
83
|
+
(0, chai_1.expect)(number_formatter_1.NumberFormatter.toFixed(value, precision)).to.equal(invariantExpected);
|
|
84
|
+
const localeExpected = invariantExpected.replace(number_formatter_1.invariantDecimalSeparator, number_formatter_1.localeDecimalSeparator);
|
|
85
|
+
(0, chai_1.expect)(number_formatter_1.NumberFormatter.toLocaleFixed(value, precision)).to.equal(localeExpected);
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
});
|
|
89
|
+
describe("toLocaleFixed", () => {
|
|
90
|
+
it("should format correctly", () => {
|
|
91
|
+
(0, chai_1.expect)(number_formatter_1.NumberFormatter.toLocaleFixed(400, 2)).to.equal("400");
|
|
92
|
+
});
|
|
93
|
+
it("should format correctly with max decimals", () => {
|
|
94
|
+
(0, chai_1.expect)(number_formatter_1.NumberFormatter.toLocaleFixed(35.123456, 8, 2)).to.equal("35.12");
|
|
95
|
+
(0, chai_1.expect)(number_formatter_1.NumberFormatter.toLocaleFixed(35.129999, 8, 2)).to.equal("35.13");
|
|
96
|
+
});
|
|
97
|
+
});
|
|
98
98
|
//# sourceMappingURL=number-formatter.spec.js.map
|
package/dist/cjs/optional.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
//# sourceMappingURL=optional.js.map
|
|
@@ -1,36 +1,36 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.PrecisionNumber = void 0;
|
|
4
|
-
const _1 = require(".");
|
|
5
|
-
const number_formatter_1 = require("./number-formatter");
|
|
6
|
-
class PrecisionNumber {
|
|
7
|
-
constructor(value, precision) {
|
|
8
|
-
this.value = value;
|
|
9
|
-
this.precision = precision;
|
|
10
|
-
}
|
|
11
|
-
static parse(input) {
|
|
12
|
-
let normalized = input.trim().replace(",", ".").replace(number_formatter_1.localeDecimalSeparator, _1.invariantDecimalSeparator);
|
|
13
|
-
if (normalized.endsWith(_1.invariantDecimalSeparator)) {
|
|
14
|
-
normalized += "0";
|
|
15
|
-
}
|
|
16
|
-
const precision = normalized.replace(".", "").replace("+", "").replace("-", "").length;
|
|
17
|
-
return new PrecisionNumber(parseFloat(normalized), precision);
|
|
18
|
-
}
|
|
19
|
-
toString() {
|
|
20
|
-
return this.value.toString();
|
|
21
|
-
}
|
|
22
|
-
toFixed() {
|
|
23
|
-
return number_formatter_1.NumberFormatter.toFixed(this.value, this.precision);
|
|
24
|
-
}
|
|
25
|
-
toPrecision() {
|
|
26
|
-
return number_formatter_1.NumberFormatter.toPrecision(this.value, this.precision);
|
|
27
|
-
}
|
|
28
|
-
toLocaleFixed() {
|
|
29
|
-
return number_formatter_1.NumberFormatter.toLocaleFixed(this.value, this.precision);
|
|
30
|
-
}
|
|
31
|
-
toLocalePrecision() {
|
|
32
|
-
return number_formatter_1.NumberFormatter.toLocalePrecision(this.value, this.precision);
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
exports.PrecisionNumber = PrecisionNumber;
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.PrecisionNumber = void 0;
|
|
4
|
+
const _1 = require(".");
|
|
5
|
+
const number_formatter_1 = require("./number-formatter");
|
|
6
|
+
class PrecisionNumber {
|
|
7
|
+
constructor(value, precision) {
|
|
8
|
+
this.value = value;
|
|
9
|
+
this.precision = precision;
|
|
10
|
+
}
|
|
11
|
+
static parse(input) {
|
|
12
|
+
let normalized = input.trim().replace(",", ".").replace(number_formatter_1.localeDecimalSeparator, _1.invariantDecimalSeparator);
|
|
13
|
+
if (normalized.endsWith(_1.invariantDecimalSeparator)) {
|
|
14
|
+
normalized += "0";
|
|
15
|
+
}
|
|
16
|
+
const precision = normalized.replace(".", "").replace("+", "").replace("-", "").length;
|
|
17
|
+
return new PrecisionNumber(parseFloat(normalized), precision);
|
|
18
|
+
}
|
|
19
|
+
toString() {
|
|
20
|
+
return this.value.toString();
|
|
21
|
+
}
|
|
22
|
+
toFixed() {
|
|
23
|
+
return number_formatter_1.NumberFormatter.toFixed(this.value, this.precision);
|
|
24
|
+
}
|
|
25
|
+
toPrecision() {
|
|
26
|
+
return number_formatter_1.NumberFormatter.toPrecision(this.value, this.precision);
|
|
27
|
+
}
|
|
28
|
+
toLocaleFixed() {
|
|
29
|
+
return number_formatter_1.NumberFormatter.toLocaleFixed(this.value, this.precision);
|
|
30
|
+
}
|
|
31
|
+
toLocalePrecision() {
|
|
32
|
+
return number_formatter_1.NumberFormatter.toLocalePrecision(this.value, this.precision);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
exports.PrecisionNumber = PrecisionNumber;
|
|
36
36
|
//# sourceMappingURL=precision-number.js.map
|
|
@@ -1,27 +1,27 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const chai_1 = require("chai");
|
|
4
|
-
const precision_number_1 = require("./precision-number");
|
|
5
|
-
describe("precision-number", () => {
|
|
6
|
-
it("should not parse ''", () => {
|
|
7
|
-
const number = precision_number_1.PrecisionNumber.parse("");
|
|
8
|
-
(0, chai_1.expect)(Number.isNaN(number.value)).to.equal(true);
|
|
9
|
-
(0, chai_1.expect)(number.precision).to.equal(0);
|
|
10
|
-
});
|
|
11
|
-
it("should parse '1'", () => {
|
|
12
|
-
const number = precision_number_1.PrecisionNumber.parse("1");
|
|
13
|
-
(0, chai_1.expect)(number.value).to.equal(1);
|
|
14
|
-
(0, chai_1.expect)(number.precision).to.equal(1);
|
|
15
|
-
});
|
|
16
|
-
it("should parse '1.'", () => {
|
|
17
|
-
const number = precision_number_1.PrecisionNumber.parse("1.");
|
|
18
|
-
(0, chai_1.expect)(number.value).to.equal(1);
|
|
19
|
-
(0, chai_1.expect)(number.precision).to.equal(2);
|
|
20
|
-
});
|
|
21
|
-
it("should parse '1.0'", () => {
|
|
22
|
-
const number = precision_number_1.PrecisionNumber.parse("1.0");
|
|
23
|
-
(0, chai_1.expect)(number.value).to.equal(1);
|
|
24
|
-
(0, chai_1.expect)(number.precision).to.equal(2);
|
|
25
|
-
});
|
|
26
|
-
});
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const chai_1 = require("chai");
|
|
4
|
+
const precision_number_1 = require("./precision-number");
|
|
5
|
+
describe("precision-number", () => {
|
|
6
|
+
it("should not parse ''", () => {
|
|
7
|
+
const number = precision_number_1.PrecisionNumber.parse("");
|
|
8
|
+
(0, chai_1.expect)(Number.isNaN(number.value)).to.equal(true);
|
|
9
|
+
(0, chai_1.expect)(number.precision).to.equal(0);
|
|
10
|
+
});
|
|
11
|
+
it("should parse '1'", () => {
|
|
12
|
+
const number = precision_number_1.PrecisionNumber.parse("1");
|
|
13
|
+
(0, chai_1.expect)(number.value).to.equal(1);
|
|
14
|
+
(0, chai_1.expect)(number.precision).to.equal(1);
|
|
15
|
+
});
|
|
16
|
+
it("should parse '1.'", () => {
|
|
17
|
+
const number = precision_number_1.PrecisionNumber.parse("1.");
|
|
18
|
+
(0, chai_1.expect)(number.value).to.equal(1);
|
|
19
|
+
(0, chai_1.expect)(number.precision).to.equal(2);
|
|
20
|
+
});
|
|
21
|
+
it("should parse '1.0'", () => {
|
|
22
|
+
const number = precision_number_1.PrecisionNumber.parse("1.0");
|
|
23
|
+
(0, chai_1.expect)(number.value).to.equal(1);
|
|
24
|
+
(0, chai_1.expect)(number.precision).to.equal(2);
|
|
25
|
+
});
|
|
26
|
+
});
|
|
27
27
|
//# sourceMappingURL=precision-number.spec.js.map
|