@utiliread/primitives 1.0.0-63 → 1.0.0-65
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/esm/aggregate-id.d.ts +1 -1
- package/dist/esm/consumption-type-id.d.ts +1 -1
- package/dist/esm/measure-id.d.ts +1 -1
- package/dist/esm/optional.d.ts +1 -1
- package/dist/esm/register-id.d.ts +1 -1
- package/dist/esm/repository.d.ts +1 -1
- package/dist/esm/step.d.ts +1 -1
- package/dist/esm/time-or-rule.d.ts +1 -1
- package/dist/esm/unit-id.d.ts +1 -1
- package/karma.config.js +3 -0
- package/locales/da/registers.json +5 -0
- package/locales/en/registers.json +5 -0
- package/package.json +6 -6
- package/src/number-formatter.spec.ts +99 -99
- package/src/number-formatter.ts +88 -88
- package/src/precision-number.spec.ts +28 -28
- package/src/precision-number.ts +36 -36
- package/src/register-definition.ts +23 -23
- package/src/register-id.ts +279 -271
- package/src/repository.spec.ts +20 -20
- package/src/unit-id.ts +126 -126
|
@@ -1 +1 @@
|
|
|
1
|
-
export
|
|
1
|
+
export type AggregateId = "instant" | "diff" | "min" | "max" | "avg" | "sum" | "cnt" | "ssq";
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export
|
|
1
|
+
export type ConsumptionTypeId = "none" | "heat" | "water" | "cooling" | "electricity" | "infrastructure" | "all";
|
package/dist/esm/measure-id.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export
|
|
1
|
+
export type MeasureId = "none" | "duration" | "energy" | "humidity" | "mass" | "massFlow" | "power" | "reactiveEnergy" | "reactivePower" | "temperature" | "time" | "volume" | "volumeFlow";
|
package/dist/esm/optional.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export
|
|
1
|
+
export type Optional<T, K extends keyof T> = Pick<Partial<T>, K> & Omit<T, K>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export
|
|
1
|
+
export type RegisterId = "none" | "heatEnergy" | "diffHeatEnergy" | "cntHeatEnergy" | "heatEnergyVolumeInInlet" | "diffHeatEnergyVolumeInInlet" | "cntHeatEnergyVolumeInInlet" | "heatEnergyVolumeInOutlet" | "diffHeatEnergyVolumeInOutlet" | "cntHeatEnergyVolumeInOutlet" | "coolingEnergy" | "diffCoolingEnergy" | "cntCoolingEnergy" | "coolingEnergyVolumeInInlet" | "diffCoolingEnergyVolumeInInlet" | "cntCoolingEnergyVolumeInInlet" | "coolingEnergyVolumeInOutlet" | "diffCoolingEnergyVolumeInOutlet" | "cntCoolingEnergyVolumeInOutlet" | "electricalEnergy" | "diffElectricalEnergy" | "cntElectricalEnergy" | "reactiveEnergy" | "diffReactiveEnergy" | "cntReactiveEnergy" | "volume" | "diffVolume" | "cntVolume" | "inletVolume" | "diffInletVolume" | "cntInletVolume" | "outletVolume" | "diffOutletVolume" | "cntOutletVolume" | "pulseInputAVolume" | "diffPulseInputAVolume" | "cntPulseInputAVolume" | "pulseInputBVolume" | "diffPulseInputBVolume" | "cntPulseInputBVolume" | "mass" | "diffMass" | "cntMass" | "inletMass" | "diffInletMass" | "cntInletMass" | "outletMass" | "diffOutletMass" | "cntOutletMass" | "power" | "maxPower" | "minPower" | "avgPower" | "sumPower" | "cntPower" | "ssqPower" | "inletPower" | "maxInletPower" | "minInletPower" | "avgInletPower" | "sumInletPower" | "cntInletPower" | "ssqInletPower" | "outletPower" | "maxOutletPower" | "minOutletPower" | "avgOutletPower" | "sumOutletPower" | "cntOutletPower" | "ssqOutletPower" | "volumeFlow" | "maxVolumeFlow" | "minVolumeFlow" | "avgVolumeFlow" | "sumVolumeFlow" | "cntVolumeFlow" | "ssqVolumeFlow" | "inletVolumeFlow" | "maxInletVolumeFlow" | "minInletVolumeFlow" | "avgInletVolumeFlow" | "sumInletVolumeFlow" | "cntInletVolumeFlow" | "ssqInletVolumeFlow" | "outletVolumeFlow" | "maxOutletVolumeFlow" | "minOutletVolumeFlow" | "avgOutletVolumeFlow" | "sumOutletVolumeFlow" | "cntOutletVolumeFlow" | "ssqOutletVolumeFlow" | "massFlow" | "maxMassFlow" | "minMassFlow" | "avgMassFlow" | "sumMassFlow" | "cntMassFlow" | "ssqMassFlow" | "inletMassFlow" | "maxInletMassFlow" | "minInletMassFlow" | "avgInletMassFlow" | "sumInletMassFlow" | "cntInletMassFlow" | "ssqInletMassFlow" | "outletMassFlow" | "maxOutletMassFlow" | "minOutletMassFlow" | "avgOutletMassFlow" | "sumOutletMassFlow" | "cntOutletMassFlow" | "ssqOutletMassFlow" | "flowTemperature" | "maxFlowTemperature" | "minFlowTemperature" | "avgFlowTemperature" | "sumFlowTemperature" | "cntFlowTemperature" | "ssqFlowTemperature" | "inletFlowTemperature" | "maxInletFlowTemperature" | "minInletFlowTemperature" | "avgInletFlowTemperature" | "sumInletFlowTemperature" | "cntInletFlowTemperature" | "ssqInletFlowTemperature" | "outletFlowTemperature" | "maxOutletFlowTemperature" | "minOutletFlowTemperature" | "avgOutletFlowTemperature" | "sumOutletFlowTemperature" | "cntOutletFlowTemperature" | "ssqOutletFlowTemperature" | "inletMinusOutletFlowTemperatureDifference" | "maxInletMinusOutletFlowTemperatureDifference" | "minInletMinusOutletFlowTemperatureDifference" | "avgInletMinusOutletFlowTemperatureDifference" | "sumInletMinusOutletFlowTemperatureDifference" | "cntInletMinusOutletFlowTemperatureDifference" | "ssqInletMinusOutletFlowTemperatureDifference" | "outletMinusInletFlowTemperatureDifference" | "maxOutletMinusInletFlowTemperatureDifference" | "minOutletMinusInletFlowTemperatureDifference" | "avgOutletMinusInletFlowTemperatureDifference" | "sumOutletMinusInletFlowTemperatureDifference" | "cntOutletMinusInletFlowTemperatureDifference" | "ssqOutletMinusInletFlowTemperatureDifference" | "ambientTemperature" | "maxAmbientTemperature" | "minAmbientTemperature" | "avgAmbientTemperature" | "sumAmbientTemperature" | "cntAmbientTemperature" | "ssqAmbientTemperature" | "radiatorTemperature" | "maxRadiatorTemperature" | "minRadiatorTemperature" | "avgRadiatorTemperature" | "sumRadiatorTemperature" | "cntRadiatorTemperature" | "ssqRadiatorTemperature" | "auxFlowTemperature" | "maxAuxFlowTemperature" | "minAuxFlowTemperature" | "avgAuxFlowTemperature" | "sumAuxFlowTemperature" | "cntAuxFlowTemperature" | "ssqAuxFlowTemperature" | "relativeHumidity" | "kamstrupE8Inlet" | "diffKamstrupE8Inlet" | "cntKamstrupE8Inlet" | "kamstrupE9Outlet" | "diffKamstrupE9Outlet" | "cntKamstrupE9Outlet" | "operatingTime" | "diffOperatingTime" | "cntOperatingTime" | "errorOperatingTime" | "diffErrorOperatingTime" | "cntErrorOperatingTime" | "remainingBatteryTime" | "diffRemainingBatteryTime" | "cntRemainingBatteryTime" | "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/dist/esm/repository.d.ts
CHANGED
package/dist/esm/step.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { DateTime } from "luxon";
|
|
2
|
-
export
|
|
2
|
+
export type TimeOrRule = Date | DateTime | string;
|
|
3
3
|
export declare namespace TimeOrRule {
|
|
4
4
|
function isTime(input: TimeOrRule): input is Date | DateTime;
|
|
5
5
|
function isRule(input: TimeOrRule): input is string;
|
package/dist/esm/unit-id.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export
|
|
1
|
+
export type UnitId = "unitless" | "milliWattHour" | "wattHour" | "kiloWattHour" | "megaWattHour" | "gigaWattHour" | "calorie" | "kiloCalorie" | "megaCalorie" | "joule" | "kiloJoule" | "megaJoule" | "gigaJoule" | "liter" | "cubicMeter" | "fluidOunce" | "gallon" | "gram" | "kiloGram" | "tonne" | "ounce" | "pound" | "ton" | "second" | "minute" | "hour" | "day" | "week" | "month" | "year" | "milliWatt" | "milliWatt2" | "watt" | "watt2" | "kiloWatt" | "kiloWatt2" | "megaWatt" | "megaWatt2" | "gigaWatt" | "gigaWatt2" | "joulesPerSecond" | "joulesPerSecond2" | "joulesPerMinute" | "joulesPerMinute2" | "joulesPerHour" | "joulesPerHour2" | "litersPerSecond" | "litersPerSecond2" | "litersPerMinute" | "litersPerMinute2" | "litersPerHour" | "litersPerHour2" | "cubicMetersPerSecond" | "cubicMetersPerSecond2" | "cubicMetersPerMinute" | "cubicMetersPerMinute2" | "cubicMetersPerHour" | "cubicMetersPerHour2" | "fluidOuncesPerSecond" | "fluidOuncesPerSecond2" | "fluidOuncesPerMinute" | "fluidOuncesPerMinute2" | "fluidOuncesPerHour" | "fluidOuncesPerHour2" | "gallonsPerSecond" | "gallonsPerSecond2" | "gallonsPerMinute" | "gallonsPerMinute2" | "gallonsPerHour" | "gallonsPerHour2" | "gramsPerSecond" | "gramsPerSecond2" | "gramsPerMinute" | "gramsPerMinute2" | "gramsPerHour" | "gramsPerHour2" | "kiloGramsPerSecond" | "kiloGramsPerSecond2" | "kiloGramsPerMinute" | "kiloGramsPerMinute2" | "kiloGramsPerHour" | "kiloGramsPerHour2" | "celcius" | "kelvin" | "fahrenheit" | "var" | "kiloVar" | "megaVar" | "varHour" | "kiloVarHour" | "megaVarHour" | "percentage" | "literCelcius" | "cubicMeterCelcius";
|
|
2
2
|
export declare const energyUnits: UnitId[];
|
package/karma.config.js
CHANGED
|
@@ -129,6 +129,11 @@
|
|
|
129
129
|
"minRadiatorTemperature": ["Radiatortemperatur", "Radiatortemperatur, min"],
|
|
130
130
|
"avgRadiatorTemperature": ["Radiatortemperatur", "Radiatortemperatur, gns"],
|
|
131
131
|
|
|
132
|
+
"auxFlowTemperature": ["Flowtemperatur, alt", "Flowtemperatur, alt"],
|
|
133
|
+
"maxAuxFlowTemperature": ["Flowtemperatur, alt", "Flowtemperatur, alt, max"],
|
|
134
|
+
"minAuxFlowTemperature": ["Flowtemperatur, alt", "Flowtemperatur, alt, min"],
|
|
135
|
+
"avgAuxFlowTemperature": ["Flowtemperatur, alt", "Flowtemperatur, alt, gns"],
|
|
136
|
+
|
|
132
137
|
"relativeHumidity": ["Relativ luftfugtighed", "Relativ luftfugtighed"],
|
|
133
138
|
|
|
134
139
|
"kamstrupE8Inlet": ["Fremløbstemperaturenergi", "Fremløbstemperaturenergi (E8, T1)"],
|
|
@@ -129,6 +129,11 @@
|
|
|
129
129
|
"minRadiatorTemperature": ["Radiator temperature", "Radiator temperature, min"],
|
|
130
130
|
"avgRadiatorTemperature": ["Radiator temperature", "Radiator temperature, gns"],
|
|
131
131
|
|
|
132
|
+
"auxFlowTemperature": ["Flow temperature, aux", "Flow temperature, aux"],
|
|
133
|
+
"maxAuxFlowTemperature": ["Flow temperature, aux", "Flow temperature, aux, max"],
|
|
134
|
+
"minAuxFlowTemperature": ["Flow temperature, aux", "Flow temperature, aux, min"],
|
|
135
|
+
"avgAuxFlowTemperature": ["Flow temperature, aux", "Flow temperature, aux, gns"],
|
|
136
|
+
|
|
132
137
|
"relativeHumidity": ["Relative humidity", "Relative humidity"],
|
|
133
138
|
|
|
134
139
|
"kamstrupE8Inlet": ["Supply Temperature Energy", "Supply Temperature Energy (E8, T1)"],
|
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-65",
|
|
4
4
|
"main": "dist/cjs/index.js",
|
|
5
5
|
"module": "dist/esm/index.js",
|
|
6
6
|
"types": "dist/esm/index.d.ts",
|
|
@@ -17,20 +17,20 @@
|
|
|
17
17
|
},
|
|
18
18
|
"devDependencies": {
|
|
19
19
|
"@types/chai": "^4.2.18",
|
|
20
|
-
"@types/luxon": "^
|
|
21
|
-
"@types/mocha": "^
|
|
20
|
+
"@types/luxon": "^3.0.1",
|
|
21
|
+
"@types/mocha": "^10.0.0",
|
|
22
22
|
"chai": "^4.3.4",
|
|
23
23
|
"karma": "^6.3.2",
|
|
24
24
|
"karma-chai": "^0.1.0",
|
|
25
25
|
"karma-chrome-launcher": "^3.1.0",
|
|
26
26
|
"karma-mocha": "^2.0.1",
|
|
27
27
|
"karma-typescript": "^5.5.1",
|
|
28
|
-
"luxon": "^
|
|
29
|
-
"mocha": "^
|
|
28
|
+
"luxon": "^3.0.4",
|
|
29
|
+
"mocha": "^10.0.0",
|
|
30
30
|
"rimraf": "^3.0.2",
|
|
31
31
|
"typescript": "^4.1.5"
|
|
32
32
|
},
|
|
33
33
|
"peerDependencies": {
|
|
34
|
-
"luxon": "^2.1.0"
|
|
34
|
+
"luxon": "^2.1.0 || ^3.0.0"
|
|
35
35
|
}
|
|
36
36
|
}
|
|
@@ -1,99 +1,99 @@
|
|
|
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
|
-
[0, 0, { invariantPrecisionString: "0", invariantCoefficientString: "0", exponent: 0, noOfCoefficientDecimals: 0 }],
|
|
8
|
-
[0.0, 1, { invariantPrecisionString: "0.0", invariantCoefficientString: "0.0", exponent: 0, noOfCoefficientDecimals: 1 }],
|
|
9
|
-
[77.1274, 1, { invariantPrecisionString: "8e+1", invariantCoefficientString: "8", exponent: 1, noOfCoefficientDecimals: 0 }],
|
|
10
|
-
[77.1274, 2, { invariantPrecisionString: "77", invariantCoefficientString: "77", exponent: 0, noOfCoefficientDecimals: 0 }],
|
|
11
|
-
[77.1274, 4, { invariantPrecisionString: "77.13", invariantCoefficientString: "77.13", exponent: 0, noOfCoefficientDecimals: 2 }],
|
|
12
|
-
[77.1274, 5, { invariantPrecisionString: "77.127", invariantCoefficientString: "77.127", exponent: 0, noOfCoefficientDecimals: 3 }],
|
|
13
|
-
[77.1274, 6, { invariantPrecisionString: "77.1274", invariantCoefficientString: "77.1274", exponent: 0, noOfCoefficientDecimals: 4 }],
|
|
14
|
-
[77.1274, 7, { invariantPrecisionString: "77.12740", invariantCoefficientString: "77.12740", exponent: 0, noOfCoefficientDecimals: 5 }],
|
|
15
|
-
[77.1274, 15, { invariantPrecisionString: "77.1274000000000", invariantCoefficientString: "77.1274000000000", exponent: 0, noOfCoefficientDecimals: 13 }],
|
|
16
|
-
[77.1274, 16, { invariantPrecisionString: "77.12739999999999", invariantCoefficientString: "77.12739999999999", exponent: 0, noOfCoefficientDecimals: 14 }],
|
|
17
|
-
[77.1274, 17, { invariantPrecisionString: "77.127399999999994", invariantCoefficientString: "77.127399999999994", exponent: 0, noOfCoefficientDecimals: 15 }],
|
|
18
|
-
[77.1274, 18, { invariantPrecisionString: "77.1273999999999944", invariantCoefficientString: "77.1273999999999944", exponent: 0, noOfCoefficientDecimals: 16 }],
|
|
19
|
-
[77.1274, 19, { invariantPrecisionString: "77.12739999999999441", invariantCoefficientString: "77.12739999999999441", exponent: 0, noOfCoefficientDecimals: 17 }],
|
|
20
|
-
[77.1274, 20, { invariantPrecisionString: "77.127399999999994407", invariantCoefficientString: "77.127399999999994407", exponent: 0, noOfCoefficientDecimals: 18 }],
|
|
21
|
-
[77.1274, 21, { invariantPrecisionString: "77.1273999999999944066", invariantCoefficientString: "77.1273999999999944066", exponent: 0, noOfCoefficientDecimals: 19 }],
|
|
22
|
-
[0.00000123, 2, { invariantPrecisionString: "0.0000012", invariantCoefficientString: "0.0000012", exponent: 0, noOfCoefficientDecimals: 7 }],
|
|
23
|
-
[0.000000123, 2, { invariantPrecisionString: "1.2e-7", invariantCoefficientString: "1.2", exponent: -7, noOfCoefficientDecimals: 1 }],
|
|
24
|
-
[0.00000123, 21, { invariantPrecisionString: "0.00000123000000000000008198", invariantCoefficientString: "0.00000123000000000000008198", exponent: 0, noOfCoefficientDecimals: 26 }],
|
|
25
|
-
[123456, 3, { invariantPrecisionString: "1.23e+5", invariantCoefficientString: "1.23", exponent: 5, noOfCoefficientDecimals: 2 }],
|
|
26
|
-
[Number.MAX_VALUE, 3, { invariantPrecisionString: "1.80e+308", invariantCoefficientString: "1.80", exponent: 308, noOfCoefficientDecimals: 2 }],
|
|
27
|
-
[123456, 15, { invariantPrecisionString: "123456.000000000", invariantCoefficientString: "123456.000000000", exponent: 0, noOfCoefficientDecimals: 9 }],
|
|
28
|
-
[0, 5, { invariantPrecisionString: "0.00000", invariantCoefficientString: "0.00000", exponent: 0, noOfCoefficientDecimals: 5 }],
|
|
29
|
-
[0.000001, 1, { invariantPrecisionString: "0.000001", invariantCoefficientString: "0.000001", exponent: 0, noOfCoefficientDecimals: 6 }],
|
|
30
|
-
[0.000001, 10, { invariantPrecisionString: "0.000001000000000", invariantCoefficientString: "0.000001000000000", exponent: 0, noOfCoefficientDecimals: 15 }],
|
|
31
|
-
[0.0000001, 10, { invariantPrecisionString: "1.000000000e-7", invariantCoefficientString: "1.000000000", exponent: -7, noOfCoefficientDecimals: 9 }],
|
|
32
|
-
[0.00000000001, 1, { invariantPrecisionString: "1e-11", invariantCoefficientString: "1", exponent: -11, noOfCoefficientDecimals: 0 }],
|
|
33
|
-
[0.00000000001, 2, { invariantPrecisionString: "1.0e-11", invariantCoefficientString: "1.0", exponent: -11, noOfCoefficientDecimals: 1 }],
|
|
34
|
-
[55, 2, { invariantPrecisionString: "55", invariantCoefficientString: "55", exponent: 0, noOfCoefficientDecimals: 0 }],
|
|
35
|
-
[55, 1, { invariantPrecisionString: "6e+1", invariantCoefficientString: "6", exponent: 1, noOfCoefficientDecimals: 0 }],
|
|
36
|
-
[-55, 2, { invariantPrecisionString: "-55", invariantCoefficientString: "-55", exponent: 0, noOfCoefficientDecimals: 0 }],
|
|
37
|
-
[-55, 1, { invariantPrecisionString: "-6e+1", invariantCoefficientString: "-6", exponent: 1, noOfCoefficientDecimals: 0 }],
|
|
38
|
-
[9.59, 1, { invariantPrecisionString: "1e+1", invariantCoefficientString: "1", exponent: 1, noOfCoefficientDecimals: 0 }],
|
|
39
|
-
[9.59, 2, { invariantPrecisionString: "10", invariantCoefficientString: "10", exponent: 0, noOfCoefficientDecimals: 0 }],
|
|
40
|
-
[9.95, 2, { invariantPrecisionString: "10", invariantCoefficientString: "10", exponent: 0, noOfCoefficientDecimals: 0 }],
|
|
41
|
-
[9.96, 2, { invariantPrecisionString: "10", invariantCoefficientString: "10", exponent: 0, noOfCoefficientDecimals: 0 }],
|
|
42
|
-
[-555555555555555555555, 1, { invariantPrecisionString: "-6e+20", invariantCoefficientString: "-6", exponent: 20, noOfCoefficientDecimals: 0 }],
|
|
43
|
-
[-5555555555555555555555, 1, { invariantPrecisionString: "-6e+21", invariantCoefficientString: "-6", exponent: 21, noOfCoefficientDecimals: 0 }],
|
|
44
|
-
[18014398509482012, 21, { invariantPrecisionString: "18014398509482012.0000", invariantCoefficientString: "18014398509482012.0000", exponent: 0, noOfCoefficientDecimals: 4 }],
|
|
45
|
-
[180143985094820121234, 21, { invariantPrecisionString: "180143985094820134912", invariantCoefficientString: "180143985094820134912", exponent: 0, noOfCoefficientDecimals: 0 }],
|
|
46
|
-
[0.1, 21, { invariantPrecisionString: "0.100000000000000005551", invariantCoefficientString: "0.100000000000000005551", exponent: 0, noOfCoefficientDecimals: 21 }],
|
|
47
|
-
[Number.EPSILON, 2, { invariantPrecisionString: "2.2e-16", invariantCoefficientString: "2.2", exponent: -16, noOfCoefficientDecimals: 1 }],
|
|
48
|
-
[Number.EPSILON, 21, { invariantPrecisionString: "2.22044604925031308085e-16", invariantCoefficientString: "2.22044604925031308085", exponent: -16, noOfCoefficientDecimals: 20 }],
|
|
49
|
-
[Number.NaN, 3, { invariantPrecisionString: "NaN", invariantCoefficientString: "NaN", exponent: 0, noOfCoefficientDecimals: 0 }],
|
|
50
|
-
[Number.POSITIVE_INFINITY, 3, { invariantPrecisionString: "Infinity", invariantCoefficientString: "Infinity", exponent: 0, noOfCoefficientDecimals: 0 }],
|
|
51
|
-
[Number.NEGATIVE_INFINITY, 3, { invariantPrecisionString: "-Infinity", invariantCoefficientString: "-Infinity", exponent: 0, noOfCoefficientDecimals: 0 }],
|
|
52
|
-
[164735280, 7, { invariantPrecisionString: "1.647353e+8", invariantCoefficientString: "1.647353", exponent: 8, noOfCoefficientDecimals: 6 }],
|
|
53
|
-
];
|
|
54
|
-
|
|
55
|
-
for (const [value, precision, expectedParts] of data) {
|
|
56
|
-
expect(NumberFormatter.toPrecision(value, precision)).to.equal(expectedParts.invariantPrecisionString);
|
|
57
|
-
|
|
58
|
-
const localePrecisionString = expectedParts.invariantPrecisionString.replace(invariantDecimalSeparator, localeDecimalSeparator);
|
|
59
|
-
expect(NumberFormatter.toLocalePrecision(value, precision)).to.equal(localePrecisionString);
|
|
60
|
-
|
|
61
|
-
const parts = NumberFormatter.getParts(value, precision);
|
|
62
|
-
expect(parts.invariantPrecisionString).to.equal(expectedParts.invariantPrecisionString);
|
|
63
|
-
expect(parts.invariantCoefficientString).to.equal(expectedParts.invariantCoefficientString);
|
|
64
|
-
expect(parts.exponent).to.equal(expectedParts.exponent);
|
|
65
|
-
expect(parts.noOfCoefficientDecimals).to.equal(expectedParts.noOfCoefficientDecimals);
|
|
66
|
-
}
|
|
67
|
-
})
|
|
68
|
-
})
|
|
69
|
-
|
|
70
|
-
describe("toFixed", () => {
|
|
71
|
-
it("should format correctly", () => {
|
|
72
|
-
const data: [number, number, string][] = [
|
|
73
|
-
[0, 0, "0"],
|
|
74
|
-
[0.0, 1, "0.0"],
|
|
75
|
-
[10, 1, "10"],
|
|
76
|
-
[10, 2, "10"],
|
|
77
|
-
[10, 3, "10.0"],
|
|
78
|
-
[10, 4, "10.00"],
|
|
79
|
-
[10.2, 1, "10"],
|
|
80
|
-
[10.2, 2, "10"],
|
|
81
|
-
[10.2, 3, "10.2"],
|
|
82
|
-
[10.2, 4, "10.20"],
|
|
83
|
-
[217887000000, 6, "217887000000"],
|
|
84
|
-
];
|
|
85
|
-
|
|
86
|
-
for (const [value, precision, invariantExpected] of data) {
|
|
87
|
-
expect(NumberFormatter.toFixed(value, precision)).to.equal(invariantExpected);
|
|
88
|
-
|
|
89
|
-
const localeExpected = invariantExpected.replace(invariantDecimalSeparator, localeDecimalSeparator);
|
|
90
|
-
expect(NumberFormatter.toLocaleFixed(value, precision)).to.equal(localeExpected);
|
|
91
|
-
}
|
|
92
|
-
})
|
|
93
|
-
})
|
|
94
|
-
|
|
95
|
-
describe("toLocaleFixed", () => {
|
|
96
|
-
it("should format correctly", () => {
|
|
97
|
-
expect(NumberFormatter.toLocaleFixed(400, 2)).to.equal("400");
|
|
98
|
-
})
|
|
99
|
-
});
|
|
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
|
+
[0, 0, { invariantPrecisionString: "0", invariantCoefficientString: "0", exponent: 0, noOfCoefficientDecimals: 0 }],
|
|
8
|
+
[0.0, 1, { invariantPrecisionString: "0.0", invariantCoefficientString: "0.0", exponent: 0, noOfCoefficientDecimals: 1 }],
|
|
9
|
+
[77.1274, 1, { invariantPrecisionString: "8e+1", invariantCoefficientString: "8", exponent: 1, noOfCoefficientDecimals: 0 }],
|
|
10
|
+
[77.1274, 2, { invariantPrecisionString: "77", invariantCoefficientString: "77", exponent: 0, noOfCoefficientDecimals: 0 }],
|
|
11
|
+
[77.1274, 4, { invariantPrecisionString: "77.13", invariantCoefficientString: "77.13", exponent: 0, noOfCoefficientDecimals: 2 }],
|
|
12
|
+
[77.1274, 5, { invariantPrecisionString: "77.127", invariantCoefficientString: "77.127", exponent: 0, noOfCoefficientDecimals: 3 }],
|
|
13
|
+
[77.1274, 6, { invariantPrecisionString: "77.1274", invariantCoefficientString: "77.1274", exponent: 0, noOfCoefficientDecimals: 4 }],
|
|
14
|
+
[77.1274, 7, { invariantPrecisionString: "77.12740", invariantCoefficientString: "77.12740", exponent: 0, noOfCoefficientDecimals: 5 }],
|
|
15
|
+
[77.1274, 15, { invariantPrecisionString: "77.1274000000000", invariantCoefficientString: "77.1274000000000", exponent: 0, noOfCoefficientDecimals: 13 }],
|
|
16
|
+
[77.1274, 16, { invariantPrecisionString: "77.12739999999999", invariantCoefficientString: "77.12739999999999", exponent: 0, noOfCoefficientDecimals: 14 }],
|
|
17
|
+
[77.1274, 17, { invariantPrecisionString: "77.127399999999994", invariantCoefficientString: "77.127399999999994", exponent: 0, noOfCoefficientDecimals: 15 }],
|
|
18
|
+
[77.1274, 18, { invariantPrecisionString: "77.1273999999999944", invariantCoefficientString: "77.1273999999999944", exponent: 0, noOfCoefficientDecimals: 16 }],
|
|
19
|
+
[77.1274, 19, { invariantPrecisionString: "77.12739999999999441", invariantCoefficientString: "77.12739999999999441", exponent: 0, noOfCoefficientDecimals: 17 }],
|
|
20
|
+
[77.1274, 20, { invariantPrecisionString: "77.127399999999994407", invariantCoefficientString: "77.127399999999994407", exponent: 0, noOfCoefficientDecimals: 18 }],
|
|
21
|
+
[77.1274, 21, { invariantPrecisionString: "77.1273999999999944066", invariantCoefficientString: "77.1273999999999944066", exponent: 0, noOfCoefficientDecimals: 19 }],
|
|
22
|
+
[0.00000123, 2, { invariantPrecisionString: "0.0000012", invariantCoefficientString: "0.0000012", exponent: 0, noOfCoefficientDecimals: 7 }],
|
|
23
|
+
[0.000000123, 2, { invariantPrecisionString: "1.2e-7", invariantCoefficientString: "1.2", exponent: -7, noOfCoefficientDecimals: 1 }],
|
|
24
|
+
[0.00000123, 21, { invariantPrecisionString: "0.00000123000000000000008198", invariantCoefficientString: "0.00000123000000000000008198", exponent: 0, noOfCoefficientDecimals: 26 }],
|
|
25
|
+
[123456, 3, { invariantPrecisionString: "1.23e+5", invariantCoefficientString: "1.23", exponent: 5, noOfCoefficientDecimals: 2 }],
|
|
26
|
+
[Number.MAX_VALUE, 3, { invariantPrecisionString: "1.80e+308", invariantCoefficientString: "1.80", exponent: 308, noOfCoefficientDecimals: 2 }],
|
|
27
|
+
[123456, 15, { invariantPrecisionString: "123456.000000000", invariantCoefficientString: "123456.000000000", exponent: 0, noOfCoefficientDecimals: 9 }],
|
|
28
|
+
[0, 5, { invariantPrecisionString: "0.00000", invariantCoefficientString: "0.00000", exponent: 0, noOfCoefficientDecimals: 5 }],
|
|
29
|
+
[0.000001, 1, { invariantPrecisionString: "0.000001", invariantCoefficientString: "0.000001", exponent: 0, noOfCoefficientDecimals: 6 }],
|
|
30
|
+
[0.000001, 10, { invariantPrecisionString: "0.000001000000000", invariantCoefficientString: "0.000001000000000", exponent: 0, noOfCoefficientDecimals: 15 }],
|
|
31
|
+
[0.0000001, 10, { invariantPrecisionString: "1.000000000e-7", invariantCoefficientString: "1.000000000", exponent: -7, noOfCoefficientDecimals: 9 }],
|
|
32
|
+
[0.00000000001, 1, { invariantPrecisionString: "1e-11", invariantCoefficientString: "1", exponent: -11, noOfCoefficientDecimals: 0 }],
|
|
33
|
+
[0.00000000001, 2, { invariantPrecisionString: "1.0e-11", invariantCoefficientString: "1.0", exponent: -11, noOfCoefficientDecimals: 1 }],
|
|
34
|
+
[55, 2, { invariantPrecisionString: "55", invariantCoefficientString: "55", exponent: 0, noOfCoefficientDecimals: 0 }],
|
|
35
|
+
[55, 1, { invariantPrecisionString: "6e+1", invariantCoefficientString: "6", exponent: 1, noOfCoefficientDecimals: 0 }],
|
|
36
|
+
[-55, 2, { invariantPrecisionString: "-55", invariantCoefficientString: "-55", exponent: 0, noOfCoefficientDecimals: 0 }],
|
|
37
|
+
[-55, 1, { invariantPrecisionString: "-6e+1", invariantCoefficientString: "-6", exponent: 1, noOfCoefficientDecimals: 0 }],
|
|
38
|
+
[9.59, 1, { invariantPrecisionString: "1e+1", invariantCoefficientString: "1", exponent: 1, noOfCoefficientDecimals: 0 }],
|
|
39
|
+
[9.59, 2, { invariantPrecisionString: "10", invariantCoefficientString: "10", exponent: 0, noOfCoefficientDecimals: 0 }],
|
|
40
|
+
[9.95, 2, { invariantPrecisionString: "10", invariantCoefficientString: "10", exponent: 0, noOfCoefficientDecimals: 0 }],
|
|
41
|
+
[9.96, 2, { invariantPrecisionString: "10", invariantCoefficientString: "10", exponent: 0, noOfCoefficientDecimals: 0 }],
|
|
42
|
+
[-555555555555555555555, 1, { invariantPrecisionString: "-6e+20", invariantCoefficientString: "-6", exponent: 20, noOfCoefficientDecimals: 0 }],
|
|
43
|
+
[-5555555555555555555555, 1, { invariantPrecisionString: "-6e+21", invariantCoefficientString: "-6", exponent: 21, noOfCoefficientDecimals: 0 }],
|
|
44
|
+
[18014398509482012, 21, { invariantPrecisionString: "18014398509482012.0000", invariantCoefficientString: "18014398509482012.0000", exponent: 0, noOfCoefficientDecimals: 4 }],
|
|
45
|
+
[180143985094820121234, 21, { invariantPrecisionString: "180143985094820134912", invariantCoefficientString: "180143985094820134912", exponent: 0, noOfCoefficientDecimals: 0 }],
|
|
46
|
+
[0.1, 21, { invariantPrecisionString: "0.100000000000000005551", invariantCoefficientString: "0.100000000000000005551", exponent: 0, noOfCoefficientDecimals: 21 }],
|
|
47
|
+
[Number.EPSILON, 2, { invariantPrecisionString: "2.2e-16", invariantCoefficientString: "2.2", exponent: -16, noOfCoefficientDecimals: 1 }],
|
|
48
|
+
[Number.EPSILON, 21, { invariantPrecisionString: "2.22044604925031308085e-16", invariantCoefficientString: "2.22044604925031308085", exponent: -16, noOfCoefficientDecimals: 20 }],
|
|
49
|
+
[Number.NaN, 3, { invariantPrecisionString: "NaN", invariantCoefficientString: "NaN", exponent: 0, noOfCoefficientDecimals: 0 }],
|
|
50
|
+
[Number.POSITIVE_INFINITY, 3, { invariantPrecisionString: "Infinity", invariantCoefficientString: "Infinity", exponent: 0, noOfCoefficientDecimals: 0 }],
|
|
51
|
+
[Number.NEGATIVE_INFINITY, 3, { invariantPrecisionString: "-Infinity", invariantCoefficientString: "-Infinity", exponent: 0, noOfCoefficientDecimals: 0 }],
|
|
52
|
+
[164735280, 7, { invariantPrecisionString: "1.647353e+8", invariantCoefficientString: "1.647353", exponent: 8, noOfCoefficientDecimals: 6 }],
|
|
53
|
+
];
|
|
54
|
+
|
|
55
|
+
for (const [value, precision, expectedParts] of data) {
|
|
56
|
+
expect(NumberFormatter.toPrecision(value, precision)).to.equal(expectedParts.invariantPrecisionString);
|
|
57
|
+
|
|
58
|
+
const localePrecisionString = expectedParts.invariantPrecisionString.replace(invariantDecimalSeparator, localeDecimalSeparator);
|
|
59
|
+
expect(NumberFormatter.toLocalePrecision(value, precision)).to.equal(localePrecisionString);
|
|
60
|
+
|
|
61
|
+
const parts = NumberFormatter.getParts(value, precision);
|
|
62
|
+
expect(parts.invariantPrecisionString).to.equal(expectedParts.invariantPrecisionString);
|
|
63
|
+
expect(parts.invariantCoefficientString).to.equal(expectedParts.invariantCoefficientString);
|
|
64
|
+
expect(parts.exponent).to.equal(expectedParts.exponent);
|
|
65
|
+
expect(parts.noOfCoefficientDecimals).to.equal(expectedParts.noOfCoefficientDecimals);
|
|
66
|
+
}
|
|
67
|
+
})
|
|
68
|
+
})
|
|
69
|
+
|
|
70
|
+
describe("toFixed", () => {
|
|
71
|
+
it("should format correctly", () => {
|
|
72
|
+
const data: [number, number, string][] = [
|
|
73
|
+
[0, 0, "0"],
|
|
74
|
+
[0.0, 1, "0.0"],
|
|
75
|
+
[10, 1, "10"],
|
|
76
|
+
[10, 2, "10"],
|
|
77
|
+
[10, 3, "10.0"],
|
|
78
|
+
[10, 4, "10.00"],
|
|
79
|
+
[10.2, 1, "10"],
|
|
80
|
+
[10.2, 2, "10"],
|
|
81
|
+
[10.2, 3, "10.2"],
|
|
82
|
+
[10.2, 4, "10.20"],
|
|
83
|
+
[217887000000, 6, "217887000000"],
|
|
84
|
+
];
|
|
85
|
+
|
|
86
|
+
for (const [value, precision, invariantExpected] of data) {
|
|
87
|
+
expect(NumberFormatter.toFixed(value, precision)).to.equal(invariantExpected);
|
|
88
|
+
|
|
89
|
+
const localeExpected = invariantExpected.replace(invariantDecimalSeparator, localeDecimalSeparator);
|
|
90
|
+
expect(NumberFormatter.toLocaleFixed(value, precision)).to.equal(localeExpected);
|
|
91
|
+
}
|
|
92
|
+
})
|
|
93
|
+
})
|
|
94
|
+
|
|
95
|
+
describe("toLocaleFixed", () => {
|
|
96
|
+
it("should format correctly", () => {
|
|
97
|
+
expect(NumberFormatter.toLocaleFixed(400, 2)).to.equal("400");
|
|
98
|
+
})
|
|
99
|
+
});
|
package/src/number-formatter.ts
CHANGED
|
@@ -1,88 +1,88 @@
|
|
|
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
|
-
invariantCoefficientString: string;
|
|
7
|
-
exponent: number;
|
|
8
|
-
noOfCoefficientDecimals: number;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
class Formatter {
|
|
12
|
-
public toPrecision(value: number, precision: number) {
|
|
13
|
-
if (value === 0) {
|
|
14
|
-
// (0).toPrecision(1) == "0"
|
|
15
|
-
// (0).toPrecision(2) == "0.0"
|
|
16
|
-
// (0).toPrecision(3) == "0.00"
|
|
17
|
-
// However, we want:
|
|
18
|
-
// Formatter.toPrecision(0, 0) == "0"
|
|
19
|
-
// Formatter.toPrecision(0, 1) == "0.0"
|
|
20
|
-
// Formatter.toPrecision(0, 2) == "0.00"
|
|
21
|
-
return value.toPrecision(precision + 1);
|
|
22
|
-
}
|
|
23
|
-
const absValue = Math.abs(value);
|
|
24
|
-
if (absValue < 1) {
|
|
25
|
-
return value.toPrecision(precision);
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
const noOfIntegerDigits = Math.round(absValue).toString().length;
|
|
29
|
-
return precision >= noOfIntegerDigits
|
|
30
|
-
? value.toFixed(precision - noOfIntegerDigits)
|
|
31
|
-
: value.toPrecision(precision);
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
public toLocalePrecision(value: number, precision: number) {
|
|
35
|
-
return this.toPrecision(value, precision)
|
|
36
|
-
.replace(invariantDecimalSeparator, localeDecimalSeparator);
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
public toFixed(value: number, precision: number) {
|
|
40
|
-
const parts = this.getParts(value, precision);
|
|
41
|
-
if (parts.noOfCoefficientDecimals < parts.exponent) {
|
|
42
|
-
const number = parts.invariantCoefficientString.replace(invariantDecimalSeparator, "");
|
|
43
|
-
const zeros = parts.exponent - parts.noOfCoefficientDecimals;
|
|
44
|
-
return `${number}${"0".repeat(zeros)}`;
|
|
45
|
-
}
|
|
46
|
-
else {
|
|
47
|
-
return parts.invariantPrecisionString;
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
public toLocaleFixed(value: number, precision: number) {
|
|
52
|
-
return this.toFixed(value, precision)
|
|
53
|
-
.replace(invariantDecimalSeparator, localeDecimalSeparator);
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
public getParts(value: number, precision: number): Parts {
|
|
57
|
-
const precisionString = this.toPrecision(value, precision);
|
|
58
|
-
if (precisionString.indexOf("e") >= 0) {
|
|
59
|
-
const match = /(.*)e([\+-]\d+)$/.exec(precisionString)!;
|
|
60
|
-
const coefficientString = match[1];
|
|
61
|
-
const exponent = parseInt(match[2]);
|
|
62
|
-
const decimalSeparatorIndex = coefficientString.indexOf(invariantDecimalSeparator);
|
|
63
|
-
const noOfCoefficientDecimals = decimalSeparatorIndex >= 0
|
|
64
|
-
? coefficientString.length - decimalSeparatorIndex - 1
|
|
65
|
-
: 0;
|
|
66
|
-
return {
|
|
67
|
-
invariantPrecisionString: precisionString,
|
|
68
|
-
invariantCoefficientString: coefficientString,
|
|
69
|
-
exponent,
|
|
70
|
-
noOfCoefficientDecimals,
|
|
71
|
-
};
|
|
72
|
-
}
|
|
73
|
-
else {
|
|
74
|
-
const decimalSeparatorIndex = precisionString.indexOf(invariantDecimalSeparator);
|
|
75
|
-
const noOfCoefficientDecimals = decimalSeparatorIndex >= 0
|
|
76
|
-
? precisionString.length - decimalSeparatorIndex - 1
|
|
77
|
-
: 0;
|
|
78
|
-
return {
|
|
79
|
-
invariantPrecisionString: precisionString,
|
|
80
|
-
invariantCoefficientString: precisionString,
|
|
81
|
-
exponent: 0,
|
|
82
|
-
noOfCoefficientDecimals,
|
|
83
|
-
};
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
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
|
+
invariantCoefficientString: string;
|
|
7
|
+
exponent: number;
|
|
8
|
+
noOfCoefficientDecimals: number;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
class Formatter {
|
|
12
|
+
public toPrecision(value: number, precision: number) {
|
|
13
|
+
if (value === 0) {
|
|
14
|
+
// (0).toPrecision(1) == "0"
|
|
15
|
+
// (0).toPrecision(2) == "0.0"
|
|
16
|
+
// (0).toPrecision(3) == "0.00"
|
|
17
|
+
// However, we want:
|
|
18
|
+
// Formatter.toPrecision(0, 0) == "0"
|
|
19
|
+
// Formatter.toPrecision(0, 1) == "0.0"
|
|
20
|
+
// Formatter.toPrecision(0, 2) == "0.00"
|
|
21
|
+
return value.toPrecision(precision + 1);
|
|
22
|
+
}
|
|
23
|
+
const absValue = Math.abs(value);
|
|
24
|
+
if (absValue < 1) {
|
|
25
|
+
return value.toPrecision(precision);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const noOfIntegerDigits = Math.round(absValue).toString().length;
|
|
29
|
+
return precision >= noOfIntegerDigits
|
|
30
|
+
? value.toFixed(precision - noOfIntegerDigits)
|
|
31
|
+
: value.toPrecision(precision);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
public toLocalePrecision(value: number, precision: number) {
|
|
35
|
+
return this.toPrecision(value, precision)
|
|
36
|
+
.replace(invariantDecimalSeparator, localeDecimalSeparator);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
public toFixed(value: number, precision: number) {
|
|
40
|
+
const parts = this.getParts(value, precision);
|
|
41
|
+
if (parts.noOfCoefficientDecimals < parts.exponent) {
|
|
42
|
+
const number = parts.invariantCoefficientString.replace(invariantDecimalSeparator, "");
|
|
43
|
+
const zeros = parts.exponent - parts.noOfCoefficientDecimals;
|
|
44
|
+
return `${number}${"0".repeat(zeros)}`;
|
|
45
|
+
}
|
|
46
|
+
else {
|
|
47
|
+
return parts.invariantPrecisionString;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
public toLocaleFixed(value: number, precision: number) {
|
|
52
|
+
return this.toFixed(value, precision)
|
|
53
|
+
.replace(invariantDecimalSeparator, localeDecimalSeparator);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
public getParts(value: number, precision: number): Parts {
|
|
57
|
+
const precisionString = this.toPrecision(value, precision);
|
|
58
|
+
if (precisionString.indexOf("e") >= 0) {
|
|
59
|
+
const match = /(.*)e([\+-]\d+)$/.exec(precisionString)!;
|
|
60
|
+
const coefficientString = match[1];
|
|
61
|
+
const exponent = parseInt(match[2]);
|
|
62
|
+
const decimalSeparatorIndex = coefficientString.indexOf(invariantDecimalSeparator);
|
|
63
|
+
const noOfCoefficientDecimals = decimalSeparatorIndex >= 0
|
|
64
|
+
? coefficientString.length - decimalSeparatorIndex - 1
|
|
65
|
+
: 0;
|
|
66
|
+
return {
|
|
67
|
+
invariantPrecisionString: precisionString,
|
|
68
|
+
invariantCoefficientString: coefficientString,
|
|
69
|
+
exponent,
|
|
70
|
+
noOfCoefficientDecimals,
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
else {
|
|
74
|
+
const decimalSeparatorIndex = precisionString.indexOf(invariantDecimalSeparator);
|
|
75
|
+
const noOfCoefficientDecimals = decimalSeparatorIndex >= 0
|
|
76
|
+
? precisionString.length - decimalSeparatorIndex - 1
|
|
77
|
+
: 0;
|
|
78
|
+
return {
|
|
79
|
+
invariantPrecisionString: precisionString,
|
|
80
|
+
invariantCoefficientString: precisionString,
|
|
81
|
+
exponent: 0,
|
|
82
|
+
noOfCoefficientDecimals,
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export const NumberFormatter = new Formatter();
|
|
@@ -1,28 +1,28 @@
|
|
|
1
|
-
import { expect } from "chai";
|
|
2
|
-
import { PrecisionNumber } from "./precision-number";
|
|
3
|
-
|
|
4
|
-
describe("precision-number", () => {
|
|
5
|
-
it("should not parse ''", () => {
|
|
6
|
-
const number = PrecisionNumber.parse("");
|
|
7
|
-
expect(Number.isNaN(number.value)).to.equal(true);
|
|
8
|
-
expect(number.precision).to.equal(0);
|
|
9
|
-
});
|
|
10
|
-
|
|
11
|
-
it("should parse '1'", () => {
|
|
12
|
-
const number = PrecisionNumber.parse("1");
|
|
13
|
-
expect(number.value).to.equal(1);
|
|
14
|
-
expect(number.precision).to.equal(1);
|
|
15
|
-
});
|
|
16
|
-
|
|
17
|
-
it("should parse '1.'", () => {
|
|
18
|
-
const number = PrecisionNumber.parse("1.");
|
|
19
|
-
expect(number.value).to.equal(1);
|
|
20
|
-
expect(number.precision).to.equal(2);
|
|
21
|
-
});
|
|
22
|
-
|
|
23
|
-
it("should parse '1.0'", () => {
|
|
24
|
-
const number = PrecisionNumber.parse("1.0");
|
|
25
|
-
expect(number.value).to.equal(1);
|
|
26
|
-
expect(number.precision).to.equal(2);
|
|
27
|
-
});
|
|
28
|
-
});
|
|
1
|
+
import { expect } from "chai";
|
|
2
|
+
import { PrecisionNumber } from "./precision-number";
|
|
3
|
+
|
|
4
|
+
describe("precision-number", () => {
|
|
5
|
+
it("should not parse ''", () => {
|
|
6
|
+
const number = PrecisionNumber.parse("");
|
|
7
|
+
expect(Number.isNaN(number.value)).to.equal(true);
|
|
8
|
+
expect(number.precision).to.equal(0);
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
it("should parse '1'", () => {
|
|
12
|
+
const number = PrecisionNumber.parse("1");
|
|
13
|
+
expect(number.value).to.equal(1);
|
|
14
|
+
expect(number.precision).to.equal(1);
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
it("should parse '1.'", () => {
|
|
18
|
+
const number = PrecisionNumber.parse("1.");
|
|
19
|
+
expect(number.value).to.equal(1);
|
|
20
|
+
expect(number.precision).to.equal(2);
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it("should parse '1.0'", () => {
|
|
24
|
+
const number = PrecisionNumber.parse("1.0");
|
|
25
|
+
expect(number.value).to.equal(1);
|
|
26
|
+
expect(number.precision).to.equal(2);
|
|
27
|
+
});
|
|
28
|
+
});
|