From affb1400dea049a0d93fdf7f91407647a1f6e481 Mon Sep 17 00:00:00 2001 From: Luke Laws Date: Fri, 9 Oct 2015 12:41:36 -0400 Subject: [PATCH 1/2] add getUnits() --- README.md | 8 ++++++++ spec/quantitiesSpec.js | 12 ++++++++++++ src/quantities.js | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+) diff --git a/README.md b/README.md index 87ab9b4..fcf52ea 100644 --- a/README.md +++ b/README.md @@ -99,6 +99,14 @@ Qty.parse('foo') // => null Qty.getKinds(); // => Array of names of every well-known kind of units ``` +### Available units of a particular kind + +```javascript +Qty.getUnits('currency'); // => [ 'dollar', 'cents' ] +// Or all alphabetically sorted +Qty.getUnits(); // => [ 'acre','Ah','ampere','AMU','angstrom'] +``` + ### Quantity compatibility, kind and various queries ```javascript diff --git a/spec/quantitiesSpec.js b/spec/quantitiesSpec.js index d2cb152..e310558 100644 --- a/spec/quantitiesSpec.js +++ b/spec/quantitiesSpec.js @@ -1239,6 +1239,18 @@ describe("js-quantities", function() { }); }); + describe("Qty.getUnits", function() { + it("should return an array of units of kind", function() { + expect(Qty.getUnits("currency")).toContain("dollar"); + }); + it("should return an array of all units without arg", function () { + expect(Qty.getUnits()).toContain("sievert"); + }); + it("should throw unknown kind", function () { + expect(function () {Qty.getUnits('bogusKind')}).toThrow("Kind not recognized"); + }); + }); + describe("information", function() { describe("bits and bytes", function() { it("should have 'information' as kind", function() { diff --git a/src/quantities.js b/src/quantities.js index 0e8d90e..852cd15 100644 --- a/src/quantities.js +++ b/src/quantities.js @@ -488,6 +488,41 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI })); }; + /** + * Returns a list of available units of kind + * + * @param {string} [kind] + * @returns {array} names of units + * @throws {QtyError} if kind is unknown + */ + Qty.getUnits = function(kind) { + var units = []; + var unitKeys = Object.keys(UNITS); + if (typeof kind === 'undefined') { + for(var i = 0; i < unitKeys.length; i++) { + if (['', 'prefix'].indexOf(UNITS[unitKeys[i]][2]) == -1) { + units.push(unitKeys[i].substr(1, unitKeys[i].length - 2)); + } + } + } + else if (Qty.getKinds().indexOf(kind) === -1) { + throw new QtyError('Kind not recognized'); + } + else { + for(var i = 0; i < unitKeys.length; i++) { + if (UNITS[unitKeys[i]][2] === kind) { + units.push(unitKeys[i].substr(1, unitKeys[i].length - 2)); + } + } + } + + return units.sort(function(a, b){ + if(a.toLowerCase() < b.toLowerCase()) return -1; + if(a.toLowerCase() > b.toLowerCase()) return 1; + return 0; + }); + }; + /** * Default formatter * @@ -1791,6 +1826,7 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI OUTPUT_MAP[unitDef] = definition[0][0]; } } + var PREFIX_REGEX = Object.keys(PREFIX_MAP).sort(function(a, b) { return b.length - a.length; }).join("|"); From 7b8398dbbeefe3107186da07848b78c8fe13f784 Mon Sep 17 00:00:00 2001 From: Luke Laws Date: Fri, 9 Oct 2015 12:46:21 -0400 Subject: [PATCH 2/2] add getAliases() --- README.md | 6 ++++++ spec/quantitiesSpec.js | 8 ++++++++ src/quantities.js | 15 ++++++++++++++- 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index fcf52ea..fdc6472 100644 --- a/README.md +++ b/README.md @@ -107,6 +107,12 @@ Qty.getUnits('currency'); // => [ 'dollar', 'cents' ] Qty.getUnits(); // => [ 'acre','Ah','ampere','AMU','angstrom'] ``` +### Alternative names of a unit + +```javascript +Qty.getAliases('m'); // => [ 'm', 'meter', 'meters', 'metre', 'metres' ] +``` + ### Quantity compatibility, kind and various queries ```javascript diff --git a/spec/quantitiesSpec.js b/spec/quantitiesSpec.js index e310558..d6a4ace 100644 --- a/spec/quantitiesSpec.js +++ b/spec/quantitiesSpec.js @@ -1251,6 +1251,14 @@ describe("js-quantities", function() { }); }); + describe("Qty.getAliases", function() { + it("should return array of alternative names for unit", function() { + expect(Qty.getAliases("m")).toContain("meter"); + expect(Qty.getAliases("meter")).toContain("metre"); + expect(Qty.getAliases("N")).toContain("newton"); + }); + }); + describe("information", function() { describe("bits and bytes", function() { it("should have 'information' as kind", function() { diff --git a/src/quantities.js b/src/quantities.js index 852cd15..1061d19 100644 --- a/src/quantities.js +++ b/src/quantities.js @@ -523,6 +523,20 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI }); }; + /** + * Returns a list of alternative names for a unit + * + * @param {string} unit + * @returns {string[]} aliases for unit + * @throws {QtyError} if unit is unknown + */ + Qty.getAliases = function(unitName) { + if (!UNIT_MAP[unitName]) { + throw new QtyError('Unit not recognized'); + } + return UNITS[UNIT_MAP[unitName]][0]; + }; + /** * Default formatter * @@ -1826,7 +1840,6 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI OUTPUT_MAP[unitDef] = definition[0][0]; } } - var PREFIX_REGEX = Object.keys(PREFIX_MAP).sort(function(a, b) { return b.length - a.length; }).join("|");