Skip to content

Commit a92f218

Browse files
committed
Accept Long-like object in constructor, fixes #8
1 parent 4be41f6 commit a92f218

7 files changed

Lines changed: 34 additions & 13 deletions

File tree

dist/Long.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,18 @@
4444
*
4545
* @exports Long
4646
* @class A Long class for representing a 64-bit two's-complement integer value.
47-
* @param {number} low The low (signed) 32 bits of the long.
48-
* @param {number} high The high (signed) 32 bits of the long.
47+
* @param {number|!{low: number, high: number, unsigned: boolean}} low The low (signed) 32 bits of the long.
48+
* Optionally accepts a Long-like object as the first parameter.
49+
* @param {number=} high The high (signed) 32 bits of the long.
4950
* @param {boolean=} unsigned Whether unsigned or not. Defaults to `false` (signed).
5051
* @constructor
5152
*/
5253
var Long = function(low, high, unsigned) {
54+
if (low && typeof low === 'object') {
55+
high = low.high;
56+
unsigned = low.unsigned;
57+
low = low.low;
58+
}
5359

5460
/**
5561
* The low 32 bits as a signed value.

dist/Long.min.js

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/Long.html

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ <h2>
4141

4242

4343
<dt>
44-
<h4 class="name" id="Long"><span class="type-signature"></span>new Long<span class="signature">(low, high, <span class="optional">unsigned</span>)</span><span class="type-signature"></span></h4>
44+
<h4 class="name" id="Long"><span class="type-signature"></span>new Long<span class="signature">(low, <span class="optional">high</span>, <span class="optional">unsigned</span>)</span><span class="type-signature"></span></h4>
4545

4646

4747
</dt>
@@ -92,6 +92,9 @@ <h5>Parameters:</h5>
9292

9393

9494
<span class="param-type">number</span>
95+
|
96+
97+
<span class="param-type">!{low: number, high: number, unsigned: boolean}</span>
9598

9699

97100

@@ -107,7 +110,7 @@ <h5>Parameters:</h5>
107110

108111

109112

110-
<td class="description last">The low (signed) 32 bits of the long.</td>
113+
<td class="description last">The low (signed) 32 bits of the long. Optionally accepts a Long-like object as the first parameter.</td>
111114
</tr>
112115

113116

@@ -129,6 +132,8 @@ <h5>Parameters:</h5>
129132

130133
<td class="attributes">
131134

135+
&lt;optional><br>
136+
132137

133138

134139
</td>

externs/Long.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@
2727
*/
2828

2929
/**
30-
* @param {number} low
31-
* @param {number} high
30+
* @param {number|!{low: number, high: number, unsigned: boolean}} low
31+
* @param {number=} high
3232
* @param {boolean=} unsigned
3333
* @constructor
3434
*/

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "long",
3-
"version": "1.1.3",
3+
"version": "1.1.4",
44
"author": "Daniel Wirtz <dcode@dcode.io>",
55
"description": "A Long class for representing a 64-bit two's-complement integer value.",
66
"main": "index.js",

src/Long.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,18 @@
4444
*
4545
* @exports Long
4646
* @class A Long class for representing a 64-bit two's-complement integer value.
47-
* @param {number} low The low (signed) 32 bits of the long.
48-
* @param {number} high The high (signed) 32 bits of the long.
47+
* @param {number|!{low: number, high: number, unsigned: boolean}} low The low (signed) 32 bits of the long.
48+
* Optionally accepts a Long-like object as the first parameter.
49+
* @param {number=} high The high (signed) 32 bits of the long.
4950
* @param {boolean=} unsigned Whether unsigned or not. Defaults to `false` (signed).
5051
* @constructor
5152
*/
5253
var Long = function(low, high, unsigned) {
54+
if (low && typeof low === 'object') {
55+
high = low.high;
56+
unsigned = low.unsigned;
57+
low = low.low;
58+
}
5359

5460
/**
5561
* The low 32 bits as a signed value.

tests/suite.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ var suite = {
2525
var longVal = new Long(0xFFFFFFFF, 0x7FFFFFFF);
2626
test.equal(longVal.toNumber(), 9223372036854775807);
2727
test.equal(longVal.toString(), "9223372036854775807");
28+
var longVal2 = new Long(longVal);
29+
test.equal(longVal2.toNumber(), 9223372036854775807);
30+
test.equal(longVal2.toString(), "9223372036854775807");
31+
test.equal(longVal2.unsigned, longVal.unsigned);
2832
test.done();
2933
},
3034

0 commit comments

Comments
 (0)