diff --git a/README.md b/README.md index 87ab9b4..fdc6472 100644 --- a/README.md +++ b/README.md @@ -99,6 +99,20 @@ 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'] +``` + +### 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 d2cb152..d6a4ace 100644 --- a/spec/quantitiesSpec.js +++ b/spec/quantitiesSpec.js @@ -1239,6 +1239,26 @@ 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("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 0e8d90e..1061d19 100644 --- a/src/quantities.js +++ b/src/quantities.js @@ -488,6 +488,55 @@ 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; + }); + }; + + /** + * 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 *