From 6da5a734c1e683d3d30179c622d2210566d6b6d7 Mon Sep 17 00:00:00 2001 From: nosferatoy Date: Wed, 18 Sep 2019 21:36:57 +0300 Subject: [PATCH 1/2] add si units for length --- README.md | 21 ++++++++ src/units/length.js | 119 +++++++++++++++++++++++++++++++++++++++++++ tests/length.test.js | 76 ++++++++++++++++++++++++++- tests/utils.test.js | 34 ++++++------- 4 files changed, 231 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index 8eb1a8f..6f47d24 100644 --- a/README.md +++ b/README.md @@ -154,9 +154,30 @@ voltage().list();
Length

+ + + +* 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 diff --git a/src/units/length.js b/src/units/length.js index 2f9ae09..a06d0c1 100644 --- a/src/units/length.js +++ b/src/units/length.js @@ -6,6 +6,55 @@ const length = { metric: { baseUnit: 'm', transform: (val) => { return val * RATIO }, + ym: { + name: { + singular: 'Yoctometre', + 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', @@ -20,6 +69,13 @@ const length = { }, to_anchor: 1 / 100 }, + dm: { + name: { + singular: 'Decimetre', + plural: 'Decimetres' + }, + to_anchor: 1 / 10 + }, m: { name: { singular: 'Meter', @@ -27,12 +83,75 @@ const length = { }, 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: { diff --git a/tests/length.test.js b/tests/length.test.js index ce8b4d2..b7969ee 100644 --- a/tests/length.test.js +++ b/tests/length.test.js @@ -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) diff --git a/tests/utils.test.js b/tests/utils.test.js index 150b71d..3df49c0 100644 --- a/tests/utils.test.js +++ b/tests/utils.test.js @@ -13,16 +13,16 @@ describe('Test to best', () => { test('best mm with excludes measurements km and m', () => { expect(converter.length(1200000).from('mm').toBest({ exclude: ['km', 'm'] })).toEqual({ - value: 120000, - unit: 'cm', + value: 12, + unit: 'hm', system: 'metric', - singular: 'Centimeter', - plural: 'Centimeters' + singular: 'Hectometre', + plural: 'Hectometres' }) }) - test('best mm with excludes measurements km and m', () => { - expect(converter.length(1200000).from('mm').toBest({ exclude: ['km', 'm'] })).toEqual({ + test('best mm with excludes measurements hm, km, dam, m and dm', () => { + expect(converter.length(1200000).from('mm').toBest({ exclude: ['hm', 'km', 'dam', 'm', 'dm'] })).toEqual({ value: 120000, unit: 'cm', system: 'metric', @@ -33,16 +33,16 @@ describe('Test to best', () => { test('best km excluding self', () => { expect(converter.length(10).from('km').toBest({ exclude: ['km'] })).toEqual({ - value: 10000, - unit: 'm', + value: 100, + unit: 'hm', system: 'metric', - singular: 'Meter', - plural: 'Meters' + singular: 'Hectometre', + plural: 'Hectometres' }) }) test('if all measurements are excluded return self', () => { - expect(converter.length(10).from('km').toBest({ exclude: ['mm, cm, m, km'] })).toEqual({ + expect(converter.length(10).from('km').toBest({ exclude: ['mm, cm, dm, m, dam ,hm, km'] })).toEqual({ value: 10, unit: 'km', system: 'metric', @@ -53,11 +53,11 @@ describe('Test to best', () => { test('pre-cut off numbe', () => { expect(converter.length(9000).from('mm').toBest({ cutOffNumber: 10 })).toEqual({ - value: 900, - unit: 'cm', + value: 90, + unit: 'dm', system: 'metric', - singular: 'Centimeter', - plural: 'Centimeters' + singular: 'Decimetre', + plural: 'Decimetres' }) }) @@ -149,7 +149,7 @@ describe('Test the possibilities', () => { }) test('m possibilities', () => { - const expected = ['mm', 'cm', 'm', 'km', 'in', 'yd', 'ft-us', 'ft', 'fathom', 'mi', 'nMi'] + const expected = ['ym', 'zm', 'am', 'fm', 'pm', 'nm', 'µm', 'mm', 'cm', 'dm', 'm', 'dam', 'hm', 'km', 'Mm', 'Gm', 'Tm', 'Pm', 'Em', 'Zm', 'Ym', 'in', 'yd', 'ft-us', 'ft', 'fathom', 'mi', 'nMi'] expect(converter.length().from('m').possibilities()).toEqual(expected) }) @@ -174,7 +174,7 @@ describe('Test the possibilities', () => { }) test('length possibilities', () => { - const expected = ['mm', 'cm', 'm', 'km', 'in', 'yd', 'ft-us', 'ft', 'fathom', 'mi', 'nMi'] + const expected = ['ym', 'zm', 'am', 'fm', 'pm', 'nm', 'µm', 'mm', 'cm', 'dm', 'm', 'dam', 'hm', 'km', 'Mm', 'Gm', 'Tm', 'Pm', 'Em', 'Zm', 'Ym', 'in', 'yd', 'ft-us', 'ft', 'fathom', 'mi', 'nMi'] expect(converter.length().possibilities()).toEqual(expected) }) From e53b67eb0592aa627ee419168b72229c340c8c04 Mon Sep 17 00:00:00 2001 From: nosferatoy Date: Wed, 18 Sep 2019 22:27:54 +0300 Subject: [PATCH 2/2] add astronomical units to length, introduce more than two systems of measurements --- src/units/length.js | 48 +++++++++++++++++++++++++++++++++++++++++---- src/utils.js | 2 +- tests/utils.test.js | 4 ++-- 3 files changed, 47 insertions(+), 7 deletions(-) diff --git a/src/units/length.js b/src/units/length.js index a06d0c1..8ed13c6 100644 --- a/src/units/length.js +++ b/src/units/length.js @@ -1,14 +1,29 @@ 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: 'Yoctometre', + singular: 'Yeptometre', plural: 'Yeptometres' }, to_anchor: 1 / 1000000000000000000000000 @@ -156,7 +171,7 @@ const length = { }, imperial: { baseUnit: 'ft', - transform: (val) => { return val * 1 / RATIO }, + transform: FUNCTION, 'in': { name: { singular: 'Inch', @@ -206,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' + }, + to_anchor: 63241.1 + }, + pc: { + name: { + singular: 'Parsec', + plural: 'Parsecs' + }, + to_anchor: 206265 + } } } diff --git a/src/utils.js b/src/utils.js index 1d171db..be92cfa 100644 --- a/src/utils.js +++ b/src/utils.js @@ -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) { diff --git a/tests/utils.test.js b/tests/utils.test.js index 3df49c0..f13eb79 100644 --- a/tests/utils.test.js +++ b/tests/utils.test.js @@ -149,7 +149,7 @@ describe('Test the possibilities', () => { }) test('m possibilities', () => { - const expected = ['ym', 'zm', 'am', 'fm', 'pm', 'nm', 'µm', 'mm', 'cm', 'dm', 'm', 'dam', 'hm', 'km', 'Mm', 'Gm', 'Tm', 'Pm', 'Em', 'Zm', 'Ym', 'in', 'yd', 'ft-us', 'ft', 'fathom', 'mi', 'nMi'] + const expected = ['ym', 'zm', 'am', 'fm', 'pm', 'nm', 'µm', 'mm', 'cm', 'dm', 'm', 'dam', 'hm', 'km', 'Mm', 'Gm', 'Tm', 'Pm', 'Em', 'Zm', 'Ym', 'in', 'yd', 'ft-us', 'ft', 'fathom', 'mi', 'nMi', 'au', 'ly', 'pc'] expect(converter.length().from('m').possibilities()).toEqual(expected) }) @@ -174,7 +174,7 @@ describe('Test the possibilities', () => { }) test('length possibilities', () => { - const expected = ['ym', 'zm', 'am', 'fm', 'pm', 'nm', 'µm', 'mm', 'cm', 'dm', 'm', 'dam', 'hm', 'km', 'Mm', 'Gm', 'Tm', 'Pm', 'Em', 'Zm', 'Ym', 'in', 'yd', 'ft-us', 'ft', 'fathom', 'mi', 'nMi'] + const expected = ['ym', 'zm', 'am', 'fm', 'pm', 'nm', 'µm', 'mm', 'cm', 'dm', 'm', 'dam', 'hm', 'km', 'Mm', 'Gm', 'Tm', 'Pm', 'Em', 'Zm', 'Ym', 'in', 'yd', 'ft-us', 'ft', 'fathom', 'mi', 'nMi', 'au', 'ly', 'pc'] expect(converter.length().possibilities()).toEqual(expected) })