From 0e1011927d28c271f018a79c29d45043d0005730 Mon Sep 17 00:00:00 2001 From: Miguel Prada Date: Sun, 7 May 2023 14:51:44 -0500 Subject: [PATCH] add ability to do arithmetics with unit classes --- src/units/force/force.ts | 23 +++++++++++------------ src/units/length/length.ts | 7 +++---- src/units/stress/stress.ts | 7 +++---- src/units/time/time.ts | 7 +++---- src/units/unit/index.ts | 1 + src/units/unit/unit.test.ts | 11 +++++++++++ src/units/unit/unit.ts | 13 +++++++++++++ 7 files changed, 45 insertions(+), 24 deletions(-) create mode 100644 src/units/unit/index.ts create mode 100644 src/units/unit/unit.test.ts create mode 100644 src/units/unit/unit.ts diff --git a/src/units/force/force.ts b/src/units/force/force.ts index ce7c61d..ef8eed2 100644 --- a/src/units/force/force.ts +++ b/src/units/force/force.ts @@ -1,42 +1,41 @@ +import Unit from '../unit'; import type { ForceUnit } from './types'; -class Force { - private stdUnit: number; - +class Force extends Unit { constructor(value: number, unit: ForceUnit) { - this.stdUnit = Force.#parseToStd(value, unit); + super(Force.#parseToStd(value, unit)); } N() { - return this.stdUnit; + return this.stdValue; } kN() { - return this.stdUnit / 1000; + return this.stdValue / 1000; } lbf() { - return this.stdUnit / 4.448; + return this.stdValue / 4.448; } kip() { - return this.stdUnit / 4448.222; + return this.stdValue / 4448.222; } kgf() { - return this.stdUnit / 9.807; + return this.stdValue / 9.807; } MN() { - return this.stdUnit / 1000000; + return this.stdValue / 1000000; } tonf() { - return this.stdUnit / 9807; + return this.stdValue / 9807; } set(value: number, unit: ForceUnit) { - this.stdUnit = Force.#parseToStd(value, unit); + this.stdValue = Force.#parseToStd(value, unit); } static #parseToStd(value: number, unit: ForceUnit) { diff --git a/src/units/length/length.ts b/src/units/length/length.ts index 0b0fbd9..b06def4 100644 --- a/src/units/length/length.ts +++ b/src/units/length/length.ts @@ -1,10 +1,9 @@ +import Unit from '../unit'; import type { LengthUnit } from './types'; -class Length { - private stdValue: number; - +class Length extends Unit { constructor(value: number, unit: LengthUnit) { - this.stdValue = Length.#parseToStd(value, unit); + super(Length.#parseToStd(value, unit)); } mm() { diff --git a/src/units/stress/stress.ts b/src/units/stress/stress.ts index f3358f4..daf9207 100644 --- a/src/units/stress/stress.ts +++ b/src/units/stress/stress.ts @@ -1,10 +1,9 @@ +import Unit from '../unit'; import type { StressUnit } from './types'; -class Stress { - private stdValue: number; - +class Stress extends Unit { constructor(value: number, unit: StressUnit) { - this.stdValue = Stress.#parseToStd(value, unit); + super(Stress.#parseToStd(value, unit)); } MPa() { diff --git a/src/units/time/time.ts b/src/units/time/time.ts index 5c274d4..ba5533e 100644 --- a/src/units/time/time.ts +++ b/src/units/time/time.ts @@ -1,10 +1,9 @@ +import Unit from '../unit'; import type { TimeUnit } from './types'; -class Time { - private stdValue: number; - +class Time extends Unit { constructor(value: number, unit: TimeUnit) { - this.stdValue = Time.#parseToStd(value, unit); + super(Time.#parseToStd(value, unit)); } ms() { diff --git a/src/units/unit/index.ts b/src/units/unit/index.ts new file mode 100644 index 0000000..38e96d6 --- /dev/null +++ b/src/units/unit/index.ts @@ -0,0 +1 @@ +export { default } from './unit'; diff --git a/src/units/unit/unit.test.ts b/src/units/unit/unit.test.ts new file mode 100644 index 0000000..edc20e9 --- /dev/null +++ b/src/units/unit/unit.test.ts @@ -0,0 +1,11 @@ +import Unit from '.'; + +describe('Unit', () => { + const something = new Unit(100); + + describe('valueOf', () => { + it('returns the stdValue', () => { + expect(something.valueOf()).toBe(100); + }); + }); +}); diff --git a/src/units/unit/unit.ts b/src/units/unit/unit.ts new file mode 100644 index 0000000..3e08567 --- /dev/null +++ b/src/units/unit/unit.ts @@ -0,0 +1,13 @@ +class Unit { + stdValue: number; + + constructor(value: number) { + this.stdValue = value; + } + + valueOf() { + return this.stdValue; + } +} + +export default Unit;