Skip to content

Commit

Permalink
Merge branch 'unique-kinds'
Browse files Browse the repository at this point in the history
  • Loading branch information
gentooboontoo committed Jul 9, 2015
2 parents 63dc430 + 97861ed commit 6d72047
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
9 changes: 9 additions & 0 deletions spec/quantitiesSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1218,6 +1218,15 @@ describe("js-quantities", function() {
it("should return an array of kind names", function() {
expect(Qty.getKinds()).toContain("resistance");
});

it("should not contain duplicate kind names", function() {
var kinds = Qty.getKinds();
var map = {};
kinds.forEach(function(kind) {
map[kind] = 1;
});
expect(kinds.length).toEqual(Object.keys(map).length);
});
});

describe("information", function() {
Expand Down
20 changes: 17 additions & 3 deletions src/quantities.js
Original file line number Diff line number Diff line change
Expand Up @@ -483,10 +483,9 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
* @returns {string[]} names of kinds of units
*/
Qty.getKinds = function() {
var knownKinds = Object.keys(KINDS).map(function(knownSignature) {
return uniq(Object.keys(KINDS).map(function(knownSignature) {
return KINDS[knownSignature];
}).sort();
return knownKinds;
}));
};

/**
Expand Down Expand Up @@ -1684,6 +1683,21 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
return value;
}

/**
* Returns unique strings from list
*
* @param {string[]} array of strings
*
*
* @returns {string[]} a new array of strings without duplicates
*/
function uniq(strings) {
var seen = {};
return strings.filter(function(item) {
return seen.hasOwnProperty(item) ? false : (seen[item] = true);
});
}

/**
* Tests if a value is a string
*
Expand Down

0 comments on commit 6d72047

Please sign in to comment.