Skip to content
Open
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
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ console.log(cc.code('EUR'));
/*
{
code: 'EUR',
number: 978,
number: '978',
digits: 2,
currency: 'Euro',
countries: [
Expand All @@ -35,7 +35,7 @@ console.log(cc.number(967));
/*
{
code: 'ZMW',
number: 967,
number: '967',
digits: 2,
currency: 'Zambian kwacha',
countries: [ 'zambia' ] }
Expand All @@ -52,13 +52,13 @@ console.log(cc.country('colombia'));
[
{
code: 'COP',
number: 170,
number: '170',
digits: 2,
currency: 'Colombian peso',
countries: [ 'colombia' ]
}, {
code: 'COU',
number: 970,
number: '970',
digits: 2,
currency: 'Unidad de Valor Real',
countries: [ 'colombia' ]
Expand Down
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);
Copy link
Author

@ronkorving ronkorving Apr 2, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could've used String(number).padStart(3, '0'), but that requires Node 8. I don't know which version of Node you're intending to support, not to mention browsers, but given that all the JavaScript I see here is fairly legacy, I went with the safe option.


return first(data, function(c) {
return c.number === String(number);
return c.number === number;
});
};
var codes = function() {
Expand Down
43 changes: 32 additions & 11 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,35 @@
var cc = require('./index');
var assert = require('assert');

assert(cc.code('EUR').countries.length === 35);
assert(cc.code('IDR').digits === 2);
assert(cc.number('967').currency === 'Zambian Kwacha');
assert(cc.number(967).currency === 'Zambian Kwacha');
assert(cc.country('Colombia').length === 2);
assert(cc.country('colombia').length === 2);
assert(cc.codes().length === 178);
assert(cc.countries().length === 250);
assert(cc.numbers().length === 178);
assert(cc.numbers()[0] === '784');
assert(cc.data.length == 178);
// test properties of specific currencies

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.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');
assert.strictEqual(cc.codes().length, cc.data.length, 'There are as many currency codes as there are currencies');
assert.strictEqual(cc.numbers().length, cc.data.length, 'There are as many currency numbers as there are currencies');
assert.strictEqual(cc.countries().length, 260, 'There are a total of 260 countries');