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

Added in function to get prefixes and units. Address #48, #14 #52

Closed
wants to merge 1 commit into from
Closed
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
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,19 @@ Qty.parse('foo') // => null
Qty.getKinds(); // => Array of names of every well-known kind of units
```

### Available unit names by category

```javascript
Qty.getUnits(); // => Object of unit arrays of every well-known kind of units
// Can be optionally restricted to only one kind
Qty.getUnits("length"); // => Array of all units of type length
```
### Available well-known prefixes

```javascript
Qty.getPrefix(); // => Array of arrays of every well-known kind of prefixes grouped by alias
```

### Quantity compatibility, kind and various queries

```javascript
Expand Down
28 changes: 28 additions & 0 deletions spec/quantitiesSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1239,6 +1239,34 @@ describe("js-quantities", function() {
});
});

describe("Qty.getUnits", function() {
it("should return a dictionary of categories with array units grouped by alises", function() {
var units = Qty.getUnits(),
expectedKey = "length",
expectedValue = ["m","meter","meters","metre","metres"];

expect(units[expectedKey]).toContain(expectedValue);
});

it("should optionally restrict returned units to one category", function() {
var units = Qty.getUnits("area"),
unexpectedValue = ["stone","stones","st"],
expectedValue = ["acre","acres"];

expect(units).not.toContain(unexpectedValue);
expect(units).toContain(expectedValue);
});
});

describe("Qty.getPrefix", function() {
it("should return an array of array grouped by pefix", function() {
var prefixes = Qty.getPrefix(),
expectedValue = ["Ki","Kibi","kibi"];

expect(prefixes).toContain(expectedValue);
});
});

describe("information", function() {
describe("bits and bytes", function() {
it("should have 'information' as kind", function() {
Expand Down
71 changes: 71 additions & 0 deletions src/quantities.js
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,77 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
return (scalar + " " + units).trim();
}

/**
* Returns the object of available well-known units of per category
* returning a list of list per category e.g.
*
* @param {EMPTY} no parameter
*
* @returns {Object[]} names of categoires as keys, with array of units grouped
* by aliases
* mass:
* [ [ 'kg', 'kilogram', 'kilograms' ],
* [ 'u', 'AMU', 'amu' ],
* [ 'Da', 'Dalton', 'Daltons', 'dalton', 'daltons' ]]
*
* @param {string} kind - kind, string formated
*
* @returns {array} List of all units, group by alias
*
* [ [ 'kg', 'kilogram', 'kilograms' ],
* ['u', 'AMU', 'amu' ],
* [ 'Da', 'Dalton', 'Daltons', 'dalton', 'daltons']]
*/
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].push(UNITS[unitId][0]);
}
if(kind) {
if(kinds[kind] === undefined) {
throw new QtyError(kind + ": Category not recognized.");
}
return kinds[kind];
}
return kinds;
};

/**
* Returns the list of prefixes groups by prefix aliases e.g.
* ["Ki","Kibi","kibi"]
*
* @returns {array[]} names of kinds as keys, with values of
*/
Qty.getPrefix = function() {
var prefixes = [];
for (var unitId in UNITS) {
if (UNITS[unitId][2] == "prefix"){
prefixes.push(UNITS[unitId][0]);
}
}
return prefixes;
};

/**
* Default formatter
*
* @param {number} scalar
* @param {string} units
*
* @returns {string} formatted result
*/
function defaultFormatter(scalar, units) {
return (scalar + " " + units).trim();
}

/**
*
* Configurable Qty default formatter
Expand Down