diff --git a/src/js/amount.js b/src/js/amount.js index f3e6d3107..be10b6f6c 100644 --- a/src/js/amount.js +++ b/src/js/amount.js @@ -137,6 +137,10 @@ UInt160.from_json = function (j) { : j.clone(); }; +UInt160.is_valid = function (j) { + return UInt160.from_json(j).is_valid(); +}; + UInt160.prototype.clone = function() { return this.copyTo(new UInt160()); }; @@ -218,7 +222,7 @@ UInt160.prototype.to_json = function () { }; UInt160.prototype.is_valid = function () { - return !isNaN(this_value); + return !isNaN(this._value); }; // XXX Internal form should be UInt160. @@ -245,6 +249,10 @@ Currency.from_json = function (j) { : j.clone(); }; +currency.is_valid = function (j) { + return currency.from_json(j).is_valid(); +}; + Currency.prototype.clone = function() { return this.copyTo(new Currency()); }; @@ -277,7 +285,7 @@ Currency.prototype.parse_json = function(j) { }; Currency.prototype.is_valid = function () { - return !isNaN(this_value); + return !isNaN(this._value); }; Currency.prototype.to_json = function () { @@ -316,6 +324,14 @@ Amount.from_json = function(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) { return this.copyTo(new Amount(), negate); }; @@ -352,11 +368,15 @@ Amount.prototype.currency = function() { return this._currency; }; -// YYY Might also provide is_valid_json. +// Only checks the value. Not the currency and issuer. Amount.prototype.is_valid = function() { 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() { return this._issuer; }; diff --git a/test/amount-test.js b/test/amount-test.js index c5bb91c4e..bbd19c8bd 100644 --- a/test/amount-test.js +++ b/test/amount-test.js @@ -34,7 +34,12 @@ buster.testCase("Amount", { "Parse mtgox export" : function () { 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" : { "Parse 800/USD/mtgox" : function () { buster.assert.equals("800/USD/"+config.accounts["mtgox"].account, Amount.from_json("800/USD/mtgox").to_text_full());