@utiliread/primitives 1.0.0-32 → 1.0.0-36
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/precision-number.js +12 -0
- package/dist/cjs/precision-number.js.map +1 -1
- package/dist/esm/precision-number.d.ts +3 -0
- package/dist/esm/precision-number.js +13 -1
- package/dist/esm/precision-number.js.map +1 -1
- package/dist/esm/register-id.d.ts +1 -1
- package/package.json +3 -3
- package/src/aggregate-id.ts +9 -9
- package/src/consumption-type-id.ts +8 -8
- package/src/index.ts +30 -30
- package/src/measure-id.ts +14 -14
- package/src/number-formatter.spec.ts +93 -93
- package/src/number-formatter.ts +78 -78
- package/src/precision-number.ts +17 -2
- package/src/register-convention.spec.ts +42 -42
- package/src/register-convention.ts +61 -61
- package/src/register-definition.ts +17 -17
- package/src/register-id.ts +197 -191
- package/src/register-library.spec.ts +41 -41
- package/src/register-library.ts +57 -57
- package/src/register-pack.ts +5 -5
- package/src/register-utils.ts +6 -6
- package/src/step.spec.ts +29 -29
- package/src/step.ts +37 -37
- package/src/time-or-rule.ts +17 -17
- package/src/unit-id.ts +100 -100
- package/coverage/Chrome 90.0.4430.85 (Windows 10)/html/base.css +0 -224
- package/coverage/Chrome 90.0.4430.85 (Windows 10)/html/block-navigation.js +0 -79
- package/coverage/Chrome 90.0.4430.85 (Windows 10)/html/favicon.png +0 -0
- package/coverage/Chrome 90.0.4430.85 (Windows 10)/html/index.html +0 -96
- package/coverage/Chrome 90.0.4430.85 (Windows 10)/html/prettify.css +0 -1
- package/coverage/Chrome 90.0.4430.85 (Windows 10)/html/prettify.js +0 -2
- package/coverage/Chrome 90.0.4430.85 (Windows 10)/html/sort-arrow-sprite.png +0 -0
- package/coverage/Chrome 90.0.4430.85 (Windows 10)/html/sorter.js +0 -170
- package/coverage/Chrome Headless 90.0.4430.85 (Windows 10)/html/base.css +0 -224
- package/coverage/Chrome Headless 90.0.4430.85 (Windows 10)/html/block-navigation.js +0 -79
- package/coverage/Chrome Headless 90.0.4430.85 (Windows 10)/html/favicon.png +0 -0
- package/coverage/Chrome Headless 90.0.4430.85 (Windows 10)/html/index.html +0 -96
- package/coverage/Chrome Headless 90.0.4430.85 (Windows 10)/html/prettify.css +0 -1
- package/coverage/Chrome Headless 90.0.4430.85 (Windows 10)/html/prettify.js +0 -2
- package/coverage/Chrome Headless 90.0.4430.85 (Windows 10)/html/sort-arrow-sprite.png +0 -0
- package/coverage/Chrome Headless 90.0.4430.85 (Windows 10)/html/sorter.js +0 -170
- package/coverage/Chrome Headless 90.0.4430.93 (Windows 10)/html/base.css +0 -224
- package/coverage/Chrome Headless 90.0.4430.93 (Windows 10)/html/block-navigation.js +0 -79
- package/coverage/Chrome Headless 90.0.4430.93 (Windows 10)/html/favicon.png +0 -0
- package/coverage/Chrome Headless 90.0.4430.93 (Windows 10)/html/index.html +0 -96
- package/coverage/Chrome Headless 90.0.4430.93 (Windows 10)/html/prettify.css +0 -1
- package/coverage/Chrome Headless 90.0.4430.93 (Windows 10)/html/prettify.js +0 -2
- package/coverage/Chrome Headless 90.0.4430.93 (Windows 10)/html/sort-arrow-sprite.png +0 -0
- package/coverage/Chrome Headless 90.0.4430.93 (Windows 10)/html/sorter.js +0 -170
- package/dist/cjs/register-converter.js +0 -31
- package/dist/cjs/register-converter.js.map +0 -1
- package/dist/cjs/register-converter.spec.js +0 -29
- package/dist/cjs/register-converter.spec.js.map +0 -1
- package/dist/esm/register-converter.d.ts +0 -9
- package/dist/esm/register-converter.js +0 -28
- package/dist/esm/register-converter.js.map +0 -1
- package/dist/esm/register-converter.spec.d.ts +0 -1
- package/dist/esm/register-converter.spec.js +0 -27
- package/dist/esm/register-converter.spec.js.map +0 -1
|
@@ -1,15 +1,27 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.PrecisionNumber = void 0;
|
|
4
|
+
var _1 = require(".");
|
|
4
5
|
var number_formatter_1 = require("./number-formatter");
|
|
5
6
|
var PrecisionNumber = /** @class */ (function () {
|
|
6
7
|
function PrecisionNumber(value, precision) {
|
|
7
8
|
this.value = value;
|
|
8
9
|
this.precision = precision;
|
|
9
10
|
}
|
|
11
|
+
PrecisionNumber.parse = function (input) {
|
|
12
|
+
var normalized = input.trim().replace(",", ".").replace(number_formatter_1.localeDecimalSeparator, _1.invariantDecimalSeparator);
|
|
13
|
+
var precision = normalized.replace(".", "").replace("+", "").replace("-", "").length;
|
|
14
|
+
return new PrecisionNumber(parseFloat(normalized), precision);
|
|
15
|
+
};
|
|
10
16
|
PrecisionNumber.prototype.toString = function () {
|
|
11
17
|
return this.value.toString();
|
|
12
18
|
};
|
|
19
|
+
PrecisionNumber.prototype.toFixed = function () {
|
|
20
|
+
return number_formatter_1.NumberFormatter.toFixed(this.value, this.precision);
|
|
21
|
+
};
|
|
22
|
+
PrecisionNumber.prototype.toPrecision = function () {
|
|
23
|
+
return number_formatter_1.NumberFormatter.toPrecision(this.value, this.precision);
|
|
24
|
+
};
|
|
13
25
|
PrecisionNumber.prototype.toLocaleFixed = function () {
|
|
14
26
|
return number_formatter_1.NumberFormatter.toLocaleFixed(this.value, this.precision);
|
|
15
27
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"precision-number.js","sourceRoot":"","sources":["../../src/precision-number.ts"],"names":[],"mappings":";;;AAAA,
|
|
1
|
+
{"version":3,"file":"precision-number.js","sourceRoot":"","sources":["../../src/precision-number.ts"],"names":[],"mappings":";;;AAAA,sBAA8C;AAC9C,uDAA6E;AAE7E;IACI,yBAAmB,KAAa,EAAS,SAAiB;QAAvC,UAAK,GAAL,KAAK,CAAQ;QAAS,cAAS,GAAT,SAAS,CAAQ;IAC1D,CAAC;IAEM,qBAAK,GAAZ,UAAa,KAAa;QACtB,IAAM,UAAU,GAAG,KAAK,CAAC,IAAI,EAAE,CAAC,OAAO,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC,OAAO,CAAC,yCAAsB,EAAE,4BAAyB,CAAC,CAAC;QAC7G,IAAM,SAAS,GAAG,UAAU,CAAC,OAAO,CAAC,GAAG,EAAE,EAAE,CAAC,CAAC,OAAO,CAAC,GAAG,EAAE,EAAE,CAAC,CAAC,OAAO,CAAC,GAAG,EAAE,EAAE,CAAC,CAAC,MAAM,CAAC;QACvF,OAAO,IAAI,eAAe,CAAC,UAAU,CAAC,UAAU,CAAC,EAAE,SAAS,CAAC,CAAC;IAClE,CAAC;IAED,kCAAQ,GAAR;QACI,OAAO,IAAI,CAAC,KAAK,CAAC,QAAQ,EAAE,CAAC;IACjC,CAAC;IAED,iCAAO,GAAP;QACI,OAAO,kCAAe,CAAC,OAAO,CAAC,IAAI,CAAC,KAAK,EAAE,IAAI,CAAC,SAAS,CAAC,CAAC;IAC/D,CAAC;IAED,qCAAW,GAAX;QACI,OAAO,kCAAe,CAAC,WAAW,CAAC,IAAI,CAAC,KAAK,EAAE,IAAI,CAAC,SAAS,CAAC,CAAC;IACnE,CAAC;IAED,uCAAa,GAAb;QACI,OAAO,kCAAe,CAAC,aAAa,CAAC,IAAI,CAAC,KAAK,EAAE,IAAI,CAAC,SAAS,CAAC,CAAC;IACrE,CAAC;IAED,2CAAiB,GAAjB;QACI,OAAO,kCAAe,CAAC,iBAAiB,CAAC,IAAI,CAAC,KAAK,EAAE,IAAI,CAAC,SAAS,CAAC,CAAC;IACzE,CAAC;IACL,sBAAC;AAAD,CAAC,AA7BD,IA6BC;AA7BY,0CAAe"}
|
|
@@ -2,7 +2,10 @@ export declare class PrecisionNumber {
|
|
|
2
2
|
value: number;
|
|
3
3
|
precision: number;
|
|
4
4
|
constructor(value: number, precision: number);
|
|
5
|
+
static parse(input: string): PrecisionNumber;
|
|
5
6
|
toString(): string;
|
|
7
|
+
toFixed(): string;
|
|
8
|
+
toPrecision(): string;
|
|
6
9
|
toLocaleFixed(): string;
|
|
7
10
|
toLocalePrecision(): string;
|
|
8
11
|
}
|
|
@@ -1,12 +1,24 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { invariantDecimalSeparator } from ".";
|
|
2
|
+
import { localeDecimalSeparator, NumberFormatter } from "./number-formatter";
|
|
2
3
|
var PrecisionNumber = /** @class */ (function () {
|
|
3
4
|
function PrecisionNumber(value, precision) {
|
|
4
5
|
this.value = value;
|
|
5
6
|
this.precision = precision;
|
|
6
7
|
}
|
|
8
|
+
PrecisionNumber.parse = function (input) {
|
|
9
|
+
var normalized = input.trim().replace(",", ".").replace(localeDecimalSeparator, invariantDecimalSeparator);
|
|
10
|
+
var precision = normalized.replace(".", "").replace("+", "").replace("-", "").length;
|
|
11
|
+
return new PrecisionNumber(parseFloat(normalized), precision);
|
|
12
|
+
};
|
|
7
13
|
PrecisionNumber.prototype.toString = function () {
|
|
8
14
|
return this.value.toString();
|
|
9
15
|
};
|
|
16
|
+
PrecisionNumber.prototype.toFixed = function () {
|
|
17
|
+
return NumberFormatter.toFixed(this.value, this.precision);
|
|
18
|
+
};
|
|
19
|
+
PrecisionNumber.prototype.toPrecision = function () {
|
|
20
|
+
return NumberFormatter.toPrecision(this.value, this.precision);
|
|
21
|
+
};
|
|
10
22
|
PrecisionNumber.prototype.toLocaleFixed = function () {
|
|
11
23
|
return NumberFormatter.toLocaleFixed(this.value, this.precision);
|
|
12
24
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"precision-number.js","sourceRoot":"","sources":["../../src/precision-number.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAC;
|
|
1
|
+
{"version":3,"file":"precision-number.js","sourceRoot":"","sources":["../../src/precision-number.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,yBAAyB,EAAE,MAAM,GAAG,CAAC;AAC9C,OAAO,EAAE,sBAAsB,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAC;AAE7E;IACI,yBAAmB,KAAa,EAAS,SAAiB;QAAvC,UAAK,GAAL,KAAK,CAAQ;QAAS,cAAS,GAAT,SAAS,CAAQ;IAC1D,CAAC;IAEM,qBAAK,GAAZ,UAAa,KAAa;QACtB,IAAM,UAAU,GAAG,KAAK,CAAC,IAAI,EAAE,CAAC,OAAO,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC,OAAO,CAAC,sBAAsB,EAAE,yBAAyB,CAAC,CAAC;QAC7G,IAAM,SAAS,GAAG,UAAU,CAAC,OAAO,CAAC,GAAG,EAAE,EAAE,CAAC,CAAC,OAAO,CAAC,GAAG,EAAE,EAAE,CAAC,CAAC,OAAO,CAAC,GAAG,EAAE,EAAE,CAAC,CAAC,MAAM,CAAC;QACvF,OAAO,IAAI,eAAe,CAAC,UAAU,CAAC,UAAU,CAAC,EAAE,SAAS,CAAC,CAAC;IAClE,CAAC;IAED,kCAAQ,GAAR;QACI,OAAO,IAAI,CAAC,KAAK,CAAC,QAAQ,EAAE,CAAC;IACjC,CAAC;IAED,iCAAO,GAAP;QACI,OAAO,eAAe,CAAC,OAAO,CAAC,IAAI,CAAC,KAAK,EAAE,IAAI,CAAC,SAAS,CAAC,CAAC;IAC/D,CAAC;IAED,qCAAW,GAAX;QACI,OAAO,eAAe,CAAC,WAAW,CAAC,IAAI,CAAC,KAAK,EAAE,IAAI,CAAC,SAAS,CAAC,CAAC;IACnE,CAAC;IAED,uCAAa,GAAb;QACI,OAAO,eAAe,CAAC,aAAa,CAAC,IAAI,CAAC,KAAK,EAAE,IAAI,CAAC,SAAS,CAAC,CAAC;IACrE,CAAC;IAED,2CAAiB,GAAjB;QACI,OAAO,eAAe,CAAC,iBAAiB,CAAC,IAAI,CAAC,KAAK,EAAE,IAAI,CAAC,SAAS,CAAC,CAAC;IACzE,CAAC;IACL,sBAAC;AAAD,CAAC,AA7BD,IA6BC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare type RegisterId = "none" | "heatEnergy" | "diffHeatEnergy" | "heatEnergyVolumeInInlet" | "diffHeatEnergyVolumeInInlet" | "heatEnergyVolumeInOutlet" | "diffHeatEnergyVolumeInOutlet" | "coolingEnergy" | "diffCoolingEnergy" | "coolingEnergyVolumeInInlet" | "diffCoolingEnergyVolumeInInlet" | "coolingEnergyVolumeInOutlet" | "diffCoolingEnergyVolumeInOutlet" | "electricalEnergy" | "diffElectricalEnergy" | "reactiveEnergy" | "diffReactiveEnergy" | "volume" | "diffVolume" | "inletVolume" | "diffInletVolume" | "outletVolume" | "diffOutletVolume" | "pulseInputAVolume" | "diffPulseInputAVolume" | "pulseInputBVolume" | "diffPulseInputBVolume" | "mass" | "diffMass" | "inletMass" | "diffInletMass" | "outletMass" | "diffOutletMass" | "power" | "maxPower" | "minPower" | "avgPower" | "inletPower" | "maxInletPower" | "minInletPower" | "avgInletPower" | "outletPower" | "maxOutletPower" | "minOutletPower" | "avgOutletPower" | "volumeFlow" | "maxVolumeFlow" | "minVolumeFlow" | "avgVolumeFlow" | "inletVolumeFlow" | "maxInletVolumeFlow" | "minInletVolumeFlow" | "avgInletVolumeFlow" | "outletVolumeFlow" | "maxOutletVolumeFlow" | "minOutletVolumeFlow" | "avgOutletVolumeFlow" | "massFlow" | "maxMassFlow" | "minMassFlow" | "avgMassFlow" | "inletMassFlow" | "maxInletMassFlow" | "minInletMassFlow" | "avgInletMassFlow" | "outletMassFlow" | "maxOutletMassFlow" | "minOutletMassFlow" | "avgOutletMassFlow" | "flowTemperature" | "maxFlowTemperature" | "minFlowTemperature" | "avgFlowTemperature" | "inletFlowTemperature" | "maxInletFlowTemperature" | "minInletFlowTemperature" | "avgInletFlowTemperature" | "outletFlowTemperature" | "maxOutletFlowTemperature" | "minOutletFlowTemperature" | "avgOutletFlowTemperature" | "inletMinusOutletFlowTemperatureDifference" | "maxInletMinusOutletFlowTemperatureDifference" | "minInletMinusOutletFlowTemperatureDifference" | "avgInletMinusOutletFlowTemperatureDifference" | "outletMinusInletFlowTemperatureDifference" | "maxOutletMinusInletFlowTemperatureDifference" | "minOutletMinusInletFlowTemperatureDifference" | "avgOutletMinusInletFlowTemperatureDifference" | "ambientTemperature" | "maxAmbientTemperature" | "minAmbientTemperature" | "avgAmbientTemperature" | "radiatorTemperature" | "maxRadiatorTemperature" | "minRadiatorTemperature" | "avgRadiatorTemperature" | "relativeHumidity" | "kamstrupE8Inlet" | "diffKamstrupE8Inlet" | "kamstrupE9Outlet" | "diffKamstrupE9Outlet" | "operatingTime" | "diffOperatingTime" | "remainingBatteryTime" | "diffRemainingBatteryTime" | "deviceDate" | "deviceTime" | "deviceDateTime" | "timeSinceLastExactOverhearingArrived" | "timeAtLastExactOverhearingArrived" | "timeSinceLastPossibleCandidateOverhearingArrived" | "timeAtLastPossibleCandidateOverhearingArrived" | "timeSinceLastSampled" | "timeAtLastSampled" | "timeSinceLastInstallationMounted" | "timeAtLastInstallationMounted" | "basetime" | "device" | "wMBusDeviceAddresses" | "kamdraftDeviceAddresses" | "tchlrDeviceAddresses" | "kamdaDeviceAddresses" | "msisdnDeviceAddresses" | "deviceRegisters" | "consumptionZone" | "mBusFormatStringSignature" | "kamstrupMulticalInfocodeType1" | "kamstrupMulticalInfocodeType2" | "kamstrupMulticalInfocodeType3" | "kamstrupMulticalInfocodeType4" | "kamstrupMulticalInfocode" | "kamstrupModuleType" | "diehlInfocodeType1" | "diehlInfocode" | "landisGyrMBusStatusType1" | "landisGyrMBusStatusType2" | "landisGyrMBusStatus" | "landisGyrInfocodeType1" | "landisGyrInfocodeType2" | "landisGyrInfocode" | "infocode";
|
|
1
|
+
export declare type RegisterId = "none" | "heatEnergy" | "diffHeatEnergy" | "heatEnergyVolumeInInlet" | "diffHeatEnergyVolumeInInlet" | "heatEnergyVolumeInOutlet" | "diffHeatEnergyVolumeInOutlet" | "coolingEnergy" | "diffCoolingEnergy" | "coolingEnergyVolumeInInlet" | "diffCoolingEnergyVolumeInInlet" | "coolingEnergyVolumeInOutlet" | "diffCoolingEnergyVolumeInOutlet" | "electricalEnergy" | "diffElectricalEnergy" | "reactiveEnergy" | "diffReactiveEnergy" | "volume" | "diffVolume" | "inletVolume" | "diffInletVolume" | "outletVolume" | "diffOutletVolume" | "pulseInputAVolume" | "diffPulseInputAVolume" | "pulseInputBVolume" | "diffPulseInputBVolume" | "mass" | "diffMass" | "inletMass" | "diffInletMass" | "outletMass" | "diffOutletMass" | "power" | "maxPower" | "minPower" | "avgPower" | "inletPower" | "maxInletPower" | "minInletPower" | "avgInletPower" | "outletPower" | "maxOutletPower" | "minOutletPower" | "avgOutletPower" | "volumeFlow" | "maxVolumeFlow" | "minVolumeFlow" | "avgVolumeFlow" | "inletVolumeFlow" | "maxInletVolumeFlow" | "minInletVolumeFlow" | "avgInletVolumeFlow" | "outletVolumeFlow" | "maxOutletVolumeFlow" | "minOutletVolumeFlow" | "avgOutletVolumeFlow" | "massFlow" | "maxMassFlow" | "minMassFlow" | "avgMassFlow" | "inletMassFlow" | "maxInletMassFlow" | "minInletMassFlow" | "avgInletMassFlow" | "outletMassFlow" | "maxOutletMassFlow" | "minOutletMassFlow" | "avgOutletMassFlow" | "flowTemperature" | "maxFlowTemperature" | "minFlowTemperature" | "avgFlowTemperature" | "inletFlowTemperature" | "maxInletFlowTemperature" | "minInletFlowTemperature" | "avgInletFlowTemperature" | "outletFlowTemperature" | "maxOutletFlowTemperature" | "minOutletFlowTemperature" | "avgOutletFlowTemperature" | "inletMinusOutletFlowTemperatureDifference" | "maxInletMinusOutletFlowTemperatureDifference" | "minInletMinusOutletFlowTemperatureDifference" | "avgInletMinusOutletFlowTemperatureDifference" | "outletMinusInletFlowTemperatureDifference" | "maxOutletMinusInletFlowTemperatureDifference" | "minOutletMinusInletFlowTemperatureDifference" | "avgOutletMinusInletFlowTemperatureDifference" | "ambientTemperature" | "maxAmbientTemperature" | "minAmbientTemperature" | "avgAmbientTemperature" | "radiatorTemperature" | "maxRadiatorTemperature" | "minRadiatorTemperature" | "avgRadiatorTemperature" | "relativeHumidity" | "kamstrupE8Inlet" | "diffKamstrupE8Inlet" | "kamstrupE9Outlet" | "diffKamstrupE9Outlet" | "operatingTime" | "diffOperatingTime" | "remainingBatteryTime" | "diffRemainingBatteryTime" | "deviceDate" | "deviceTime" | "deviceDateTime" | "timeSinceLastExactOverhearingArrived" | "timeAtLastExactOverhearingArrived" | "timeSinceLastPossibleCandidateOverhearingArrived" | "timeAtLastPossibleCandidateOverhearingArrived" | "timeSinceLastSampled" | "timeAtLastSampled" | "timeSinceLastInstallationMounted" | "timeAtLastInstallationMounted" | "basetime" | "instantTime" | "periodStartTime" | "periodEndTime" | "instantOrPeriodStartTime" | "instantOrPeriodEndTime" | "device" | "wMBusDeviceAddresses" | "kamdraftDeviceAddresses" | "tchlrDeviceAddresses" | "kamdaDeviceAddresses" | "msisdnDeviceAddresses" | "deviceRegisters" | "consumptionZone" | "mBusFormatStringSignature" | "deviceSoftwareVersion" | "kamstrupMulticalInfocodeType1" | "kamstrupMulticalInfocodeType2" | "kamstrupMulticalInfocodeType3" | "kamstrupMulticalInfocodeType4" | "kamstrupMulticalInfocode" | "kamstrupModuleType" | "diehlInfocodeType1" | "diehlInfocode" | "landisGyrMBusStatusType1" | "landisGyrMBusStatusType2" | "landisGyrMBusStatus" | "landisGyrInfocodeType1" | "landisGyrInfocodeType2" | "landisGyrInfocode" | "infocode";
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@utiliread/primitives",
|
|
3
|
-
"version": "1.0.0-
|
|
3
|
+
"version": "1.0.0-36",
|
|
4
4
|
"main": "dist/cjs/index.js",
|
|
5
5
|
"module": "dist/esm/index.js",
|
|
6
6
|
"types": "dist/esm/index.d.ts",
|
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
"access": "public"
|
|
17
17
|
},
|
|
18
18
|
"devDependencies": {
|
|
19
|
-
"@types/chai": "^4.2.
|
|
19
|
+
"@types/chai": "^4.2.18",
|
|
20
20
|
"@types/luxon": "^1.26.5",
|
|
21
21
|
"@types/mocha": "^8.2.2",
|
|
22
22
|
"chai": "^4.3.4",
|
|
@@ -26,7 +26,7 @@
|
|
|
26
26
|
"karma-mocha": "^2.0.1",
|
|
27
27
|
"karma-typescript": "^5.5.1",
|
|
28
28
|
"luxon": "^1.26.0",
|
|
29
|
-
"mocha": "^8.
|
|
29
|
+
"mocha": "^8.4.0",
|
|
30
30
|
"rimraf": "^3.0.2",
|
|
31
31
|
"typescript": "^4.1.5"
|
|
32
32
|
},
|
package/src/aggregate-id.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
export type AggregateId =
|
|
2
|
-
"instant" |
|
|
3
|
-
"diff" |
|
|
4
|
-
"min" |
|
|
5
|
-
"max" |
|
|
6
|
-
"avg" |
|
|
7
|
-
"sum" |
|
|
8
|
-
"cnt" |
|
|
9
|
-
"ssq";
|
|
1
|
+
export type AggregateId =
|
|
2
|
+
"instant" |
|
|
3
|
+
"diff" |
|
|
4
|
+
"min" |
|
|
5
|
+
"max" |
|
|
6
|
+
"avg" |
|
|
7
|
+
"sum" |
|
|
8
|
+
"cnt" |
|
|
9
|
+
"ssq";
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
export type ConsumptionTypeId =
|
|
2
|
-
"none" |
|
|
3
|
-
"heat" |
|
|
4
|
-
"water" |
|
|
5
|
-
"cooling" |
|
|
6
|
-
"electricity" |
|
|
7
|
-
"infrastructure" |
|
|
8
|
-
"all";
|
|
1
|
+
export type ConsumptionTypeId =
|
|
2
|
+
"none" |
|
|
3
|
+
"heat" |
|
|
4
|
+
"water" |
|
|
5
|
+
"cooling" |
|
|
6
|
+
"electricity" |
|
|
7
|
+
"infrastructure" |
|
|
8
|
+
"all";
|
package/src/index.ts
CHANGED
|
@@ -1,30 +1,30 @@
|
|
|
1
|
-
import { ConsumptionTypeId } from "./consumption-type-id";
|
|
2
|
-
import { RegisterId } from "./register-id";
|
|
3
|
-
import { UnitId } from "./unit-id";
|
|
4
|
-
import { RegisterLibrary } from "./register-library";
|
|
5
|
-
import { RegisterDefinition } from "./register-definition";
|
|
6
|
-
import { NumberFormatter, invariantDecimalSeparator, localeDecimalSeparator } from "./number-formatter";
|
|
7
|
-
import { RegisterConvention } from "./register-convention";
|
|
8
|
-
import { AggregateId } from "./aggregate-id";
|
|
9
|
-
import { MeasureId } from "./measure-id";
|
|
10
|
-
import { Step, DayOfWeek } from "./step";
|
|
11
|
-
import { TimeOrRule } from "./time-or-rule";
|
|
12
|
-
import { PrecisionNumber } from "./precision-number";
|
|
13
|
-
|
|
14
|
-
export {
|
|
15
|
-
AggregateId,
|
|
16
|
-
ConsumptionTypeId,
|
|
17
|
-
DayOfWeek,
|
|
18
|
-
MeasureId,
|
|
19
|
-
NumberFormatter,
|
|
20
|
-
PrecisionNumber,
|
|
21
|
-
RegisterConvention,
|
|
22
|
-
RegisterDefinition,
|
|
23
|
-
RegisterId,
|
|
24
|
-
RegisterLibrary,
|
|
25
|
-
Step,
|
|
26
|
-
TimeOrRule,
|
|
27
|
-
UnitId,
|
|
28
|
-
invariantDecimalSeparator,
|
|
29
|
-
localeDecimalSeparator,
|
|
30
|
-
};
|
|
1
|
+
import { ConsumptionTypeId } from "./consumption-type-id";
|
|
2
|
+
import { RegisterId } from "./register-id";
|
|
3
|
+
import { UnitId } from "./unit-id";
|
|
4
|
+
import { RegisterLibrary } from "./register-library";
|
|
5
|
+
import { RegisterDefinition } from "./register-definition";
|
|
6
|
+
import { NumberFormatter, invariantDecimalSeparator, localeDecimalSeparator } from "./number-formatter";
|
|
7
|
+
import { RegisterConvention } from "./register-convention";
|
|
8
|
+
import { AggregateId } from "./aggregate-id";
|
|
9
|
+
import { MeasureId } from "./measure-id";
|
|
10
|
+
import { Step, DayOfWeek } from "./step";
|
|
11
|
+
import { TimeOrRule } from "./time-or-rule";
|
|
12
|
+
import { PrecisionNumber } from "./precision-number";
|
|
13
|
+
|
|
14
|
+
export {
|
|
15
|
+
AggregateId,
|
|
16
|
+
ConsumptionTypeId,
|
|
17
|
+
DayOfWeek,
|
|
18
|
+
MeasureId,
|
|
19
|
+
NumberFormatter,
|
|
20
|
+
PrecisionNumber,
|
|
21
|
+
RegisterConvention,
|
|
22
|
+
RegisterDefinition,
|
|
23
|
+
RegisterId,
|
|
24
|
+
RegisterLibrary,
|
|
25
|
+
Step,
|
|
26
|
+
TimeOrRule,
|
|
27
|
+
UnitId,
|
|
28
|
+
invariantDecimalSeparator,
|
|
29
|
+
localeDecimalSeparator,
|
|
30
|
+
};
|
package/src/measure-id.ts
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
export type MeasureId =
|
|
2
|
-
"none" |
|
|
3
|
-
"duration" |
|
|
4
|
-
"energy" |
|
|
5
|
-
"humidity" |
|
|
6
|
-
"mass" |
|
|
7
|
-
"massFlow" |
|
|
8
|
-
"power" |
|
|
9
|
-
"reactiveEnergy" |
|
|
10
|
-
"reactivePower" |
|
|
11
|
-
"temperature" |
|
|
12
|
-
"time" |
|
|
13
|
-
"volume" |
|
|
14
|
-
"volumeFlow";
|
|
1
|
+
export type MeasureId =
|
|
2
|
+
"none" |
|
|
3
|
+
"duration" |
|
|
4
|
+
"energy" |
|
|
5
|
+
"humidity" |
|
|
6
|
+
"mass" |
|
|
7
|
+
"massFlow" |
|
|
8
|
+
"power" |
|
|
9
|
+
"reactiveEnergy" |
|
|
10
|
+
"reactivePower" |
|
|
11
|
+
"temperature" |
|
|
12
|
+
"time" |
|
|
13
|
+
"volume" |
|
|
14
|
+
"volumeFlow";
|
|
@@ -1,94 +1,94 @@
|
|
|
1
|
-
import { expect } from 'chai';
|
|
2
|
-
import { NumberFormatter, Parts, localeDecimalSeparator, invariantDecimalSeparator } from './number-formatter';
|
|
3
|
-
|
|
4
|
-
describe("toParts and toPrecision", () => {
|
|
5
|
-
it("should format correctly", () => {
|
|
6
|
-
const data: [number, number, Parts][] = [
|
|
7
|
-
[77.1274, 1, { invariantPrecisionString: "8e+1", coefficient: 8, exponent: 1, noOfCoefficientDecimals: 0 }],
|
|
8
|
-
[77.1274, 2, { invariantPrecisionString: "77", coefficient: 77, exponent: 0, noOfCoefficientDecimals: 0 }],
|
|
9
|
-
[77.1274, 4, { invariantPrecisionString: "77.13", coefficient: 77.13, exponent: 0, noOfCoefficientDecimals: 2 }],
|
|
10
|
-
[77.1274, 5, { invariantPrecisionString: "77.127", coefficient: 77.127, exponent: 0, noOfCoefficientDecimals: 3 }],
|
|
11
|
-
[77.1274, 6, { invariantPrecisionString: "77.1274", coefficient: 77.1274, exponent: 0, noOfCoefficientDecimals: 4 }],
|
|
12
|
-
[77.1274, 7, { invariantPrecisionString: "77.12740", coefficient: 77.12740, exponent: 0, noOfCoefficientDecimals: 5 }],
|
|
13
|
-
[77.1274, 15, { invariantPrecisionString: "77.1274000000000", coefficient: 77.1274000000000, exponent: 0, noOfCoefficientDecimals: 13 }],
|
|
14
|
-
[77.1274, 16, { invariantPrecisionString: "77.12739999999999", coefficient: 77.12739999999999, exponent: 0, noOfCoefficientDecimals: 14 }],
|
|
15
|
-
[77.1274, 17, { invariantPrecisionString: "77.127399999999994", coefficient: 77.127399999999994, exponent: 0, noOfCoefficientDecimals: 15 }],
|
|
16
|
-
[77.1274, 18, { invariantPrecisionString: "77.1273999999999944", coefficient: 77.1273999999999944, exponent: 0, noOfCoefficientDecimals: 16 }],
|
|
17
|
-
[77.1274, 19, { invariantPrecisionString: "77.12739999999999441", coefficient: 77.12739999999999441, exponent: 0, noOfCoefficientDecimals: 17 }],
|
|
18
|
-
[77.1274, 20, { invariantPrecisionString: "77.127399999999994407", coefficient: 77.127399999999994407, exponent: 0, noOfCoefficientDecimals: 18 }],
|
|
19
|
-
[77.1274, 21, { invariantPrecisionString: "77.1273999999999944066", coefficient: 77.1273999999999944066, exponent: 0, noOfCoefficientDecimals: 19 }],
|
|
20
|
-
[0.00000123, 2, { invariantPrecisionString: "0.0000012", coefficient: 0.0000012, exponent: 0, noOfCoefficientDecimals: 7 }],
|
|
21
|
-
[0.000000123, 2, { invariantPrecisionString: "1.2e-7", coefficient: 1.2, exponent: -7, noOfCoefficientDecimals: 1 }],
|
|
22
|
-
[0.00000123, 21, { invariantPrecisionString: "0.00000123000000000000008198", coefficient: 0.00000123000000000000008198, exponent: 0, noOfCoefficientDecimals: 26 }],
|
|
23
|
-
[123456, 3, { invariantPrecisionString: "1.23e+5", coefficient: 1.23, exponent: 5, noOfCoefficientDecimals: 2 }],
|
|
24
|
-
[Number.MAX_VALUE, 3, { invariantPrecisionString: "1.80e+308", coefficient: 1.80, exponent: 308, noOfCoefficientDecimals: 2 }],
|
|
25
|
-
[123456, 15, { invariantPrecisionString: "123456.000000000", coefficient: 123456.000000000, exponent: 0, noOfCoefficientDecimals: 9 }],
|
|
26
|
-
[0, 5, { invariantPrecisionString: "0.0000", coefficient: 0.0000, exponent: 0, noOfCoefficientDecimals: 4 }],
|
|
27
|
-
[0.000001, 1, { invariantPrecisionString: "0.000001", coefficient: 0.000001, exponent: 0, noOfCoefficientDecimals: 6 }],
|
|
28
|
-
[0.000001, 10, { invariantPrecisionString: "0.000001000000000", coefficient: 0.000001000000000, exponent: 0, noOfCoefficientDecimals: 15 }],
|
|
29
|
-
[0.0000001, 10, { invariantPrecisionString: "1.000000000e-7", coefficient: 1.000000000, exponent: -7, noOfCoefficientDecimals: 9 }],
|
|
30
|
-
[0.00000000001, 1, { invariantPrecisionString: "1e-11", coefficient: 1, exponent: -11, noOfCoefficientDecimals: 0 }],
|
|
31
|
-
[0.00000000001, 2, { invariantPrecisionString: "1.0e-11", coefficient: 1.0, exponent: -11, noOfCoefficientDecimals: 1 }],
|
|
32
|
-
[55, 2, { invariantPrecisionString: "55", coefficient: 55, exponent: 0, noOfCoefficientDecimals: 0 }],
|
|
33
|
-
[55, 1, { invariantPrecisionString: "6e+1", coefficient: 6, exponent: 1, noOfCoefficientDecimals: 0 }],
|
|
34
|
-
[-55, 2, { invariantPrecisionString: "-55", coefficient: -55, exponent: 0, noOfCoefficientDecimals: 0 }],
|
|
35
|
-
[-55, 1, { invariantPrecisionString: "-6e+1", coefficient: -6, exponent: 1, noOfCoefficientDecimals: 0 }],
|
|
36
|
-
[9.59, 1, { invariantPrecisionString: "1e+1", coefficient: 1, exponent: 1, noOfCoefficientDecimals: 0 }],
|
|
37
|
-
[9.59, 2, { invariantPrecisionString: "10", coefficient: 10, exponent: 0, noOfCoefficientDecimals: 0 }],
|
|
38
|
-
[9.95, 2, { invariantPrecisionString: "10", coefficient: 10, exponent: 0, noOfCoefficientDecimals: 0 }],
|
|
39
|
-
[9.96, 2, { invariantPrecisionString: "10", coefficient: 10, exponent: 0, noOfCoefficientDecimals: 0 }],
|
|
40
|
-
[-555555555555555555555, 1, { invariantPrecisionString: "-6e+20", coefficient: -6, exponent: 20, noOfCoefficientDecimals: 0 }],
|
|
41
|
-
[-5555555555555555555555, 1, { invariantPrecisionString: "-6e+21", coefficient: -6, exponent: 21, noOfCoefficientDecimals: 0 }],
|
|
42
|
-
[18014398509482012, 21, { invariantPrecisionString: "18014398509482012.0000", coefficient: 18014398509482012.0000, exponent: 0, noOfCoefficientDecimals: 4 }],
|
|
43
|
-
[180143985094820121234, 21, { invariantPrecisionString: "180143985094820134912", coefficient: 180143985094820134912, exponent: 0, noOfCoefficientDecimals: 0 }],
|
|
44
|
-
[0.1, 21, { invariantPrecisionString: "0.100000000000000005551", coefficient: 0.100000000000000005551, exponent: 0, noOfCoefficientDecimals: 21 }],
|
|
45
|
-
[Number.EPSILON, 2, { invariantPrecisionString: "2.2e-16", coefficient: 2.2, exponent: -16, noOfCoefficientDecimals: 1 }],
|
|
46
|
-
[Number.EPSILON, 21, { invariantPrecisionString: "2.22044604925031308085e-16", coefficient: 2.22044604925031308085, exponent: -16, noOfCoefficientDecimals: 20 }],
|
|
47
|
-
[Number.NaN, 3, { invariantPrecisionString: "NaN", coefficient: NaN, exponent: 0, noOfCoefficientDecimals: 0 }],
|
|
48
|
-
[Number.POSITIVE_INFINITY, 3, { invariantPrecisionString: "Infinity", coefficient: Infinity, exponent: 0, noOfCoefficientDecimals: 0 }],
|
|
49
|
-
[Number.NEGATIVE_INFINITY, 3, { invariantPrecisionString: "-Infinity", coefficient: -Infinity, exponent: 0, noOfCoefficientDecimals: 0 }],
|
|
50
|
-
[164735280, 7, { invariantPrecisionString: "1.647353e+8", coefficient: 1.647353, exponent: 8, noOfCoefficientDecimals: 6 }],
|
|
51
|
-
];
|
|
52
|
-
|
|
53
|
-
for (const [value, precision, expectedParts] of data) {
|
|
54
|
-
expect(NumberFormatter.toPrecision(value, precision)).to.equal(expectedParts.invariantPrecisionString);
|
|
55
|
-
|
|
56
|
-
const localePrecisionString = expectedParts.invariantPrecisionString.replace(invariantDecimalSeparator, localeDecimalSeparator);
|
|
57
|
-
expect(NumberFormatter.toLocalePrecision(value, precision)).to.equal(localePrecisionString);
|
|
58
|
-
|
|
59
|
-
const parts = NumberFormatter.getParts(value, precision);
|
|
60
|
-
expect(parts.invariantPrecisionString).to.equal(expectedParts.invariantPrecisionString);
|
|
61
|
-
if (isNaN(expectedParts.coefficient)) {
|
|
62
|
-
expect(isNaN(parts.coefficient)).to.equal(true);
|
|
63
|
-
}
|
|
64
|
-
else {
|
|
65
|
-
expect(parts.coefficient).to.equal(expectedParts.coefficient);
|
|
66
|
-
}
|
|
67
|
-
expect(parts.exponent).to.equal(expectedParts.exponent);
|
|
68
|
-
expect(parts.noOfCoefficientDecimals).to.equal(expectedParts.noOfCoefficientDecimals);
|
|
69
|
-
}
|
|
70
|
-
})
|
|
1
|
+
import { expect } from 'chai';
|
|
2
|
+
import { NumberFormatter, Parts, localeDecimalSeparator, invariantDecimalSeparator } from './number-formatter';
|
|
3
|
+
|
|
4
|
+
describe("toParts and toPrecision", () => {
|
|
5
|
+
it("should format correctly", () => {
|
|
6
|
+
const data: [number, number, Parts][] = [
|
|
7
|
+
[77.1274, 1, { invariantPrecisionString: "8e+1", coefficient: 8, exponent: 1, noOfCoefficientDecimals: 0 }],
|
|
8
|
+
[77.1274, 2, { invariantPrecisionString: "77", coefficient: 77, exponent: 0, noOfCoefficientDecimals: 0 }],
|
|
9
|
+
[77.1274, 4, { invariantPrecisionString: "77.13", coefficient: 77.13, exponent: 0, noOfCoefficientDecimals: 2 }],
|
|
10
|
+
[77.1274, 5, { invariantPrecisionString: "77.127", coefficient: 77.127, exponent: 0, noOfCoefficientDecimals: 3 }],
|
|
11
|
+
[77.1274, 6, { invariantPrecisionString: "77.1274", coefficient: 77.1274, exponent: 0, noOfCoefficientDecimals: 4 }],
|
|
12
|
+
[77.1274, 7, { invariantPrecisionString: "77.12740", coefficient: 77.12740, exponent: 0, noOfCoefficientDecimals: 5 }],
|
|
13
|
+
[77.1274, 15, { invariantPrecisionString: "77.1274000000000", coefficient: 77.1274000000000, exponent: 0, noOfCoefficientDecimals: 13 }],
|
|
14
|
+
[77.1274, 16, { invariantPrecisionString: "77.12739999999999", coefficient: 77.12739999999999, exponent: 0, noOfCoefficientDecimals: 14 }],
|
|
15
|
+
[77.1274, 17, { invariantPrecisionString: "77.127399999999994", coefficient: 77.127399999999994, exponent: 0, noOfCoefficientDecimals: 15 }],
|
|
16
|
+
[77.1274, 18, { invariantPrecisionString: "77.1273999999999944", coefficient: 77.1273999999999944, exponent: 0, noOfCoefficientDecimals: 16 }],
|
|
17
|
+
[77.1274, 19, { invariantPrecisionString: "77.12739999999999441", coefficient: 77.12739999999999441, exponent: 0, noOfCoefficientDecimals: 17 }],
|
|
18
|
+
[77.1274, 20, { invariantPrecisionString: "77.127399999999994407", coefficient: 77.127399999999994407, exponent: 0, noOfCoefficientDecimals: 18 }],
|
|
19
|
+
[77.1274, 21, { invariantPrecisionString: "77.1273999999999944066", coefficient: 77.1273999999999944066, exponent: 0, noOfCoefficientDecimals: 19 }],
|
|
20
|
+
[0.00000123, 2, { invariantPrecisionString: "0.0000012", coefficient: 0.0000012, exponent: 0, noOfCoefficientDecimals: 7 }],
|
|
21
|
+
[0.000000123, 2, { invariantPrecisionString: "1.2e-7", coefficient: 1.2, exponent: -7, noOfCoefficientDecimals: 1 }],
|
|
22
|
+
[0.00000123, 21, { invariantPrecisionString: "0.00000123000000000000008198", coefficient: 0.00000123000000000000008198, exponent: 0, noOfCoefficientDecimals: 26 }],
|
|
23
|
+
[123456, 3, { invariantPrecisionString: "1.23e+5", coefficient: 1.23, exponent: 5, noOfCoefficientDecimals: 2 }],
|
|
24
|
+
[Number.MAX_VALUE, 3, { invariantPrecisionString: "1.80e+308", coefficient: 1.80, exponent: 308, noOfCoefficientDecimals: 2 }],
|
|
25
|
+
[123456, 15, { invariantPrecisionString: "123456.000000000", coefficient: 123456.000000000, exponent: 0, noOfCoefficientDecimals: 9 }],
|
|
26
|
+
[0, 5, { invariantPrecisionString: "0.0000", coefficient: 0.0000, exponent: 0, noOfCoefficientDecimals: 4 }],
|
|
27
|
+
[0.000001, 1, { invariantPrecisionString: "0.000001", coefficient: 0.000001, exponent: 0, noOfCoefficientDecimals: 6 }],
|
|
28
|
+
[0.000001, 10, { invariantPrecisionString: "0.000001000000000", coefficient: 0.000001000000000, exponent: 0, noOfCoefficientDecimals: 15 }],
|
|
29
|
+
[0.0000001, 10, { invariantPrecisionString: "1.000000000e-7", coefficient: 1.000000000, exponent: -7, noOfCoefficientDecimals: 9 }],
|
|
30
|
+
[0.00000000001, 1, { invariantPrecisionString: "1e-11", coefficient: 1, exponent: -11, noOfCoefficientDecimals: 0 }],
|
|
31
|
+
[0.00000000001, 2, { invariantPrecisionString: "1.0e-11", coefficient: 1.0, exponent: -11, noOfCoefficientDecimals: 1 }],
|
|
32
|
+
[55, 2, { invariantPrecisionString: "55", coefficient: 55, exponent: 0, noOfCoefficientDecimals: 0 }],
|
|
33
|
+
[55, 1, { invariantPrecisionString: "6e+1", coefficient: 6, exponent: 1, noOfCoefficientDecimals: 0 }],
|
|
34
|
+
[-55, 2, { invariantPrecisionString: "-55", coefficient: -55, exponent: 0, noOfCoefficientDecimals: 0 }],
|
|
35
|
+
[-55, 1, { invariantPrecisionString: "-6e+1", coefficient: -6, exponent: 1, noOfCoefficientDecimals: 0 }],
|
|
36
|
+
[9.59, 1, { invariantPrecisionString: "1e+1", coefficient: 1, exponent: 1, noOfCoefficientDecimals: 0 }],
|
|
37
|
+
[9.59, 2, { invariantPrecisionString: "10", coefficient: 10, exponent: 0, noOfCoefficientDecimals: 0 }],
|
|
38
|
+
[9.95, 2, { invariantPrecisionString: "10", coefficient: 10, exponent: 0, noOfCoefficientDecimals: 0 }],
|
|
39
|
+
[9.96, 2, { invariantPrecisionString: "10", coefficient: 10, exponent: 0, noOfCoefficientDecimals: 0 }],
|
|
40
|
+
[-555555555555555555555, 1, { invariantPrecisionString: "-6e+20", coefficient: -6, exponent: 20, noOfCoefficientDecimals: 0 }],
|
|
41
|
+
[-5555555555555555555555, 1, { invariantPrecisionString: "-6e+21", coefficient: -6, exponent: 21, noOfCoefficientDecimals: 0 }],
|
|
42
|
+
[18014398509482012, 21, { invariantPrecisionString: "18014398509482012.0000", coefficient: 18014398509482012.0000, exponent: 0, noOfCoefficientDecimals: 4 }],
|
|
43
|
+
[180143985094820121234, 21, { invariantPrecisionString: "180143985094820134912", coefficient: 180143985094820134912, exponent: 0, noOfCoefficientDecimals: 0 }],
|
|
44
|
+
[0.1, 21, { invariantPrecisionString: "0.100000000000000005551", coefficient: 0.100000000000000005551, exponent: 0, noOfCoefficientDecimals: 21 }],
|
|
45
|
+
[Number.EPSILON, 2, { invariantPrecisionString: "2.2e-16", coefficient: 2.2, exponent: -16, noOfCoefficientDecimals: 1 }],
|
|
46
|
+
[Number.EPSILON, 21, { invariantPrecisionString: "2.22044604925031308085e-16", coefficient: 2.22044604925031308085, exponent: -16, noOfCoefficientDecimals: 20 }],
|
|
47
|
+
[Number.NaN, 3, { invariantPrecisionString: "NaN", coefficient: NaN, exponent: 0, noOfCoefficientDecimals: 0 }],
|
|
48
|
+
[Number.POSITIVE_INFINITY, 3, { invariantPrecisionString: "Infinity", coefficient: Infinity, exponent: 0, noOfCoefficientDecimals: 0 }],
|
|
49
|
+
[Number.NEGATIVE_INFINITY, 3, { invariantPrecisionString: "-Infinity", coefficient: -Infinity, exponent: 0, noOfCoefficientDecimals: 0 }],
|
|
50
|
+
[164735280, 7, { invariantPrecisionString: "1.647353e+8", coefficient: 1.647353, exponent: 8, noOfCoefficientDecimals: 6 }],
|
|
51
|
+
];
|
|
52
|
+
|
|
53
|
+
for (const [value, precision, expectedParts] of data) {
|
|
54
|
+
expect(NumberFormatter.toPrecision(value, precision)).to.equal(expectedParts.invariantPrecisionString);
|
|
55
|
+
|
|
56
|
+
const localePrecisionString = expectedParts.invariantPrecisionString.replace(invariantDecimalSeparator, localeDecimalSeparator);
|
|
57
|
+
expect(NumberFormatter.toLocalePrecision(value, precision)).to.equal(localePrecisionString);
|
|
58
|
+
|
|
59
|
+
const parts = NumberFormatter.getParts(value, precision);
|
|
60
|
+
expect(parts.invariantPrecisionString).to.equal(expectedParts.invariantPrecisionString);
|
|
61
|
+
if (isNaN(expectedParts.coefficient)) {
|
|
62
|
+
expect(isNaN(parts.coefficient)).to.equal(true);
|
|
63
|
+
}
|
|
64
|
+
else {
|
|
65
|
+
expect(parts.coefficient).to.equal(expectedParts.coefficient);
|
|
66
|
+
}
|
|
67
|
+
expect(parts.exponent).to.equal(expectedParts.exponent);
|
|
68
|
+
expect(parts.noOfCoefficientDecimals).to.equal(expectedParts.noOfCoefficientDecimals);
|
|
69
|
+
}
|
|
70
|
+
})
|
|
71
|
+
})
|
|
72
|
+
|
|
73
|
+
describe("toFixed", () => {
|
|
74
|
+
it("should format correctly", () => {
|
|
75
|
+
const data: [number, number, string][] = [
|
|
76
|
+
[10, 1, "10"],
|
|
77
|
+
[10, 2, "10"],
|
|
78
|
+
[10, 3, "10.0"],
|
|
79
|
+
[10, 4, "10.00"],
|
|
80
|
+
[10.2, 1, "10"],
|
|
81
|
+
[10.2, 2, "10"],
|
|
82
|
+
[10.2, 3, "10.2"],
|
|
83
|
+
[10.2, 4, "10.20"],
|
|
84
|
+
[217887000000, 6, "217887000000"],
|
|
85
|
+
];
|
|
86
|
+
|
|
87
|
+
for (const [value, precision, invariantExpected] of data) {
|
|
88
|
+
expect(NumberFormatter.toFixed(value, precision)).to.equal(invariantExpected);
|
|
89
|
+
|
|
90
|
+
const localeExpected = invariantExpected.replace(invariantDecimalSeparator, localeDecimalSeparator);
|
|
91
|
+
expect(NumberFormatter.toLocaleFixed(value, precision)).to.equal(localeExpected);
|
|
92
|
+
}
|
|
93
|
+
})
|
|
71
94
|
})
|
|
72
|
-
|
|
73
|
-
describe("toFixed", () => {
|
|
74
|
-
it("should format correctly", () => {
|
|
75
|
-
const data: [number, number, string][] = [
|
|
76
|
-
[10, 1, "10"],
|
|
77
|
-
[10, 2, "10"],
|
|
78
|
-
[10, 3, "10.0"],
|
|
79
|
-
[10, 4, "10.00"],
|
|
80
|
-
[10.2, 1, "10"],
|
|
81
|
-
[10.2, 2, "10"],
|
|
82
|
-
[10.2, 3, "10.2"],
|
|
83
|
-
[10.2, 4, "10.20"],
|
|
84
|
-
[217887000000, 6, "217887000000"],
|
|
85
|
-
];
|
|
86
|
-
|
|
87
|
-
for (const [value, precision, invariantExpected] of data) {
|
|
88
|
-
expect(NumberFormatter.toFixed(value, precision)).to.equal(invariantExpected);
|
|
89
|
-
|
|
90
|
-
const localeExpected = invariantExpected.replace(invariantDecimalSeparator, localeDecimalSeparator);
|
|
91
|
-
expect(NumberFormatter.toLocaleFixed(value, precision)).to.equal(localeExpected);
|
|
92
|
-
}
|
|
93
|
-
})
|
|
94
|
-
})
|
package/src/number-formatter.ts
CHANGED
|
@@ -1,78 +1,78 @@
|
|
|
1
|
-
export const invariantDecimalSeparator = (0.1).toString()[1];
|
|
2
|
-
export const localeDecimalSeparator = (0.1).toLocaleString()[1];
|
|
3
|
-
|
|
4
|
-
export interface Parts {
|
|
5
|
-
invariantPrecisionString: string;
|
|
6
|
-
exponent: number;
|
|
7
|
-
coefficient: number;
|
|
8
|
-
noOfCoefficientDecimals: number;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
class Formatter {
|
|
12
|
-
public toPrecision(value: number, precision: number) {
|
|
13
|
-
const absValue = Math.abs(value);
|
|
14
|
-
if (absValue < 1) {
|
|
15
|
-
return value.toPrecision(precision);
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
const noOfIntegerDigits = Math.round(absValue).toString().length;
|
|
19
|
-
return precision >= noOfIntegerDigits
|
|
20
|
-
? value.toFixed(precision - noOfIntegerDigits)
|
|
21
|
-
: value.toPrecision(precision);
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
public toLocalePrecision(value: number, precision: number) {
|
|
25
|
-
return this.toPrecision(value, precision)
|
|
26
|
-
.replace(invariantDecimalSeparator, localeDecimalSeparator);
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
public toFixed(value: number, precision: number) {
|
|
30
|
-
const parts = this.getParts(value, precision);
|
|
31
|
-
if (parts.noOfCoefficientDecimals < parts.exponent) {
|
|
32
|
-
const number = parts.coefficient.toString().replace(invariantDecimalSeparator, "");
|
|
33
|
-
const zeros = parts.exponent - parts.noOfCoefficientDecimals;
|
|
34
|
-
return `${number}${"0".repeat(zeros)}`;
|
|
35
|
-
}
|
|
36
|
-
else {
|
|
37
|
-
return parts.invariantPrecisionString;
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
public toLocaleFixed(value: number, precision: number) {
|
|
42
|
-
return this.toFixed(value, precision)
|
|
43
|
-
.replace(invariantDecimalSeparator, localeDecimalSeparator);
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
public getParts(value: number, precision: number): Parts {
|
|
47
|
-
const precisionString = this.toPrecision(value, precision);
|
|
48
|
-
if (precisionString.indexOf("e") >= 0) {
|
|
49
|
-
const match = /(.*)e([\+-]\d+)$/.exec(precisionString)!;
|
|
50
|
-
const coefficient = match[1];
|
|
51
|
-
const exponent = parseInt(match[2]);
|
|
52
|
-
const decimalSeparatorIndex = coefficient.indexOf(invariantDecimalSeparator);
|
|
53
|
-
const noOfCoefficientDecimals = decimalSeparatorIndex >= 0
|
|
54
|
-
? coefficient.length - decimalSeparatorIndex - 1
|
|
55
|
-
: 0;
|
|
56
|
-
return {
|
|
57
|
-
invariantPrecisionString: precisionString,
|
|
58
|
-
coefficient: Number(coefficient),
|
|
59
|
-
exponent,
|
|
60
|
-
noOfCoefficientDecimals,
|
|
61
|
-
};
|
|
62
|
-
}
|
|
63
|
-
else {
|
|
64
|
-
const decimalSeparatorIndex = precisionString.indexOf(invariantDecimalSeparator);
|
|
65
|
-
const noOfCoefficientDecimals = decimalSeparatorIndex >= 0
|
|
66
|
-
? precisionString.length - decimalSeparatorIndex - 1
|
|
67
|
-
: 0;
|
|
68
|
-
return {
|
|
69
|
-
invariantPrecisionString: precisionString,
|
|
70
|
-
coefficient: Number(precisionString),
|
|
71
|
-
exponent: 0,
|
|
72
|
-
noOfCoefficientDecimals,
|
|
73
|
-
};
|
|
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
|
+
|
|
4
|
+
export interface Parts {
|
|
5
|
+
invariantPrecisionString: string;
|
|
6
|
+
exponent: number;
|
|
7
|
+
coefficient: number;
|
|
8
|
+
noOfCoefficientDecimals: number;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
class Formatter {
|
|
12
|
+
public toPrecision(value: number, precision: number) {
|
|
13
|
+
const absValue = Math.abs(value);
|
|
14
|
+
if (absValue < 1) {
|
|
15
|
+
return value.toPrecision(precision);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const noOfIntegerDigits = Math.round(absValue).toString().length;
|
|
19
|
+
return precision >= noOfIntegerDigits
|
|
20
|
+
? value.toFixed(precision - noOfIntegerDigits)
|
|
21
|
+
: value.toPrecision(precision);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
public toLocalePrecision(value: number, precision: number) {
|
|
25
|
+
return this.toPrecision(value, precision)
|
|
26
|
+
.replace(invariantDecimalSeparator, localeDecimalSeparator);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
public toFixed(value: number, precision: number) {
|
|
30
|
+
const parts = this.getParts(value, precision);
|
|
31
|
+
if (parts.noOfCoefficientDecimals < parts.exponent) {
|
|
32
|
+
const number = parts.coefficient.toString().replace(invariantDecimalSeparator, "");
|
|
33
|
+
const zeros = parts.exponent - parts.noOfCoefficientDecimals;
|
|
34
|
+
return `${number}${"0".repeat(zeros)}`;
|
|
35
|
+
}
|
|
36
|
+
else {
|
|
37
|
+
return parts.invariantPrecisionString;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
public toLocaleFixed(value: number, precision: number) {
|
|
42
|
+
return this.toFixed(value, precision)
|
|
43
|
+
.replace(invariantDecimalSeparator, localeDecimalSeparator);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
public getParts(value: number, precision: number): Parts {
|
|
47
|
+
const precisionString = this.toPrecision(value, precision);
|
|
48
|
+
if (precisionString.indexOf("e") >= 0) {
|
|
49
|
+
const match = /(.*)e([\+-]\d+)$/.exec(precisionString)!;
|
|
50
|
+
const coefficient = match[1];
|
|
51
|
+
const exponent = parseInt(match[2]);
|
|
52
|
+
const decimalSeparatorIndex = coefficient.indexOf(invariantDecimalSeparator);
|
|
53
|
+
const noOfCoefficientDecimals = decimalSeparatorIndex >= 0
|
|
54
|
+
? coefficient.length - decimalSeparatorIndex - 1
|
|
55
|
+
: 0;
|
|
56
|
+
return {
|
|
57
|
+
invariantPrecisionString: precisionString,
|
|
58
|
+
coefficient: Number(coefficient),
|
|
59
|
+
exponent,
|
|
60
|
+
noOfCoefficientDecimals,
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
else {
|
|
64
|
+
const decimalSeparatorIndex = precisionString.indexOf(invariantDecimalSeparator);
|
|
65
|
+
const noOfCoefficientDecimals = decimalSeparatorIndex >= 0
|
|
66
|
+
? precisionString.length - decimalSeparatorIndex - 1
|
|
67
|
+
: 0;
|
|
68
|
+
return {
|
|
69
|
+
invariantPrecisionString: precisionString,
|
|
70
|
+
coefficient: Number(precisionString),
|
|
71
|
+
exponent: 0,
|
|
72
|
+
noOfCoefficientDecimals,
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export const NumberFormatter = new Formatter();
|