From 785a5d4b73f2eba133fc2fbf67fd101db6d45ea3 Mon Sep 17 00:00:00 2001 From: dcode Date: Thu, 13 Feb 2025 10:46:40 +0100 Subject: [PATCH] Format with prettier --- .github/workflows/publish.yml | 48 +- .github/workflows/test.yml | 34 +- .prettierrc.js | 1 + README.md | 174 ++++---- index.d.ts | 2 +- index.js | 562 ++++++++++++------------ package-lock.json | 23 + package.json | 3 + tests/goog/base.js | 26 +- tests/goog/long_test.js | 227 ++++++---- tests/index.js | 119 +++-- tests/typescript/test-import.ts | 2 +- tests/typescript/tsconfig.base.json | 2 +- tests/typescript/tsconfig.esnext.json | 2 +- tests/typescript/tsconfig.nodenext.json | 2 +- types.d.ts | 9 +- umd/index.d.ts | 2 +- 17 files changed, 678 insertions(+), 560 deletions(-) create mode 100644 .prettierrc.js diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index ab8e05d..4b9f498 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -7,27 +7,27 @@ jobs: if: github.repository == 'dcodeIO/long.js' runs-on: ubuntu-latest steps: - - uses: actions/checkout@v1 - with: - ref: main - - uses: dcodeIO/setup-node-nvm@master - with: - node-version: current - - name: Install dependencies - run: npm ci - - name: Build - run: npm run build - - name: Run tests - run: npm test - - name: Publish - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - NPM_TOKEN: ${{ secrets.NPM_TOKEN }} - run: | - VERSION=$(npx aspublish --version) - if [ -z "$VERSION" ]; then - echo "Changes do not trigger a release" - else - echo "Publishing new version: $VERSION" - npx aspublish - fi + - uses: actions/checkout@v1 + with: + ref: main + - uses: dcodeIO/setup-node-nvm@master + with: + node-version: current + - name: Install dependencies + run: npm ci + - name: Build + run: npm run build + - name: Run tests + run: npm test + - name: Publish + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + NPM_TOKEN: ${{ secrets.NPM_TOKEN }} + run: | + VERSION=$(npx aspublish --version) + if [ -z "$VERSION" ]; then + echo "Changes do not trigger a release" + else + echo "Publishing new version: $VERSION" + npx aspublish + fi diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index f206482..7ce9a1f 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -2,24 +2,34 @@ name: Test on: push: branches: - - main + - main pull_request: workflow_dispatch: jobs: + lint: + name: Check formatting + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-node@v4 + - name: Install dependencies + run: npm ci --no-audit + - name: Lint + run: npm run lint test: - name: Test + name: Build and test runs-on: ubuntu-latest strategy: matrix: node_version: ["current", "lts_latest"] steps: - - uses: actions/checkout@v1.0.0 - - uses: dcodeIO/setup-node-nvm@master - with: - node-version: ${{ matrix.node_version }} - - name: Install dependencies - run: npm ci --no-audit - - name: Build - run: npm run build - - name: Run tests - run: npm test + - uses: actions/checkout@v1.0.0 + - uses: dcodeIO/setup-node-nvm@master + with: + node-version: ${{ matrix.node_version }} + - name: Install dependencies + run: npm ci --no-audit + - name: Build + run: npm run build + - name: Run tests + run: npm test diff --git a/.prettierrc.js b/.prettierrc.js new file mode 100644 index 0000000..ff8b4c5 --- /dev/null +++ b/.prettierrc.js @@ -0,0 +1 @@ +export default {}; diff --git a/README.md b/README.md index 6f551c9..ca4b2f8 100644 --- a/README.md +++ b/README.md @@ -1,13 +1,11 @@ -long.js -======= +# long.js A Long class for representing a 64 bit two's-complement integer value derived from the [Closure Library](https://github.com/google/closure-library) for stand-alone use and extended with unsigned support. [![Build Status](https://img.shields.io/github/actions/workflow/status/dcodeIO/long.js/test.yml?branch=main&label=test&logo=github)](https://github.com/dcodeIO/long.js/actions/workflows/test.yml) [![Publish Status](https://img.shields.io/github/actions/workflow/status/dcodeIO/long.js/publish.yml?branch=main&label=publish&logo=github)](https://github.com/dcodeIO/long.js/actions/workflows/publish.yml) [![npm](https://img.shields.io/npm/v/long.svg?label=npm&color=007acc&logo=npm)](https://www.npmjs.com/package/long) -Background ----------- +## Background As of [ECMA-262 5th Edition](http://ecma262-5.com/ELS5_HTML.htm#Section_8.5), "all the positive and negative integers whose magnitude is no greater than 253 are representable in the Number type", which is "representing the @@ -24,8 +22,7 @@ the Number type but first convert each such value to one of 232 integ 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 ------ +## Usage The package exports an ECMAScript module with an UMD fallback. @@ -45,239 +42,236 @@ Note that mixing ESM and CommonJS is not recommended as it yields different clas ### Usage with a CDN - * From GitHub via [jsDelivr](https://www.jsdelivr.com):
- `https://cdn.jsdelivr.net/gh/dcodeIO/long.js@TAG/index.js` (ESM) - * From npm via [jsDelivr](https://www.jsdelivr.com):
- `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):
- `https://unpkg.com/long@VERSION/index.js` (ESM)
- `https://unpkg.com/long@VERSION/umd/index.js` (UMD) +- From GitHub via [jsDelivr](https://www.jsdelivr.com):
+ `https://cdn.jsdelivr.net/gh/dcodeIO/long.js@TAG/index.js` (ESM) +- From npm via [jsDelivr](https://www.jsdelivr.com):
+ `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):
+ `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](https://github.com/dcodeIO/long.js/releases) or omit it (not recommended in production) to use main/latest. +Replace `TAG` respectively `VERSION` with a [specific version](https://github.com/dcodeIO/long.js/releases) or omit it (not recommended in production) to use main/latest. -API ---- +## 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. +- 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`
+- Long#**low**: `number`
The low 32 bits as a signed value. -* Long#**high**: `number`
+- Long#**high**: `number`
The high 32 bits as a signed value. -* Long#**unsigned**: `boolean`
+- Long#**unsigned**: `boolean`
Whether unsigned or not. ### Constants -* Long.**ZERO**: `Long`
+- Long.**ZERO**: `Long`
Signed zero. -* Long.**ONE**: `Long`
+- Long.**ONE**: `Long`
Signed one. -* Long.**NEG_ONE**: `Long`
+- Long.**NEG_ONE**: `Long`
Signed negative one. -* Long.**UZERO**: `Long`
+- Long.**UZERO**: `Long`
Unsigned zero. -* Long.**UONE**: `Long`
+- Long.**UONE**: `Long`
Unsigned one. -* Long.**MAX_VALUE**: `Long`
+- Long.**MAX_VALUE**: `Long`
Maximum signed value. -* Long.**MIN_VALUE**: `Long`
+- Long.**MIN_VALUE**: `Long`
Minimum signed value. -* Long.**MAX_UNSIGNED_VALUE**: `Long`
+- Long.**MAX_UNSIGNED_VALUE**: `Long`
Maximum unsigned value. ### Utility -* type **LongLike**: `Long | number | bigint | string`
+- type **LongLike**: `Long | number | bigint | string`
Any value or object that either is or can be converted to a Long. -* Long.**isLong**(obj: `any`): `boolean`
+- Long.**isLong**(obj: `any`): `boolean`
Tests if the specified object is a Long. -* Long.**fromBits**(lowBits: `number`, highBits: `number`, unsigned?: `boolean`): `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`
+- Long.**fromBytes**(bytes: `number[]`, unsigned?: `boolean`, le?: `boolean`): `Long`
Creates a Long from its byte representation. -* Long.**fromBytesLE**(bytes: `number[]`, unsigned?: `boolean`): `Long`
+- Long.**fromBytesLE**(bytes: `number[]`, unsigned?: `boolean`): `Long`
Creates a Long from its little endian byte representation. -* Long.**fromBytesBE**(bytes: `number[]`, unsigned?: `boolean`): `Long`
+- Long.**fromBytesBE**(bytes: `number[]`, unsigned?: `boolean`): `Long`
Creates a Long from its big endian byte representation. -* Long.**fromInt**(value: `number`, unsigned?: `boolean`): `Long`
+- Long.**fromInt**(value: `number`, unsigned?: `boolean`): `Long`
Returns a Long representing the given 32 bit integer value. -* Long.**fromNumber**(value: `number`, unsigned?: `boolean`): `Long`
+- 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.**fromBigInt**(value: `bigint`, unsigned?: `boolean`): `Long`
+- Long.**fromBigInt**(value: `bigint`, unsigned?: `boolean`): `Long`
Returns a Long representing the given big integer. -* Long.**fromString**(str: `string`, unsigned?: `boolean`, radix?: `number`)
+- 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: `LongLike`, unsigned?: `boolean`): `Long`
- Converts the specified value to a Long using the appropriate from* function for its type. +- Long.**fromValue**(val: `LongLike`, unsigned?: `boolean`): `Long`
+ Converts the specified value to a Long using the appropriate from\* function for its type. ### Methods -* Long#**add**(addend: `LongLike`): `Long`
+- Long#**add**(addend: `LongLike`): `Long`
Returns the sum of this and the specified Long. -* Long#**and**(other: `LongLike`): `Long`
+- Long#**and**(other: `LongLike`): `Long`
Returns the bitwise AND of this Long and the specified. -* Long#**compare**/**comp**(other: `LongLike`): `number`
+- Long#**compare**/**comp**(other: `LongLike`): `number`
Compares this Long's value with the specified's. Returns `0` if they are the same, `1` if the this is greater and `-1` if the given one is greater. -* Long#**divide**/**div**(divisor: `LongLike`): `Long`
+- Long#**divide**/**div**(divisor: `LongLike`): `Long`
Returns this Long divided by the specified. -* Long#**equals**/**eq**(other: `LongLike`): `boolean`
+- Long#**equals**/**eq**(other: `LongLike`): `boolean`
Tests if this Long's value equals the specified's. -* Long#**getHighBits**(): `number`
+- Long#**getHighBits**(): `number`
Gets the high 32 bits as a signed integer. -* Long#**getHighBitsUnsigned**(): `number`
+- Long#**getHighBitsUnsigned**(): `number`
Gets the high 32 bits as an unsigned integer. -* Long#**getLowBits**(): `number`
+- Long#**getLowBits**(): `number`
Gets the low 32 bits as a signed integer. -* Long#**getLowBitsUnsigned**(): `number`
+- Long#**getLowBitsUnsigned**(): `number`
Gets the low 32 bits as an unsigned integer. -* Long#**getNumBitsAbs**(): `number`
+- Long#**getNumBitsAbs**(): `number`
Gets the number of bits needed to represent the absolute value of this Long. -* Long#**greaterThan**/**gt**(other: `LongLike`): `boolean`
+- Long#**greaterThan**/**gt**(other: `LongLike`): `boolean`
Tests if this Long's value is greater than the specified's. -* Long#**greaterThanOrEqual**/**gte**/**ge**(other: `LongLike`): `boolean`
+- Long#**greaterThanOrEqual**/**gte**/**ge**(other: `LongLike`): `boolean`
Tests if this Long's value is greater than or equal the specified's. -* Long#**isEven**(): `boolean`
+- Long#**isEven**(): `boolean`
Tests if this Long's value is even. -* Long#**isNegative**(): `boolean`
+- Long#**isNegative**(): `boolean`
Tests if this Long's value is negative. -* Long#**isOdd**(): `boolean`
+- Long#**isOdd**(): `boolean`
Tests if this Long's value is odd. -* Long#**isPositive**(): `boolean`
+- Long#**isPositive**(): `boolean`
Tests if this Long's value is positive or zero. -* Long#**isSafeInteger**(): `boolean`
+- Long#**isSafeInteger**(): `boolean`
Tests if this Long can be safely represented as a JavaScript number. -* Long#**isZero**/**eqz**(): `boolean`
+- Long#**isZero**/**eqz**(): `boolean`
Tests if this Long's value equals zero. -* Long#**lessThan**/**lt**(other: `LongLike`): `boolean`
+- Long#**lessThan**/**lt**(other: `LongLike`): `boolean`
Tests if this Long's value is less than the specified's. -* Long#**lessThanOrEqual**/**lte**/**le**(other: `LongLike`): `boolean`
+- Long#**lessThanOrEqual**/**lte**/**le**(other: `LongLike`): `boolean`
Tests if this Long's value is less than or equal the specified's. -* Long#**modulo**/**mod**/**rem**(divisor: `LongLike`): `Long`
+- Long#**modulo**/**mod**/**rem**(divisor: `LongLike`): `Long`
Returns this Long modulo the specified. -* Long#**multiply**/**mul**(multiplier: `LongLike`): `Long`
+- Long#**multiply**/**mul**(multiplier: `LongLike`): `Long`
Returns the product of this and the specified Long. -* Long#**negate**/**neg**(): `Long`
+- Long#**negate**/**neg**(): `Long`
Negates this Long's value. -* Long#**not**(): `Long`
+- Long#**not**(): `Long`
Returns the bitwise NOT of this Long. -* Long#**countLeadingZeros**/**clz**(): `number`
+- Long#**countLeadingZeros**/**clz**(): `number`
Returns count leading zeros of this Long. -* Long#**countTrailingZeros**/**ctz**(): `number`
+- Long#**countTrailingZeros**/**ctz**(): `number`
Returns count trailing zeros of this Long. -* Long#**notEquals**/**neq**/**ne**(other: `LongLike`): `boolean`
+- Long#**notEquals**/**neq**/**ne**(other: `LongLike`): `boolean`
Tests if this Long's value differs from the specified's. -* Long#**or**(other: `LongLike`): `Long`
+- Long#**or**(other: `LongLike`): `Long`
Returns the bitwise OR of this Long and the specified. -* Long#**shiftLeft**/**shl**(numBits: `Long | number`): `Long`
+- Long#**shiftLeft**/**shl**(numBits: `Long | number`): `Long`
Returns this Long with bits shifted to the left by the given amount. -* Long#**shiftRight**/**shr**(numBits: `Long | number`): `Long`
+- Long#**shiftRight**/**shr**(numBits: `Long | number`): `Long`
Returns this Long with bits arithmetically shifted to the right by the given amount. -* Long#**shiftRightUnsigned**/**shru**/**shr_u**(numBits: `Long | number`): `Long`
+- Long#**shiftRightUnsigned**/**shru**/**shr_u**(numBits: `Long | number`): `Long`
Returns this Long with bits logically shifted to the right by the given amount. -* Long#**rotateLeft**/**rotl**(numBits: `Long | number`): `Long`
+- Long#**rotateLeft**/**rotl**(numBits: `Long | number`): `Long`
Returns this Long with bits rotated to the left by the given amount. -* Long#**rotateRight**/**rotr**(numBits: `Long | number`): `Long`
+- Long#**rotateRight**/**rotr**(numBits: `Long | number`): `Long`
Returns this Long with bits rotated to the right by the given amount. -* Long#**subtract**/**sub**(subtrahend: `LongLike`): `Long`
+- Long#**subtract**/**sub**(subtrahend: `LongLike`): `Long`
Returns the difference of this and the specified Long. -* Long#**toBytes**(le?: `boolean`): `number[]`
+- Long#**toBytes**(le?: `boolean`): `number[]`
Converts this Long to its byte representation. -* Long#**toBytesLE**(): `number[]`
+- Long#**toBytesLE**(): `number[]`
Converts this Long to its little endian byte representation. -* Long#**toBytesBE**(): `number[]`
+- Long#**toBytesBE**(): `number[]`
Converts this Long to its big endian byte representation. -* Long#**toInt**(): `number`
+- Long#**toInt**(): `number`
Converts the Long to a 32 bit integer, assuming it is a 32 bit integer. -* Long#**toNumber**(): `number`
+- Long#**toNumber**(): `number`
Converts the Long to a the nearest floating-point representation of this value (double, 53 bit mantissa). -* Long#**toBigInt**(): `bigint`
+- Long#**toBigInt**(): `bigint`
Converts the Long to its big integer representation. -* Long#**toSigned**(): `Long`
+- Long#**toSigned**(): `Long`
Converts this Long to signed. -* Long#**toString**(radix?: `number`): `string`
+- Long#**toString**(radix?: `number`): `string`
Converts the Long to a string written in the specified radix. -* Long#**toUnsigned**(): `Long`
+- Long#**toUnsigned**(): `Long`
Converts this Long to unsigned. -* Long#**xor**(other: `Long | number | string`): `Long`
+- Long#**xor**(other: `Long | number | string`): `Long`
Returns the bitwise XOR of this Long and the given one. -WebAssembly support -------------------- +## WebAssembly support [WebAssembly](http://webassembly.org) supports 64-bit integer arithmetic out of the box, hence a [tiny WebAssembly module](./wasm.wat) 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 Building the UMD fallback: diff --git a/index.d.ts b/index.d.ts index d16583c..7d4b017 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,2 +1,2 @@ -import { Long } from './types.js'; +import { Long } from "./types.js"; export default Long; diff --git a/index.js b/index.js index ca2615d..9a0fe70 100644 --- a/index.js +++ b/index.js @@ -21,69 +21,84 @@ // WebAssembly optimizations to do native i64 multiplication and divide var wasm = null; try { - wasm = new WebAssembly.Instance(new WebAssembly.Module(new Uint8Array([ - // \0asm - 0, 97, 115, 109, - // version 1 - 1, 0, 0, 0, - - // section "type" - 1, 13, 2, - // 0, () => i32 - 96, 0, 1, 127, - // 1, (i32, i32, i32, i32) => i32 - 96, 4, 127, 127, 127, 127, 1, 127, - - // section "function" - 3, 7, 6, - // 0, type 0 - 0, - // 1, type 1 - 1, - // 2, type 1 - 1, - // 3, type 1 - 1, - // 4, type 1 - 1, - // 5, type 1 - 1, - - // section "global" - 6, 6, 1, - // 0, "high", mutable i32 - 127, 1, 65, 0, 11, - - // section "export" - 7, 50, 6, - // 0, "mul" - 3, 109, 117, 108, 0, 1, - // 1, "div_s" - 5, 100, 105, 118, 95, 115, 0, 2, - // 2, "div_u" - 5, 100, 105, 118, 95, 117, 0, 3, - // 3, "rem_s" - 5, 114, 101, 109, 95, 115, 0, 4, - // 4, "rem_u" - 5, 114, 101, 109, 95, 117, 0, 5, - // 5, "get_high" - 8, 103, 101, 116, 95, 104, 105, 103, 104, 0, 0, - - // section "code" - 10, 191, 1, 6, - // 0, "get_high" - 4, 0, 35, 0, 11, - // 1, "mul" - 36, 1, 1, 126, 32, 0, 173, 32, 1, 173, 66, 32, 134, 132, 32, 2, 173, 32, 3, 173, 66, 32, 134, 132, 126, 34, 4, 66, 32, 135, 167, 36, 0, 32, 4, 167, 11, - // 2, "div_s" - 36, 1, 1, 126, 32, 0, 173, 32, 1, 173, 66, 32, 134, 132, 32, 2, 173, 32, 3, 173, 66, 32, 134, 132, 127, 34, 4, 66, 32, 135, 167, 36, 0, 32, 4, 167, 11, - // 3, "div_u" - 36, 1, 1, 126, 32, 0, 173, 32, 1, 173, 66, 32, 134, 132, 32, 2, 173, 32, 3, 173, 66, 32, 134, 132, 128, 34, 4, 66, 32, 135, 167, 36, 0, 32, 4, 167, 11, - // 4, "rem_s" - 36, 1, 1, 126, 32, 0, 173, 32, 1, 173, 66, 32, 134, 132, 32, 2, 173, 32, 3, 173, 66, 32, 134, 132, 129, 34, 4, 66, 32, 135, 167, 36, 0, 32, 4, 167, 11, - // 5, "rem_u" - 36, 1, 1, 126, 32, 0, 173, 32, 1, 173, 66, 32, 134, 132, 32, 2, 173, 32, 3, 173, 66, 32, 134, 132, 130, 34, 4, 66, 32, 135, 167, 36, 0, 32, 4, 167, 11 - ])), {}).exports; + wasm = new WebAssembly.Instance( + new WebAssembly.Module( + new Uint8Array([ + // \0asm + 0, 97, 115, 109, + // version 1 + 1, 0, 0, 0, + + // section "type" + 1, 13, 2, + // 0, () => i32 + 96, 0, 1, 127, + // 1, (i32, i32, i32, i32) => i32 + 96, 4, 127, 127, 127, 127, 1, 127, + + // section "function" + 3, 7, 6, + // 0, type 0 + 0, + // 1, type 1 + 1, + // 2, type 1 + 1, + // 3, type 1 + 1, + // 4, type 1 + 1, + // 5, type 1 + 1, + + // section "global" + 6, 6, 1, + // 0, "high", mutable i32 + 127, 1, 65, 0, 11, + + // section "export" + 7, 50, 6, + // 0, "mul" + 3, 109, 117, 108, 0, 1, + // 1, "div_s" + 5, 100, 105, 118, 95, 115, 0, 2, + // 2, "div_u" + 5, 100, 105, 118, 95, 117, 0, 3, + // 3, "rem_s" + 5, 114, 101, 109, 95, 115, 0, 4, + // 4, "rem_u" + 5, 114, 101, 109, 95, 117, 0, 5, + // 5, "get_high" + 8, 103, 101, 116, 95, 104, 105, 103, 104, 0, 0, + + // section "code" + 10, 191, 1, 6, + // 0, "get_high" + 4, 0, 35, 0, 11, + // 1, "mul" + 36, 1, 1, 126, 32, 0, 173, 32, 1, 173, 66, 32, 134, 132, 32, 2, 173, 32, + 3, 173, 66, 32, 134, 132, 126, 34, 4, 66, 32, 135, 167, 36, 0, 32, 4, + 167, 11, + // 2, "div_s" + 36, 1, 1, 126, 32, 0, 173, 32, 1, 173, 66, 32, 134, 132, 32, 2, 173, 32, + 3, 173, 66, 32, 134, 132, 127, 34, 4, 66, 32, 135, 167, 36, 0, 32, 4, + 167, 11, + // 3, "div_u" + 36, 1, 1, 126, 32, 0, 173, 32, 1, 173, 66, 32, 134, 132, 32, 2, 173, 32, + 3, 173, 66, 32, 134, 132, 128, 34, 4, 66, 32, 135, 167, 36, 0, 32, 4, + 167, 11, + // 4, "rem_s" + 36, 1, 1, 126, 32, 0, 173, 32, 1, 173, 66, 32, 134, 132, 32, 2, 173, 32, + 3, 173, 66, 32, 134, 132, 129, 34, 4, 66, 32, 135, 167, 36, 0, 32, 4, + 167, 11, + // 5, "rem_u" + 36, 1, 1, 126, 32, 0, 173, 32, 1, 173, 66, 32, 134, 132, 32, 2, 173, 32, + 3, 173, 66, 32, 134, 132, 130, 34, 4, 66, 32, 135, 167, 36, 0, 32, 4, + 167, 11, + ]), + ), + {}, + ).exports; } catch { // no wasm support :( } @@ -99,7 +114,6 @@ try { * @constructor */ function Long(low, high, unsigned) { - /** * The low 32 bits as a signed value. * @type {number} @@ -199,25 +213,21 @@ function fromInt(value, unsigned) { var obj, cachedObj, cache; if (unsigned) { value >>>= 0; - if (cache = (0 <= value && value < 256)) { + if ((cache = 0 <= value && value < 256)) { cachedObj = UINT_CACHE[value]; - if (cachedObj) - return cachedObj; + if (cachedObj) return cachedObj; } obj = fromBits(value, 0, true); - if (cache) - UINT_CACHE[value] = obj; + if (cache) UINT_CACHE[value] = obj; return obj; } else { value |= 0; - if (cache = (-128 <= value && value < 128)) { + if ((cache = -128 <= value && value < 128)) { cachedObj = INT_CACHE[value]; - if (cachedObj) - return cachedObj; + if (cachedObj) return cachedObj; } obj = fromBits(value, value < 0 ? -1 : 0, false); - if (cache) - INT_CACHE[value] = obj; + if (cache) INT_CACHE[value] = obj; return obj; } } @@ -238,22 +248,20 @@ Long.fromInt = fromInt; * @inner */ function fromNumber(value, unsigned) { - if (isNaN(value)) - return unsigned ? UZERO : ZERO; + if (isNaN(value)) return unsigned ? UZERO : ZERO; if (unsigned) { - if (value < 0) - return UZERO; - if (value >= TWO_PWR_64_DBL) - return MAX_UNSIGNED_VALUE; + if (value < 0) return UZERO; + if (value >= TWO_PWR_64_DBL) return MAX_UNSIGNED_VALUE; } else { - if (value <= -TWO_PWR_63_DBL) - return MIN_VALUE; - if (value + 1 >= TWO_PWR_63_DBL) - return MAX_VALUE; + if (value <= -TWO_PWR_63_DBL) return MIN_VALUE; + if (value + 1 >= TWO_PWR_63_DBL) return MAX_VALUE; } - if (value < 0) - return fromNumber(-value, unsigned).neg(); - return fromBits((value % TWO_PWR_32_DBL) | 0, (value / TWO_PWR_32_DBL) | 0, unsigned); + if (value < 0) return fromNumber(-value, unsigned).neg(); + return fromBits( + value % TWO_PWR_32_DBL | 0, + (value / TWO_PWR_32_DBL) | 0, + unsigned, + ); } /** @@ -304,24 +312,26 @@ var pow_dbl = Math.pow; // Used 4 times (4*8 to 15+4) * @inner */ function fromString(str, unsigned, radix) { - if (str.length === 0) - throw Error('empty string'); - if (typeof unsigned === 'number') { + if (str.length === 0) throw Error("empty string"); + if (typeof unsigned === "number") { // For goog.math.long compatibility radix = unsigned; unsigned = false; } else { unsigned = !!unsigned; } - if (str === "NaN" || str === "Infinity" || str === "+Infinity" || str === "-Infinity") + if ( + str === "NaN" || + str === "Infinity" || + str === "+Infinity" || + str === "-Infinity" + ) return unsigned ? UZERO : ZERO; radix = radix || 10; - if (radix < 2 || 36 < radix) - throw RangeError('radix'); + if (radix < 2 || 36 < radix) throw RangeError("radix"); var p; - if ((p = str.indexOf('-')) > 0) - throw Error('interior hyphen'); + if ((p = str.indexOf("-")) > 0) throw Error("interior hyphen"); else if (p === 0) { return fromString(str.substring(1), unsigned, radix).neg(); } @@ -364,12 +374,14 @@ Long.fromString = fromString; * @inner */ function fromValue(val, unsigned) { - if (typeof val === 'number') - return fromNumber(val, unsigned); - if (typeof val === 'string') - return fromString(val, unsigned); + if (typeof val === "number") return fromNumber(val, unsigned); + if (typeof val === "string") return fromString(val, unsigned); // Throws for non-objects, converts non-instanceof Long: - return fromBits(val.low, val.high, typeof unsigned === 'boolean' ? unsigned : val.unsigned); + return fromBits( + val.low, + val.high, + typeof unsigned === "boolean" ? unsigned : val.unsigned, + ); } /** @@ -490,7 +502,7 @@ Long.NEG_ONE = NEG_ONE; * @type {!Long} * @inner */ -var MAX_VALUE = fromBits(0xFFFFFFFF | 0, 0x7FFFFFFF | 0, false); +var MAX_VALUE = fromBits(0xffffffff | 0, 0x7fffffff | 0, false); /** * Maximum signed value. @@ -502,7 +514,7 @@ Long.MAX_VALUE = MAX_VALUE; * @type {!Long} * @inner */ -var MAX_UNSIGNED_VALUE = fromBits(0xFFFFFFFF | 0, 0xFFFFFFFF | 0, true); +var MAX_UNSIGNED_VALUE = fromBits(0xffffffff | 0, 0xffffffff | 0, true); /** * Maximum unsigned value. @@ -544,7 +556,7 @@ LongPrototype.toInt = function toInt() { */ LongPrototype.toNumber = function toNumber() { if (this.unsigned) - return ((this.high >>> 0) * TWO_PWR_32_DBL) + (this.low >>> 0); + return (this.high >>> 0) * TWO_PWR_32_DBL + (this.low >>> 0); return this.high * TWO_PWR_32_DBL + (this.low >>> 0); }; @@ -558,11 +570,10 @@ LongPrototype.toNumber = function toNumber() { */ LongPrototype.toString = function toString(radix) { radix = radix || 10; - if (radix < 2 || 36 < radix) - throw RangeError('radix'); - if (this.isZero()) - return '0'; - if (this.isNegative()) { // Unsigned Longs are never negative + if (radix < 2 || 36 < radix) throw RangeError("radix"); + if (this.isZero()) return "0"; + if (this.isNegative()) { + // Unsigned Longs are never negative if (this.eq(MIN_VALUE)) { // We need to change the Long value before it can be negated, so we remove // the bottom-most digit in this base and then recurse to do the rest. @@ -570,26 +581,23 @@ LongPrototype.toString = function toString(radix) { div = this.div(radixLong), rem1 = div.mul(radixLong).sub(this); return div.toString(radix) + rem1.toInt().toString(radix); - } else - return '-' + this.neg().toString(radix); + } else return "-" + this.neg().toString(radix); } // Do several (6) digits each time through the loop, so as to // minimize the calls to the very expensive emulated div. var radixToPower = fromNumber(pow_dbl(radix, 6), this.unsigned), rem = this; - var result = ''; + var result = ""; while (true) { var remDiv = rem.div(radixToPower), intval = rem.sub(remDiv.mul(radixToPower)).toInt() >>> 0, digits = intval.toString(radix); rem = remDiv; - if (rem.isZero()) - return digits + result; + if (rem.isZero()) return digits + result; else { - while (digits.length < 6) - digits = '0' + digits; - result = '' + digits + result; + while (digits.length < 6) digits = "0" + digits; + result = "" + digits + result; } } }; @@ -636,12 +644,11 @@ LongPrototype.getLowBitsUnsigned = function getLowBitsUnsigned() { * @returns {number} */ LongPrototype.getNumBitsAbs = function getNumBitsAbs() { - if (this.isNegative()) // Unsigned Longs are never negative + if (this.isNegative()) + // Unsigned Longs are never negative return this.eq(MIN_VALUE) ? 64 : this.neg().getNumBitsAbs(); var val = this.high != 0 ? this.high : this.low; - for (var bit = 31; bit > 0; bit--) - if ((val & (1 << bit)) != 0) - break; + for (var bit = 31; bit > 0; bit--) if ((val & (1 << bit)) != 0) break; return this.high != 0 ? bit + 33 : bit + 1; }; @@ -719,9 +726,12 @@ LongPrototype.isEven = function isEven() { * @returns {boolean} */ LongPrototype.equals = function equals(other) { - if (!isLong(other)) - other = fromValue(other); - if (this.unsigned !== other.unsigned && (this.high >>> 31) === 1 && (other.high >>> 31) === 1) + if (!isLong(other)) other = fromValue(other); + if ( + this.unsigned !== other.unsigned && + this.high >>> 31 === 1 && + other.high >>> 31 === 1 + ) return false; return this.high === other.high && this.low === other.low; }; @@ -856,21 +866,19 @@ LongPrototype.ge = LongPrototype.greaterThanOrEqual; * if the given one is greater */ LongPrototype.compare = function compare(other) { - if (!isLong(other)) - other = fromValue(other); - if (this.eq(other)) - return 0; + if (!isLong(other)) other = fromValue(other); + if (this.eq(other)) return 0; var thisNeg = this.isNegative(), otherNeg = other.isNegative(); - if (thisNeg && !otherNeg) - return -1; - if (!thisNeg && otherNeg) - return 1; + if (thisNeg && !otherNeg) return -1; + if (!thisNeg && otherNeg) return 1; // At this point the sign bits are the same - if (!this.unsigned) - return this.sub(other).isNegative() ? -1 : 1; + if (!this.unsigned) return this.sub(other).isNegative() ? -1 : 1; // Both are positive if at least one is unsigned - return (other.high >>> 0) > (this.high >>> 0) || (other.high === this.high && (other.low >>> 0) > (this.low >>> 0)) ? -1 : 1; + return other.high >>> 0 > this.high >>> 0 || + (other.high === this.high && other.low >>> 0 > this.low >>> 0) + ? -1 + : 1; }; /** @@ -888,8 +896,7 @@ LongPrototype.comp = LongPrototype.compare; * @returns {!Long} Negated Long */ LongPrototype.negate = function negate() { - if (!this.unsigned && this.eq(MIN_VALUE)) - return MIN_VALUE; + if (!this.unsigned && this.eq(MIN_VALUE)) return MIN_VALUE; return this.not().add(ONE); }; @@ -907,33 +914,35 @@ LongPrototype.neg = LongPrototype.negate; * @returns {!Long} Sum */ LongPrototype.add = function add(addend) { - if (!isLong(addend)) - addend = fromValue(addend); + if (!isLong(addend)) addend = fromValue(addend); // Divide each number into 4 chunks of 16 bits, and then sum the chunks. var a48 = this.high >>> 16; - var a32 = this.high & 0xFFFF; + var a32 = this.high & 0xffff; var a16 = this.low >>> 16; - var a00 = this.low & 0xFFFF; + var a00 = this.low & 0xffff; var b48 = addend.high >>> 16; - var b32 = addend.high & 0xFFFF; + var b32 = addend.high & 0xffff; var b16 = addend.low >>> 16; - var b00 = addend.low & 0xFFFF; + var b00 = addend.low & 0xffff; - var c48 = 0, c32 = 0, c16 = 0, c00 = 0; + var c48 = 0, + c32 = 0, + c16 = 0, + c00 = 0; c00 += a00 + b00; c16 += c00 >>> 16; - c00 &= 0xFFFF; + c00 &= 0xffff; c16 += a16 + b16; c32 += c16 >>> 16; - c16 &= 0xFFFF; + c16 &= 0xffff; c32 += a32 + b32; c48 += c32 >>> 16; - c32 &= 0xFFFF; + c32 &= 0xffff; c48 += a48 + b48; - c48 &= 0xFFFF; + c48 &= 0xffff; return fromBits((c16 << 16) | c00, (c48 << 16) | c32, this.unsigned); }; @@ -944,8 +953,7 @@ LongPrototype.add = function add(addend) { * @returns {!Long} Difference */ LongPrototype.subtract = function subtract(subtrahend) { - if (!isLong(subtrahend)) - subtrahend = fromValue(subtrahend); + if (!isLong(subtrahend)) subtrahend = fromValue(subtrahend); return this.add(subtrahend.neg()); }; @@ -964,34 +972,23 @@ LongPrototype.sub = LongPrototype.subtract; * @returns {!Long} Product */ LongPrototype.multiply = function multiply(multiplier) { - if (this.isZero()) - return this; - if (!isLong(multiplier)) - multiplier = fromValue(multiplier); + if (this.isZero()) return this; + if (!isLong(multiplier)) multiplier = fromValue(multiplier); // use wasm support if present if (wasm) { - var low = wasm["mul"](this.low, - this.high, - multiplier.low, - multiplier.high); + var low = wasm["mul"](this.low, this.high, multiplier.low, multiplier.high); return fromBits(low, wasm["get_high"](), this.unsigned); } - if (multiplier.isZero()) - return this.unsigned ? UZERO : ZERO; - if (this.eq(MIN_VALUE)) - return multiplier.isOdd() ? MIN_VALUE : ZERO; - if (multiplier.eq(MIN_VALUE)) - return this.isOdd() ? MIN_VALUE : ZERO; + if (multiplier.isZero()) return this.unsigned ? UZERO : ZERO; + if (this.eq(MIN_VALUE)) return multiplier.isOdd() ? MIN_VALUE : ZERO; + if (multiplier.eq(MIN_VALUE)) return this.isOdd() ? MIN_VALUE : ZERO; if (this.isNegative()) { - if (multiplier.isNegative()) - return this.neg().mul(multiplier.neg()); - else - return this.neg().mul(multiplier).neg(); - } else if (multiplier.isNegative()) - return this.mul(multiplier.neg()).neg(); + if (multiplier.isNegative()) return this.neg().mul(multiplier.neg()); + else return this.neg().mul(multiplier).neg(); + } else if (multiplier.isNegative()) return this.mul(multiplier.neg()).neg(); // If both longs are small, use float multiplication if (this.lt(TWO_PWR_24) && multiplier.lt(TWO_PWR_24)) @@ -1001,36 +998,39 @@ LongPrototype.multiply = function multiply(multiplier) { // We can skip products that would overflow. var a48 = this.high >>> 16; - var a32 = this.high & 0xFFFF; + var a32 = this.high & 0xffff; var a16 = this.low >>> 16; - var a00 = this.low & 0xFFFF; + var a00 = this.low & 0xffff; var b48 = multiplier.high >>> 16; - var b32 = multiplier.high & 0xFFFF; + var b32 = multiplier.high & 0xffff; var b16 = multiplier.low >>> 16; - var b00 = multiplier.low & 0xFFFF; + var b00 = multiplier.low & 0xffff; - var c48 = 0, c32 = 0, c16 = 0, c00 = 0; + var c48 = 0, + c32 = 0, + c16 = 0, + c00 = 0; c00 += a00 * b00; c16 += c00 >>> 16; - c00 &= 0xFFFF; + c00 &= 0xffff; c16 += a16 * b00; c32 += c16 >>> 16; - c16 &= 0xFFFF; + c16 &= 0xffff; c16 += a00 * b16; c32 += c16 >>> 16; - c16 &= 0xFFFF; + c16 &= 0xffff; c32 += a32 * b00; c48 += c32 >>> 16; - c32 &= 0xFFFF; + c32 &= 0xffff; c32 += a16 * b16; c48 += c32 >>> 16; - c32 &= 0xFFFF; + c32 &= 0xffff; c32 += a00 * b32; c48 += c32 >>> 16; - c32 &= 0xFFFF; + c32 &= 0xffff; c48 += a48 * b00 + a32 * b16 + a16 * b32 + a00 * b48; - c48 &= 0xFFFF; + c48 &= 0xffff; return fromBits((c16 << 16) | c00, (c48 << 16) | c32, this.unsigned); }; @@ -1050,19 +1050,20 @@ LongPrototype.mul = LongPrototype.multiply; * @returns {!Long} Quotient */ LongPrototype.divide = function divide(divisor) { - if (!isLong(divisor)) - divisor = fromValue(divisor); - if (divisor.isZero()) - throw Error('division by zero'); + if (!isLong(divisor)) divisor = fromValue(divisor); + if (divisor.isZero()) throw Error("division by zero"); // use wasm support if present if (wasm) { // guard against signed division overflow: the largest // negative number / -1 would be 1 larger than the largest // positive number, due to two's complement. - if (!this.unsigned && + if ( + !this.unsigned && this.high === -0x80000000 && - divisor.low === -1 && divisor.high === -1) { + divisor.low === -1 && + divisor.high === -1 + ) { // be consistent with non-wasm code path return this; } @@ -1070,22 +1071,20 @@ LongPrototype.divide = function divide(divisor) { this.low, this.high, divisor.low, - divisor.high + divisor.high, ); return fromBits(low, wasm["get_high"](), this.unsigned); } - if (this.isZero()) - return this.unsigned ? UZERO : ZERO; + if (this.isZero()) return this.unsigned ? UZERO : ZERO; var approx, rem, res; if (!this.unsigned) { // This section is only relevant for signed longs and is derived from the // closure library as a whole. if (this.eq(MIN_VALUE)) { if (divisor.eq(ONE) || divisor.eq(NEG_ONE)) - return MIN_VALUE; // recall that -MIN_VALUE == MIN_VALUE - else if (divisor.eq(MIN_VALUE)) - return ONE; + return MIN_VALUE; // recall that -MIN_VALUE == MIN_VALUE + else if (divisor.eq(MIN_VALUE)) return ONE; else { // At this point, we have |other| >= 2, so |this/other| < |MIN_VALUE|. var halfThis = this.shr(1); @@ -1098,23 +1097,19 @@ LongPrototype.divide = function divide(divisor) { return res; } } - } else if (divisor.eq(MIN_VALUE)) - return this.unsigned ? UZERO : ZERO; + } else if (divisor.eq(MIN_VALUE)) return this.unsigned ? UZERO : ZERO; if (this.isNegative()) { - if (divisor.isNegative()) - return this.neg().div(divisor.neg()); + if (divisor.isNegative()) return this.neg().div(divisor.neg()); return this.neg().div(divisor).neg(); - } else if (divisor.isNegative()) - return this.div(divisor.neg()).neg(); + } else if (divisor.isNegative()) return this.div(divisor.neg()).neg(); res = ZERO; } else { // The algorithm below has not been made for unsigned longs. It's therefore // required to take special care of the MSB prior to running it. - if (!divisor.unsigned) - divisor = divisor.toUnsigned(); - if (divisor.gt(this)) - return UZERO; - if (divisor.gt(this.shru(1))) // 15 >>> 1 = 7 ; with divisor = 8 ; true + if (!divisor.unsigned) divisor = divisor.toUnsigned(); + if (divisor.gt(this)) return UZERO; + if (divisor.gt(this.shru(1))) + // 15 >>> 1 = 7 ; with divisor = 8 ; true return UONE; res = UZERO; } @@ -1133,8 +1128,7 @@ LongPrototype.divide = function divide(divisor) { // We will tweak the approximate result by changing it in the 48-th digit or // the smallest non-fractional digit, whichever is larger. var log2 = Math.ceil(Math.log(approx) / Math.LN2), - delta = (log2 <= 48) ? 1 : pow_dbl(2, log2 - 48), - + delta = log2 <= 48 ? 1 : pow_dbl(2, log2 - 48), // Decrease the approximation until it is smaller than the remainder. Note // that if it is too large, the product overflows and is negative. approxRes = fromNumber(approx), @@ -1147,8 +1141,7 @@ LongPrototype.divide = function divide(divisor) { // We know the answer can't be zero... and actually, zero would cause // infinite recursion since we would make no progress. - if (approxRes.isZero()) - approxRes = ONE; + if (approxRes.isZero()) approxRes = ONE; res = res.add(approxRes); rem = rem.sub(approxRem); @@ -1171,8 +1164,7 @@ LongPrototype.div = LongPrototype.divide; * @returns {!Long} Remainder */ LongPrototype.modulo = function modulo(divisor) { - if (!isLong(divisor)) - divisor = fromValue(divisor); + if (!isLong(divisor)) divisor = fromValue(divisor); // use wasm support if present if (wasm) { @@ -1180,7 +1172,7 @@ LongPrototype.modulo = function modulo(divisor) { this.low, this.high, divisor.low, - divisor.high + divisor.high, ); return fromBits(low, wasm["get_high"](), this.unsigned); } @@ -1254,8 +1246,7 @@ LongPrototype.ctz = LongPrototype.countTrailingZeros; * @returns {!Long} */ LongPrototype.and = function and(other) { - if (!isLong(other)) - other = fromValue(other); + if (!isLong(other)) other = fromValue(other); return fromBits(this.low & other.low, this.high & other.high, this.unsigned); }; @@ -1266,8 +1257,7 @@ LongPrototype.and = function and(other) { * @returns {!Long} */ LongPrototype.or = function or(other) { - if (!isLong(other)) - other = fromValue(other); + if (!isLong(other)) other = fromValue(other); return fromBits(this.low | other.low, this.high | other.high, this.unsigned); }; @@ -1278,8 +1268,7 @@ LongPrototype.or = function or(other) { * @returns {!Long} */ LongPrototype.xor = function xor(other) { - if (!isLong(other)) - other = fromValue(other); + if (!isLong(other)) other = fromValue(other); return fromBits(this.low ^ other.low, this.high ^ other.high, this.unsigned); }; @@ -1290,14 +1279,15 @@ LongPrototype.xor = function xor(other) { * @returns {!Long} Shifted Long */ LongPrototype.shiftLeft = function shiftLeft(numBits) { - if (isLong(numBits)) - numBits = numBits.toInt(); - if ((numBits &= 63) === 0) - return this; + if (isLong(numBits)) numBits = numBits.toInt(); + if ((numBits &= 63) === 0) return this; else if (numBits < 32) - return fromBits(this.low << numBits, (this.high << numBits) | (this.low >>> (32 - numBits)), this.unsigned); - else - return fromBits(0, this.low << (numBits - 32), this.unsigned); + return fromBits( + this.low << numBits, + (this.high << numBits) | (this.low >>> (32 - numBits)), + this.unsigned, + ); + else return fromBits(0, this.low << (numBits - 32), this.unsigned); }; /** @@ -1315,14 +1305,20 @@ LongPrototype.shl = LongPrototype.shiftLeft; * @returns {!Long} Shifted Long */ LongPrototype.shiftRight = function shiftRight(numBits) { - if (isLong(numBits)) - numBits = numBits.toInt(); - if ((numBits &= 63) === 0) - return this; + if (isLong(numBits)) numBits = numBits.toInt(); + if ((numBits &= 63) === 0) return this; else if (numBits < 32) - return fromBits((this.low >>> numBits) | (this.high << (32 - numBits)), this.high >> numBits, this.unsigned); + return fromBits( + (this.low >>> numBits) | (this.high << (32 - numBits)), + this.high >> numBits, + this.unsigned, + ); else - return fromBits(this.high >> (numBits - 32), this.high >= 0 ? 0 : -1, this.unsigned); + return fromBits( + this.high >> (numBits - 32), + this.high >= 0 ? 0 : -1, + this.unsigned, + ); }; /** @@ -1342,7 +1338,12 @@ LongPrototype.shr = LongPrototype.shiftRight; LongPrototype.shiftRightUnsigned = function shiftRightUnsigned(numBits) { if (isLong(numBits)) numBits = numBits.toInt(); if ((numBits &= 63) === 0) return this; - if (numBits < 32) return fromBits((this.low >>> numBits) | (this.high << (32 - numBits)), this.high >>> numBits, this.unsigned); + if (numBits < 32) + return fromBits( + (this.low >>> numBits) | (this.high << (32 - numBits)), + this.high >>> numBits, + this.unsigned, + ); if (numBits === 32) return fromBits(this.high, 0, this.unsigned); return fromBits(this.high >>> (numBits - 32), 0, this.unsigned); }; @@ -1375,13 +1376,21 @@ LongPrototype.rotateLeft = function rotateLeft(numBits) { if ((numBits &= 63) === 0) return this; if (numBits === 32) return fromBits(this.high, this.low, this.unsigned); if (numBits < 32) { - b = (32 - numBits); - return fromBits(((this.low << numBits) | (this.high >>> b)), ((this.high << numBits) | (this.low >>> b)), this.unsigned); + b = 32 - numBits; + return fromBits( + (this.low << numBits) | (this.high >>> b), + (this.high << numBits) | (this.low >>> b), + this.unsigned, + ); } numBits -= 32; - b = (32 - numBits); - return fromBits(((this.high << numBits) | (this.low >>> b)), ((this.low << numBits) | (this.high >>> b)), this.unsigned); -} + b = 32 - numBits; + return fromBits( + (this.high << numBits) | (this.low >>> b), + (this.low << numBits) | (this.high >>> b), + this.unsigned, + ); +}; /** * Returns this Long with bits rotated to the left by the given amount. This is an alias of {@link Long#rotateLeft}. * @function @@ -1402,13 +1411,21 @@ LongPrototype.rotateRight = function rotateRight(numBits) { if ((numBits &= 63) === 0) return this; if (numBits === 32) return fromBits(this.high, this.low, this.unsigned); if (numBits < 32) { - b = (32 - numBits); - return fromBits(((this.high << b) | (this.low >>> numBits)), ((this.low << b) | (this.high >>> numBits)), this.unsigned); + b = 32 - numBits; + return fromBits( + (this.high << b) | (this.low >>> numBits), + (this.low << b) | (this.high >>> numBits), + this.unsigned, + ); } numBits -= 32; - b = (32 - numBits); - return fromBits(((this.low << b) | (this.high >>> numBits)), ((this.high << b) | (this.low >>> numBits)), this.unsigned); -} + b = 32 - numBits; + return fromBits( + (this.low << b) | (this.high >>> numBits), + (this.high << b) | (this.low >>> numBits), + this.unsigned, + ); +}; /** * Returns this Long with bits rotated to the right by the given amount. This is an alias of {@link Long#rotateRight}. * @function @@ -1423,8 +1440,7 @@ LongPrototype.rotr = LongPrototype.rotateRight; * @returns {!Long} Signed long */ LongPrototype.toSigned = function toSigned() { - if (!this.unsigned) - return this; + if (!this.unsigned) return this; return fromBits(this.low, this.high, false); }; @@ -1434,8 +1450,7 @@ LongPrototype.toSigned = function toSigned() { * @returns {!Long} Unsigned long */ LongPrototype.toUnsigned = function toUnsigned() { - if (this.unsigned) - return this; + if (this.unsigned) return this; return fromBits(this.low, this.high, true); }; @@ -1459,13 +1474,13 @@ LongPrototype.toBytesLE = function toBytesLE() { lo = this.low; return [ lo & 0xff, - lo >>> 8 & 0xff, - lo >>> 16 & 0xff, + (lo >>> 8) & 0xff, + (lo >>> 16) & 0xff, lo >>> 24, hi & 0xff, - hi >>> 8 & 0xff, - hi >>> 16 & 0xff, - hi >>> 24 + (hi >>> 8) & 0xff, + (hi >>> 16) & 0xff, + hi >>> 24, ]; }; @@ -1479,13 +1494,13 @@ LongPrototype.toBytesBE = function toBytesBE() { lo = this.low; return [ hi >>> 24, - hi >>> 16 & 0xff, - hi >>> 8 & 0xff, + (hi >>> 16) & 0xff, + (hi >>> 8) & 0xff, hi & 0xff, lo >>> 24, - lo >>> 16 & 0xff, - lo >>> 8 & 0xff, - lo & 0xff + (lo >>> 16) & 0xff, + (lo >>> 8) & 0xff, + lo & 0xff, ]; }; @@ -1497,7 +1512,9 @@ LongPrototype.toBytesBE = function toBytesBE() { * @returns {Long} The corresponding Long value */ Long.fromBytes = function fromBytes(bytes, unsigned, le) { - return le ? Long.fromBytesLE(bytes, unsigned) : Long.fromBytesBE(bytes, unsigned); + return le + ? Long.fromBytesLE(bytes, unsigned) + : Long.fromBytesBE(bytes, unsigned); }; /** @@ -1508,15 +1525,9 @@ Long.fromBytes = function fromBytes(bytes, unsigned, le) { */ Long.fromBytesLE = function fromBytesLE(bytes, unsigned) { return new Long( - bytes[0] | - bytes[1] << 8 | - bytes[2] << 16 | - bytes[3] << 24, - bytes[4] | - bytes[5] << 8 | - bytes[6] << 16 | - bytes[7] << 24, - unsigned + bytes[0] | (bytes[1] << 8) | (bytes[2] << 16) | (bytes[3] << 24), + bytes[4] | (bytes[5] << 8) | (bytes[6] << 16) | (bytes[7] << 24), + unsigned, ); }; @@ -1528,21 +1539,14 @@ Long.fromBytesLE = function fromBytesLE(bytes, unsigned) { */ Long.fromBytesBE = function fromBytesBE(bytes, unsigned) { return new Long( - bytes[4] << 24 | - bytes[5] << 16 | - bytes[6] << 8 | - bytes[7], - bytes[0] << 24 | - bytes[1] << 16 | - bytes[2] << 8 | - bytes[3], - unsigned + (bytes[4] << 24) | (bytes[5] << 16) | (bytes[6] << 8) | bytes[7], + (bytes[0] << 24) | (bytes[1] << 16) | (bytes[2] << 8) | bytes[3], + unsigned, ); }; // Support conversion to/from BigInt where available if (typeof BigInt === "function") { - /** * Returns a Long representing the given big integer. * @function @@ -1558,10 +1562,9 @@ if (typeof BigInt === "function") { // Override Long.fromValue = function fromValueWithBigInt(value, unsigned) { - if (typeof value === "bigint") - return fromBigInt(value, unsigned); + if (typeof value === "bigint") return fromBigInt(value, unsigned); return fromValue(value, unsigned); - } + }; /** * Converts the Long to its big integer representation. @@ -1571,9 +1574,8 @@ if (typeof BigInt === "function") { LongPrototype.toBigInt = function toBigInt() { var lowBigInt = BigInt(this.low >>> 0); var highBigInt = BigInt(this.unsigned ? this.high >>> 0 : this.high); - return highBigInt << BigInt(32) | lowBigInt; + return (highBigInt << BigInt(32)) | lowBigInt; }; - } export default Long; diff --git a/package-lock.json b/package-lock.json index cdefeb9..73dbcfc 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,6 +10,7 @@ "license": "Apache-2.0", "devDependencies": { "esm2umd": "^0.2.2", + "prettier": "^3.5.0", "typescript": "^5.7.3" } }, @@ -538,6 +539,22 @@ "dev": true, "license": "ISC" }, + "node_modules/prettier": { + "version": "3.5.0", + "resolved": "https://registry.npmjs.org/prettier/-/prettier-3.5.0.tgz", + "integrity": "sha512-quyMrVt6svPS7CjQ9gKb3GLEX/rl3BCL2oa/QkNcXv4YNVBC9olt3s+H7ukto06q7B1Qz46PbrKLO34PR6vXcA==", + "dev": true, + "license": "MIT", + "bin": { + "prettier": "bin/prettier.cjs" + }, + "engines": { + "node": ">=14" + }, + "funding": { + "url": "https://github.com/prettier/prettier?sponsor=1" + } + }, "node_modules/semver": { "version": "6.3.1", "resolved": "https://registry.npmjs.org/semver/-/semver-6.3.1.tgz", @@ -946,6 +963,12 @@ "integrity": "sha512-xceH2snhtb5M9liqDsmEw56le376mTZkEX/jEb/RxNFyegNul7eNslCXP9FDj/Lcu0X8KEyMceP2ntpaHrDEVA==", "dev": true }, + "prettier": { + "version": "3.5.0", + "resolved": "https://registry.npmjs.org/prettier/-/prettier-3.5.0.tgz", + "integrity": "sha512-quyMrVt6svPS7CjQ9gKb3GLEX/rl3BCL2oa/QkNcXv4YNVBC9olt3s+H7ukto06q7B1Qz46PbrKLO34PR6vXcA==", + "dev": true + }, "semver": { "version": "6.3.1", "resolved": "https://registry.npmjs.org/semver/-/semver-6.3.1.tgz", diff --git a/package.json b/package.json index 343d7cf..307cf3e 100644 --- a/package.json +++ b/package.json @@ -33,6 +33,8 @@ }, "scripts": { "build": "esm2umd Long index.js > umd/index.js", + "lint": "prettier --check .", + "format": "prettier --write .", "test": "npm run test:unit && npm run test:typescript", "test:unit": "node tests", "test:typescript": "tsc --project tests/typescript/tsconfig.esnext.json && tsc --project tests/typescript/tsconfig.nodenext.json && tsc --project tests/typescript/tsconfig.commonjs.json" @@ -49,6 +51,7 @@ ], "devDependencies": { "esm2umd": "^0.2.2", + "prettier": "^3.5.0", "typescript": "^5.7.3" } } diff --git a/tests/goog/base.js b/tests/goog/base.js index 9de1964..9552596 100644 --- a/tests/goog/base.js +++ b/tests/goog/base.js @@ -2,14 +2,14 @@ if (typeof global === "undefined") global = window; -var goog = global.goog = {}; +var goog = (global.goog = {}); -goog.require = function(pkg) {}; +goog.require = function (pkg) {}; -goog.setTestOnly = function() {}; +goog.setTestOnly = function () {}; -goog.provide = function(pkg) { - var parts = pkg.split('.'); +goog.provide = function (pkg) { + var parts = pkg.split("."); var current = global; while (parts.length) { var part = parts.shift(); @@ -25,19 +25,25 @@ goog.provide("goog.asserts"); import assert from "assert"; -goog.asserts.assert = function(condition, opt_message, var_args) { +goog.asserts.assert = function (condition, opt_message, var_args) { assert(condition, opt_message, Array.prototype.slice.call(arguments, 2)); }; -global.assertEquals = function(expected, actual) { goog.asserts.assert(expected === actual); } +global.assertEquals = function (expected, actual) { + goog.asserts.assert(expected === actual); +}; -global.assertTrue = function(value) { goog.asserts.assert(value === true); } +global.assertTrue = function (value) { + goog.asserts.assert(value === true); +}; -global.assertNotNull = function(value) { goog.asserts.assert(value !== null); } +global.assertNotNull = function (value) { + goog.asserts.assert(value !== null); +}; goog.provide("goog.reflect"); -goog.reflect.cache = function(cacheObj, key, valueFn, opt_keyFn) { +goog.reflect.cache = function (cacheObj, key, valueFn, opt_keyFn) { var storedKey = opt_keyFn ? opt_keyFn(key) : key; if (Object.prototype.hasOwnProperty.call(cacheObj, storedKey)) { return cacheObj[storedKey]; diff --git a/tests/goog/long_test.js b/tests/goog/long_test.js index 1e5f3a4..733dbbf 100644 --- a/tests/goog/long_test.js +++ b/tests/goog/long_test.js @@ -14,18 +14,18 @@ import goog from "./base.js"; -goog.provide('goog.math.LongTest'); -goog.setTestOnly('goog.math.LongTest'); +goog.provide("goog.math.LongTest"); +goog.setTestOnly("goog.math.LongTest"); -goog.require('goog.asserts'); -goog.require('goog.math.Long'); -goog.require('goog.testing.jsunit'); +goog.require("goog.asserts"); +goog.require("goog.math.Long"); +goog.require("goog.testing.jsunit"); // Interprets the given numbers as the bits of a 32-bit int. In particular, // this takes care of the 32-bit being interpretted as the sign. function toInt32s(arr) { for (var i = 0; i < arr.length; ++i) { - arr[i] = arr[i] & 0xFFFFFFFF; + arr[i] = arr[i] & 0xffffffff; } } @@ -42,7 +42,7 @@ var TEST_BITS = [ 0x00000000, 0x01000000, 0x00000000, 0x5634e2db, 0x00000000, 0xb776d5f5, 0x00000000, 0xffffffff, 0x00000001, 0x00000000, 0x0000ffff, 0xffffffff, 0x00010000, 0x00000000, 0x000fffff, 0xffffffff, 0x00100000, 0x00000000, - 0x5634e2db, 0xb776d5f5, 0x7fffffff, 0xffffffff + 0x5634e2db, 0xb776d5f5, 0x7fffffff, 0xffffffff, ]; toInt32s(TEST_BITS); @@ -245,7 +245,7 @@ var TEST_ADD_BITS = [ 0x80000000, 0x00ffffff, 0x80000000, 0x5634e2da, 0x80000000, 0xb776d5f4, 0x80000000, 0xfffffffe, 0x80000000, 0xffffffff, 0x8000ffff, 0xfffffffe, 0x8000ffff, 0xffffffff, 0x800fffff, 0xfffffffe, 0x800fffff, 0xffffffff, - 0xd634e2db, 0xb776d5f4 + 0xd634e2db, 0xb776d5f4, ]; toInt32s(TEST_ADD_BITS); @@ -658,7 +658,7 @@ var TEST_SUB_BITS = [ 0x7fffffff, 0xa9cb1d24, 0x7fffffff, 0x48892a0a, 0x7fffffff, 0x00000000, 0x7ffffffe, 0xffffffff, 0x7fff0000, 0x00000000, 0x7ffeffff, 0xffffffff, 0x7ff00000, 0x00000000, 0x7fefffff, 0xffffffff, 0x29cb1d24, 0x48892a0a, - 0x00000000, 0x00000000 + 0x00000000, 0x00000000, ]; toInt32s(TEST_SUB_BITS); @@ -861,7 +861,7 @@ var TEST_MUL_BITS = [ 0xffffffff, 0xff000000, 0x7fffffff, 0xa9cb1d25, 0x7fffffff, 0x48892a0b, 0x7fffffff, 0x00000001, 0xffffffff, 0x00000000, 0x7fff0000, 0x00000001, 0xffff0000, 0x00000000, 0x7ff00000, 0x00000001, 0xfff00000, 0x00000000, - 0x29cb1d24, 0x48892a0b + 0x29cb1d24, 0x48892a0b, ]; toInt32s(TEST_MUL_BITS); @@ -1262,46 +1262,46 @@ var TEST_DIV_BITS = [ 0x0000007f, 0xffffffff, 0x00000001, 0x7c1c33e6, 0x00000000, 0xb29b67a6, 0x00000000, 0x80000000, 0x00000000, 0x7fffffff, 0x00000000, 0x00008000, 0x00000000, 0x00007fff, 0x00000000, 0x00000800, 0x00000000, 0x000007ff, - 0x00000000, 0x00000001, 0x00000000, 0x00000001 + 0x00000000, 0x00000001, 0x00000000, 0x00000001, ]; toInt32s(TEST_DIV_BITS); var TEST_STRINGS = [ - '-9223372036854775808', - '-5226755067826871589', - '-4503599627370497', - '-4503599627370496', - '-281474976710657', - '-281474976710656', - '-4294967297', - '-4294967296', - '-16777217', - '-16777216', - '-65537', - '-65536', - '-32769', - '-32768', - '-2', - '-1', - '0', - '1', - '2', - '32767', - '32768', - '65535', - '65536', - '16777215', - '16777216', - '1446306523', - '3078018549', - '4294967295', - '4294967296', - '281474976710655', - '281474976710656', - '4503599627370495', - '4503599627370496', - '6211839219354490357', - '9223372036854775807' + "-9223372036854775808", + "-5226755067826871589", + "-4503599627370497", + "-4503599627370496", + "-281474976710657", + "-281474976710656", + "-4294967297", + "-4294967296", + "-16777217", + "-16777216", + "-65537", + "-65536", + "-32769", + "-32768", + "-2", + "-1", + "0", + "1", + "2", + "32767", + "32768", + "65535", + "65536", + "16777215", + "16777216", + "1446306523", + "3078018549", + "4294967295", + "4294967296", + "281474976710655", + "281474976710656", + "4503599627370495", + "4503599627370496", + "6211839219354490357", + "9223372036854775807", ]; function setUp() { @@ -1336,27 +1336,33 @@ function testToFromInt() { function testToFromNumber() { for (var i = 0; i < TEST_BITS.length; i += 2) { - var num = TEST_BITS[i] * Math.pow(2, 32) + TEST_BITS[i + 1] >= 0 ? - TEST_BITS[i + 1] : - Math.pow(2, 32) + TEST_BITS[i + 1]; + var num = + TEST_BITS[i] * Math.pow(2, 32) + TEST_BITS[i + 1] >= 0 + ? TEST_BITS[i + 1] + : Math.pow(2, 32) + TEST_BITS[i + 1]; var val = goog.math.Long.fromNumber(num); assertEquals(num, val.toNumber()); } // Test edge cases assertEquals(goog.math.Long.getZero(), goog.math.Long.fromNumber(NaN)); assertEquals( - goog.math.Long.getMaxValue(), goog.math.Long.fromNumber(Infinity)); + goog.math.Long.getMaxValue(), + goog.math.Long.fromNumber(Infinity), + ); assertEquals( - goog.math.Long.getMinValue(), goog.math.Long.fromNumber(-Infinity)); + goog.math.Long.getMinValue(), + goog.math.Long.fromNumber(-Infinity), + ); } - // Make sure we are not leaking longs by incorrect caching of decimal numbers // and failing-fast in debug mode. function testFromDecimalCachedValues() { try { var handledException; - goog.asserts.setErrorHandler(function(e) { handledException = e; }); + goog.asserts.setErrorHandler(function (e) { + handledException = e; + }); assertEquals(goog.math.Long.getZero(), goog.math.Long.fromInt(0.1)); assertTrue(handledException != null); @@ -1383,7 +1389,7 @@ function testIsZero() { function testIsNegative() { for (var i = 0; i < TEST_BITS.length; i += 2) { var val = goog.math.Long.fromBits(TEST_BITS[i + 1], TEST_BITS[i]); - assertEquals((TEST_BITS[i] >> 31) != 0, val.isNegative()); + assertEquals(TEST_BITS[i] >> 31 != 0, val.isNegative()); } } @@ -1395,7 +1401,7 @@ function testIsOdd() { } function createTestComparisons(i) { - return function() { + return function () { var vi = goog.math.Long.fromBits(TEST_BITS[i + 1], TEST_BITS[i]); for (var j = 0; j < TEST_BITS.length; j += 2) { var vj = goog.math.Long.fromBits(TEST_BITS[j + 1], TEST_BITS[j]); @@ -1414,11 +1420,11 @@ function createTestComparisons(i) { // is necessary because, in some testing configurations, the full combined test // can take so long that it times out. These smaller tests run much faster. for (var i = 0; i < TEST_BITS.length; i += 2) { - goog.global['testComparisons' + i] = createTestComparisons(i); + goog.global["testComparisons" + i] = createTestComparisons(i); } function createTestBitOperations(i) { - return function() { + return function () { var vi = goog.math.Long.fromBits(TEST_BITS[i + 1], TEST_BITS[i]); assertEquals(~TEST_BITS[i], vi.not().getHighBits()); assertEquals(~TEST_BITS[i + 1], vi.not().getLowBits()); @@ -1427,12 +1433,16 @@ function createTestBitOperations(i) { var vj = goog.math.Long.fromBits(TEST_BITS[j + 1], TEST_BITS[j]); assertEquals(TEST_BITS[i] & TEST_BITS[j], vi.and(vj).getHighBits()); assertEquals( - TEST_BITS[i + 1] & TEST_BITS[j + 1], vi.and(vj).getLowBits()); + TEST_BITS[i + 1] & TEST_BITS[j + 1], + vi.and(vj).getLowBits(), + ); assertEquals(TEST_BITS[i] | TEST_BITS[j], vi.or(vj).getHighBits()); assertEquals(TEST_BITS[i + 1] | TEST_BITS[j + 1], vi.or(vj).getLowBits()); assertEquals(TEST_BITS[i] ^ TEST_BITS[j], vi.xor(vj).getHighBits()); assertEquals( - TEST_BITS[i + 1] ^ TEST_BITS[j + 1], vi.xor(vj).getLowBits()); + TEST_BITS[i + 1] ^ TEST_BITS[j + 1], + vi.xor(vj).getLowBits(), + ); } assertEquals(TEST_BITS[i], vi.shiftLeft(0).getHighBits()); @@ -1445,37 +1455,49 @@ function createTestBitOperations(i) { for (var len = 1; len < 64; ++len) { if (len < 32) { assertEquals( - (TEST_BITS[i] << len) | (TEST_BITS[i + 1] >>> (32 - len)), - vi.shiftLeft(len).getHighBits()); + (TEST_BITS[i] << len) | (TEST_BITS[i + 1] >>> (32 - len)), + vi.shiftLeft(len).getHighBits(), + ); assertEquals(TEST_BITS[i + 1] << len, vi.shiftLeft(len).getLowBits()); assertEquals(TEST_BITS[i] >> len, vi.shiftRight(len).getHighBits()); assertEquals( - (TEST_BITS[i + 1] >>> len) | (TEST_BITS[i] << (32 - len)), - vi.shiftRight(len).getLowBits()); + (TEST_BITS[i + 1] >>> len) | (TEST_BITS[i] << (32 - len)), + vi.shiftRight(len).getLowBits(), + ); assertEquals( - TEST_BITS[i] >>> len, vi.shiftRightUnsigned(len).getHighBits()); + TEST_BITS[i] >>> len, + vi.shiftRightUnsigned(len).getHighBits(), + ); assertEquals( - (TEST_BITS[i + 1] >>> len) | (TEST_BITS[i] << (32 - len)), - vi.shiftRightUnsigned(len).getLowBits()); + (TEST_BITS[i + 1] >>> len) | (TEST_BITS[i] << (32 - len)), + vi.shiftRightUnsigned(len).getLowBits(), + ); } else { assertEquals( - TEST_BITS[i + 1] << (len - 32), vi.shiftLeft(len).getHighBits()); + TEST_BITS[i + 1] << (len - 32), + vi.shiftLeft(len).getHighBits(), + ); assertEquals(0, vi.shiftLeft(len).getLowBits()); assertEquals( - TEST_BITS[i] >= 0 ? 0 : -1, vi.shiftRight(len).getHighBits()); + TEST_BITS[i] >= 0 ? 0 : -1, + vi.shiftRight(len).getHighBits(), + ); assertEquals( - TEST_BITS[i] >> (len - 32), vi.shiftRight(len).getLowBits()); + TEST_BITS[i] >> (len - 32), + vi.shiftRight(len).getLowBits(), + ); assertEquals(0, vi.shiftRightUnsigned(len).getHighBits()); if (len == 32) { assertEquals(TEST_BITS[i], vi.shiftRightUnsigned(len).getLowBits()); } else { assertEquals( - TEST_BITS[i] >>> (len - 32), - vi.shiftRightUnsigned(len).getLowBits()); + TEST_BITS[i] >>> (len - 32), + vi.shiftRightUnsigned(len).getLowBits(), + ); } } } @@ -1490,7 +1512,7 @@ function createTestBitOperations(i) { } for (var i = 0; i < TEST_BITS.length; i += 2) { - goog.global['testBitOperations' + i] = createTestBitOperations(i); + goog.global["testBitOperations" + i] = createTestBitOperations(i); } function testNegation() { @@ -1546,7 +1568,7 @@ function testMultiply() { } function createTestDivMod(i, count) { - return function() { + return function () { var vi = goog.math.Long.fromBits(TEST_BITS[i + 1], TEST_BITS[i]); for (var j = 0; j < TEST_BITS.length; j += 2) { var vj = goog.math.Long.fromBits(TEST_BITS[j + 1], TEST_BITS[j]); @@ -1560,7 +1582,7 @@ function createTestDivMod(i, count) { assertTrue(vi.equals(combinedResult)); } } - } + }; } var countPerDivModCall = 0; @@ -1573,40 +1595,47 @@ for (var j = 0; j < TEST_BITS.length; j += 2) { var countDivMod = 0; for (var i = 0; i < TEST_BITS.length; i += 2) { - goog.global['testDivMod' + i] = createTestDivMod(i, countDivMod); + goog.global["testDivMod" + i] = createTestDivMod(i, countDivMod); countDivMod += countPerDivModCall; } function createTestToFromString(i) { - return function() { + return function () { var vi = goog.math.Long.fromBits(TEST_BITS[i + 1], TEST_BITS[i]); var str = vi.toString(10); assertEquals(TEST_STRINGS[i / 2], str); assertEquals( - TEST_BITS[i], goog.math.Long.fromString(str, 10).getHighBits()); + TEST_BITS[i], + goog.math.Long.fromString(str, 10).getHighBits(), + ); assertEquals( - TEST_BITS[i + 1], goog.math.Long.fromString(str, 10).getLowBits()); + TEST_BITS[i + 1], + goog.math.Long.fromString(str, 10).getLowBits(), + ); for (var radix = 2; radix <= 36; ++radix) { var result = vi.toString(radix); assertEquals( - TEST_BITS[i], goog.math.Long.fromString(result, radix).getHighBits()); + TEST_BITS[i], + goog.math.Long.fromString(result, radix).getHighBits(), + ); assertEquals( - TEST_BITS[i + 1], - goog.math.Long.fromString(result, radix).getLowBits()); + TEST_BITS[i + 1], + goog.math.Long.fromString(result, radix).getLowBits(), + ); } - } + }; } for (var i = 0; i < TEST_BITS.length; i += 2) { - goog.global['testToFromString' + i] = createTestToFromString(i); + goog.global["testToFromString" + i] = createTestToFromString(i); } function testIsStringInRange() { - var string1 = '9223372036854775808'; - var string2 = '1000000000000000000000000'; - var string3 = '-9223372036854775809'; - var string4 = '-1000000000000000000000000'; + var string1 = "9223372036854775808"; + var string2 = "1000000000000000000000000"; + var string3 = "-9223372036854775809"; + var string4 = "-1000000000000000000000000"; assertEquals(false, goog.math.Long.isStringInRange(string1, 10)); assertEquals(false, goog.math.Long.isStringInRange(string2, 10)); assertEquals(false, goog.math.Long.isStringInRange(string3, 10)); @@ -1620,17 +1649,25 @@ function testIsStringInRange() { // Regression test for // https://github.com/google/closure-library/pull/498 function testBase36ToString() { - assertEquals('zzzzzz', goog.math.Long.fromString('zzzzzz', 36).toString(36)); + assertEquals("zzzzzz", goog.math.Long.fromString("zzzzzz", 36).toString(36)); } // BEGIN MONKEY PATCH // long.js doesn't have getZero etc. but instead ZERO if (goog.math.Long.ZERO) { - goog.math.Long.getZero = function() { return this.ZERO; }; - goog.math.Long.getOne = function() { return this.ONE; }; - goog.math.Long.getMaxValue = function() { return this.MAX_VALUE; }; - goog.math.Long.getMinValue = function() { return this.MIN_VALUE; }; + goog.math.Long.getZero = function () { + return this.ZERO; + }; + goog.math.Long.getOne = function () { + return this.ONE; + }; + goog.math.Long.getMaxValue = function () { + return this.MAX_VALUE; + }; + goog.math.Long.getMinValue = function () { + return this.MIN_VALUE; + }; } // the test runner can't just 'see' these functions, so add them explicitly @@ -1645,7 +1682,9 @@ if (goog.math.Long.ZERO) { testAdd, testSubtract, testMultiply, - testBase36ToString -].forEach(function(fn) { goog.global[fn.name] = fn; }); + testBase36ToString, +].forEach(function (fn) { + goog.global[fn.name] = fn; +}); // END MONKEY PATCH diff --git a/tests/index.js b/tests/index.js index 4ee1183..56ba76c 100644 --- a/tests/index.js +++ b/tests/index.js @@ -1,10 +1,11 @@ import assert from "assert"; import Long from "../index.js"; -var tests = [ // BEGIN TEST CASES +var tests = [ + // BEGIN TEST CASES function testBasic() { - var longVal = new Long(0xFFFFFFFF, 0x7FFFFFFF); + var longVal = new Long(0xffffffff, 0x7fffffff); assert.strictEqual(longVal.toNumber(), 9223372036854775807); assert.strictEqual(longVal.toString(), "9223372036854775807"); @@ -15,14 +16,14 @@ var tests = [ // BEGIN TEST CASES }, function testIsLong() { - var longVal = new Long(0xFFFFFFFF, 0x7FFFFFFF); + var longVal = new Long(0xffffffff, 0x7fffffff); assert.strictEqual(Long.isLong(longVal), true); - longVal = { "__isLong__": true }; + longVal = { __isLong__: true }; assert.strictEqual(Long.isLong(longVal), true); }, function testToString() { - var longVal = Long.fromBits(0xFFFFFFFF, 0xFFFFFFFF, true); + var longVal = Long.fromBits(0xffffffff, 0xffffffff, true); // #10 assert.strictEqual(longVal.toString(16), "ffffffffffffffff"); assert.strictEqual(longVal.toString(10), "18446744073709551615"); @@ -34,30 +35,57 @@ var tests = [ // BEGIN TEST CASES function testToBytes() { var longVal = Long.fromBits(0x01234567, 0x12345678); - assert.deepEqual(longVal.toBytesBE(), [ - 0x12, 0x34, 0x56, 0x78, - 0x01, 0x23, 0x45, 0x67 - ]); - assert.deepEqual(longVal.toBytesLE(), [ - 0x67, 0x45, 0x23, 0x01, - 0x78, 0x56, 0x34, 0x12 - ]); + assert.deepEqual( + longVal.toBytesBE(), + [0x12, 0x34, 0x56, 0x78, 0x01, 0x23, 0x45, 0x67], + ); + assert.deepEqual( + longVal.toBytesLE(), + [0x67, 0x45, 0x23, 0x01, 0x78, 0x56, 0x34, 0x12], + ); }, function testFromBytes() { var longVal = Long.fromBits(0x01234567, 0x12345678); var ulongVal = Long.fromBits(0x01234567, 0x12345678, true); assert.deepEqual(Long.fromBytes(longVal.toBytes()), longVal); - assert.deepEqual(Long.fromBytes([0x12, 0x34, 0x56, 0x78, 0x01, 0x23, 0x45, 0x67]), longVal); - assert.deepEqual(Long.fromBytes([0x12, 0x34, 0x56, 0x78, 0x01, 0x23, 0x45, 0x67], false, false), longVal); - assert.deepEqual(Long.fromBytes([0x67, 0x45, 0x23, 0x01, 0x78, 0x56, 0x34, 0x12], false, true), longVal); - assert.deepEqual(Long.fromBytes([0x67, 0x45, 0x23, 0x01, 0x78, 0x56, 0x34, 0x12], true, true), ulongVal); + assert.deepEqual( + Long.fromBytes([0x12, 0x34, 0x56, 0x78, 0x01, 0x23, 0x45, 0x67]), + longVal, + ); + assert.deepEqual( + Long.fromBytes( + [0x12, 0x34, 0x56, 0x78, 0x01, 0x23, 0x45, 0x67], + false, + false, + ), + longVal, + ); + assert.deepEqual( + Long.fromBytes( + [0x67, 0x45, 0x23, 0x01, 0x78, 0x56, 0x34, 0x12], + false, + true, + ), + longVal, + ); + assert.deepEqual( + Long.fromBytes( + [0x67, 0x45, 0x23, 0x01, 0x78, 0x56, 0x34, 0x12], + true, + true, + ), + ulongVal, + ); }, function testUnsignedMinMax() { assert.strictEqual(Long.MIN_VALUE.toString(), "-9223372036854775808"); assert.strictEqual(Long.MAX_VALUE.toString(), "9223372036854775807"); - assert.strictEqual(Long.MAX_UNSIGNED_VALUE.toString(), "18446744073709551615"); + assert.strictEqual( + Long.MAX_UNSIGNED_VALUE.toString(), + "18446744073709551615", + ); }, function testUnsignedConstructNegint() { @@ -70,7 +98,7 @@ var tests = [ // BEGIN TEST CASES }, function testUnsignedConstructHighLow() { - var longVal = new Long(0xFFFFFFFF, 0xFFFFFFFF, true); + var longVal = new Long(0xffffffff, 0xffffffff, true); assert.strictEqual(longVal.low, -1); assert.strictEqual(longVal.high, -1); assert.strictEqual(longVal.unsigned, true); @@ -79,7 +107,7 @@ var tests = [ // BEGIN TEST CASES }, function testUnsignedConstructNumber() { - var longVal = Long.fromNumber(0xFFFFFFFFFFFFFFFF, true); + var longVal = Long.fromNumber(0xffffffffffffffff, true); assert.strictEqual(longVal.low, -1); assert.strictEqual(longVal.high, -1); assert.strictEqual(longVal.unsigned, true); @@ -91,14 +119,16 @@ var tests = [ // BEGIN TEST CASES var longVal = Long.fromNumber(-1, false); assert.strictEqual(longVal.toNumber(), -1); longVal = longVal.toUnsigned(); - assert.strictEqual(longVal.toNumber(), 0xFFFFFFFFFFFFFFFF); - assert.strictEqual(longVal.toString(16), 'ffffffffffffffff'); + assert.strictEqual(longVal.toNumber(), 0xffffffffffffffff); + assert.strictEqual(longVal.toString(16), "ffffffffffffffff"); longVal = longVal.toSigned(); assert.strictEqual(longVal.toNumber(), -1); }, function testUnsignedMaxSubMaxSigned() { - var longVal = Long.MAX_UNSIGNED_VALUE.subtract(Long.MAX_VALUE).subtract(Long.ONE); + var longVal = Long.MAX_UNSIGNED_VALUE.subtract(Long.MAX_VALUE).subtract( + Long.ONE, + ); assert.strictEqual(longVal.toNumber(), Long.MAX_VALUE.toNumber()); assert.strictEqual(longVal.toString(), Long.MAX_VALUE.toString()); }, @@ -129,15 +159,18 @@ var tests = [ // BEGIN TEST CASES function testUnsignedDivMaxUnsigned() { var longVal = Long.MAX_UNSIGNED_VALUE; - assert.strictEqual(longVal.div(longVal).toString(), '1'); + assert.strictEqual(longVal.div(longVal).toString(), "1"); }, function testUnsignedDivNegSigned() { var a = Long.MAX_UNSIGNED_VALUE; var b = Long.fromInt(-2); - assert.strictEqual(b.toUnsigned().toString(), Long.MAX_UNSIGNED_VALUE.sub(1).toString()); + assert.strictEqual( + b.toUnsigned().toString(), + Long.MAX_UNSIGNED_VALUE.sub(1).toString(), + ); var longVal = a.div(b); - assert.strictEqual(longVal.toString(), '1'); + assert.strictEqual(longVal.toString(), "1"); }, function testUnsignedMinSignedDivOne() { @@ -149,7 +182,10 @@ var tests = [ // BEGIN TEST CASES var longVal = Long.UONE.shiftLeft(63); assert.strictEqual(longVal.notEquals(Long.MIN_VALUE), true); assert.strictEqual(longVal.toString(), "9223372036854775808"); - assert.strictEqual(Long.fromString("9223372036854775808", true).toString(), "9223372036854775808"); + assert.strictEqual( + Long.fromString("9223372036854775808", true).toString(), + "9223372036854775808", + ); }, function testIssue31() { @@ -158,15 +194,15 @@ var tests = [ // BEGIN TEST CASES assert.strictEqual(a.unsigned, true); assert.strictEqual(b.unsigned, true); var x = a.div(b); - assert.strictEqual(x.toString(), '12'); + assert.strictEqual(x.toString(), "12"); assert.strictEqual(x.unsigned, true); }, function testRotateLeft() { - var longVal = Long.fromBits(0x01234567, 0x89ABCDEF); - var longValL = Long.fromBits(0x12345678, 0x9ABCDEF0); - var longValR = Long.fromBits(0xF0123456, 0x789ABCDE); - var longValS = Long.fromBits(0x89ABCDEF, 0x01234567); + var longVal = Long.fromBits(0x01234567, 0x89abcdef); + var longValL = Long.fromBits(0x12345678, 0x9abcdef0); + var longValR = Long.fromBits(0xf0123456, 0x789abcde); + var longValS = Long.fromBits(0x89abcdef, 0x01234567); // little rotate var v = longVal.rotateLeft(4); assert.deepEqual(v, longValL); @@ -179,10 +215,10 @@ var tests = [ // BEGIN TEST CASES }, function testRotateRight() { - var longVal = Long.fromBits(0x01234567, 0x89ABCDEF); - var longValL = Long.fromBits(0x12345678, 0x9ABCDEF0); - var longValR = Long.fromBits(0xF0123456, 0x789ABCDE); - var longValS = Long.fromBits(0x89ABCDEF, 0x01234567); + var longVal = Long.fromBits(0x01234567, 0x89abcdef); + var longValL = Long.fromBits(0x12345678, 0x9abcdef0); + var longValR = Long.fromBits(0xf0123456, 0x789abcde); + var longValS = Long.fromBits(0x89abcdef, 0x01234567); // little rotate var v = longVal.rotateRight(4); assert.deepEqual(v, longValR); @@ -206,20 +242,20 @@ var tests = [ // BEGIN TEST CASES assert.deepEqual(longVal3.clz(), 31); assert.deepEqual(longVal0.ctz(), 64); - assert.deepEqual(longVal1.ctz(), 0); + assert.deepEqual(longVal1.ctz(), 0); assert.deepEqual(longVal2.ctz(), 32); - assert.deepEqual(longVal3.ctz(), 0); + assert.deepEqual(longVal3.ctz(), 0); }, function testBigInt() { - if (typeof BigInt !== "function") return + if (typeof BigInt !== "function") return; var values = [ { signed: 0n, unsigned: 0n }, { signed: 1n, unsigned: 1n }, { signed: -1n, unsigned: 18446744073709551615n }, { signed: 9223372036854775807n, unsigned: 9223372036854775807n }, - { signed: -9223372036854775808n, unsigned: 9223372036854775808n } + { signed: -9223372036854775808n, unsigned: 9223372036854775808n }, ]; for (var i = 0; i < values.length; i++) { @@ -245,8 +281,7 @@ var tests = [ // BEGIN TEST CASES assert(Long.fromNumber(-Math.pow(2, 53) + 1).isSafeInteger()); assert(!Long.fromNumber(Math.pow(2, 53)).isSafeInteger()); assert(!Long.fromNumber(-Math.pow(2, 53)).isSafeInteger()); - } - + }, ]; // END TEST CASES console.log("Running our tests"); diff --git a/tests/typescript/test-import.ts b/tests/typescript/test-import.ts index c9a7836..46a7f75 100644 --- a/tests/typescript/test-import.ts +++ b/tests/typescript/test-import.ts @@ -1,4 +1,4 @@ -import Long from '../../index.js'; +import Long from "../../index.js"; Long.fromValue(1); diff --git a/tests/typescript/tsconfig.base.json b/tests/typescript/tsconfig.base.json index 46e02c5..2b38a3b 100644 --- a/tests/typescript/tsconfig.base.json +++ b/tests/typescript/tsconfig.base.json @@ -3,4 +3,4 @@ "target": "ESNext", "noEmit": true } -} \ No newline at end of file +} diff --git a/tests/typescript/tsconfig.esnext.json b/tests/typescript/tsconfig.esnext.json index d4d907b..2cccf2e 100644 --- a/tests/typescript/tsconfig.esnext.json +++ b/tests/typescript/tsconfig.esnext.json @@ -1,7 +1,7 @@ { "extends": "./tsconfig.base.json", "compilerOptions": { - "module": "ESNext", + "module": "ESNext" }, "include": ["./test-import.ts"] } diff --git a/tests/typescript/tsconfig.nodenext.json b/tests/typescript/tsconfig.nodenext.json index 823d527..de7fc60 100644 --- a/tests/typescript/tsconfig.nodenext.json +++ b/tests/typescript/tsconfig.nodenext.json @@ -2,7 +2,7 @@ "extends": "./tsconfig.base.json", "compilerOptions": { "module": "NodeNext", - "moduleResolution": "nodenext", + "moduleResolution": "nodenext" }, "include": ["./test-import.ts"] } diff --git a/types.d.ts b/types.d.ts index 7fc3f9b..c8eaf14 100644 --- a/types.d.ts +++ b/types.d.ts @@ -1,4 +1,9 @@ -type LongLike = Long | number | bigint | string | { low: number; high: number; unsigned: boolean }; +type LongLike = + | Long + | number + | bigint + | string + | { low: number; high: number; unsigned: boolean }; export declare class Long { /** @@ -87,7 +92,7 @@ export declare class Long { static fromString( str: string, unsigned?: boolean | number, - radix?: number + radix?: number, ): Long; /** diff --git a/umd/index.d.ts b/umd/index.d.ts index 3599abb..e37943e 100644 --- a/umd/index.d.ts +++ b/umd/index.d.ts @@ -1,2 +1,2 @@ -import { Long } from '../types.js'; +import { Long } from "../types.js"; export = Long;