Skip to content

Commit

Permalink
feat(units system): build classes for length, stress and force dimens…
Browse files Browse the repository at this point in the history
…ions
  • Loading branch information
mapra99 committed May 7, 2023
1 parent a4bd87c commit cb7b748
Show file tree
Hide file tree
Showing 16 changed files with 353 additions and 0 deletions.
54 changes: 54 additions & 0 deletions src/units/force/force.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import Force from '.';

describe('Force', () => {
const force = new Force(100, 'kN');

describe('kgf', () => {
it('returns the force in kilogram-force', () => {
expect(force.kgf().toFixed(2)).toBe('10196.80');
});
});

describe('tonf', () => {
it('returns the force in ton-force', () => {
expect(force.tonf().toFixed(2)).toBe('10.20');
});
});

describe('lbf', () => {
it('returns the force in pound-force', () => {
expect(force.lbf().toFixed(2)).toBe('22482.01');
});
});

describe('kip', () => {
it('returns the force in kip', () => {
expect(force.kip().toFixed(2)).toBe('22.48');
});
});

describe('N', () => {
it('returns the force in newton', () => {
expect(force.N().toFixed(2)).toBe('100000.00');
});
});

describe('kN', () => {
it('returns the force in kilonewton', () => {
expect(force.kN().toFixed(2)).toBe('100.00');
});
});

describe('MN', () => {
it('returns the force in meganewton', () => {
expect(force.MN().toFixed(2)).toBe('0.10');
});
});

describe('set', () => {
it('updates the current value', () => {
force.set(100, 'MN');
expect(force.N()).toBe(100000000);
});
});
});
64 changes: 64 additions & 0 deletions src/units/force/force.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import type { ForceUnit } from './types';

class Force {
private stdUnit: number;

constructor(value: number, unit: ForceUnit) {
this.stdUnit = Force.#parseToStd(value, unit);
}

N() {
return this.stdUnit;
}

kN() {
return this.stdUnit / 1000;
}

lbf() {
return this.stdUnit / 4.448;
}

kip() {
return this.stdUnit / 4448.222;
}

kgf() {
return this.stdUnit / 9.807;
}

MN() {
return this.stdUnit / 1000000;
}

tonf() {
return this.stdUnit / 9807;
}

set(value: number, unit: ForceUnit) {
this.stdUnit = Force.#parseToStd(value, unit);
}

static #parseToStd(value: number, unit: ForceUnit) {
switch (unit) {
case 'N':
return value;
case 'kN':
return value * 1000;
case 'lbf':
return value * 4.448;
case 'kip':
return value * 4448.222;
case 'kgf':
return value * 9.807;
case 'MN':
return value * 1000000;
case 'tonf':
return value * 9807;
default:
throw new Error('Invalid unit');
}
}
}

export default Force;
1 change: 1 addition & 0 deletions src/units/force/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from './force';
1 change: 1 addition & 0 deletions src/units/force/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type ForceUnit = 'kgf' | 'tonf' | 'lbf' | 'kip' | 'N' | 'kN' | 'MN';
1 change: 1 addition & 0 deletions src/units/length/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from './length';
36 changes: 36 additions & 0 deletions src/units/length/length.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import Length from '.';

describe('Length', () => {
const length = new Length(100, 'm');

describe('mm', () => {
it('returns the length in milimeters', () => {
expect(length.mm()).toBe(100000);
});
});

describe('m', () => {
it('returns the length in meters', () => {
expect(length.m()).toBe(100);
});
});

describe('in', () => {
it('returns the length in inches', () => {
expect(length.in().toFixed(2)).toBe('3937.01');
});
});

describe('ft', () => {
it('returns the length in feet', () => {
expect(length.ft().toFixed(2)).toBe('328.08');
});
});

describe('set', () => {
it('updates the current value', () => {
length.set(10, 'mm');
expect(length.mm()).toBe(10);
});
});
});
46 changes: 46 additions & 0 deletions src/units/length/length.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import type { LengthUnit } from './types';

