A Long class for representing a 64-bit two's-complement integer value.
Installations
npm install @xtuc/long
Score
100
Supply Chain
99.6
Quality
75.4
Maintenance
100
Vulnerability
100
License
Releases
Contributors
Developer
dcodeIO
Developer Guide
Module System
CommonJS
Min. Node Version
Typescript Support
Yes
Node Version
10.11.0
NPM Version
6.4.1
Statistics
889 Stars
125 Commits
205 Forks
23 Watching
5 Branches
16 Contributors
Updated on 29 Oct 2024
Bundle Size
9.50 kB
Minified
3.00 kB
Minified + Gzipped
Languages
JavaScript (97.26%)
WebAssembly (2.74%)
Total Downloads
Cumulative downloads
Total Downloads
3,686,942,621
Last day
-7.3%
3,736,622
Compared to previous day
Last week
1.5%
21,433,404
Compared to previous week
Last month
11.6%
88,720,264
Compared to previous month
Last year
7.9%
924,818,693
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dev Dependencies
1
long.js
A Long class for representing a 64 bit two's-complement integer value derived from the Closure Library for stand-alone use and extended with unsigned support.
Background
As of ECMA-262 5th Edition, "all the positive and negative integers whose magnitude is no greater than 253 are representable in the Number type", which is "representing the doubleprecision 64-bit format IEEE 754 values as specified in the IEEE Standard for Binary Floating-Point Arithmetic". The maximum safe integer in JavaScript is 253-1.
Example: 264-1 is 18446744073709551615 but in JavaScript it evaluates to 18446744073709552000.
Furthermore, bitwise operators in JavaScript "deal only with integers in the range −231 through 231−1, inclusive, or in the range 0 through 232−1, inclusive. These operators accept any value of the Number type but first convert each such value to one of 232 integer values."
In some use cases, however, it is required to be able to reliably work with and perform bitwise operations on the full 64 bits. This is where long.js comes into play.
Usage
The package exports an ECMAScript module with an UMD fallback.
$> npm install long
1import Long from "long"; 2 3var value = new Long(0xFFFFFFFF, 0x7FFFFFFF); 4console.log(value.toString()); 5...
Note that mixing ESM and CommonJS is not recommended as it yields different classes, albeit with the same functionality.
Usage with a CDN
- From GitHub via jsDelivr:
https://cdn.jsdelivr.net/gh/dcodeIO/long.js@TAG/index.js
(ESM) - From npm via jsDelivr:
https://cdn.jsdelivr.net/npm/long@VERSION/index.js
(ESM)
https://cdn.jsdelivr.net/npm/long@VERSION/umd/index.js
(UMD) - From npm via unpkg:
https://unpkg.com/long@VERSION/index.js
(ESM)
https://unpkg.com/long@VERSION/umd/index.js
(UMD)
Replace TAG
respectively VERSION
with a specific version or omit it (not recommended in production) to use main/latest.
API
Constructor
- new Long(low:
number
, high?:number
, unsigned?:boolean
)
Constructs a 64 bit two's-complement integer, given its low and high 32 bit values as signed integers. See the from* functions below for more convenient ways of constructing Longs.
Fields
-
Long#low:
number
The low 32 bits as a signed value. -
Long#high:
number
The high 32 bits as a signed value. -
Long#unsigned:
boolean
Whether unsigned or not.
Constants
-
Long.ZERO:
Long
Signed zero. -
Long.ONE:
Long
Signed one. -
Long.NEG_ONE:
Long
Signed negative one. -
Long.UZERO:
Long
Unsigned zero. -
Long.UONE:
Long
Unsigned one. -
Long.MAX_VALUE:
Long
Maximum signed value. -
Long.MIN_VALUE:
Long
Minimum signed value. -
Long.MAX_UNSIGNED_VALUE:
Long
Maximum unsigned value.
Utility
-
Long.isLong(obj:
*
):boolean
Tests if the specified object is a Long. -
Long.fromBits(lowBits:
number
, highBits:number
, unsigned?:boolean
):Long
Returns a Long representing the 64 bit integer that comes by concatenating the given low and high bits. Each is assumed to use 32 bits. -
Long.fromBytes(bytes:
number[]
, unsigned?:boolean
, le?:boolean
):Long
Creates a Long from its byte representation. -
Long.fromBytesLE(bytes:
number[]
, unsigned?:boolean
):Long
Creates a Long from its little endian byte representation. -
Long.fromBytesBE(bytes:
number[]
, unsigned?:boolean
):Long
Creates a Long from its big endian byte representation. -
Long.fromInt(value:
number
, unsigned?:boolean
):Long
Returns a Long representing the given 32 bit integer value. -
Long.fromNumber(value:
number
, unsigned?:boolean
):Long
Returns a Long representing the given value, provided that it is a finite number. Otherwise, zero is returned. -
Long.fromString(str:
string
, unsigned?:boolean
, radix?:number
)
Long.fromString(str:string
, radix:number
)
Returns a Long representation of the given string, written using the specified radix. -
Long.fromValue(val:
*
, unsigned?:boolean
):Long
Converts the specified value to a Long using the appropriate from* function for its type.
Methods
-
Long#add(addend:
Long | number | string
):Long
Returns the sum of this and the specified Long. -
Long#and(other:
Long | number | string
):Long
Returns the bitwise AND of this Long and the specified. -
Long#compare/comp(other:
Long | number | string
):number
Compares this Long's value with the specified's. Returns0
if they are the same,1
if the this is greater and-1
if the given one is greater. -
Long#divide/div(divisor:
Long | number | string
):Long
Returns this Long divided by the specified. -
Long#equals/eq(other:
Long | number | string
):boolean
Tests if this Long's value equals the specified's. -
Long#getHighBits():
number
Gets the high 32 bits as a signed integer. -
Long#getHighBitsUnsigned():
number
Gets the high 32 bits as an unsigned integer. -
Long#getLowBits():
number
Gets the low 32 bits as a signed integer. -
Long#getLowBitsUnsigned():
number
Gets the low 32 bits as an unsigned integer. -
Long#getNumBitsAbs():
number
Gets the number of bits needed to represent the absolute value of this Long. -
Long#greaterThan/gt(other:
Long | number | string
):boolean
Tests if this Long's value is greater than the specified's. -
Long#greaterThanOrEqual/gte/ge(other:
Long | number | string
):boolean
Tests if this Long's value is greater than or equal the specified's. -
Long#isEven():
boolean
Tests if this Long's value is even. -
Long#isNegative():
boolean
Tests if this Long's value is negative. -
Long#isOdd():
boolean
Tests if this Long's value is odd. -
Long#isPositive():
boolean
Tests if this Long's value is positive or zero. -
Long#isZero/eqz():
boolean
Tests if this Long's value equals zero. -
Long#lessThan/lt(other:
Long | number | string
):boolean
Tests if this Long's value is less than the specified's. -
Long#lessThanOrEqual/lte/le(other:
Long | number | string
):boolean
Tests if this Long's value is less than or equal the specified's. -
Long#modulo/mod/rem(divisor:
Long | number | string
):Long
Returns this Long modulo the specified. -
Long#multiply/mul(multiplier:
Long | number | string
):Long
Returns the product of this and the specified Long. -
Long#negate/neg():
Long
Negates this Long's value. -
Long#not():
Long
Returns the bitwise NOT of this Long. -
Long#countLeadingZeros/clz():
number
Returns count leading zeros of this Long. -
Long#countTrailingZeros/ctz():
number
Returns count trailing zeros of this Long. -
Long#notEquals/neq/ne(other:
Long | number | string
):boolean
Tests if this Long's value differs from the specified's. -
Long#or(other:
Long | number | string
):Long
Returns the bitwise OR of this Long and the specified. -
Long#shiftLeft/shl(numBits:
Long | number | string
):Long
Returns this Long with bits shifted to the left by the given amount. -
Long#shiftRight/shr(numBits:
Long | number | string
):Long
Returns this Long with bits arithmetically shifted to the right by the given amount. -
Long#shiftRightUnsigned/shru/shr_u(numBits:
Long | number | string
):Long
Returns this Long with bits logically shifted to the right by the given amount. -
Long#rotateLeft/rotl(numBits:
Long | number | string
):Long
Returns this Long with bits rotated to the left by the given amount. -
Long#rotateRight/rotr(numBits:
Long | number | string
):Long
Returns this Long with bits rotated to the right by the given amount. -
Long#subtract/sub(subtrahend:
Long | number | string
):Long
Returns the difference of this and the specified Long. -
Long#toBytes(le?:
boolean
):number[]
Converts this Long to its byte representation. -
Long#toBytesLE():
number[]
Converts this Long to its little endian byte representation. -
Long#toBytesBE():
number[]
Converts this Long to its big endian byte representation. -
Long#toInt():
number
Converts the Long to a 32 bit integer, assuming it is a 32 bit integer. -
Long#toNumber():
number
Converts the Long to a the nearest floating-point representation of this value (double, 53 bit mantissa). -
Long#toSigned():
Long
Converts this Long to signed. -
Long#toString(radix?:
number
):string
Converts the Long to a string written in the specified radix. -
Long#toUnsigned():
Long
Converts this Long to unsigned. -
Long#xor(other:
Long | number | string
):Long
Returns the bitwise XOR of this Long and the given one.
WebAssembly support
WebAssembly supports 64-bit integer arithmetic out of the box, hence a tiny WebAssembly module is used to compute operations like multiplication, division and remainder more efficiently (slow operations like division are around twice as fast), falling back to floating point based computations in JavaScript where WebAssembly is not yet supported, e.g., in older versions of node.
Building
Building the UMD fallback:
$> npm run build
Running the tests:
$> npm test
No vulnerabilities found.
Reason
no vulnerabilities detected
Reason
license file detected
Details
- Info: : LICENSE:1
Reason
no binaries found in the repo
Reason
no dangerous workflow patterns detected
Reason
dependency not pinned by hash detected -- score normalized to 7
Details
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/publish.yml:12: update your workflow using https://app.stepsecurity.io/secureworkflow/os33/wireguard-go/publish.yml/master?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/publish.yml:15: update your workflow using https://app.stepsecurity.io/secureworkflow/os33/wireguard-go/publish.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/test.yml:16: update your workflow using https://app.stepsecurity.io/secureworkflow/os33/wireguard-go/test.yml/master?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/test.yml:17: update your workflow using https://app.stepsecurity.io/secureworkflow/os33/wireguard-go/test.yml/master?enable=pin
- Info: Dockerfile dependencies are pinned
- Info: no insecure (not pinned by hash) dependency downloads found in Dockerfiles
- Info: no insecure (not pinned by hash) dependency downloads found in shell scripts
Reason
GitHub code reviews found for 11 commits out of the last 30 -- score normalized to 3
Details
- Warn: no reviews found for commit: 93b06bdab3127d0fb00aa65a38ddde8ca79f33ca
- Warn: no reviews found for commit: d30e39c6e84df26d679f3c350c63029ae31a25c1
- Warn: no reviews found for commit: 9d90c0b23d9d94d24522ecf55b4395accecbd5a1
- Warn: no reviews found for commit: fca1fa0d254dac8ea147c56bd7f29ca9d772e285
- Warn: no reviews found for commit: d90122076a0eb6ec399f06039bb79c09fc4d829e
- Warn: no reviews found for commit: bf6854993a73e94e7885b4f6022e57f238666591
- Warn: no reviews found for commit: 19ac17bf2246250b9e0c7dae521ae0ac9d4fd43b
- Warn: no reviews found for commit: bd8e61406722df73739b727b8e5bd200e6866e23
- Warn: no reviews found for commit: 86410397c36ad64aa6c9a45293bff918ce0ea14d
- Warn: no reviews found for commit: ee79b89c24a6642a26d9695a26f9afaa3936219b
- Warn: no reviews found for commit: ce11b4b2bd3ba1240a057d62018563d99db318f9
- Warn: no reviews found for commit: 9dec5155003c0c892b33ff11e413158bafc5a8d6
- Warn: no reviews found for commit: eb03ce2723f2c9265ed914a222adaa0793df1c5c
- Warn: no reviews found for commit: 941c5c62471168b5d18153755c2a7b38d2560e58
- Warn: no reviews found for commit: e1e538f11d7af6bae84846b45770fcbe556f037d
- Warn: no reviews found for commit: 8a40a41fbaf49b04084f1af0bdd76f9e635cab1a
- Warn: no reviews found for commit: bc96a70758cf1e431af6956e56d70dd181943162
- Warn: no reviews found for commit: b38c49ede9ff50a8a4096dbb272c1fe18508c036
- Warn: no reviews found for commit: 2065b8e98b5b6b5aceccdf217e0d6a10b1e82668
Reason
0 commit(s) out of 30 and 0 issue activity out of 30 found in the last 90 days -- score normalized to 0
Reason
no badge detected
Reason
non read-only tokens detected in GitHub workflows
Details
- Warn: no topLevel permission defined: .github/workflows/publish.yml:1: update your workflow using https://app.stepsecurity.io/secureworkflow/os33/wireguard-go/publish.yml/master?enable=permissions
- Warn: no topLevel permission defined: .github/workflows/test.yml:1: update your workflow using https://app.stepsecurity.io/secureworkflow/os33/wireguard-go/test.yml/master?enable=permissions
Reason
security policy file not detected
Reason
no update tool detected
Details
- Warn: dependabot config file not detected in source location. We recommend setting this configuration in code so it can be easily verified by others.
- Warn: renovatebot config file not detected in source location. We recommend setting this configuration in code so it can be easily verified by others.
Reason
project is not fuzzed
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'main'
Score
4
/10
Last Scanned on 2022-08-15
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn MoreOther packages similar to @xtuc/long
@webassemblyjs/floating-point-hex-parser
A function to parse floating point hexadecimal strings as defined by the WebAssembly specification
@types/charcodes
TypeScript definitions for charcodes
long
A Long class for representing a 64-bit two's-complement integer value.
stack-trace
Get v8 stack traces as an array of CallSite objects.