From f8587c36c3c271d8260ffa19069db53a61da9d01 Mon Sep 17 00:00:00 2001 From: Arthur Britto Date: Sat, 8 Dec 2012 17:55:37 -0800 Subject: [PATCH] JS: Add is_zero and compareTo to Amount. --- src/js/amount.js | 55 ++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 53 insertions(+), 2 deletions(-) diff --git a/src/js/amount.js b/src/js/amount.js index 9e7c4124d8..e02a0c72f3 100644 --- a/src/js/amount.js +++ b/src/js/amount.js @@ -425,6 +425,51 @@ Amount.prototype.clone = function(negate) { return this.copyTo(new Amount(), negate); }; +Amount.prototype.compareTo = function(v) { + var result; + + if (!this.is_comparable(v)) { + result = Amount.NaN(); + } + else if (this._is_native) { + result = this._value.compareTo(v._value); + + if (result > 1) + result = 1; + else if (result < -1) + result = -1; + } + else if (this._is_negative != v._is_negative) { + result = this._is_negative ? -1 : 1; + } + else if (this._value.equals(BigInteger.ZERO)) { + result = v._is_negative + ? 1 + : v._value.equals(BigInteger.ZERO) + ? 0 + : 1; + } + else if (v._value.equals(BigInteger.ZERO)) { + result = 1; + } + else if (this._offset > v._offset) { + result = this._is_negative ? -1 : 1; + } + else if (this._offset < v._offset) { + result = this._is_negative ? 1 : -1; + } + else { + result = this._value.compareTo(v._value); + + if (result > 1) + result = 1; + else if (result < -1) + result = -1; + } + + return result; +}; + // Returns copy. Amount.prototype.copyTo = function(d, negate) { if ('object' === typeof this._value) @@ -503,6 +548,12 @@ Amount.prototype.is_valid_full = function() { return this.is_valid() && this._currency.is_valid() && this._issuer.is_valid(); }; +Amount.prototype.is_zero = function() { + return this._value instanceof BigInteger + ? this._value.equals(BigInteger.ZERO) + : false; +}; + Amount.prototype.issuer = function() { return this._issuer; }; @@ -536,7 +587,7 @@ Amount.prototype.to_text = function(allow_nan) { return this._value.toString(); } } - else if (this._value.equals(BigInteger.ZERO)) + else if (this.is_zero()) { return "0"; } @@ -632,7 +683,7 @@ Amount.prototype.canonicalize = function() { // Native. // nothing } - else if (this._value.equals(BigInteger.ZERO)) { + else if (this.is_zero()) { this._offset = -100; this._is_negative = false; }