mirror of
https://github.com/Xahau/xahau.js.git
synced 2025-11-20 20:25:48 +00:00
JS: Add clone() and copyTo() to Amount and friends.
This commit is contained in:
committed by
Stefan Thomas
parent
8b04a5d68d
commit
f40dd0fa83
60
js/amount.js
60
js/amount.js
@@ -10,12 +10,24 @@ var UInt160 = function () {
|
|||||||
// Internal form:
|
// Internal form:
|
||||||
// 0, 1, 'iXXXXX', 20 byte string, or NaN.
|
// 0, 1, 'iXXXXX', 20 byte string, or NaN.
|
||||||
// XXX Should standardize on 'i' format or 20 format.
|
// XXX Should standardize on 'i' format or 20 format.
|
||||||
|
this.value = NaN;
|
||||||
};
|
};
|
||||||
|
|
||||||
UInt160.from_json = function (j) {
|
UInt160.from_json = function (j) {
|
||||||
return (new UInt160()).parse_json(j);
|
return (new UInt160()).parse_json(j);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
UInt160.prototype.clone = function() {
|
||||||
|
return this.copyTo(new UInt160());
|
||||||
|
};
|
||||||
|
|
||||||
|
// Returns copy.
|
||||||
|
UInt160.prototype.copyTo = function(d) {
|
||||||
|
d.value = this.value;
|
||||||
|
|
||||||
|
return d;
|
||||||
|
};
|
||||||
|
|
||||||
// value === NaN on error.
|
// value === NaN on error.
|
||||||
UInt160.prototype.parse_json = function (j) {
|
UInt160.prototype.parse_json = function (j) {
|
||||||
// Canonicalize and validate
|
// Canonicalize and validate
|
||||||
@@ -86,14 +98,27 @@ var Currency = function () {
|
|||||||
// '', 'XNS', '0': 0
|
// '', 'XNS', '0': 0
|
||||||
// 3-letter code: ...
|
// 3-letter code: ...
|
||||||
// XXX Should support hex, C++ doesn't currently allow it.
|
// XXX Should support hex, C++ doesn't currently allow it.
|
||||||
|
|
||||||
|
this.value = NaN;
|
||||||
}
|
}
|
||||||
|
|
||||||
Currency.from_json = function (j) {
|
Currency.from_json = function (j) {
|
||||||
return (new Currency()).parse_json(j);
|
return (new Currency()).parse_json(j);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Currency.prototype.clone = function() {
|
||||||
|
return this.copyTo(new Currency());
|
||||||
|
};
|
||||||
|
|
||||||
|
// Returns copy.
|
||||||
|
Currency.prototype.copyTo = function(d) {
|
||||||
|
d.value = this.value;
|
||||||
|
|
||||||
|
return d;
|
||||||
|
};
|
||||||
|
|
||||||
// this.value === NaN on error.
|
// this.value === NaN on error.
|
||||||
Currency.prototype.parse_json = function (j) {
|
Currency.prototype.parse_json = function(j) {
|
||||||
if ("" === j || "0" === j || "XNS" === j) {
|
if ("" === j || "0" === j || "XNS" === j) {
|
||||||
this.value = 0;
|
this.value = 0;
|
||||||
}
|
}
|
||||||
@@ -129,14 +154,39 @@ var Amount = function () {
|
|||||||
this.issuer = new UInt160();
|
this.issuer = new UInt160();
|
||||||
};
|
};
|
||||||
|
|
||||||
Amount.from_json = function (j) {
|
Amount.from_json = function(j) {
|
||||||
return (new Amount()).parse_json(j);
|
return (new Amount()).parse_json(j);
|
||||||
};
|
};
|
||||||
|
|
||||||
// YYY Might also check range.
|
Amount.prototype.clone = function() {
|
||||||
|
return this.copyTo(new Amount());
|
||||||
|
};
|
||||||
|
|
||||||
|
// Returns copy.
|
||||||
|
Amount.prototype.copyTo = function(d) {
|
||||||
|
if ('object' === typeof this.value)
|
||||||
|
{
|
||||||
|
this.value.copyTo(d.value);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
d.value = this.value;
|
||||||
|
}
|
||||||
|
|
||||||
|
d.offset = this.offset;
|
||||||
|
d.is_native = this.is_native;
|
||||||
|
d.is_negative = this.is_negative;
|
||||||
|
|
||||||
|
this.currency.copyTo(d.currency);
|
||||||
|
this.issuer.copyTo(d.issuer);
|
||||||
|
|
||||||
|
return d;
|
||||||
|
};
|
||||||
|
|
||||||
|
// YYY Might also provide is_valid_json.
|
||||||
Amount.prototype.is_valid = function() {
|
Amount.prototype.is_valid = function() {
|
||||||
return NaN !== this.value;
|
return NaN !== this.value;
|
||||||
}
|
};
|
||||||
|
|
||||||
// Convert only value to JSON wire format.
|
// Convert only value to JSON wire format.
|
||||||
Amount.prototype.to_text = function(allow_nan) {
|
Amount.prototype.to_text = function(allow_nan) {
|
||||||
@@ -234,7 +284,7 @@ Amount.prototype.parse_native = function(j) {
|
|||||||
if ('string' === typeof j)
|
if ('string' === typeof j)
|
||||||
m = j.match(/^(\d+)(\.\d{1,6})?$/);
|
m = j.match(/^(\d+)(\.\d{1,6})?$/);
|
||||||
|
|
||||||
if (null !== m) {
|
if (m) {
|
||||||
if (undefined === m[2]) {
|
if (undefined === m[2]) {
|
||||||
// Integer notation
|
// Integer notation
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user