Skip to content

Commit

Permalink
Allow lookup using numbers < 100 (fixes freeall#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
GitHub authored and GitHub committed Apr 1, 2021
1 parent fd7f9c5 commit a141460
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ var country = function(country) {
});
};
var number = function(number) {
number = ('000' + number).slice(-3);

return first(data, function(c) {
return c.number === String(number);
return c.number === number;
});
};
var codes = function() {
Expand Down
15 changes: 14 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,27 @@ var assert = require('assert');
assert.strictEqual(cc.code('EUR').countries.length, 35, 'The Euro is used by 35 countries');
assert.strictEqual(cc.code('IDR').digits, 2, 'The Indonesian Rupiah uses a fraction of 2 digits');
assert.strictEqual(cc.number('967').currency, 'Zambian Kwacha', 'Zambian Kwacha is number 967');
assert.strictEqual(cc.number('036').code, 'AUD', 'Australian Dollar is AUD');
assert.strictEqual(cc.country('Colombia').length, 2, 'Colombia has 2 currencies');
assert.strictEqual(typeof cc.code('JPY').number, 'string', 'Currency numbers are strings');

// test input flexibility

assert.strictEqual(cc.number(967).currency, 'Zambian Kwacha', 'Currencies can be found with number as integer');
assert.deepStrictEqual(cc.country('colombia'), cc.country('Colombia'), 'Searching by country name is case-insensitive');
assert.strictEqual(cc.number(967).currency, 'Zambian Kwacha', 'Currencies can be found with a number as integer');
assert.deepStrictEqual(cc.number(36), cc.number('036'), 'Currencies can be found with a number < 100 as integer');
assert.deepStrictEqual(cc.number('36'), cc.number('036'), 'Currencies can be found with a number < 100 as string');

// test data across all currencies

cc.numbers().forEach((number) => {
assert(number.match(/^[0-9]{3}$/), `"${number}" is expected to be exactly 3 digits`);
});

cc.codes().forEach((code) => {
assert(code.match(/^[A-Z]{3}$/), `"${code}" is expected to be exactly 3 uppercase characters`);
});

// test totals

assert.strictEqual(cc.data.length, 179, 'There are a total of 179 currencies');
Expand Down

0 comments on commit a141460

Please sign in to comment.