Skip to content

Commit

Permalink
add getUnits()
Browse files Browse the repository at this point in the history
  • Loading branch information
lukelaws committed Oct 9, 2015
1 parent 3a18ec8 commit affb140
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,14 @@ 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']
```

### Quantity compatibility, kind and various queries

```javascript
Expand Down
12 changes: 12 additions & 0 deletions spec/quantitiesSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1239,6 +1239,18 @@ 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("information", function() {
describe("bits and bytes", function() {
it("should have 'information' as kind", function() {
Expand Down
36 changes: 36 additions & 0 deletions src/quantities.js
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,41 @@ 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;
});
};

/**
* Default formatter
*
Expand Down Expand Up @@ -1791,6 +1826,7 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
OUTPUT_MAP[unitDef] = definition[0][0];
}
}

var PREFIX_REGEX = Object.keys(PREFIX_MAP).sort(function(a, b) {
return b.length - a.length;
}).join("|");
Expand Down

0 comments on commit affb140

Please sign in to comment.