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

Add getUnits and getAliases addresses #48 #53

Merged
merged 2 commits into from
Oct 14, 2015
Merged
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
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 20 additions & 0 deletions spec/quantitiesSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
49 changes: 49 additions & 0 deletions src/quantities.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down