From 65cbe11757b4aa6061c4228d6bc4f44b59e27beb Mon Sep 17 00:00:00 2001 From: Jonah Scheinerman Date: Thu, 16 May 2024 23:33:03 -0400 Subject: [PATCH] Add valueIn method (#185) --- src/measure/__test__/numberMeasureTests.ts | 6 ++++++ src/measure/genericMeasure.ts | 8 ++++++++ src/measure/genericMeasureClass.ts | 4 ++++ test/{exponents.ts => exponentTypeTests.ts} | 0 test/{measures.ts => measureTypeTests.ts} | 6 ++++++ test/{units.ts => unitTypeTests.ts} | 0 6 files changed, 24 insertions(+) rename test/{exponents.ts => exponentTypeTests.ts} (100%) rename test/{measures.ts => measureTypeTests.ts} (91%) rename test/{units.ts => unitTypeTests.ts} (100%) diff --git a/src/measure/__test__/numberMeasureTests.ts b/src/measure/__test__/numberMeasureTests.ts index 173586b..d7d8aa6 100644 --- a/src/measure/__test__/numberMeasureTests.ts +++ b/src/measure/__test__/numberMeasureTests.ts @@ -287,6 +287,12 @@ describe("Number measures", () => { expect(Measure.of(1000, meters).in(kilometers)).toBe("1000 m"); }); + it("should get value in another unit", () => { + const kilometers = Measure.of(1000, meters); + expect(Measure.of(2000, meters).valueIn(kilometers)).toBe(2); + expect(Measure.of(500, meters).valueIn(kilometers)).toBe(0.5); + }); + it("should use base unit symbols to format when available", () => { const m = Measure.dimension("test-length", "meter"); const s = Measure.dimension("test-time", "second"); diff --git a/src/measure/genericMeasure.ts b/src/measure/genericMeasure.ts index aeb38ae..9a4aa13 100644 --- a/src/measure/genericMeasure.ts +++ b/src/measure/genericMeasure.ts @@ -193,6 +193,14 @@ export interface GenericMeasure { */ in(unit: GenericMeasure, formatter?: MeasureFormatter): string; + /** + * Returns the value of this measure as a product of another unit. This can be used to quickly convert a measure to + * that unit and extract its underlying value. + * @param unit a measure of the same unit to convert this measure into + * @returns the numeric value of this unit expressed in the given unit + */ + valueIn(unit: GenericMeasure): N; + /** * Adds a symbol to this measure. * @param symbol the symbol of the unit represented by this measure diff --git a/src/measure/genericMeasureClass.ts b/src/measure/genericMeasureClass.ts index 3982a07..337cd27 100644 --- a/src/measure/genericMeasureClass.ts +++ b/src/measure/genericMeasureClass.ts @@ -147,6 +147,10 @@ export function createMeasureClass(num: NumericOperations): GenericMeasure return `${value} ${unit.symbol}`; } + public valueIn(unit: GenericMeasure): N { + return num.div(this.value, unit.value); + } + public withSymbol(symbol: string | undefined): GenericMeasure { return new Measure(this.value, this.unit, symbol); } diff --git a/test/exponents.ts b/test/exponentTypeTests.ts similarity index 100% rename from test/exponents.ts rename to test/exponentTypeTests.ts diff --git a/test/measures.ts b/test/measureTypeTests.ts similarity index 91% rename from test/measures.ts rename to test/measureTypeTests.ts index 9b4f7b4..786cdbf 100644 --- a/test/measures.ts +++ b/test/measureTypeTests.ts @@ -58,3 +58,9 @@ area.minus(volume); // TODO(jscheinerman): This should be an error but is not because of structural typing length.plus(velocity); + +// TODO(jscheinerman): This should be an error but is not because of structural typing +length.valueIn(velocity); + +// TODO(jscheinerman): This should be an error but is not because of structural typing +export const test: Length = velocity; diff --git a/test/units.ts b/test/unitTypeTests.ts similarity index 100% rename from test/units.ts rename to test/unitTypeTests.ts