@utiliread/primitives 1.0.0-70 → 1.0.0-71
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 +1 -1
- package/locales/en/registers.json +1 -1
- package/package.json +3 -3
- package/src/register-id.ts +303 -303
package/dist/cjs/step.spec.js
CHANGED
|
@@ -1,29 +1,29 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const chai_1 = require("chai");
|
|
4
|
-
const luxon_1 = require("luxon");
|
|
5
|
-
const step_1 = require("./step");
|
|
6
|
-
describe("startOf", () => {
|
|
7
|
-
it("returns start of day", () => {
|
|
8
|
-
const dt = luxon_1.DateTime.fromISO("2020-01-11T05:00:00+01:00");
|
|
9
|
-
const result = step_1.Step.startOf(dt, "day");
|
|
10
|
-
(0, chai_1.expect)(+result).to.equal(+luxon_1.DateTime.fromISO("2020-01-11T00:00:00+01:00"));
|
|
11
|
-
});
|
|
12
|
-
it("returns previous monday from saturday", () => {
|
|
13
|
-
const dt = luxon_1.DateTime.fromISO("2020-01-11T05:00:00+01:00");
|
|
14
|
-
const result = step_1.Step.startOf(dt, "week", step_1.DayOfWeek.Monday);
|
|
15
|
-
(0, chai_1.expect)(+result).to.equal(+luxon_1.DateTime.fromISO("2020-01-06T00:00:00+01:00"));
|
|
16
|
-
});
|
|
17
|
-
it("returns same monday from monday with hour", () => {
|
|
18
|
-
const dt = luxon_1.DateTime.fromISO("2020-01-13T05:00:00+01:00");
|
|
19
|
-
const result = step_1.Step.startOf(dt, "week", step_1.DayOfWeek.Monday);
|
|
20
|
-
(0, chai_1.expect)(+result).to.equal(+luxon_1.DateTime.fromISO("2020-01-13T00:00:00+01:00"));
|
|
21
|
-
});
|
|
22
|
-
});
|
|
23
|
-
describe("asDuration", () => {
|
|
24
|
-
it("should return equivalent duration", () => {
|
|
25
|
-
const duration = step_1.Step.asDuration("week");
|
|
26
|
-
(0, chai_1.expect)(duration.days).to.equal(7);
|
|
27
|
-
});
|
|
28
|
-
});
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const chai_1 = require("chai");
|
|
4
|
+
const luxon_1 = require("luxon");
|
|
5
|
+
const step_1 = require("./step");
|
|
6
|
+
describe("startOf", () => {
|
|
7
|
+
it("returns start of day", () => {
|
|
8
|
+
const dt = luxon_1.DateTime.fromISO("2020-01-11T05:00:00+01:00");
|
|
9
|
+
const result = step_1.Step.startOf(dt, "day");
|
|
10
|
+
(0, chai_1.expect)(+result).to.equal(+luxon_1.DateTime.fromISO("2020-01-11T00:00:00+01:00"));
|
|
11
|
+
});
|
|
12
|
+
it("returns previous monday from saturday", () => {
|
|
13
|
+
const dt = luxon_1.DateTime.fromISO("2020-01-11T05:00:00+01:00");
|
|
14
|
+
const result = step_1.Step.startOf(dt, "week", step_1.DayOfWeek.Monday);
|
|
15
|
+
(0, chai_1.expect)(+result).to.equal(+luxon_1.DateTime.fromISO("2020-01-06T00:00:00+01:00"));
|
|
16
|
+
});
|
|
17
|
+
it("returns same monday from monday with hour", () => {
|
|
18
|
+
const dt = luxon_1.DateTime.fromISO("2020-01-13T05:00:00+01:00");
|
|
19
|
+
const result = step_1.Step.startOf(dt, "week", step_1.DayOfWeek.Monday);
|
|
20
|
+
(0, chai_1.expect)(+result).to.equal(+luxon_1.DateTime.fromISO("2020-01-13T00:00:00+01:00"));
|
|
21
|
+
});
|
|
22
|
+
});
|
|
23
|
+
describe("asDuration", () => {
|
|
24
|
+
it("should return equivalent duration", () => {
|
|
25
|
+
const duration = step_1.Step.asDuration("week");
|
|
26
|
+
(0, chai_1.expect)(duration.days).to.equal(7);
|
|
27
|
+
});
|
|
28
|
+
});
|
|
29
29
|
//# sourceMappingURL=step.spec.js.map
|
package/dist/cjs/time-or-rule.js
CHANGED
|
@@ -1,19 +1,19 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.TimeOrRule = void 0;
|
|
4
|
-
var TimeOrRule;
|
|
5
|
-
(function (TimeOrRule) {
|
|
6
|
-
function isTime(input) {
|
|
7
|
-
return typeof input !== "string";
|
|
8
|
-
}
|
|
9
|
-
TimeOrRule.isTime = isTime;
|
|
10
|
-
function isRule(input) {
|
|
11
|
-
return typeof input === "string";
|
|
12
|
-
}
|
|
13
|
-
TimeOrRule.isRule = isRule;
|
|
14
|
-
function asJSON(input) {
|
|
15
|
-
return isRule(input) ? input : input.toJSON();
|
|
16
|
-
}
|
|
17
|
-
TimeOrRule.asJSON = asJSON;
|
|
18
|
-
})(TimeOrRule
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.TimeOrRule = void 0;
|
|
4
|
+
var TimeOrRule;
|
|
5
|
+
(function (TimeOrRule) {
|
|
6
|
+
function isTime(input) {
|
|
7
|
+
return typeof input !== "string";
|
|
8
|
+
}
|
|
9
|
+
TimeOrRule.isTime = isTime;
|
|
10
|
+
function isRule(input) {
|
|
11
|
+
return typeof input === "string";
|
|
12
|
+
}
|
|
13
|
+
TimeOrRule.isRule = isRule;
|
|
14
|
+
function asJSON(input) {
|
|
15
|
+
return isRule(input) ? input : input.toJSON();
|
|
16
|
+
}
|
|
17
|
+
TimeOrRule.asJSON = asJSON;
|
|
18
|
+
})(TimeOrRule || (exports.TimeOrRule = TimeOrRule = {}));
|
|
19
19
|
//# sourceMappingURL=time-or-rule.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"time-or-rule.js","sourceRoot":"","sources":["../../src/time-or-rule.ts"],"names":[],"mappings":";;;AAIA,IAAiB,UAAU,CAY1B;AAZD,WAAiB,UAAU;IACvB,SAAgB,MAAM,CAAC,KAAiB;QACpC,OAAO,OAAO,KAAK,KAAK,QAAQ,CAAC;IACrC,CAAC;IAFe,iBAAM,SAErB,CAAA;IAED,SAAgB,MAAM,CAAC,KAAiB;QACpC,OAAO,OAAO,KAAK,KAAK,QAAQ,CAAC;IACrC,CAAC;IAFe,iBAAM,SAErB,CAAA;IAED,SAAgB,MAAM,CAAC,KAAiB;QACpC,OAAO,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,MAAM,EAAE,CAAC;IAClD,CAAC;IAFe,iBAAM,SAErB,CAAA;AACL,CAAC,EAZgB,UAAU,
|
|
1
|
+
{"version":3,"file":"time-or-rule.js","sourceRoot":"","sources":["../../src/time-or-rule.ts"],"names":[],"mappings":";;;AAIA,IAAiB,UAAU,CAY1B;AAZD,WAAiB,UAAU;IACvB,SAAgB,MAAM,CAAC,KAAiB;QACpC,OAAO,OAAO,KAAK,KAAK,QAAQ,CAAC;IACrC,CAAC;IAFe,iBAAM,SAErB,CAAA;IAED,SAAgB,MAAM,CAAC,KAAiB;QACpC,OAAO,OAAO,KAAK,KAAK,QAAQ,CAAC;IACrC,CAAC;IAFe,iBAAM,SAErB,CAAA;IAED,SAAgB,MAAM,CAAC,KAAiB;QACpC,OAAO,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,MAAM,EAAE,CAAC;IAClD,CAAC;IAFe,iBAAM,SAErB,CAAA;AACL,CAAC,EAZgB,UAAU,0BAAV,UAAU,QAY1B"}
|
package/dist/cjs/unit-id.js
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.energyUnits = void 0;
|
|
4
|
-
exports.energyUnits = [
|
|
5
|
-
"milliWattHour",
|
|
6
|
-
"wattHour",
|
|
7
|
-
"kiloWattHour",
|
|
8
|
-
"megaWattHour",
|
|
9
|
-
"gigaWattHour",
|
|
10
|
-
"calorie",
|
|
11
|
-
"kiloCalorie",
|
|
12
|
-
"megaCalorie",
|
|
13
|
-
];
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.energyUnits = void 0;
|
|
4
|
+
exports.energyUnits = [
|
|
5
|
+
"milliWattHour",
|
|
6
|
+
"wattHour",
|
|
7
|
+
"kiloWattHour",
|
|
8
|
+
"megaWattHour",
|
|
9
|
+
"gigaWattHour",
|
|
10
|
+
"calorie",
|
|
11
|
+
"kiloCalorie",
|
|
12
|
+
"megaCalorie",
|
|
13
|
+
];
|
|
14
14
|
//# sourceMappingURL=unit-id.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export type AggregateId = "instant" | "diff" | "min" | "max" | "avg" | "sum" | "cnt" | "ssq";
|
|
1
|
+
export type AggregateId = "instant" | "diff" | "min" | "max" | "avg" | "sum" | "cnt" | "ssq";
|
package/dist/esm/aggregate-id.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export {};
|
|
1
|
+
export {};
|
|
2
2
|
//# sourceMappingURL=aggregate-id.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export type ConsumptionTypeId = "none" | "heat" | "water" | "cooling" | "electricity" | "infrastructure" | "all";
|
|
1
|
+
export type ConsumptionTypeId = "none" | "heat" | "water" | "cooling" | "electricity" | "infrastructure" | "all";
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export {};
|
|
1
|
+
export {};
|
|
2
2
|
//# sourceMappingURL=consumption-type-id.js.map
|
package/dist/esm/index.d.ts
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
|
-
import type { ConsumptionTypeId } from "./consumption-type-id";
|
|
2
|
-
import type { RegisterId } from "./register-id";
|
|
3
|
-
import type { UnitId } from "./unit-id";
|
|
4
|
-
import { RegisterLibrary } from "./register-library";
|
|
5
|
-
import type { RegisterDefinition } from "./register-definition";
|
|
6
|
-
import { NumberFormatter, invariantDecimalSeparator, localeDecimalSeparator } from "./number-formatter";
|
|
7
|
-
import { RegisterConvention } from "./register-convention";
|
|
8
|
-
import type { AggregateId } from "./aggregate-id";
|
|
9
|
-
import type { MeasureId } from "./measure-id";
|
|
10
|
-
import { Step, DayOfWeek } from "./step";
|
|
11
|
-
import { TimeOrRule } from "./time-or-rule";
|
|
12
|
-
import { PrecisionNumber } from "./precision-number";
|
|
13
|
-
import "./repository";
|
|
14
|
-
import { Repository } from "./repository";
|
|
15
|
-
import type { Optional } from "./optional";
|
|
16
|
-
export { AggregateId, ConsumptionTypeId, DayOfWeek, MeasureId, NumberFormatter, PrecisionNumber, RegisterConvention, RegisterDefinition, RegisterId, RegisterLibrary, Repository, Step, TimeOrRule, UnitId, invariantDecimalSeparator, localeDecimalSeparator, Optional, };
|
|
1
|
+
import type { ConsumptionTypeId } from "./consumption-type-id";
|
|
2
|
+
import type { RegisterId } from "./register-id";
|
|
3
|
+
import type { UnitId } from "./unit-id";
|
|
4
|
+
import { RegisterLibrary } from "./register-library";
|
|
5
|
+
import type { RegisterDefinition } from "./register-definition";
|
|
6
|
+
import { NumberFormatter, invariantDecimalSeparator, localeDecimalSeparator } from "./number-formatter";
|
|
7
|
+
import { RegisterConvention } from "./register-convention";
|
|
8
|
+
import type { AggregateId } from "./aggregate-id";
|
|
9
|
+
import type { MeasureId } from "./measure-id";
|
|
10
|
+
import { Step, DayOfWeek } from "./step";
|
|
11
|
+
import { TimeOrRule } from "./time-or-rule";
|
|
12
|
+
import { PrecisionNumber } from "./precision-number";
|
|
13
|
+
import "./repository";
|
|
14
|
+
import { Repository } from "./repository";
|
|
15
|
+
import type { Optional } from "./optional";
|
|
16
|
+
export { AggregateId, ConsumptionTypeId, DayOfWeek, MeasureId, NumberFormatter, PrecisionNumber, RegisterConvention, RegisterDefinition, RegisterId, RegisterLibrary, Repository, Step, TimeOrRule, UnitId, invariantDecimalSeparator, localeDecimalSeparator, Optional, };
|
package/dist/esm/index.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import { RegisterLibrary } from "./register-library";
|
|
2
|
-
import { NumberFormatter, invariantDecimalSeparator, localeDecimalSeparator } from "./number-formatter";
|
|
3
|
-
import { RegisterConvention } from "./register-convention";
|
|
4
|
-
import { Step, DayOfWeek } from "./step";
|
|
5
|
-
import { TimeOrRule } from "./time-or-rule";
|
|
6
|
-
import { PrecisionNumber } from "./precision-number";
|
|
7
|
-
import "./repository"; // We need this to get the Repository namespace exported
|
|
8
|
-
import { Repository } from "./repository";
|
|
9
|
-
export { DayOfWeek, NumberFormatter, PrecisionNumber, RegisterConvention, RegisterLibrary, Repository, Step, TimeOrRule, invariantDecimalSeparator, localeDecimalSeparator, };
|
|
1
|
+
import { RegisterLibrary } from "./register-library";
|
|
2
|
+
import { NumberFormatter, invariantDecimalSeparator, localeDecimalSeparator } from "./number-formatter";
|
|
3
|
+
import { RegisterConvention } from "./register-convention";
|
|
4
|
+
import { Step, DayOfWeek } from "./step";
|
|
5
|
+
import { TimeOrRule } from "./time-or-rule";
|
|
6
|
+
import { PrecisionNumber } from "./precision-number";
|
|
7
|
+
import "./repository"; // We need this to get the Repository namespace exported
|
|
8
|
+
import { Repository } from "./repository";
|
|
9
|
+
export { DayOfWeek, NumberFormatter, PrecisionNumber, RegisterConvention, RegisterLibrary, Repository, Step, TimeOrRule, invariantDecimalSeparator, localeDecimalSeparator, };
|
|
10
10
|
//# sourceMappingURL=index.js.map
|
package/dist/esm/measure-id.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export type MeasureId = "none" | "duration" | "energy" | "humidity" | "mass" | "massFlow" | "pressure" | "power" | "reactiveEnergy" | "reactivePower" | "temperature" | "time" | "volume" | "volumeFlow";
|
|
1
|
+
export type MeasureId = "none" | "duration" | "energy" | "humidity" | "mass" | "massFlow" | "pressure" | "power" | "reactiveEnergy" | "reactivePower" | "temperature" | "time" | "volume" | "volumeFlow";
|
package/dist/esm/measure-id.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export {};
|
|
1
|
+
export {};
|
|
2
2
|
//# sourceMappingURL=measure-id.js.map
|
|
@@ -1,17 +1,17 @@
|
|
|
1
|
-
export declare const invariantDecimalSeparator: string;
|
|
2
|
-
export declare const localeDecimalSeparator: string;
|
|
3
|
-
export interface Parts {
|
|
4
|
-
invariantPrecisionString: string;
|
|
5
|
-
invariantCoefficientString: string;
|
|
6
|
-
exponent: number;
|
|
7
|
-
noOfCoefficientDecimals: number;
|
|
8
|
-
}
|
|
9
|
-
declare class Formatter {
|
|
10
|
-
toPrecision(value: number, precision: number, maxDecimals?: number): string;
|
|
11
|
-
toLocalePrecision(value: number, precision: number, maxDecimals?: number): string;
|
|
12
|
-
toFixed(value: number, precision: number, maxDecimals?: number): string;
|
|
13
|
-
toLocaleFixed(value: number, precision: number, maxDecimals?: number): string;
|
|
14
|
-
getParts(value: number, precision: number, maxDecimals?: number): Parts;
|
|
15
|
-
}
|
|
16
|
-
export declare const NumberFormatter: Formatter;
|
|
17
|
-
export {};
|
|
1
|
+
export declare const invariantDecimalSeparator: string;
|
|
2
|
+
export declare const localeDecimalSeparator: string;
|
|
3
|
+
export interface Parts {
|
|
4
|
+
invariantPrecisionString: string;
|
|
5
|
+
invariantCoefficientString: string;
|
|
6
|
+
exponent: number;
|
|
7
|
+
noOfCoefficientDecimals: number;
|
|
8
|
+
}
|
|
9
|
+
declare class Formatter {
|
|
10
|
+
toPrecision(value: number, precision: number, maxDecimals?: number): string;
|
|
11
|
+
toLocalePrecision(value: number, precision: number, maxDecimals?: number): string;
|
|
12
|
+
toFixed(value: number, precision: number, maxDecimals?: number): string;
|
|
13
|
+
toLocaleFixed(value: number, precision: number, maxDecimals?: number): string;
|
|
14
|
+
getParts(value: number, precision: number, maxDecimals?: number): Parts;
|
|
15
|
+
}
|
|
16
|
+
export declare const NumberFormatter: Formatter;
|
|
17
|
+
export {};
|
|
@@ -1,79 +1,79 @@
|
|
|
1
|
-
export const invariantDecimalSeparator = (0.1).toString()[1];
|
|
2
|
-
export const localeDecimalSeparator = (0.1).toLocaleString()[1];
|
|
3
|
-
class Formatter {
|
|
4
|
-
toPrecision(value, precision, maxDecimals) {
|
|
5
|
-
const absValue = Math.abs(value);
|
|
6
|
-
const noOfIntegerDigits = Math.round(absValue).toString().length;
|
|
7
|
-
// Adjust precision to maxDecimals
|
|
8
|
-
if (maxDecimals !== undefined) {
|
|
9
|
-
precision = Math.min(precision, noOfIntegerDigits + maxDecimals);
|
|
10
|
-
}
|
|
11
|
-
if (value === 0) {
|
|
12
|
-
// (0).toPrecision(1) == "0"
|
|
13
|
-
// (0).toPrecision(2) == "0.0"
|
|
14
|
-
// (0).toPrecision(3) == "0.00"
|
|
15
|
-
// However, we want:
|
|
16
|
-
// Formatter.toPrecision(0, 0) == "0"
|
|
17
|
-
// Formatter.toPrecision(0, 1) == "0.0"
|
|
18
|
-
// Formatter.toPrecision(0, 2) == "0.00"
|
|
19
|
-
return value.toPrecision(precision + 1);
|
|
20
|
-
}
|
|
21
|
-
if (absValue < 1) {
|
|
22
|
-
return value.toPrecision(precision);
|
|
23
|
-
}
|
|
24
|
-
return precision >= noOfIntegerDigits
|
|
25
|
-
? value.toFixed(precision - noOfIntegerDigits)
|
|
26
|
-
: value.toPrecision(precision);
|
|
27
|
-
}
|
|
28
|
-
toLocalePrecision(value, precision, maxDecimals) {
|
|
29
|
-
return this.toPrecision(value, precision, maxDecimals)
|
|
30
|
-
.replace(invariantDecimalSeparator, localeDecimalSeparator);
|
|
31
|
-
}
|
|
32
|
-
toFixed(value, precision, maxDecimals) {
|
|
33
|
-
const parts = this.getParts(value, precision, maxDecimals);
|
|
34
|
-
if (parts.noOfCoefficientDecimals < parts.exponent) {
|
|
35
|
-
const number = parts.invariantCoefficientString.replace(invariantDecimalSeparator, "");
|
|
36
|
-
const zeros = parts.exponent - parts.noOfCoefficientDecimals;
|
|
37
|
-
return `${number}${"0".repeat(zeros)}`;
|
|
38
|
-
}
|
|
39
|
-
else {
|
|
40
|
-
return parts.invariantPrecisionString;
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
toLocaleFixed(value, precision, maxDecimals) {
|
|
44
|
-
return this.toFixed(value, precision, maxDecimals)
|
|
45
|
-
.replace(invariantDecimalSeparator, localeDecimalSeparator);
|
|
46
|
-
}
|
|
47
|
-
getParts(value, precision, maxDecimals) {
|
|
48
|
-
const precisionString = this.toPrecision(value, precision, maxDecimals);
|
|
49
|
-
if (precisionString.indexOf("e") >= 0) {
|
|
50
|
-
const match = /(.*)e([\+-]\d+)$/.exec(precisionString);
|
|
51
|
-
const coefficientString = match[1];
|
|
52
|
-
const exponent = parseInt(match[2]);
|
|
53
|
-
const decimalSeparatorIndex = coefficientString.indexOf(invariantDecimalSeparator);
|
|
54
|
-
const noOfCoefficientDecimals = decimalSeparatorIndex >= 0
|
|
55
|
-
? coefficientString.length - decimalSeparatorIndex - 1
|
|
56
|
-
: 0;
|
|
57
|
-
return {
|
|
58
|
-
invariantPrecisionString: precisionString,
|
|
59
|
-
invariantCoefficientString: coefficientString,
|
|
60
|
-
exponent,
|
|
61
|
-
noOfCoefficientDecimals,
|
|
62
|
-
};
|
|
63
|
-
}
|
|
64
|
-
else {
|
|
65
|
-
const decimalSeparatorIndex = precisionString.indexOf(invariantDecimalSeparator);
|
|
66
|
-
const noOfCoefficientDecimals = decimalSeparatorIndex >= 0
|
|
67
|
-
? precisionString.length - decimalSeparatorIndex - 1
|
|
68
|
-
: 0;
|
|
69
|
-
return {
|
|
70
|
-
invariantPrecisionString: precisionString,
|
|
71
|
-
invariantCoefficientString: precisionString,
|
|
72
|
-
exponent: 0,
|
|
73
|
-
noOfCoefficientDecimals,
|
|
74
|
-
};
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
export const NumberFormatter = new Formatter();
|
|
1
|
+
export const invariantDecimalSeparator = (0.1).toString()[1];
|
|
2
|
+
export const localeDecimalSeparator = (0.1).toLocaleString()[1];
|
|
3
|
+
class Formatter {
|
|
4
|
+
toPrecision(value, precision, maxDecimals) {
|
|
5
|
+
const absValue = Math.abs(value);
|
|
6
|
+
const noOfIntegerDigits = Math.round(absValue).toString().length;
|
|
7
|
+
// Adjust precision to maxDecimals
|
|
8
|
+
if (maxDecimals !== undefined) {
|
|
9
|
+
precision = Math.min(precision, noOfIntegerDigits + maxDecimals);
|
|
10
|
+
}
|
|
11
|
+
if (value === 0) {
|
|
12
|
+
// (0).toPrecision(1) == "0"
|
|
13
|
+
// (0).toPrecision(2) == "0.0"
|
|
14
|
+
// (0).toPrecision(3) == "0.00"
|
|
15
|
+
// However, we want:
|
|
16
|
+
// Formatter.toPrecision(0, 0) == "0"
|
|
17
|
+
// Formatter.toPrecision(0, 1) == "0.0"
|
|
18
|
+
// Formatter.toPrecision(0, 2) == "0.00"
|
|
19
|
+
return value.toPrecision(precision + 1);
|
|
20
|
+
}
|
|
21
|
+
if (absValue < 1) {
|
|
22
|
+
return value.toPrecision(precision);
|
|
23
|
+
}
|
|
24
|
+
return precision >= noOfIntegerDigits
|
|
25
|
+
? value.toFixed(precision - noOfIntegerDigits)
|
|
26
|
+
: value.toPrecision(precision);
|
|
27
|
+
}
|
|
28
|
+
toLocalePrecision(value, precision, maxDecimals) {
|
|
29
|
+
return this.toPrecision(value, precision, maxDecimals)
|
|
30
|
+
.replace(invariantDecimalSeparator, localeDecimalSeparator);
|
|
31
|
+
}
|
|
32
|
+
toFixed(value, precision, maxDecimals) {
|
|
33
|
+
const parts = this.getParts(value, precision, maxDecimals);
|
|
34
|
+
if (parts.noOfCoefficientDecimals < parts.exponent) {
|
|
35
|
+
const number = parts.invariantCoefficientString.replace(invariantDecimalSeparator, "");
|
|
36
|
+
const zeros = parts.exponent - parts.noOfCoefficientDecimals;
|
|
37
|
+
return `${number}${"0".repeat(zeros)}`;
|
|
38
|
+
}
|
|
39
|
+
else {
|
|
40
|
+
return parts.invariantPrecisionString;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
toLocaleFixed(value, precision, maxDecimals) {
|
|
44
|
+
return this.toFixed(value, precision, maxDecimals)
|
|
45
|
+
.replace(invariantDecimalSeparator, localeDecimalSeparator);
|
|
46
|
+
}
|
|
47
|
+
getParts(value, precision, maxDecimals) {
|
|
48
|
+
const precisionString = this.toPrecision(value, precision, maxDecimals);
|
|
49
|
+
if (precisionString.indexOf("e") >= 0) {
|
|
50
|
+
const match = /(.*)e([\+-]\d+)$/.exec(precisionString);
|
|
51
|
+
const coefficientString = match[1];
|
|
52
|
+
const exponent = parseInt(match[2]);
|
|
53
|
+
const decimalSeparatorIndex = coefficientString.indexOf(invariantDecimalSeparator);
|
|
54
|
+
const noOfCoefficientDecimals = decimalSeparatorIndex >= 0
|
|
55
|
+
? coefficientString.length - decimalSeparatorIndex - 1
|
|
56
|
+
: 0;
|
|
57
|
+
return {
|
|
58
|
+
invariantPrecisionString: precisionString,
|
|
59
|
+
invariantCoefficientString: coefficientString,
|
|
60
|
+
exponent,
|
|
61
|
+
noOfCoefficientDecimals,
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
else {
|
|
65
|
+
const decimalSeparatorIndex = precisionString.indexOf(invariantDecimalSeparator);
|
|
66
|
+
const noOfCoefficientDecimals = decimalSeparatorIndex >= 0
|
|
67
|
+
? precisionString.length - decimalSeparatorIndex - 1
|
|
68
|
+
: 0;
|
|
69
|
+
return {
|
|
70
|
+
invariantPrecisionString: precisionString,
|
|
71
|
+
invariantCoefficientString: precisionString,
|
|
72
|
+
exponent: 0,
|
|
73
|
+
noOfCoefficientDecimals,
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
export const NumberFormatter = new Formatter();
|
|
79
79
|
//# sourceMappingURL=number-formatter.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export {};
|
|
1
|
+
export {};
|
|
@@ -1,96 +1,96 @@
|
|
|
1
|
-
import { NumberFormatter, invariantDecimalSeparator, localeDecimalSeparator } from './number-formatter';
|
|
2
|
-
import { expect } from 'chai';
|
|
3
|
-
describe("toParts and toPrecision", () => {
|
|
4
|
-
it("should format correctly", () => {
|
|
5
|
-
const data = [
|
|
6
|
-
[0, 0, { invariantPrecisionString: "0", invariantCoefficientString: "0", exponent: 0, noOfCoefficientDecimals: 0 }],
|
|
7
|
-
[0.0, 1, { invariantPrecisionString: "0.0", invariantCoefficientString: "0.0", exponent: 0, noOfCoefficientDecimals: 1 }],
|
|
8
|
-
[77.1274, 1, { invariantPrecisionString: "8e+1", invariantCoefficientString: "8", exponent: 1, noOfCoefficientDecimals: 0 }],
|
|
9
|
-
[77.1274, 2, { invariantPrecisionString: "77", invariantCoefficientString: "77", exponent: 0, noOfCoefficientDecimals: 0 }],
|
|
10
|
-
[77.1274, 4, { invariantPrecisionString: "77.13", invariantCoefficientString: "77.13", exponent: 0, noOfCoefficientDecimals: 2 }],
|
|
11
|
-
[77.1274, 5, { invariantPrecisionString: "77.127", invariantCoefficientString: "77.127", exponent: 0, noOfCoefficientDecimals: 3 }],
|
|
12
|
-
[77.1274, 6, { invariantPrecisionString: "77.1274", invariantCoefficientString: "77.1274", exponent: 0, noOfCoefficientDecimals: 4 }],
|
|
13
|
-
[77.1274, 7, { invariantPrecisionString: "77.12740", invariantCoefficientString: "77.12740", exponent: 0, noOfCoefficientDecimals: 5 }],
|
|
14
|
-
[77.1274, 15, { invariantPrecisionString: "77.1274000000000", invariantCoefficientString: "77.1274000000000", exponent: 0, noOfCoefficientDecimals: 13 }],
|
|
15
|
-
[77.1274, 16, { invariantPrecisionString: "77.12739999999999", invariantCoefficientString: "77.12739999999999", exponent: 0, noOfCoefficientDecimals: 14 }],
|
|
16
|
-
[77.1274, 17, { invariantPrecisionString: "77.127399999999994", invariantCoefficientString: "77.127399999999994", exponent: 0, noOfCoefficientDecimals: 15 }],
|
|
17
|
-
[77.1274, 18, { invariantPrecisionString: "77.1273999999999944", invariantCoefficientString: "77.1273999999999944", exponent: 0, noOfCoefficientDecimals: 16 }],
|
|
18
|
-
[77.1274, 19, { invariantPrecisionString: "77.12739999999999441", invariantCoefficientString: "77.12739999999999441", exponent: 0, noOfCoefficientDecimals: 17 }],
|
|
19
|
-
[77.1274, 20, { invariantPrecisionString: "77.127399999999994407", invariantCoefficientString: "77.127399999999994407", exponent: 0, noOfCoefficientDecimals: 18 }],
|
|
20
|
-
[77.1274, 21, { invariantPrecisionString: "77.1273999999999944066", invariantCoefficientString: "77.1273999999999944066", exponent: 0, noOfCoefficientDecimals: 19 }],
|
|
21
|
-
[0.00000123, 2, { invariantPrecisionString: "0.0000012", invariantCoefficientString: "0.0000012", exponent: 0, noOfCoefficientDecimals: 7 }],
|
|
22
|
-
[0.000000123, 2, { invariantPrecisionString: "1.2e-7", invariantCoefficientString: "1.2", exponent: -7, noOfCoefficientDecimals: 1 }],
|
|
23
|
-
[0.00000123, 21, { invariantPrecisionString: "0.00000123000000000000008198", invariantCoefficientString: "0.00000123000000000000008198", exponent: 0, noOfCoefficientDecimals: 26 }],
|
|
24
|
-
[123456, 3, { invariantPrecisionString: "1.23e+5", invariantCoefficientString: "1.23", exponent: 5, noOfCoefficientDecimals: 2 }],
|
|
25
|
-
[Number.MAX_VALUE, 3, { invariantPrecisionString: "1.80e+308", invariantCoefficientString: "1.80", exponent: 308, noOfCoefficientDecimals: 2 }],
|
|
26
|
-
[123456, 15, { invariantPrecisionString: "123456.000000000", invariantCoefficientString: "123456.000000000", exponent: 0, noOfCoefficientDecimals: 9 }],
|
|
27
|
-
[0, 5, { invariantPrecisionString: "0.00000", invariantCoefficientString: "0.00000", exponent: 0, noOfCoefficientDecimals: 5 }],
|
|
28
|
-
[0.000001, 1, { invariantPrecisionString: "0.000001", invariantCoefficientString: "0.000001", exponent: 0, noOfCoefficientDecimals: 6 }],
|
|
29
|
-
[0.000001, 10, { invariantPrecisionString: "0.000001000000000", invariantCoefficientString: "0.000001000000000", exponent: 0, noOfCoefficientDecimals: 15 }],
|
|
30
|
-
[0.0000001, 10, { invariantPrecisionString: "1.000000000e-7", invariantCoefficientString: "1.000000000", exponent: -7, noOfCoefficientDecimals: 9 }],
|
|
31
|
-
[0.00000000001, 1, { invariantPrecisionString: "1e-11", invariantCoefficientString: "1", exponent: -11, noOfCoefficientDecimals: 0 }],
|
|
32
|
-
[0.00000000001, 2, { invariantPrecisionString: "1.0e-11", invariantCoefficientString: "1.0", exponent: -11, noOfCoefficientDecimals: 1 }],
|
|
33
|
-
[55, 2, { invariantPrecisionString: "55", invariantCoefficientString: "55", exponent: 0, noOfCoefficientDecimals: 0 }],
|
|
34
|
-
[55, 1, { invariantPrecisionString: "6e+1", invariantCoefficientString: "6", exponent: 1, noOfCoefficientDecimals: 0 }],
|
|
35
|
-
[-55, 2, { invariantPrecisionString: "-55", invariantCoefficientString: "-55", exponent: 0, noOfCoefficientDecimals: 0 }],
|
|
36
|
-
[-55, 1, { invariantPrecisionString: "-6e+1", invariantCoefficientString: "-6", exponent: 1, noOfCoefficientDecimals: 0 }],
|
|
37
|
-
[9.59, 1, { invariantPrecisionString: "1e+1", invariantCoefficientString: "1", exponent: 1, noOfCoefficientDecimals: 0 }],
|
|
38
|
-
[9.59, 2, { invariantPrecisionString: "10", invariantCoefficientString: "10", exponent: 0, noOfCoefficientDecimals: 0 }],
|
|
39
|
-
[9.95, 2, { invariantPrecisionString: "10", invariantCoefficientString: "10", exponent: 0, noOfCoefficientDecimals: 0 }],
|
|
40
|
-
[9.96, 2, { invariantPrecisionString: "10", invariantCoefficientString: "10", exponent: 0, noOfCoefficientDecimals: 0 }],
|
|
41
|
-
[-555555555555555555555, 1, { invariantPrecisionString: "-6e+20", invariantCoefficientString: "-6", exponent: 20, noOfCoefficientDecimals: 0 }],
|
|
42
|
-
[-5555555555555555555555, 1, { invariantPrecisionString: "-6e+21", invariantCoefficientString: "-6", exponent: 21, noOfCoefficientDecimals: 0 }],
|
|
43
|
-
[18014398509482012, 21, { invariantPrecisionString: "18014398509482012.0000", invariantCoefficientString: "18014398509482012.0000", exponent: 0, noOfCoefficientDecimals: 4 }],
|
|
44
|
-
[180143985094820121234, 21, { invariantPrecisionString: "180143985094820134912", invariantCoefficientString: "180143985094820134912", exponent: 0, noOfCoefficientDecimals: 0 }],
|
|
45
|
-
[0.1, 21, { invariantPrecisionString: "0.100000000000000005551", invariantCoefficientString: "0.100000000000000005551", exponent: 0, noOfCoefficientDecimals: 21 }],
|
|
46
|
-
[Number.EPSILON, 2, { invariantPrecisionString: "2.2e-16", invariantCoefficientString: "2.2", exponent: -16, noOfCoefficientDecimals: 1 }],
|
|
47
|
-
[Number.EPSILON, 21, { invariantPrecisionString: "2.22044604925031308085e-16", invariantCoefficientString: "2.22044604925031308085", exponent: -16, noOfCoefficientDecimals: 20 }],
|
|
48
|
-
[Number.NaN, 3, { invariantPrecisionString: "NaN", invariantCoefficientString: "NaN", exponent: 0, noOfCoefficientDecimals: 0 }],
|
|
49
|
-
[Number.POSITIVE_INFINITY, 3, { invariantPrecisionString: "Infinity", invariantCoefficientString: "Infinity", exponent: 0, noOfCoefficientDecimals: 0 }],
|
|
50
|
-
[Number.NEGATIVE_INFINITY, 3, { invariantPrecisionString: "-Infinity", invariantCoefficientString: "-Infinity", exponent: 0, noOfCoefficientDecimals: 0 }],
|
|
51
|
-
[164735280, 7, { invariantPrecisionString: "1.647353e+8", invariantCoefficientString: "1.647353", exponent: 8, noOfCoefficientDecimals: 6 }],
|
|
52
|
-
];
|
|
53
|
-
for (const [value, precision, expectedParts] of data) {
|
|
54
|
-
expect(NumberFormatter.toPrecision(value, precision)).to.equal(expectedParts.invariantPrecisionString);
|
|
55
|
-
const localePrecisionString = expectedParts.invariantPrecisionString.replace(invariantDecimalSeparator, localeDecimalSeparator);
|
|
56
|
-
expect(NumberFormatter.toLocalePrecision(value, precision)).to.equal(localePrecisionString);
|
|
57
|
-
const parts = NumberFormatter.getParts(value, precision);
|
|
58
|
-
expect(parts.invariantPrecisionString).to.equal(expectedParts.invariantPrecisionString);
|
|
59
|
-
expect(parts.invariantCoefficientString).to.equal(expectedParts.invariantCoefficientString);
|
|
60
|
-
expect(parts.exponent).to.equal(expectedParts.exponent);
|
|
61
|
-
expect(parts.noOfCoefficientDecimals).to.equal(expectedParts.noOfCoefficientDecimals);
|
|
62
|
-
}
|
|
63
|
-
});
|
|
64
|
-
});
|
|
65
|
-
describe("toFixed", () => {
|
|
66
|
-
it("should format correctly", () => {
|
|
67
|
-
const data = [
|
|
68
|
-
[0, 0, "0"],
|
|
69
|
-
[0.0, 1, "0.0"],
|
|
70
|
-
[10, 1, "10"],
|
|
71
|
-
[10, 2, "10"],
|
|
72
|
-
[10, 3, "10.0"],
|
|
73
|
-
[10, 4, "10.00"],
|
|
74
|
-
[10.2, 1, "10"],
|
|
75
|
-
[10.2, 2, "10"],
|
|
76
|
-
[10.2, 3, "10.2"],
|
|
77
|
-
[10.2, 4, "10.20"],
|
|
78
|
-
[217887000000, 6, "217887000000"],
|
|
79
|
-
];
|
|
80
|
-
for (const [value, precision, invariantExpected] of data) {
|
|
81
|
-
expect(NumberFormatter.toFixed(value, precision)).to.equal(invariantExpected);
|
|
82
|
-
const localeExpected = invariantExpected.replace(invariantDecimalSeparator, localeDecimalSeparator);
|
|
83
|
-
expect(NumberFormatter.toLocaleFixed(value, precision)).to.equal(localeExpected);
|
|
84
|
-
}
|
|
85
|
-
});
|
|
86
|
-
});
|
|
87
|
-
describe("toLocaleFixed", () => {
|
|
88
|
-
it("should format correctly", () => {
|
|
89
|
-
expect(NumberFormatter.toLocaleFixed(400, 2)).to.equal("400");
|
|
90
|
-
});
|
|
91
|
-
it("should format correctly with max decimals", () => {
|
|
92
|
-
expect(NumberFormatter.toLocaleFixed(35.123456, 8, 2)).to.equal("35.12");
|
|
93
|
-
expect(NumberFormatter.toLocaleFixed(35.129999, 8, 2)).to.equal("35.13");
|
|
94
|
-
});
|
|
95
|
-
});
|
|
1
|
+
import { NumberFormatter, invariantDecimalSeparator, localeDecimalSeparator } from './number-formatter';
|
|
2
|
+
import { expect } from 'chai';
|
|
3
|
+
describe("toParts and toPrecision", () => {
|
|
4
|
+
it("should format correctly", () => {
|
|
5
|
+
const data = [
|
|
6
|
+
[0, 0, { invariantPrecisionString: "0", invariantCoefficientString: "0", exponent: 0, noOfCoefficientDecimals: 0 }],
|
|
7
|
+
[0.0, 1, { invariantPrecisionString: "0.0", invariantCoefficientString: "0.0", exponent: 0, noOfCoefficientDecimals: 1 }],
|
|
8
|
+
[77.1274, 1, { invariantPrecisionString: "8e+1", invariantCoefficientString: "8", exponent: 1, noOfCoefficientDecimals: 0 }],
|
|
9
|
+
[77.1274, 2, { invariantPrecisionString: "77", invariantCoefficientString: "77", exponent: 0, noOfCoefficientDecimals: 0 }],
|
|
10
|
+
[77.1274, 4, { invariantPrecisionString: "77.13", invariantCoefficientString: "77.13", exponent: 0, noOfCoefficientDecimals: 2 }],
|
|
11
|
+
[77.1274, 5, { invariantPrecisionString: "77.127", invariantCoefficientString: "77.127", exponent: 0, noOfCoefficientDecimals: 3 }],
|
|
12
|
+
[77.1274, 6, { invariantPrecisionString: "77.1274", invariantCoefficientString: "77.1274", exponent: 0, noOfCoefficientDecimals: 4 }],
|
|
13
|
+
[77.1274, 7, { invariantPrecisionString: "77.12740", invariantCoefficientString: "77.12740", exponent: 0, noOfCoefficientDecimals: 5 }],
|
|
14
|
+
[77.1274, 15, { invariantPrecisionString: "77.1274000000000", invariantCoefficientString: "77.1274000000000", exponent: 0, noOfCoefficientDecimals: 13 }],
|
|
15
|
+
[77.1274, 16, { invariantPrecisionString: "77.12739999999999", invariantCoefficientString: "77.12739999999999", exponent: 0, noOfCoefficientDecimals: 14 }],
|
|
16
|
+
[77.1274, 17, { invariantPrecisionString: "77.127399999999994", invariantCoefficientString: "77.127399999999994", exponent: 0, noOfCoefficientDecimals: 15 }],
|
|
17
|
+
[77.1274, 18, { invariantPrecisionString: "77.1273999999999944", invariantCoefficientString: "77.1273999999999944", exponent: 0, noOfCoefficientDecimals: 16 }],
|
|
18
|
+
[77.1274, 19, { invariantPrecisionString: "77.12739999999999441", invariantCoefficientString: "77.12739999999999441", exponent: 0, noOfCoefficientDecimals: 17 }],
|
|
19
|
+
[77.1274, 20, { invariantPrecisionString: "77.127399999999994407", invariantCoefficientString: "77.127399999999994407", exponent: 0, noOfCoefficientDecimals: 18 }],
|
|
20
|
+
[77.1274, 21, { invariantPrecisionString: "77.1273999999999944066", invariantCoefficientString: "77.1273999999999944066", exponent: 0, noOfCoefficientDecimals: 19 }],
|
|
21
|
+
[0.00000123, 2, { invariantPrecisionString: "0.0000012", invariantCoefficientString: "0.0000012", exponent: 0, noOfCoefficientDecimals: 7 }],
|
|
22
|
+
[0.000000123, 2, { invariantPrecisionString: "1.2e-7", invariantCoefficientString: "1.2", exponent: -7, noOfCoefficientDecimals: 1 }],
|
|
23
|
+
[0.00000123, 21, { invariantPrecisionString: "0.00000123000000000000008198", invariantCoefficientString: "0.00000123000000000000008198", exponent: 0, noOfCoefficientDecimals: 26 }],
|
|
24
|
+
[123456, 3, { invariantPrecisionString: "1.23e+5", invariantCoefficientString: "1.23", exponent: 5, noOfCoefficientDecimals: 2 }],
|
|
25
|
+
[Number.MAX_VALUE, 3, { invariantPrecisionString: "1.80e+308", invariantCoefficientString: "1.80", exponent: 308, noOfCoefficientDecimals: 2 }],
|
|
26
|
+
[123456, 15, { invariantPrecisionString: "123456.000000000", invariantCoefficientString: "123456.000000000", exponent: 0, noOfCoefficientDecimals: 9 }],
|
|
27
|
+
[0, 5, { invariantPrecisionString: "0.00000", invariantCoefficientString: "0.00000", exponent: 0, noOfCoefficientDecimals: 5 }],
|
|
28
|
+
[0.000001, 1, { invariantPrecisionString: "0.000001", invariantCoefficientString: "0.000001", exponent: 0, noOfCoefficientDecimals: 6 }],
|
|
29
|
+
[0.000001, 10, { invariantPrecisionString: "0.000001000000000", invariantCoefficientString: "0.000001000000000", exponent: 0, noOfCoefficientDecimals: 15 }],
|
|
30
|
+
[0.0000001, 10, { invariantPrecisionString: "1.000000000e-7", invariantCoefficientString: "1.000000000", exponent: -7, noOfCoefficientDecimals: 9 }],
|
|
31
|
+
[0.00000000001, 1, { invariantPrecisionString: "1e-11", invariantCoefficientString: "1", exponent: -11, noOfCoefficientDecimals: 0 }],
|
|
32
|
+
[0.00000000001, 2, { invariantPrecisionString: "1.0e-11", invariantCoefficientString: "1.0", exponent: -11, noOfCoefficientDecimals: 1 }],
|
|
33
|
+
[55, 2, { invariantPrecisionString: "55", invariantCoefficientString: "55", exponent: 0, noOfCoefficientDecimals: 0 }],
|
|
34
|
+
[55, 1, { invariantPrecisionString: "6e+1", invariantCoefficientString: "6", exponent: 1, noOfCoefficientDecimals: 0 }],
|
|
35
|
+
[-55, 2, { invariantPrecisionString: "-55", invariantCoefficientString: "-55", exponent: 0, noOfCoefficientDecimals: 0 }],
|
|
36
|
+
[-55, 1, { invariantPrecisionString: "-6e+1", invariantCoefficientString: "-6", exponent: 1, noOfCoefficientDecimals: 0 }],
|
|
37
|
+
[9.59, 1, { invariantPrecisionString: "1e+1", invariantCoefficientString: "1", exponent: 1, noOfCoefficientDecimals: 0 }],
|
|
38
|
+
[9.59, 2, { invariantPrecisionString: "10", invariantCoefficientString: "10", exponent: 0, noOfCoefficientDecimals: 0 }],
|
|
39
|
+
[9.95, 2, { invariantPrecisionString: "10", invariantCoefficientString: "10", exponent: 0, noOfCoefficientDecimals: 0 }],
|
|
40
|
+
[9.96, 2, { invariantPrecisionString: "10", invariantCoefficientString: "10", exponent: 0, noOfCoefficientDecimals: 0 }],
|
|
41
|
+
[-555555555555555555555, 1, { invariantPrecisionString: "-6e+20", invariantCoefficientString: "-6", exponent: 20, noOfCoefficientDecimals: 0 }],
|
|
42
|
+
[-5555555555555555555555, 1, { invariantPrecisionString: "-6e+21", invariantCoefficientString: "-6", exponent: 21, noOfCoefficientDecimals: 0 }],
|
|
43
|
+
[18014398509482012, 21, { invariantPrecisionString: "18014398509482012.0000", invariantCoefficientString: "18014398509482012.0000", exponent: 0, noOfCoefficientDecimals: 4 }],
|
|
44
|
+
[180143985094820121234, 21, { invariantPrecisionString: "180143985094820134912", invariantCoefficientString: "180143985094820134912", exponent: 0, noOfCoefficientDecimals: 0 }],
|
|
45
|
+
[0.1, 21, { invariantPrecisionString: "0.100000000000000005551", invariantCoefficientString: "0.100000000000000005551", exponent: 0, noOfCoefficientDecimals: 21 }],
|
|
46
|
+
[Number.EPSILON, 2, { invariantPrecisionString: "2.2e-16", invariantCoefficientString: "2.2", exponent: -16, noOfCoefficientDecimals: 1 }],
|
|
47
|
+
[Number.EPSILON, 21, { invariantPrecisionString: "2.22044604925031308085e-16", invariantCoefficientString: "2.22044604925031308085", exponent: -16, noOfCoefficientDecimals: 20 }],
|
|
48
|
+
[Number.NaN, 3, { invariantPrecisionString: "NaN", invariantCoefficientString: "NaN", exponent: 0, noOfCoefficientDecimals: 0 }],
|
|
49
|
+
[Number.POSITIVE_INFINITY, 3, { invariantPrecisionString: "Infinity", invariantCoefficientString: "Infinity", exponent: 0, noOfCoefficientDecimals: 0 }],
|
|
50
|
+
[Number.NEGATIVE_INFINITY, 3, { invariantPrecisionString: "-Infinity", invariantCoefficientString: "-Infinity", exponent: 0, noOfCoefficientDecimals: 0 }],
|
|
51
|
+
[164735280, 7, { invariantPrecisionString: "1.647353e+8", invariantCoefficientString: "1.647353", exponent: 8, noOfCoefficientDecimals: 6 }],
|
|
52
|
+
];
|
|
53
|
+
for (const [value, precision, expectedParts] of data) {
|
|
54
|
+
expect(NumberFormatter.toPrecision(value, precision)).to.equal(expectedParts.invariantPrecisionString);
|
|
55
|
+
const localePrecisionString = expectedParts.invariantPrecisionString.replace(invariantDecimalSeparator, localeDecimalSeparator);
|
|
56
|
+
expect(NumberFormatter.toLocalePrecision(value, precision)).to.equal(localePrecisionString);
|
|
57
|
+
const parts = NumberFormatter.getParts(value, precision);
|
|
58
|
+
expect(parts.invariantPrecisionString).to.equal(expectedParts.invariantPrecisionString);
|
|
59
|
+
expect(parts.invariantCoefficientString).to.equal(expectedParts.invariantCoefficientString);
|
|
60
|
+
expect(parts.exponent).to.equal(expectedParts.exponent);
|
|
61
|
+
expect(parts.noOfCoefficientDecimals).to.equal(expectedParts.noOfCoefficientDecimals);
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
});
|
|
65
|
+
describe("toFixed", () => {
|
|
66
|
+
it("should format correctly", () => {
|
|
67
|
+
const data = [
|
|
68
|
+
[0, 0, "0"],
|
|
69
|
+
[0.0, 1, "0.0"],
|
|
70
|
+
[10, 1, "10"],
|
|
71
|
+
[10, 2, "10"],
|
|
72
|
+
[10, 3, "10.0"],
|
|
73
|
+
[10, 4, "10.00"],
|
|
74
|
+
[10.2, 1, "10"],
|
|
75
|
+
[10.2, 2, "10"],
|
|
76
|
+
[10.2, 3, "10.2"],
|
|
77
|
+
[10.2, 4, "10.20"],
|
|
78
|
+
[217887000000, 6, "217887000000"],
|
|
79
|
+
];
|
|
80
|
+
for (const [value, precision, invariantExpected] of data) {
|
|
81
|
+
expect(NumberFormatter.toFixed(value, precision)).to.equal(invariantExpected);
|
|
82
|
+
const localeExpected = invariantExpected.replace(invariantDecimalSeparator, localeDecimalSeparator);
|
|
83
|
+
expect(NumberFormatter.toLocaleFixed(value, precision)).to.equal(localeExpected);
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
});
|
|
87
|
+
describe("toLocaleFixed", () => {
|
|
88
|
+
it("should format correctly", () => {
|
|
89
|
+
expect(NumberFormatter.toLocaleFixed(400, 2)).to.equal("400");
|
|
90
|
+
});
|
|
91
|
+
it("should format correctly with max decimals", () => {
|
|
92
|
+
expect(NumberFormatter.toLocaleFixed(35.123456, 8, 2)).to.equal("35.12");
|
|
93
|
+
expect(NumberFormatter.toLocaleFixed(35.129999, 8, 2)).to.equal("35.13");
|
|
94
|
+
});
|
|
95
|
+
});
|
|
96
96
|
//# sourceMappingURL=number-formatter.spec.js.map
|