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") {