URI/Unit: Added toObject method and changed toJSON to return a string

This commit is contained in:
Braydon Fuller 2014-12-12 18:53:27 -05:00
parent c0bbf96dc1
commit cddd55afc4
3 changed files with 13 additions and 5 deletions

View File

@ -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
*

View File

@ -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
*

View File

@ -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);
});