From cddd55afc4b17b40b7cb916f6f2c8e8f33645194 Mon Sep 17 00:00:00 2001 From: Braydon Fuller Date: Fri, 12 Dec 2014 18:53:27 -0500 Subject: [PATCH] URI/Unit: Added toObject method and changed toJSON to return a string --- lib/unit.js | 8 ++++++-- lib/uri.js | 6 +++++- test/uri.js | 4 ++-- 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/lib/unit.js b/lib/unit.js index dc6bb7433..f51eda6cd 100644 --- a/lib/unit.js +++ b/lib/unit.js @@ -175,17 +175,21 @@ Unit.prototype.toString = function() { }; /** - * Will return a the JSON object representation of the unit + * Will return a plain object representation of the Unit * * @returns {Object} An object with the keys: amount and code */ -Unit.prototype.toJSON = function toJSON(){ +Unit.prototype.toObject = function toObject() { return { amount: this._amount, code: this._code }; }; +Unit.prototype.toJSON = function toJSON() { + return this.toObject(); +}; + /** * Will return a string formatted for the console * diff --git a/lib/uri.js b/lib/uri.js index 8013cf7f4..a551605e8 100644 --- a/lib/uri.js +++ b/lib/uri.js @@ -163,7 +163,7 @@ URI.prototype._parseAmount = function(amount) { return Unit.fromBTC(amount).toSatoshis(); }; -URI.prototype.toJSON = function() { +URI.prototype.toObject = function toObject() { var json = {}; for (var i = 0; i < URI.Members.length; i++) { var m = URI.Members[i]; @@ -179,6 +179,10 @@ URI.prototype.toJSON = function() { return json; }; +URI.prototype.toJSON = function toJSON() { + return JSON.stringify(this.toObject()); +}; + /** * Will return a the string representation of the URI * diff --git a/test/uri.js b/test/uri.js index e0b135e15..aad0934a6 100644 --- a/test/uri.js +++ b/test/uri.js @@ -146,12 +146,12 @@ describe('URI', function() { }); it('should input/output JSON', function() { - var json = { + var json = JSON.stringify({ address: '1DP69gMMvSuYhbnxsi4EJEFufUAbDrEQfj', message: 'Donation for project xyz', label: 'myLabel', other: 'xD' - }; + }); URI.fromJSON(json).toJSON().should.deep.equal(json); });