From a92f218b70b331e8469c771fc395d0b8b844ca97 Mon Sep 17 00:00:00 2001 From: Daniel Wirtz Date: Wed, 18 Jun 2014 16:10:25 +0200 Subject: [PATCH] Accept Long-like object in constructor, fixes #8 --- dist/Long.js | 10 ++++++++-- dist/Long.min.js | 8 ++++---- docs/Long.html | 9 +++++++-- externs/Long.js | 4 ++-- package.json | 2 +- src/Long.js | 10 ++++++++-- tests/suite.js | 4 ++++ 7 files changed, 34 insertions(+), 13 deletions(-) diff --git a/dist/Long.js b/dist/Long.js index cde012d..03afe1b 100644 --- a/dist/Long.js +++ b/dist/Long.js @@ -44,12 +44,18 @@ * * @exports Long * @class A Long class for representing a 64-bit two's-complement integer value. - * @param {number} low The low (signed) 32 bits of the long. - * @param {number} high The high (signed) 32 bits of the long. + * @param {number|!{low: number, high: number, unsigned: boolean}} low The low (signed) 32 bits of the long. + * Optionally accepts a Long-like object as the first parameter. + * @param {number=} high The high (signed) 32 bits of the long. * @param {boolean=} unsigned Whether unsigned or not. Defaults to `false` (signed). * @constructor */ var Long = function(low, high, unsigned) { + if (low && typeof low === 'object') { + high = low.high; + unsigned = low.unsigned; + low = low.low; + } /** * The low 32 bits as a signed value. diff --git a/dist/Long.min.js b/dist/Long.min.js index 2e5a578..4e01dc0 100644 --- a/dist/Long.min.js +++ b/dist/Long.min.js @@ -5,10 +5,10 @@ see: https://github.com/dcodeIO/Long.js for details */ var q=!0,r=!1; -(function(s){function b(a,b,d){this.low=a|0;this.high=b|0;this.unsigned=!!d}var t={},u={};b.fromInt=function(a,c){var d;if(c){a>>>=0;if(0<=a&&256>a&&(d=u[a]))return d;d=new b(a,0>(a|0)?-1:0,q);0<=a&&256>a&&(u[a]=d)}else{a|=0;if(-128<=a&&128>a&&(d=t[a]))return d;d=new b(a,0>a?-1:0,r);-128<=a&&128>a&&(t[a]=d)}return d};b.fromNumber=function(a,c){c=!!c;return isNaN(a)||!isFinite(a)?b.ZERO:!c&&a<=-v?b.MIN_SIGNED_VALUE:c&&0>=a?b.MIN_UNSIGNED_VALUE:!c&&a+1>=v?b.MAX_SIGNED_VALUE:c&&a>=w?b.MAX_UNSIGNED_VALUE:0> -a?b.fromNumber(-a,r).negate():new b(a%l|0,a/l|0,c)};b.fromBits=function(a,c,d){return new b(a,c,d)};b.from28Bits=function(a,c,d,e){return b.fromBits(a|c<<28,c>>>4|d<<24,e)};b.fromString=function(a,c,d){if(0==a.length)throw Error("number format error: empty string");if("NaN"===a||"Infinity"===a||"+Infinity"===a||"-Infinity"===a)return b.ZERO;"number"===typeof c&&(d=c,c=r);d=d||10;if(2>d||36k?(k=b.fromNumber(Math.pow(d,k)),f=f.multiply(k).add(b.fromNumber(m))):(f=f.multiply(e),f=f.add(b.fromNumber(m)))}f.unsigned=c;return f};var l=4294967296,w=l*l,v=w/2,x=b.fromInt(16777216);b.ZERO=b.fromInt(0);b.UZERO=b.fromInt(0,q);b.ONE=b.fromInt(1);b.UONE=b.fromInt(1,q);b.NEG_ONE=b.fromInt(-1); -b.MAX_SIGNED_VALUE=b.fromBits(-1,2147483647,r);b.MAX_UNSIGNED_VALUE=b.fromBits(-1,-1,q);b.MAX_VALUE=b.MAX_SIGNED_VALUE;b.MIN_SIGNED_VALUE=b.fromBits(0,-2147483648,r);b.MIN_UNSIGNED_VALUE=b.fromBits(0,0,q);b.MIN_VALUE=b.MIN_SIGNED_VALUE;b.prototype.toInt=function(){return this.unsigned?this.low>>>0:this.low};b.prototype.toNumber=function(){return this.unsigned?(this.high>>>0)*l+(this.low>>>0):this.high*l+(this.low>>>0)};b.prototype.toString=function(a){a=a||10;if(2>a||36>>=0;if(0<=a&&256>a&&(d=u[a]))return d;d=new b(a,0>(a|0)?-1:0,q);0<=a&&256>a&&(u[a]=d)}else{a|=0;if(-128<=a&&128>a&&(d=t[a]))return d;d=new b(a,0>a?-1:0,r);-128<=a&&128>a&&(t[a]=d)}return d};b.fromNumber=function(a,c){c=!!c;return isNaN(a)||!isFinite(a)?b.ZERO:!c&&a<=-v?b.MIN_SIGNED_VALUE:c&&0>=a?b.MIN_UNSIGNED_VALUE:!c&& +a+1>=v?b.MAX_SIGNED_VALUE:c&&a>=w?b.MAX_UNSIGNED_VALUE:0>a?b.fromNumber(-a,r).negate():new b(a%l|0,a/l|0,c)};b.fromBits=function(a,c,d){return new b(a,c,d)};b.from28Bits=function(a,c,d,e){return b.fromBits(a|c<<28,c>>>4|d<<24,e)};b.fromString=function(a,c,d){if(0==a.length)throw Error("number format error: empty string");if("NaN"===a||"Infinity"===a||"+Infinity"===a||"-Infinity"===a)return b.ZERO;"number"===typeof c&&(d=c,c=r);d=d||10;if(2>d||36k?(k=b.fromNumber(Math.pow(d,k)),f=f.multiply(k).add(b.fromNumber(m))):(f=f.multiply(e),f=f.add(b.fromNumber(m)))}f.unsigned=c;return f};var l=4294967296,w=l*l,v=w/2,x=b.fromInt(16777216);b.ZERO=b.fromInt(0);b.UZERO=b.fromInt(0,q);b.ONE=b.fromInt(1);b.UONE=b.fromInt(1,q); +b.NEG_ONE=b.fromInt(-1);b.MAX_SIGNED_VALUE=b.fromBits(-1,2147483647,r);b.MAX_UNSIGNED_VALUE=b.fromBits(-1,-1,q);b.MAX_VALUE=b.MAX_SIGNED_VALUE;b.MIN_SIGNED_VALUE=b.fromBits(0,-2147483648,r);b.MIN_UNSIGNED_VALUE=b.fromBits(0,0,q);b.MIN_VALUE=b.MIN_SIGNED_VALUE;b.prototype.toInt=function(){return this.unsigned?this.low>>>0:this.low};b.prototype.toNumber=function(){return this.unsigned?(this.high>>>0)*l+(this.low>>>0):this.high*l+(this.low>>>0)};b.prototype.toString=function(a){a=a||10;if(2>a||36g.length;)g="0"+g;e=""+g+e}};b.prototype.getHighBits=function(){return this.high};b.prototype.getHighBitsUnsigned= function(){return this.high>>>0};b.prototype.getLowBits=function(){return this.low};b.prototype.getLowBitsUnsigned=function(){return this.low>>>0};b.prototype.getNumBitsAbs=function(){if(this.isNegative())return this.equals(b.MIN_SIGNED_VALUE)?64:this.negate().getNumBitsAbs();for(var a=0!=this.high?this.high:this.low,c=31;0this.high}; b.prototype.isOdd=function(){return 1==(this.low&1)};b.prototype.equals=function(a){return this.unsigned!=a.unsigned&&this.high>>>31!=a.high>>>31?r:this.high==a.high&&this.low==a.low};b.prototype.notEquals=function(a){return!this.equals(a)};b.prototype.lessThan=function(a){return 0>this.compare(a)};b.prototype.lessThanOrEqual=function(a){return 0>=this.compare(a)};b.prototype.greaterThan=function(a){return 0
-

