Skip to content

Commit

Permalink
Add currency number list function (#11)
Browse files Browse the repository at this point in the history
* Fix failing assertion for countries count (245 -> 246)

* Add numbers() function

* Document numbers() in readme

* Handle undefined currency numbers (e.g. for XFU and XBT)

* Revert to single quotes

* Update .gitignore
  • Loading branch information
chris-jamieson authored and freeall committed Apr 11, 2018
1 parent a855878 commit 9d09714
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 21 deletions.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,23 @@ console.log(cc.codes());
*/
```

## numbers()

``` js
var cc = require('currency-codes');
console.log(cc.numbers());

/*
[
'784',
'971',
...
'710',
'967'
]
*/
```

## countries()

``` js
Expand Down
55 changes: 35 additions & 20 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,55 @@ var nub = require('nub');
var data = require('./data');

var code = function(code) {
code = code.toUpperCase();
code = code.toUpperCase();

return first(data, function(c) {
return c.code === code;
});
return first(data, function(c) {
return c.code === code;
});
};
var country = function(country) {
country = country.toLowerCase();
country = country.toLowerCase();

return data.filter(function(c) {
return (c.countries || []).indexOf(country) > -1;
});
return data.filter(function(c) {
return (c.countries || []).indexOf(country) > -1;
});
};
var number = function(number) {
return first(data, function(c) {
return c.number === String(number);
});
return first(data, function(c) {
return c.number === String(number);
});
};
var codes = function() {
return data.map(function(c) {
return c.code;
});
return data.map(function(c) {
return c.code;
});
};
var numbers = function() {
var items = data.map(function(c) {
return c.number;
});

// handle cases where number is undefined (e.g. XFU and XBT)
return items.filter(function(n) {
if (n) {
return n;
}
});
};
var countries = function() {
var m = data.filter(function(c) {
return c.countries;
}).map(function(c) {
return c.countries;
});
return nub(Array.prototype.concat.apply([], m));
var m = data
.filter(function(c) {
return c.countries;
})
.map(function(c) {
return c.countries;
});
return nub(Array.prototype.concat.apply([], m));
};

exports.code = code;
exports.country = country;
exports.number = number;
exports.codes = codes;
exports.numbers = numbers;
exports.countries = countries;
18 changes: 18 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ assert(cc.number('967').currency === 'Zambian kwacha');
assert(cc.number(967).currency === 'Zambian kwacha');
assert(cc.country('colombia').length === 2);
assert(cc.codes().length === 179);
assert(cc.countries().length === 245);
assert(cc.countries().length === 246);
assert(cc.numbers().length === 177);
assert(cc.numbers()[0] === '784');

0 comments on commit 9d09714

Please sign in to comment.