From 55378cedf7a66ebce58e1c9de214e93cfbd27948 Mon Sep 17 00:00:00 2001 From: Arthur Britto Date: Fri, 26 Apr 2013 14:54:08 -0700 Subject: [PATCH 1/4] Add Amount.from_number(). --- src/js/amount.js | 16 ++++++++++++++++ src/js/currency.js | 4 ++++ src/js/uint160.js | 5 ++++- 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/js/amount.js b/src/js/amount.js index 14d65267..8fa93851 100644 --- a/src/js/amount.js +++ b/src/js/amount.js @@ -65,6 +65,10 @@ Amount.json_rewrite = function (j) { return Amount.from_json(j).to_json(); }; +Amount.from_number = function (n) { + return (new Amount()).parse_number(n); +}; + Amount.from_json = function (j) { return (new Amount()).parse_json(j); }; @@ -624,6 +628,18 @@ Amount.prototype.parse_quality = function (q, c, i) { return this; } +Amount.prototype.parse_number = function (n) { + this._currency = Currency.from_json(1); + this._issuer = UInt160.from_json(1); + this._is_negative = n < 0 ? 1 : 0; + this._value = new BigInteger(this._is_negative ? -n : n); + this._offset = 0; + + this.canonicalize(); + + return this; +}; + // <-> j Amount.prototype.parse_json = function (j) { if ('string' === typeof j) { diff --git a/src/js/currency.js b/src/js/currency.js index 22bd112f..38fb9ee5 100644 --- a/src/js/currency.js +++ b/src/js/currency.js @@ -52,6 +52,10 @@ Currency.prototype.parse_json = function (j) { if ("" === j || "0" === j || "XRP" === j) { this._value = 0; } + else if ('number' === typeof j) { + // XXX This is a hack + this._value = j; + } else if ('string' != typeof j || 3 !== j.length) { this._value = NaN; } diff --git a/src/js/uint160.js b/src/js/uint160.js index 66c980ec..52ef9728 100644 --- a/src/js/uint160.js +++ b/src/js/uint160.js @@ -37,7 +37,10 @@ UInt160.prototype.parse_json = function (j) { if (config.accounts && j in config.accounts) j = config.accounts[j].account; - if ('string' !== typeof j) { + if ('number' === typeof j) { + this._value = new BigInteger(j); + } + else if ('string' !== typeof j) { this._value = NaN; } else if (j[0] === "r") { From 3ce811ccddaa087f8acb056ff30b9d7bc1e738de Mon Sep 17 00:00:00 2001 From: Arthur Britto Date: Fri, 26 Apr 2013 15:17:09 -0700 Subject: [PATCH 2/4] Fix parse_number. --- src/js/amount.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/js/amount.js b/src/js/amount.js index 8fa93851..db006f9b 100644 --- a/src/js/amount.js +++ b/src/js/amount.js @@ -629,6 +629,7 @@ Amount.prototype.parse_quality = function (q, c, i) { } Amount.prototype.parse_number = function (n) { + this._is_native = false; this._currency = Currency.from_json(1); this._issuer = UInt160.from_json(1); this._is_negative = n < 0 ? 1 : 0; From c82ff0575b11b811ccdba222ef1f865c36c8f2e1 Mon Sep 17 00:00:00 2001 From: Arthur Britto Date: Fri, 26 Apr 2013 15:32:49 -0700 Subject: [PATCH 3/4] More fixes for from_number. --- src/js/amount.js | 2 +- src/js/uint160.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/js/amount.js b/src/js/amount.js index db006f9b..3d3fee8e 100644 --- a/src/js/amount.js +++ b/src/js/amount.js @@ -633,7 +633,7 @@ Amount.prototype.parse_number = function (n) { this._currency = Currency.from_json(1); this._issuer = UInt160.from_json(1); this._is_negative = n < 0 ? 1 : 0; - this._value = new BigInteger(this._is_negative ? -n : n); + this._value = new BigInteger(String(this._is_negative ? -n : n)); this._offset = 0; this.canonicalize(); diff --git a/src/js/uint160.js b/src/js/uint160.js index 52ef9728..830eeaa8 100644 --- a/src/js/uint160.js +++ b/src/js/uint160.js @@ -38,7 +38,7 @@ UInt160.prototype.parse_json = function (j) { j = config.accounts[j].account; if ('number' === typeof j) { - this._value = new BigInteger(j); + this._value = new BigInteger(String(j)); } else if ('string' !== typeof j) { this._value = NaN; From d37f0b9082499d92b5940de44c234ef1b675c7f2 Mon Sep 17 00:00:00 2001 From: Arthur Britto Date: Fri, 26 Apr 2013 15:38:04 -0700 Subject: [PATCH 4/4] Fix currency from integer. --- src/js/currency.js | 2 +- test/amount-test.js | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/js/currency.js b/src/js/currency.js index 38fb9ee5..49bb5429 100644 --- a/src/js/currency.js +++ b/src/js/currency.js @@ -23,7 +23,7 @@ Currency.json_rewrite = function (j) { Currency.from_json = function (j) { if (j instanceof Currency) return j.clone(); - else if ('string' === typeof j) return (new Currency()).parse_json(j); + else if ('string' === typeof j || 'number' === typeof j) return (new Currency()).parse_json(j); else return new Currency(); // NaN }; diff --git a/test/amount-test.js b/test/amount-test.js index fac357ad..a6d3e0a5 100644 --- a/test/amount-test.js +++ b/test/amount-test.js @@ -12,6 +12,12 @@ var config = require('../src/js/config').load(require('./config')); // XXX Add test cases that push XRP vs non-XRP behavior. buster.testCase("Amount", { + "from_number" : { + "Number 1" : function () { + buster.assert.equals("1/1/rrrrrrrrrrrrrrrrrrrrBZbvji", Amount.from_number(1).to_text_full()); + }, + }, + "UInt160" : { "Parse 0" : function () { buster.assert.equals(nbi(), UInt160.from_generic("0")._value);