Skip to content

Commit

Permalink
add ability to do arithmetics with unit classes
Browse files Browse the repository at this point in the history
  • Loading branch information
mapra99 committed May 7, 2023
1 parent cb7b748 commit dbc33f6
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 24 deletions.
23 changes: 11 additions & 12 deletions src/units/force/force.ts
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
7 changes: 3 additions & 4 deletions src/units/length/length.ts
Original file line number Diff line number Diff line change
@@ -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() {
Expand Down
7 changes: 3 additions & 4 deletions src/units/stress/stress.ts
Original file line number Diff line number Diff line change
@@ -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() {
Expand Down
7 changes: 3 additions & 4 deletions src/units/time/time.ts
Original file line number Diff line number Diff line change
@@ -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() {
Expand Down
1 change: 1 addition & 0 deletions src/units/unit/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from './unit';
15 changes: 15 additions & 0 deletions src/units/unit/unit.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import Unit from '.';

describe('Unit', () => {
const something = new Unit(100);

describe('valueOf', () => {
it('returns the stdValue', () => {
expect(something.valueOf()).toBe(100);
});

it('enables arithmetic operations', () => {
expect(+something + 100).toBe(200);
});
});
});
13 changes: 13 additions & 0 deletions src/units/unit/unit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Unit {
stdValue: number;

constructor(value: number) {
this.stdValue = value;
}

valueOf() {
return this.stdValue;
}
}

export default Unit;

0 comments on commit dbc33f6

Please sign in to comment.