From 33432c6735658f06a9427ec9873dac889b20fc38 Mon Sep 17 00:00:00 2001 From: Kagami Hiiragi Date: Thu, 22 Jan 2015 02:39:51 +0300 Subject: [PATCH] Address.isAddress --- lib/address.js | 13 ++++++++++++- test.js | 7 +++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/lib/address.js b/lib/address.js index eed31ef..f399536 100644 --- a/lib/address.js +++ b/lib/address.js @@ -37,13 +37,24 @@ function Address(opts) { objectAssign(this, opts); } +/** + * Test if given object is an Address instance. NOTE: Implementation is + * just simple `instanceof` but it improves readability and consistent + * with `isArray`, `isBuffer`, etc. + * @param {Object} obj - Given object + * @return {boolean} + */ +Address.isAddress = function(obj) { + return obj instanceof Address; +}; + /** * Parse Bitmessage address into address object. * @param {String} str - Address string (with or without `BM-` prefix) * @return {Address} Decoded address object. */ Address.decode = function(str) { - if (str instanceof Address) { + if (Address.isAddress(str)) { return str; } diff --git a/test.js b/test.js index 068b257..a276e9d 100644 --- a/test.js +++ b/test.js @@ -606,6 +606,13 @@ describe("High-level classes", function() { expect(addr.getRipe({short: true}).toString("hex")).to.equal("69617ddb1946dc327cadffcf33889fed587fc1e7"); }); + it("should implement isAddress method", function() { + var addr = Address(); + expect(Address.isAddress(addr)).to.be.true; + expect(Address.isAddress(null)).to.be.false; + expect(Address.isAddress({})).to.be.false; + }); + // FIXME(Kagami): Don't run it in browser currently because it's // very slow. This need to be fixed. if (allTests && typeof window === "undefined") {