new Long(low, high, unsigned)

+

new Long(low, high, unsigned)

@@ -92,6 +92,9 @@
Parameters:
number +| + +!{low: number, high: number, unsigned: boolean} @@ -107,7 +110,7 @@
Parameters:
- The low (signed) 32 bits of the long. + The low (signed) 32 bits of the long. Optionally accepts a Long-like object as the first parameter. @@ -129,6 +132,8 @@
Parameters:
+ <optional>
+ diff --git a/externs/Long.js b/externs/Long.js index 6ff9897..9538fa3 100644 --- a/externs/Long.js +++ b/externs/Long.js @@ -27,8 +27,8 @@ */ /** - * @param {number} low - * @param {number} high + * @param {number|!{low: number, high: number, unsigned: boolean}} low + * @param {number=} high * @param {boolean=} unsigned * @constructor */ diff --git a/package.json b/package.json index 8a90169..f181223 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "long", - "version": "1.1.3", + "version": "1.1.4", "author": "Daniel Wirtz ", "description": "A Long class for representing a 64-bit two's-complement integer value.", "main": "index.js", diff --git a/src/Long.js b/src/Long.js index cde012d..03afe1b 100644 --- a/src/Long.js +++ b/src/Long.js @@ -44,12 +44,18 @@ * * @exports Long * @class A Long class for representing a 64-bit two's-complement integer value. - * @param {number} low The low (signed) 32 bits of the long. - * @param {number} high The high (signed) 32 bits of the long. + * @param {number|!{low: number, high: number, unsigned: boolean}} low The low (signed) 32 bits of the long. + * Optionally accepts a Long-like object as the first parameter. + * @param {number=} high The high (signed) 32 bits of the long. * @param {boolean=} unsigned Whether unsigned or not. Defaults to `false` (signed). * @constructor */ var Long = function(low, high, unsigned) { + if (low && typeof low === 'object') { + high = low.high; + unsigned = low.unsigned; + low = low.low; + } /** * The low 32 bits as a signed value. diff --git a/tests/suite.js b/tests/suite.js index 4e1ff8c..ab37452 100644 --- a/tests/suite.js +++ b/tests/suite.js @@ -25,6 +25,10 @@ var suite = { var longVal = new Long(0xFFFFFFFF, 0x7FFFFFFF); test.equal(longVal.toNumber(), 9223372036854775807); test.equal(longVal.toString(), "9223372036854775807"); + var longVal2 = new Long(longVal); + test.equal(longVal2.toNumber(), 9223372036854775807); + test.equal(longVal2.toString(), "9223372036854775807"); + test.equal(longVal2.unsigned, longVal.unsigned); test.done(); },