Skip to content

Commit

Permalink
Merge pull request #53 from antecursor/master
Browse files Browse the repository at this point in the history
Add getUnits and getAliases 

Addresses #48
  • Loading branch information
gentooboontoo committed Oct 14, 2015
2 parents 3a18ec8 + 7b8398d commit ba60d89
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 0 deletions.
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

0 comments on commit ba60d89

Please sign in to comment.