Add UInt160.is_valid(), Amount.is_valid(), Currency.is_valid(), & Currency.is_valid_full().

This commit is contained in:
Arthur Britto
2012-11-23 18:47:39 -08:00
parent 03e1357448
commit 19bdee89cd
2 changed files with 28 additions and 3 deletions

View File

@@ -137,6 +137,10 @@ UInt160.from_json = function (j) {
: j.clone(); : j.clone();
}; };
UInt160.is_valid = function (j) {
return UInt160.from_json(j).is_valid();
};
UInt160.prototype.clone = function() { UInt160.prototype.clone = function() {
return this.copyTo(new UInt160()); return this.copyTo(new UInt160());
}; };
@@ -218,7 +222,7 @@ UInt160.prototype.to_json = function () {
}; };
UInt160.prototype.is_valid = function () { UInt160.prototype.is_valid = function () {
return !isNaN(this_value); return !isNaN(this._value);
}; };
// XXX Internal form should be UInt160. // XXX Internal form should be UInt160.
@@ -245,6 +249,10 @@ Currency.from_json = function (j) {
: j.clone(); : j.clone();
}; };
currency.is_valid = function (j) {
return currency.from_json(j).is_valid();
};
Currency.prototype.clone = function() { Currency.prototype.clone = function() {
return this.copyTo(new Currency()); return this.copyTo(new Currency());
}; };
@@ -277,7 +285,7 @@ Currency.prototype.parse_json = function(j) {
}; };
Currency.prototype.is_valid = function () { Currency.prototype.is_valid = function () {
return !isNaN(this_value); return !isNaN(this._value);
}; };
Currency.prototype.to_json = function () { Currency.prototype.to_json = function () {
@@ -316,6 +324,14 @@ Amount.from_json = function(j) {
return (new Amount()).parse_json(j); return (new Amount()).parse_json(j);
}; };
Amount.is_valid = function (j) {
return Amount.from_json(j).is_valid();
};
Amount.is_valid_full = function (j) {
return Amount.from_json(j).is_valid_full();
};
Amount.prototype.clone = function(negate) { Amount.prototype.clone = function(negate) {
return this.copyTo(new Amount(), negate); return this.copyTo(new Amount(), negate);
}; };
@@ -352,11 +368,15 @@ Amount.prototype.currency = function() {
return this._currency; return this._currency;
}; };
// YYY Might also provide is_valid_json. // Only checks the value. Not the currency and issuer.
Amount.prototype.is_valid = function() { Amount.prototype.is_valid = function() {
return !isNaN(this._value); return !isNaN(this._value);
}; };
Amount.prototype.is_valid_full = function() {
return this.is_valid() && this._currency.is_valid() && this._issuer.is_valid();
};
Amount.prototype.issuer = function() { Amount.prototype.issuer = function() {
return this._issuer; return this._issuer;
}; };

View File

@@ -34,7 +34,12 @@ buster.testCase("Amount", {
"Parse mtgox export" : function () { "Parse mtgox export" : function () {
buster.assert.equals(config.accounts["mtgox"].account, UInt160.from_json("mtgox").to_json()); buster.assert.equals(config.accounts["mtgox"].account, UInt160.from_json("mtgox").to_json());
}, },
"is_valid('rrrrrrrrrrrrrrrrrrrrrhoLvTp')" : function () {
buster.assert(UInt160.is_valid("rrrrrrrrrrrrrrrrrrrrrhoLvTp"));
},
}, },
"Amount parsing" : { "Amount parsing" : {
"Parse 800/USD/mtgox" : function () { "Parse 800/USD/mtgox" : function () {
buster.assert.equals("800/USD/"+config.accounts["mtgox"].account, Amount.from_json("800/USD/mtgox").to_text_full()); buster.assert.equals("800/USD/"+config.accounts["mtgox"].account, Amount.from_json("800/USD/mtgox").to_text_full());