Skip to content

Commit

Permalink
add getAliases()
Browse files Browse the repository at this point in the history
  • Loading branch information
lukelaws committed Oct 9, 2015
1 parent affb140 commit 7b8398d
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,12 @@ Qty.getUnits('currency'); // => [ 'dollar', 'cents' ]
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
8 changes: 8 additions & 0 deletions spec/quantitiesSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1251,6 +1251,14 @@ describe("js-quantities", function() {
});
});

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
15 changes: 14 additions & 1 deletion src/quantities.js
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,20 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
});
};

/**
* 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 Expand Up @@ -1826,7 +1840,6 @@ 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 7b8398d

Please sign in to comment.