From f8fcfbdc7cb0a164222a0453917e1dcd70affa63 Mon Sep 17 00:00:00 2001 From: Mikael Berg Date: Sat, 14 Jun 2014 15:40:30 +0200 Subject: [PATCH 1/6] Added basic tests for Qty.getPrefixes, Qty.getKinds and Qty.getUnits. --- spec/quantitiesSpec.js | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/spec/quantitiesSpec.js b/spec/quantitiesSpec.js index 6b51528..c8d9683 100644 --- a/spec/quantitiesSpec.js +++ b/spec/quantitiesSpec.js @@ -1154,4 +1154,42 @@ describe("js-quantities", function() { }); }); + describe("Qty.getPrefixes", function() { + it("should return a populated > dictionary", function() { + var prefixes = Qty.getPrefixes(), + expectedKey = "kibi", + expectedValue = ["Ki","Kibi","kibi"]; + + expect(prefixes[expectedKey]).toEqual(expectedValue); + }); + }); + + describe("Qty.getKinds", function() { + it("should return an array of kind names", function() { + var kinds = Qty.getKinds(); + + expect(kinds.indexOf("resistance")).not.toBe(-1); + }); + }); + + describe("Qty.getUnits", function() { + it("should return a populated > dictionary of all unit names", function() { + var units = Qty.getUnits(), + expectedKey = "meter", + expectedValue = ["m","meter","meters","metre","metres"]; + + expect(units[expectedKey]).toEqual(expectedValue); + }); + + it ("should optionally restrict returned units to one kind", function() { + var resistanceUnits = Qty.getUnits("resistance"), + unexpectedKey = "meter", + expectedKey = "ohm", + expectedValue = ["Ohm", "ohm"]; + + expect(Object.keys(resistanceUnits).indexOf(unexpectedKey)).toBe(-1); + expect(resistanceUnits[expectedKey]).toEqual(expectedValue); + }); + }); + }); From d0239ab37f3a65bc80ee28a8b5f3fe3ef015a9fc Mon Sep 17 00:00:00 2001 From: Mikael Berg Date: Sat, 14 Jun 2014 17:00:54 +0200 Subject: [PATCH 2/6] Implemented Qty.getPrefixes, Qty.getKinds and Qty.getUnits. --- spec/quantitiesSpec.js | 8 ++++++- src/quantities.js | 53 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 1 deletion(-) diff --git a/spec/quantitiesSpec.js b/spec/quantitiesSpec.js index c8d9683..3bc5fff 100644 --- a/spec/quantitiesSpec.js +++ b/spec/quantitiesSpec.js @@ -1170,6 +1170,12 @@ describe("js-quantities", function() { expect(kinds.indexOf("resistance")).not.toBe(-1); }); + + it("should not contain empty strings", function() { + var kinds = Qty.getKinds(); + + expect(kinds.indexOf("")).toBe(-1); + }); }); describe("Qty.getUnits", function() { @@ -1178,7 +1184,7 @@ describe("js-quantities", function() { expectedKey = "meter", expectedValue = ["m","meter","meters","metre","metres"]; - expect(units[expectedKey]).toEqual(expectedValue); + expect(units.length[expectedKey]).toEqual(expectedValue); }); it ("should optionally restrict returned units to one kind", function() { diff --git a/src/quantities.js b/src/quantities.js index 35eae4e..344a88f 100644 --- a/src/quantities.js +++ b/src/quantities.js @@ -452,6 +452,59 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI }; }; + /** + * Returns an object with the names of all available prefixes. + * + * @returns {Object>} prefix names + */ + Qty.getPrefixes = function() { + var prefixes = {}; + for (var unitId in UNITS) { + if (UNITS[unitId][2] === "prefix") { + prefixes[unitId.substr(1, unitId.length - 2)] = UNITS[unitId][0]; + } + } + return prefixes; + }; + + /** + * Returns an array of the names of the different kinds of units, e.g. + * "radiation" or "length". + * + * @returns {Array} names of kinds + */ + Qty.getKinds = function() { + var kinds = {}; // use object as a set + for (var unitId in UNITS) { + kinds[UNITS[unitId][2]] = true; + } + return Object.keys(kinds).filter(function(kind) { + return kind.length > 0; + }); + }; + + Qty.getUnits = function(kind) { + var kinds = {}, + k; + for (var unitId in UNITS) { + k = UNITS[unitId][2]; + if (k.length === 0) { + continue; + } + if (kinds[k] === undefined) { + kinds[k] = {}; + } + kinds[k][unitId.substr(1, unitId.length - 2)] = UNITS[unitId][0]; + } + if (kind) { + if (kinds[kind] === undefined) { + throw new QtyError(kind + ": Kind not recognized."); + } + return kinds[kind]; + } + return kinds; + }; + /** * Default formatter * From 08ce2cab348c07f6680de24beb0752ec30ad73fe Mon Sep 17 00:00:00 2001 From: Mikael Berg Date: Sat, 14 Jun 2014 17:11:23 +0200 Subject: [PATCH 3/6] Wrote missing documentation for Qty.getUnits. --- src/quantities.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/quantities.js b/src/quantities.js index 344a88f..26b5e05 100644 --- a/src/quantities.js +++ b/src/quantities.js @@ -483,6 +483,13 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI }); }; + /** + * Returns an object with the names of the available units, categorized by + * kind. Can optionally be restricted to only one kind. + * + * @param {string} kind optionally restrict to this kind + * @returns {Object} unit names + */ Qty.getUnits = function(kind) { var kinds = {}, k; From 690fbb4e7a0e0457f9f9338fe584540722a99364 Mon Sep 17 00:00:00 2001 From: Mikael Berg Date: Sat, 14 Jun 2014 17:15:57 +0200 Subject: [PATCH 4/6] Removed Qty.getPrefixes and Qty.getKinds because they are pretty much redundant by Qty.getUnits' dictionary structure. --- spec/quantitiesSpec.js | 24 ------------------------ src/quantities.js | 31 ------------------------------- 2 files changed, 55 deletions(-) diff --git a/spec/quantitiesSpec.js b/spec/quantitiesSpec.js index 3bc5fff..2c9b194 100644 --- a/spec/quantitiesSpec.js +++ b/spec/quantitiesSpec.js @@ -1154,30 +1154,6 @@ describe("js-quantities", function() { }); }); - describe("Qty.getPrefixes", function() { - it("should return a populated > dictionary", function() { - var prefixes = Qty.getPrefixes(), - expectedKey = "kibi", - expectedValue = ["Ki","Kibi","kibi"]; - - expect(prefixes[expectedKey]).toEqual(expectedValue); - }); - }); - - describe("Qty.getKinds", function() { - it("should return an array of kind names", function() { - var kinds = Qty.getKinds(); - - expect(kinds.indexOf("resistance")).not.toBe(-1); - }); - - it("should not contain empty strings", function() { - var kinds = Qty.getKinds(); - - expect(kinds.indexOf("")).toBe(-1); - }); - }); - describe("Qty.getUnits", function() { it("should return a populated > dictionary of all unit names", function() { var units = Qty.getUnits(), diff --git a/src/quantities.js b/src/quantities.js index 26b5e05..8e3cabb 100644 --- a/src/quantities.js +++ b/src/quantities.js @@ -452,37 +452,6 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI }; }; - /** - * Returns an object with the names of all available prefixes. - * - * @returns {Object>} prefix names - */ - Qty.getPrefixes = function() { - var prefixes = {}; - for (var unitId in UNITS) { - if (UNITS[unitId][2] === "prefix") { - prefixes[unitId.substr(1, unitId.length - 2)] = UNITS[unitId][0]; - } - } - return prefixes; - }; - - /** - * Returns an array of the names of the different kinds of units, e.g. - * "radiation" or "length". - * - * @returns {Array} names of kinds - */ - Qty.getKinds = function() { - var kinds = {}; // use object as a set - for (var unitId in UNITS) { - kinds[UNITS[unitId][2]] = true; - } - return Object.keys(kinds).filter(function(kind) { - return kind.length > 0; - }); - }; - /** * Returns an object with the names of the available units, categorized by * kind. Can optionally be restricted to only one kind. From 49d3c3e40e7522b6872cb85c7eb6da3ca562e9ff Mon Sep 17 00:00:00 2001 From: Mikael Berg Date: Sat, 14 Jun 2014 17:38:25 +0200 Subject: [PATCH 5/6] Updated README.md with Qty.getUnits. --- README.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/README.md b/README.md index 13ca50c..de02509 100644 --- a/README.md +++ b/README.md @@ -256,6 +256,25 @@ var qty = Qty('1.1234 m'); qty.format(); // same units, current default formatter => '1.12 m' ``` +### Unit names + +Unit names can be extracted using `Qty#getUnits`, which returns an object with +all available unit names, including prefixes, and their synonyms, categorized by +kind. Can optionally be restricted to only one kind. + +```javascript +var units = Qty.getUnits(); // dictionary containing all units, by category + // { ..., + // length: { m: ["m","meter","meters", + // "metre","metres"], ...}, + // ... } + +var lengthUnits = Qty.getUnits("length"); // dictionary containing all length + // units + // { m: ["m","meter","meters","metre", + // "metres"] } +``` + ### Temperatures Like ruby-units, JS-quantities makes a distinction between a temperature (which From 0883de247cdd788eb5ad0815057fdd50c98641b5 Mon Sep 17 00:00:00 2001 From: Mikael Berg Date: Sat, 14 Jun 2014 17:50:11 +0200 Subject: [PATCH 6/6] This commit resolves #14