Use new Amount(NaN) rather than Amount.NaN()

This commit is contained in:
wltsmrz
2015-03-06 15:38:44 -08:00
parent 6e16bf68ae
commit ed0b75bcde
3 changed files with 40 additions and 41 deletions

View File

@@ -1,7 +1,5 @@
'use strict'; 'use strict';
/*eslint-disable new-cap*/
// Represent Ripple amounts and currencies. // Represent Ripple amounts and currencies.
// - Numbers in hex are big-endian. // - Numbers in hex are big-endian.
@@ -137,7 +135,7 @@ Amount.prototype.add = function(addend) {
var addendAmount = Amount.from_json(addend); var addendAmount = Amount.from_json(addend);
if (!this.is_comparable(addendAmount)) { if (!this.is_comparable(addendAmount)) {
return Amount.NaN(); return new Amount(NaN);
} }
return this._copy(this._value.plus(addendAmount._value)); return this._copy(this._value.plus(addendAmount._value));
@@ -207,11 +205,11 @@ Amount.prototype.ratio_human = function(denominator, opts) {
// If either operand is NaN, the result is NaN. // If either operand is NaN, the result is NaN.
if (!numerator.is_valid() || !denominator.is_valid()) { if (!numerator.is_valid() || !denominator.is_valid()) {
return Amount.NaN(); return new Amount(NaN);
} }
if (denominator.is_zero()) { if (denominator.is_zero()) {
return Amount.NaN(); return new Amount(NaN);
} }
// Apply interest/demurrage // Apply interest/demurrage
@@ -267,7 +265,7 @@ Amount.prototype.product_human = function(factor, opts) {
// If either operand is NaN, the result is NaN. // If either operand is NaN, the result is NaN.
if (!this.is_valid() || !factor.is_valid()) { if (!this.is_valid() || !factor.is_valid()) {
return Amount.NaN(); return new Amount(NaN);
} }
// Apply interest/demurrage // Apply interest/demurrage
@@ -388,7 +386,7 @@ Amount.prototype._copy = function(value) {
Amount.prototype.compareTo = function(to) { Amount.prototype.compareTo = function(to) {
var toAmount = Amount.from_json(to); var toAmount = Amount.from_json(to);
if (!this.is_comparable(toAmount)) { if (!this.is_comparable(toAmount)) {
return Amount.NaN(); return new Amount(NaN);
} }
return this._value.comparedTo(toAmount._value); return this._value.comparedTo(toAmount._value);
}; };
@@ -507,7 +505,7 @@ Amount.prototype.parse_human = function(j, opts) {
value = words[0].slice(0, -3); value = words[0].slice(0, -3);
currency = words[0].slice(-3); currency = words[0].slice(-3);
if (!(isNumber(value) && currency.match(currency_RE))) { if (!(isNumber(value) && currency.match(currency_RE))) {
return Amount.NaN(); return new Amount(NaN);
} }
} }
} else if (words.length === 2) { } else if (words.length === 2) {
@@ -521,10 +519,10 @@ Amount.prototype.parse_human = function(j, opts) {
value = words[0]; value = words[0];
currency = words[1]; currency = words[1];
} else { } else {
return Amount.NaN(); return new Amount(NaN);
} }
} else { } else {
return Amount.NaN(); return new Amount(NaN);
} }
currency = currency.toUpperCase(); currency = currency.toUpperCase();

View File

@@ -242,6 +242,37 @@ function serialize(so, field_name, value) {
exports.serialize = exports.serialize_whatever = serialize; exports.serialize = exports.serialize_whatever = serialize;
// Take the serialized object, figure out what type/field it is, and return the
// parsing of that.
function parse(so) {
var tag_byte = so.read(1)[0];
var type_bits = tag_byte >> 4;
if (type_bits === 0) {
type_bits = so.read(1)[0];
}
var field_bits = tag_byte & 0x0f;
var field_name = (field_bits === 0)
? field_name = binformat.fields[type_bits][so.read(1)[0]]
: field_name = binformat.fields[type_bits][field_bits];
assert(field_name, 'Unknown field - header byte is 0x'
+ tag_byte.toString(16));
// Get the parser class (ST...) for a field based on the type bits.
var type = (field_name === 'Memo')
? exports.STMemo
: exports[binformat.types[type_bits]];
assert(type, 'Unknown type - header byte is 0x' + tag_byte.toString(16));
return [field_name, type.parse(so)]; // key, value
}
exports.parse = exports.parse_whatever = parse;
var STInt16 = exports.Int16 = new SerializedType({ var STInt16 = exports.Int16 = new SerializedType({
serialize: function (so, val) { serialize: function (so, val) {
so.append(convertIntegerToByteArray(val, 2)); so.append(convertIntegerToByteArray(val, 2));
@@ -765,37 +796,6 @@ exports.STMemo = new SerializedType({
}); });
// Take the serialized object, figure out what type/field it is, and return the
// parsing of that.
function parse(so) {
var tag_byte = so.read(1)[0];
var type_bits = tag_byte >> 4;
if (type_bits === 0) {
type_bits = so.read(1)[0];
}
var field_bits = tag_byte & 0x0f;
var field_name = (field_bits === 0)
? field_name = binformat.fields[type_bits][so.read(1)[0]]
: field_name = binformat.fields[type_bits][field_bits];
assert(field_name, 'Unknown field - header byte is 0x'
+ tag_byte.toString(16));
// Get the parser class (ST...) for a field based on the type bits.
var type = (field_name === 'Memo')
? exports.STMemo
: exports[binformat.types[type_bits]];
assert(type, 'Unknown type - header byte is 0x' + tag_byte.toString(16));
return [field_name, type.parse(so)]; // key, value
}
exports.parse = exports.parse_whatever = parse;
var STObject = exports.Object = new SerializedType({ var STObject = exports.Object = new SerializedType({
serialize: function (so, val, no_marker) { serialize: function (so, val, no_marker) {
var keys = []; var keys = [];

View File

@@ -5,6 +5,7 @@
/* eslint-disable no-spaced-func*/ /* eslint-disable no-spaced-func*/
/* eslint-disable space-before-blocks*/ /* eslint-disable space-before-blocks*/
/* eslint-disable space-unary-ops*/ /* eslint-disable space-unary-ops*/
/* eslint-disable no-multi-spaces*/
/* eslint-disable no-void*/ /* eslint-disable no-void*/
/* eslint-disable semi*/ /* eslint-disable semi*/
/* eslint-disable no-unused-vars*/ /* eslint-disable no-unused-vars*/