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

Read available units/unit data structure #14

Open
wants to merge 6 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
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 20 additions & 0 deletions spec/quantitiesSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1154,4 +1154,24 @@ describe("js-quantities", function() {
});
});

describe("Qty.getUnits", function() {
it("should return a populated <string, Array<string>> dictionary of all unit names", function() {
var units = Qty.getUnits(),
expectedKey = "meter",
expectedValue = ["m","meter","meters","metre","metres"];

expect(units.length[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);
});
});

});
29 changes: 29 additions & 0 deletions src/quantities.js
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,35 @@ 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<string, Array<string>} unit names
*/
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
*
Expand Down