class Length {
private stdValue: number;

constructor(value: number, unit: LengthUnit) {
this.stdValue = Length.#parseToStd(value, unit);
}

mm() {
return this.stdValue;
}

m() {
return this.stdValue / 1000;
}

in() {
return this.stdValue / 25.4;
}

ft() {
return this.stdValue / 304.8;
}

set(value: number, unit: LengthUnit) {
this.stdValue = Length.#parseToStd(value, unit);
}

static #parseToStd(value: number, unit: LengthUnit) {
switch (unit) {
case 'mm':
return value;
case 'm':
return value * 1000;
case 'in':
return value * 25.4;
case 'ft':
return value * 304.8;
default:
throw new Error('Invalid unit');
}
}
}

export default Length;
1 change: 1 addition & 0 deletions src/units/length/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type LengthUnit = 'mm' | 'm' | 'in' | 'ft';
1 change: 1 addition & 0 deletions src/units/stress/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from './stress';
35 changes: 35 additions & 0 deletions src/units/stress/stress.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import Stress from '.';

describe('Stress', () => {
const stress = new Stress(100, 'kPa');

describe('MPa', () => {
it('returns the stress in megapascals', () => {
expect(stress.MPa()).toBe(0.1);
});
});

describe('kPa', () => {
it('returns the stress in kilopascals', () => {
expect(stress.kPa()).toBe(100);
});
});

describe('kgf_cm2', () => {
it('returns the stress in kilograms-force per square centimeter', () => {
expect(stress.kgf_cm2().toFixed(2)).toBe('1.02');
});
});

describe('psi', () => {
it('returns the stress in pounds-force per square inch', () => {
expect(stress.psi().toFixed(2)).toBe('14.50');
});
});

describe('ksi', () => {
it('returns the stress in kilopounds-force per square inch', () => {
expect(stress.ksi().toFixed(4)).toBe('0.0145');
});
});
});
52 changes: 52 additions & 0 deletions src/units/stress/stress.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import type { StressUnit } from './types';

class Stress {
private stdValue: number;

constructor(value: number, unit: StressUnit) {
this.stdValue = Stress.#parseToStd(value, unit);
}

MPa() {
return this.stdValue;
}

kPa() {
return this.stdValue * 1000;
}

kgf_cm2() {
return this.stdValue * 10.19716213;
}

psi() {
return this.stdValue * 145.0377377;
}

ksi() {
return this.stdValue * 0.145037738;
}

set(value: number, unit: StressUnit) {
this.stdValue = Stress.#parseToStd(value, unit);
}

static #parseToStd(value: number, unit: StressUnit) {
switch (unit) {
case 'MPa':
return value;
case 'kPa':
return value / 1000;
case 'kgf_cm2':
return value / 10.19716213;
case 'psi':
return value / 145.0377377;
case 'ksi':
return value / 0.145037738;
default:
throw new Error('Invalid unit');
}
}
}

export default Stress;
1 change: 1 addition & 0 deletions src/units/stress/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type StressUnit = 'kgf_cm2' | 'kPa' | 'MPa' | 'psi' | 'ksi';
1 change: 1 addition & 0 deletions src/units/time/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from './time';
24 changes: 24 additions & 0 deletions src/units/time/time.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import Time from '.';

describe('Time', () => {
const time = new Time(100, 's');

describe('ms', () => {
it('returns the time in miliseconds', () => {
expect(time.ms()).toBe(100000);
});
});

describe('s', () => {
it('returns the time in seconds', () => {
expect(time.s()).toBe(100);
});
});

describe('set', () => {
it('updates the current value', () => {
time.set(10, 'ms');
expect(time.s()).toBe(0.01);
});
});
});
34 changes: 34 additions & 0 deletions src/units/time/time.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import type { TimeUnit } from './types';

class Time {
private stdValue: number;

constructor(value: number, unit: TimeUnit) {
this.stdValue = Time.#parseToStd(value, unit);
}

ms() {
return this.stdValue * 1000;
}

s() {
return this.stdValue;
}

set(value: number, unit: TimeUnit) {
this.stdValue = Time.#parseToStd(value, unit);
}

static #parseToStd(value: number, unit: TimeUnit) {
switch (unit) {
case 'ms':
return value / 1000;
case 's':
return value;
default:
throw new Error('Invalid unit');
}
}
}

export default Time;
1 change: 1 addition & 0 deletions src/units/time/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type TimeUnit = 'ms' | 's';

0 comments on commit cb7b748

Please sign in to comment.