-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(units system): build classes for length, stress and force dimens…
…ions
- Loading branch information
Showing
16 changed files
with
353 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default } from './force'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export type ForceUnit = 'kgf' | 'tonf' | 'lbf' | 'kip' | 'N' | 'kN' | 'MN'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default } from './length'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export type LengthUnit = 'mm' | 'm' | 'in' | 'ft'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default } from './stress'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export type StressUnit = 'kgf_cm2' | 'kPa' | 'MPa' | 'psi' | 'ksi'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default } from './time'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export type TimeUnit = 'ms' | 's'; |