From 4f4b2b93fbcbd71882bdc3ebd9f6278c487d1894 Mon Sep 17 00:00:00 2001 From: eordano Date: Sun, 29 Mar 2015 22:21:31 -0300 Subject: [PATCH] Allow arrays as second arg to Networks.get --- lib/networks.js | 11 +++++++---- test/networks.js | 6 ++++++ 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/lib/networks.js b/lib/networks.js index da90f27..21d5723 100644 --- a/lib/networks.js +++ b/lib/networks.js @@ -22,16 +22,19 @@ Network.prototype.toString = function toString() { * @member Networks#get * Retrieves the network associated with a magic number or string. * @param {string|number|Network} arg - * @param {string} key - if set, only check if the magic number associated with this name matches + * @param {string|Array} keys - if set, only check if the magic number associated with this name matches * @return Network */ -function getNetwork(arg, key) { +function getNetwork(arg, keys) { if (~networks.indexOf(arg)) { return arg; } - if (key) { + if (keys) { + if (!_.isArray(keys)) { + keys = [keys]; + } for (var index in networks) { - if (networks[index][key] === arg) { + if (_.any(keys, function(key) { return networks[index][key] === arg; })) { return networks[index]; } } diff --git a/test/networks.js b/test/networks.js index 9e2d7dd..d4d9d4d 100644 --- a/test/networks.js +++ b/test/networks.js @@ -83,6 +83,12 @@ describe('Networks', function() { expect(networks.get(0x6f, 'privatekey')).to.equal(undefined); }); + it('can test for multiple keys', function() { + expect(networks.get(0x6f, ['pubkeyhash', 'scripthash'])).to.equal(networks.testnet); + expect(networks.get(0xc4, ['pubkeyhash', 'scripthash'])).to.equal(networks.testnet); + expect(networks.get(0x6f, ['privatekey', 'port'])).to.equal(undefined); + }); + it('converts to string using the "name" property', function() { networks.livenet.toString().should.equal('livenet'); });