Address.isAddress

This commit is contained in:
Kagami Hiiragi 2015-01-22 02:39:51 +03:00
parent 26da0b1d2f
commit 33432c6735
2 changed files with 19 additions and 1 deletions

View File

@ -37,13 +37,24 @@ function Address(opts) {
objectAssign(this, 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. * Parse Bitmessage address into address object.
* @param {String} str - Address string (with or without `BM-` prefix) * @param {String} str - Address string (with or without `BM-` prefix)
* @return {Address} Decoded address object. * @return {Address} Decoded address object.
*/ */
Address.decode = function(str) { Address.decode = function(str) {
if (str instanceof Address) { if (Address.isAddress(str)) {
return str; return str;
} }

View File

@ -606,6 +606,13 @@ describe("High-level classes", function() {
expect(addr.getRipe({short: true}).toString("hex")).to.equal("69617ddb1946dc327cadffcf33889fed587fc1e7"); 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 // FIXME(Kagami): Don't run it in browser currently because it's
// very slow. This need to be fixed. // very slow. This need to be fixed.
if (allTests && typeof window === "undefined") { if (allTests && typeof window === "undefined") {