Skip to content

Commit

Permalink
Throw error if duplicate names [fix]
Browse files Browse the repository at this point in the history
  • Loading branch information
overlookmotel committed Dec 30, 2019
1 parent 1d46dfc commit a9d3e8a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,14 @@ module.exports = function makeSymbols(namespace, names, options) {
if (!isFullString(namespace)) throw new TypeError('namespace must be a string');
if (!isArray(names)) throw new TypeError('names must be an array of strings');

const namesSet = new Set();
for (const name of names) {
if (!isFullString(name)) throw new TypeError('names must be an array of strings');
if (name.toUpperCase() !== name) throw new Error('symbol names must be all capitalized');
if (namesSet.has(name)) {
throw new Error(`symbol names must be unique - '${name}' was duplicated`);
}
namesSet.add(name);
}

const symbols = {};
Expand Down
6 changes: 6 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ describe('throws error if', () => {
makeSymbols('foo', ['BAR', 'Qux']);
}).toThrowWithMessage(Error, 'symbol names must be all capitalized');
});

it('contains duplicates', () => {
expect(() => {
makeSymbols('foo', ['BAR', 'QUX', 'BAR']);
}).toThrowWithMessage(Error, "symbol names must be unique - 'BAR' was duplicated");
});
});

describe('options', () => {
Expand Down

0 comments on commit a9d3e8a

Please sign in to comment.