diff --git a/README.md b/README.md index 3a613b2..bd0cab4 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ console.log(cc.code('EUR')); /* { code: 'EUR', - number: 978, + number: '978', digits: 2, currency: 'Euro', countries: [ @@ -35,7 +35,7 @@ console.log(cc.number(967)); /* { code: 'ZMW', - number: 967, + number: '967', digits: 2, currency: 'Zambian kwacha', countries: [ 'zambia' ] } @@ -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' ] diff --git a/index.js b/index.js index 804cc31..336b008 100644 --- a/index.js +++ b/index.js @@ -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() { diff --git a/test.js b/test.js index 1a39ea0..64ec950 100644 --- a/test.js +++ b/test.js @@ -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');