Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add si prefixes #14

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,30 @@ voltage().list();
<details><summary>Length</summary>
<p>




* ym
* zm
* am
* fm
* pm
* nm
* μm
* mm
* dm
* cm
* m
* dam
* hm
* km
* Mm
* Gm
* Tm
* Pm
* Em
* Zm
* Ym
* in
* ft-us
* ft
Expand Down
165 changes: 162 additions & 3 deletions src/units/length.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,75 @@
import utils from '../utils.js'

const RATIO = 3.28084
const RATIO = {
metric: {
astronomical: 1.496e+11,
imperial: 3.28084
},
astronomical: {
metric: 1 / 1.496e+11,
imperial: 1 / 4.908e+11
},
imperial: {
astronomical: 4.908e+11,
metric: 1 / 3.28084
}
}

const FUNCTION = (val, origin, destination) => { return val * RATIO[origin][destination] }

const length = {
metric: {
baseUnit: 'm',
transform: (val) => { return val * RATIO },
transform: FUNCTION,
ym: {
name: {
singular: 'Yeptometre',
plural: 'Yeptometres'
},
to_anchor: 1 / 1000000000000000000000000
},
zm: {
name: {
singular: 'Zeptometre',
plural: 'Zeptometres'
},
to_anchor: 1 / 1000000000000000000000
},
am: {
name: {
singular: 'Attometre',
plural: 'Attometres'
},
to_anchor: 1 / 1000000000000000000
},
fm: {
name: {
singular: 'Femtometre',
plural: 'Femtometres'
},
to_anchor: 1 / 1000000000000000
},
pm: {
name: {
singular: 'Picometre',
plural: 'Picometres'
},
to_anchor: 1 / 1000000000000
},
nm: {
name: {
singular: 'Nanometre',
plural: 'Nanometres'
},
to_anchor: 1 / 1000000000
},
µm: {
name: {
singular: 'Micrometre',
plural: 'Micrometres'
},
to_anchor: 1 / 1000000
},
mm: {
name: {
singular: 'Millimeter',
Expand All @@ -20,24 +84,94 @@ const length = {
},
to_anchor: 1 / 100
},
dm: {
name: {
singular: 'Decimetre',
plural: 'Decimetres'
},
to_anchor: 1 / 10
},
m: {
name: {
singular: 'Meter',
plural: 'Meters'
},
to_anchor: 1
},
dam: {
name: {
singular: 'Decametre',
plural: 'Decametres'
},
to_anchor: 10
},
hm: {
name: {
singular: 'Hectometre',
plural: 'Hectometres'
},
to_anchor: 100
},
km: {
name: {
singular: 'Kilometer',
plural: 'Kilometers'
},
to_anchor: 1000
},
Mm: {
name: {
singular: 'Megametre',
plural: 'Megametres'
},
to_anchor: 1000000
},
Gm: {
name: {
singular: 'Gigametre',
plural: 'Gigametres'
},
to_anchor: 1000000000
},
Tm: {
name: {
singular: 'Terametre',
plural: 'Terametres'
},
to_anchor: 1000000000000
},
Pm: {
name: {
singular: 'Petametre',
plural: 'Petametres'
},
to_anchor: 1000000000000000
},
Em: {
name: {
singular: 'Exametre',
plural: 'Exametres'
},
to_anchor: 1000000000000000000
},
Zm: {
name: {
singular: 'Zettametre',
plural: 'Zettametres'
},
to_anchor: 1000000000000000000000
},
Ym: {
name: {
singular: 'Yottametre',
plural: 'Yottametres'
},
to_anchor: 1000000000000000000000000
}
},
imperial: {
baseUnit: 'ft',
transform: (val) => { return val * 1 / RATIO },
transform: FUNCTION,
'in': {
name: {
singular: 'Inch',
Expand Down Expand Up @@ -87,6 +221,31 @@ const length = {
},
to_anchor: 6076.12
}
},
astronomical: {
baseUnit: 'au',
transform: FUNCTION,
au: {
name: {
singular: 'Astronomical unit',
plural: 'Astronomical units'
},
to_anchor: 1
},
ly: {
name: {
singular: 'Light-year',
plural: 'Light years'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dash missing

},
to_anchor: 63241.1
},
pc: {
name: {
singular: 'Parsec',
plural: 'Parsecs'
},
to_anchor: 206265
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Converter.prototype.to = function (to) {
}

if (this.origin.system !== this.destination.system) {
result = this.definitions[this.origin.system].transform(result)
result = this.definitions[this.origin.system].transform(result, this.origin.system, this.destination.system)
}

if (this.destination.unit.anchor_shift !== undefined) {
Expand Down
76 changes: 74 additions & 2 deletions tests/length.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,90 @@ test('m to cm', () => {
expect(length(1).from('m').to('cm').value).toEqual(100)
})

test('m to mm', () => {
expect(length(1).from('m').to('mm').value).toEqual(1000)
})

test('ym to m', () => {
expect(length(1).from('ym').to('m').value).toEqual(1 / 1000000000000000000000000)
})

test('zm to m', () => {
expect(length(1).from('zm').to('m').value).toEqual(1 / 1000000000000000000000)
})

test('am to m', () => {
expect(length(1).from('am').to('m').value).toEqual(1 / 1000000000000000000)
})

test('fm to m', () => {
expect(length(1).from('fm').to('m').value).toEqual(1 / 1000000000000000)
})

test('pm to m', () => {
expect(length(1).from('pm').to('m').value).toEqual(1 / 1000000000000)
})

test('nm to m', () => {
expect(length(1).from('nm').to('m').value).toEqual(1 / 1000000000)
})

test('µm to m', () => {
expect(length(1).from('µm').to('m').value).toEqual(1 / 1000000)
})

test('mm to m', () => {
expect(length(1).from('mm').to('m').value).toEqual(1 / 1000)
})

test('cm to m', () => {
expect(length(1).from('cm').to('m').value).toEqual(1 / 100)
})

test('m to mm', () => {
expect(length(1).from('m').to('mm').value).toEqual(1000)
test('dm to m', () => {
expect(length(1).from('dm').to('m').value).toEqual(1 / 10)
})

test('dam to m', () => {
expect(length(1).from('dam').to('m').value).toEqual(10)
})

test('hm to m', () => {
expect(length(1).from('hm').to('m').value).toEqual(100)
})

test('km to m', () => {
expect(length(1).from('km').to('m').value).toEqual(1000)
})

test('Mm to m', () => {
expect(length(1).from('Mm').to('m').value).toEqual(1000000)
})

test('Gm to m', () => {
expect(length(1).from('Gm').to('m').value).toEqual(1000000000)
})

test('Tm to m', () => {
expect(length(1).from('Tm').to('m').value).toEqual(1000000000000)
})

test('Pm to m', () => {
expect(length(1).from('Pm').to('m').value).toEqual(1000000000000000)
})

test('Em to m', () => {
expect(length(1).from('Em').to('m').value).toEqual(1000000000000000000)
})

test('Zm to m', () => {
expect(length(1).from('Zm').to('m').value).toEqual(1000000000000000000000)
})

test('Ym to m', () => {
expect(length(1).from('Ym').to('m').value).toEqual(1000000000000000000000000)
})

// When converting between systems, expect < 0.1% error
test('m to ft', () => {
expect(percentError(3.28084, length(1).from('m').to('ft').value)).toBeLessThanOrEqual(ACCURACY)
Expand Down
Loading