@polkadot/util
Advanced tools
Comparing version 12.1.2 to 12.2.1
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.packageInfo = void 0; | ||
exports.packageInfo = { name: '@polkadot/util', path: typeof __dirname === 'string' ? __dirname : 'auto', type: 'cjs', version: '12.1.2' }; | ||
exports.packageInfo = { name: '@polkadot/util', path: typeof __dirname === 'string' ? __dirname : 'auto', type: 'cjs', version: '12.2.1' }; |
@@ -8,2 +8,3 @@ "use strict"; | ||
const U16_MAX = (0, x_bigint_1.BigInt)(256 * 256); | ||
const U64_MAX = (0, x_bigint_1.BigInt)('0x10000000000000000'); | ||
/** | ||
@@ -14,15 +15,24 @@ * @name u8aToBigInt | ||
function u8aToBigInt(value, { isLe = true, isNegative = false } = {}) { | ||
if (!value || !value.length) { | ||
return (0, x_bigint_1.BigInt)(0); | ||
} | ||
// BE is not the optimal path - LE is the SCALE default | ||
const u8a = isLe | ||
? value | ||
: value.reverse(); | ||
const dvI = new DataView(u8a.buffer, u8a.byteOffset); | ||
: value.slice().reverse(); | ||
const count = u8a.length; | ||
const mod = count % 2; | ||
let result = (0, x_bigint_1.BigInt)(0); | ||
// This is mostly written for readability (with the single isNegative shortcut), | ||
// as opposed to performance, e.g. `u8aToBn` does loop unrolling, etc. | ||
if (isNegative) { | ||
switch (count) { | ||
case 0: | ||
return (0, x_bigint_1.BigInt)(0); | ||
case 1: | ||
return (0, x_bigint_1.BigInt)(((u8a[0] ^ 255) * -1) - 1); | ||
case 2: | ||
return (0, x_bigint_1.BigInt)((((u8a[0] + (u8a[1] << 8)) ^ 65535) * -1) - 1); | ||
case 4: | ||
return (0, x_bigint_1.BigInt)((((u8a[0] + (u8a[1] << 8) + (u8a[2] << 16) + (u8a[3] * 16777216)) ^ 4294967295) * -1) - 1); | ||
} | ||
const dvI = new DataView(u8a.buffer, u8a.byteOffset); | ||
if (count === 8) { | ||
return dvI.getBigInt64(0, true); | ||
} | ||
let result = (0, x_bigint_1.BigInt)(0); | ||
const mod = count % 2; | ||
for (let i = count - 2; i >= mod; i -= 2) { | ||
@@ -34,15 +44,33 @@ result = (result * U16_MAX) + (0, x_bigint_1.BigInt)(dvI.getUint16(i, true) ^ 0xffff); | ||
} | ||
return (result * -consts_js_1._1n) - consts_js_1._1n; | ||
} | ||
else { | ||
for (let i = count - 2; i >= mod; i -= 2) { | ||
result = (result * U16_MAX) + (0, x_bigint_1.BigInt)(dvI.getUint16(i, true)); | ||
switch (count) { | ||
case 0: | ||
return (0, x_bigint_1.BigInt)(0); | ||
case 1: | ||
return (0, x_bigint_1.BigInt)(u8a[0]); | ||
case 2: | ||
return (0, x_bigint_1.BigInt)(u8a[0] + (u8a[1] << 8)); | ||
case 4: | ||
return (0, x_bigint_1.BigInt)(u8a[0] + (u8a[1] << 8) + (u8a[2] << 16) + (u8a[3] * 16777216)); | ||
} | ||
const dvI = new DataView(u8a.buffer, u8a.byteOffset); | ||
switch (count) { | ||
case 8: | ||
return dvI.getBigUint64(0, true); | ||
case 16: | ||
return (dvI.getBigUint64(8, true) * U64_MAX) + dvI.getBigUint64(0, true); | ||
default: { | ||
let result = (0, x_bigint_1.BigInt)(0); | ||
const mod = count % 2; | ||
for (let i = count - 2; i >= mod; i -= 2) { | ||
result = (result * U16_MAX) + (0, x_bigint_1.BigInt)(dvI.getUint16(i, true)); | ||
} | ||
if (mod) { | ||
result = (result * U8_MAX) + (0, x_bigint_1.BigInt)(u8a[0]); | ||
} | ||
return result; | ||
} | ||
if (mod) { | ||
result = (result * U8_MAX) + (0, x_bigint_1.BigInt)(u8a[0]); | ||
} | ||
} | ||
return isNegative | ||
? ((result * -consts_js_1._1n) - consts_js_1._1n) | ||
: result; | ||
} | ||
exports.u8aToBigInt = u8aToBigInt; |
@@ -27,49 +27,6 @@ "use strict"; | ||
// here seems to be more efficient than passing the full array | ||
if (count <= 6) { | ||
if (isNegative) { | ||
let result = 0; | ||
if (isLe) { | ||
// Most common case i{8, 16, 32} default LE SCALE-encoded | ||
// For <= 32, we also optimize the xor to a single op | ||
// (see the comments around unrolling in the next section) | ||
switch (count) { | ||
case 0: | ||
return new bn_js_1.BN(0); | ||
case 1: | ||
result = value[0] ^ 255; | ||
break; | ||
case 2: | ||
result = (value[0] + (value[1] << 8)) ^ 65535; | ||
break; | ||
case 3: | ||
result = (value[0] + (value[1] << 8) + (value[2] << 16)) ^ 16777215; | ||
break; | ||
case 4: | ||
// for the 3rd byte, we don't << 24 - since JS converts all bitwise operators to | ||
// 32-bit, in the case where the top-most bit is set this yields a negative value | ||
result = (value[0] + (value[1] << 8) + (value[2] << 16) + (value[3] * 16777216)) ^ 4294967295; | ||
break; | ||
case 5: | ||
result = ((value[0] + (value[1] << 8) + (value[2] << 16) + (value[3] * 16777216)) ^ 4294967295) + ((value[4] ^ 0xff) * 4294967296); | ||
break; | ||
default: // 6 | ||
result = ((value[0] + (value[1] << 8) + (value[2] << 16) + (value[3] * 16777216)) ^ 4294967295) + (((value[4] + (value[5] << 8)) ^ 65535) * 4294967296); | ||
break; | ||
} | ||
} | ||
else { | ||
for (let i = 0; i < count; i++) { | ||
result = (result * 256) + (value[i] ^ 0xff); | ||
} | ||
} | ||
return count | ||
? new bn_js_1.BN((result * -1) - 1) | ||
: new bn_js_1.BN(0); | ||
} | ||
else if (isLe) { | ||
// Most common case - u{8, 16, 32} default LE SCALE-encoded | ||
// | ||
// There are some slight benefits in unrolling this specific loop, | ||
// however it comes with diminishing returns since here the actual | ||
// `new BN` does seem to take up the bulk of the time | ||
if (isNegative) { | ||
if (isLe) { | ||
// Most common case i{8, 16, 32} default LE SCALE-encoded | ||
// For <= 32, we also optimize the xor to a single op | ||
switch (count) { | ||
@@ -79,29 +36,70 @@ case 0: | ||
case 1: | ||
return new bn_js_1.BN(value[0]); | ||
return new bn_js_1.BN(((value[0] ^ 255) * -1) - 1); | ||
case 2: | ||
return new bn_js_1.BN(value[0] + (value[1] << 8)); | ||
return new bn_js_1.BN((((value[0] + (value[1] << 8)) ^ 65535) * -1) - 1); | ||
case 3: | ||
return new bn_js_1.BN(value[0] + (value[1] << 8) + (value[2] << 16)); | ||
return new bn_js_1.BN((((value[0] + (value[1] << 8) + (value[2] << 16)) ^ 16777215) * -1) - 1); | ||
case 4: | ||
// for the 3rd byte, we don't << 24 - since JS converts all bitwise operators to | ||
// 32-bit, in the case where the top-most bit is set this yields a negative value | ||
return new bn_js_1.BN(value[0] + (value[1] << 8) + (value[2] << 16) + (value[3] * 16777216)); | ||
return new bn_js_1.BN((((value[0] + (value[1] << 8) + (value[2] << 16) + (value[3] * 16777216)) ^ 4294967295) * -1) - 1); | ||
case 5: | ||
return new bn_js_1.BN(value[0] + (value[1] << 8) + (value[2] << 16) + ((value[3] + (value[4] << 8)) * 16777216)); | ||
default: // 6 | ||
return new bn_js_1.BN(value[0] + (value[1] << 8) + (value[2] << 16) + ((value[3] + (value[4] << 8) + (value[5] << 16)) * 16777216)); | ||
return new bn_js_1.BN(((((value[0] + (value[1] << 8) + (value[2] << 16) + (value[3] * 16777216)) ^ 4294967295) + ((value[4] ^ 0xff) * 4294967296)) * -1) - 1); | ||
case 6: | ||
return new bn_js_1.BN(((((value[0] + (value[1] << 8) + (value[2] << 16) + (value[3] * 16777216)) ^ 4294967295) + (((value[4] + (value[5] << 8)) ^ 65535) * 4294967296)) * -1) - 1); | ||
default: | ||
return new bn_js_1.BN(value, 'le').fromTwos(value.length * 8); | ||
} | ||
} | ||
else { | ||
let result = 0; | ||
for (let i = 0; i < count; i++) { | ||
result = (result * 256) + value[i]; | ||
} | ||
return new bn_js_1.BN(result); | ||
if (count === 0) { | ||
return new bn_js_1.BN(0); | ||
} | ||
else if (count > 6) { | ||
return new bn_js_1.BN(value, 'be').fromTwos(value.length * 8); | ||
} | ||
let result = 0; | ||
for (let i = 0; i < count; i++) { | ||
result = (result * 256) + (value[i] ^ 0xff); | ||
} | ||
return new bn_js_1.BN((result * -1) - 1); | ||
} | ||
return isNegative | ||
? new bn_js_1.BN(value, isLe ? 'le' : 'be').fromTwos(value.length * 8) | ||
: new bn_js_1.BN(value, isLe ? 'le' : 'be'); | ||
if (isLe) { | ||
// Most common case - u{8, 16, 32} default LE SCALE-encoded | ||
// | ||
// There are some slight benefits in unrolling this specific loop, | ||
// however it comes with diminishing returns since here the actual | ||
// `new BN` does seem to take up the bulk of the time | ||
switch (count) { | ||
case 0: | ||
return new bn_js_1.BN(0); | ||
case 1: | ||
return new bn_js_1.BN(value[0]); | ||
case 2: | ||
return new bn_js_1.BN(value[0] + (value[1] << 8)); | ||
case 3: | ||
return new bn_js_1.BN(value[0] + (value[1] << 8) + (value[2] << 16)); | ||
case 4: | ||
// for the 3rd byte, we don't << 24 - since JS converts all bitwise operators to | ||
// 32-bit, in the case where the top-most bit is set this yields a negative value | ||
return new bn_js_1.BN(value[0] + (value[1] << 8) + (value[2] << 16) + (value[3] * 16777216)); | ||
case 5: | ||
return new bn_js_1.BN(value[0] + (value[1] << 8) + (value[2] << 16) + ((value[3] + (value[4] << 8)) * 16777216)); | ||
case 6: | ||
return new bn_js_1.BN(value[0] + (value[1] << 8) + (value[2] << 16) + ((value[3] + (value[4] << 8) + (value[5] << 16)) * 16777216)); | ||
default: | ||
return new bn_js_1.BN(value, 'le'); | ||
} | ||
} | ||
if (count === 0) { | ||
return new bn_js_1.BN(0); | ||
} | ||
else if (count > 6) { | ||
return new bn_js_1.BN(value, 'be'); | ||
} | ||
let result = 0; | ||
for (let i = 0; i < count; i++) { | ||
result = (result * 256) + value[i]; | ||
} | ||
return new bn_js_1.BN(result); | ||
} | ||
exports.u8aToBn = u8aToBn; |
@@ -6,3 +6,3 @@ "use strict"; | ||
* @name u8aToNumber | ||
* @summary Creates a number from a Uint8Array object. | ||
* @summary Creates a number from a Uint8Array object. This only operates on LE values as used in SCALE. | ||
*/ | ||
@@ -12,3 +12,2 @@ function u8aToNumber(value, { isNegative = false } = {}) { | ||
if (isNegative) { | ||
let result = 0; | ||
switch (count) { | ||
@@ -18,25 +17,18 @@ case 0: | ||
case 1: | ||
result = value[0] ^ 255; | ||
break; | ||
return (((value[0] ^ 255) * -1) - 1); | ||
case 2: | ||
result = (value[0] + (value[1] << 8)) ^ 65535; | ||
break; | ||
return ((((value[0] + (value[1] << 8)) ^ 65535) * -1) - 1); | ||
case 3: | ||
result = (value[0] + (value[1] << 8) + (value[2] << 16)) ^ 16777215; | ||
break; | ||
return ((((value[0] + (value[1] << 8) + (value[2] << 16)) ^ 16777215) * -1) - 1); | ||
case 4: | ||
// for the 3rd byte, we don't << 24 - since JS converts all bitwise operators to | ||
// 32-bit, in the case where the top-most bit is set this yields a negative value | ||
result = (value[0] + (value[1] << 8) + (value[2] << 16) + (value[3] * 16777216)) ^ 4294967295; | ||
break; | ||
return ((((value[0] + (value[1] << 8) + (value[2] << 16) + (value[3] * 16777216)) ^ 4294967295) * -1) - 1); | ||
case 5: | ||
result = ((value[0] + (value[1] << 8) + (value[2] << 16) + (value[3] * 16777216)) ^ 4294967295) + ((value[4] ^ 0xff) * 4294967296); | ||
break; | ||
return (((((value[0] + (value[1] << 8) + (value[2] << 16) + (value[3] * 16777216)) ^ 4294967295) + ((value[4] ^ 0xff) * 4294967296)) * -1) - 1); | ||
case 6: | ||
result = ((value[0] + (value[1] << 8) + (value[2] << 16) + (value[3] * 16777216)) ^ 4294967295) + (((value[4] + (value[5] << 8)) ^ 65535) * 4294967296); | ||
break; | ||
return (((((value[0] + (value[1] << 8) + (value[2] << 16) + (value[3] * 16777216)) ^ 4294967295) + (((value[4] + (value[5] << 8)) ^ 65535) * 4294967296)) * -1) - 1); | ||
default: | ||
throw new Error('Value more than 48-bits cannot be reliably converted'); | ||
} | ||
return (result * -1) - 1; | ||
} | ||
@@ -43,0 +35,0 @@ switch (count) { |
149
package.json
@@ -21,3 +21,3 @@ { | ||
"type": "module", | ||
"version": "12.1.2", | ||
"version": "12.2.1", | ||
"main": "./cjs/index.js", | ||
@@ -31,2 +31,3 @@ "module": "./index.js", | ||
"types": "./index.d.ts", | ||
"module": "./index.js", | ||
"require": "./cjs/index.js", | ||
@@ -37,2 +38,3 @@ "default": "./index.js" | ||
"types": "./array/index.d.ts", | ||
"module": "./array/index.js", | ||
"require": "./cjs/array/index.js", | ||
@@ -43,2 +45,3 @@ "default": "./array/index.js" | ||
"types": "./array/chunk.d.ts", | ||
"module": "./array/chunk.js", | ||
"require": "./cjs/array/chunk.js", | ||
@@ -49,2 +52,3 @@ "default": "./array/chunk.js" | ||
"types": "./array/filter.d.ts", | ||
"module": "./array/filter.js", | ||
"require": "./cjs/array/filter.js", | ||
@@ -55,2 +59,3 @@ "default": "./array/filter.js" | ||
"types": "./array/flatten.d.ts", | ||
"module": "./array/flatten.js", | ||
"require": "./cjs/array/flatten.js", | ||
@@ -61,2 +66,3 @@ "default": "./array/flatten.js" | ||
"types": "./array/range.d.ts", | ||
"module": "./array/range.js", | ||
"require": "./cjs/array/range.js", | ||
@@ -67,2 +73,3 @@ "default": "./array/range.js" | ||
"types": "./array/shuffle.d.ts", | ||
"module": "./array/shuffle.js", | ||
"require": "./cjs/array/shuffle.js", | ||
@@ -73,2 +80,3 @@ "default": "./array/shuffle.js" | ||
"types": "./array/unzip.d.ts", | ||
"module": "./array/unzip.js", | ||
"require": "./cjs/array/unzip.js", | ||
@@ -79,2 +87,3 @@ "default": "./array/unzip.js" | ||
"types": "./array/zip.d.ts", | ||
"module": "./array/zip.js", | ||
"require": "./cjs/array/zip.js", | ||
@@ -85,2 +94,3 @@ "default": "./array/zip.js" | ||
"types": "./assert.d.ts", | ||
"module": "./assert.js", | ||
"require": "./cjs/assert.js", | ||
@@ -91,2 +101,3 @@ "default": "./assert.js" | ||
"types": "./bi/index.d.ts", | ||
"module": "./bi/index.js", | ||
"require": "./cjs/bi/index.js", | ||
@@ -97,2 +108,3 @@ "default": "./bi/index.js" | ||
"types": "./bi/consts.d.ts", | ||
"module": "./bi/consts.js", | ||
"require": "./cjs/bi/consts.js", | ||
@@ -103,2 +115,3 @@ "default": "./bi/consts.js" | ||
"types": "./bi/helpers.d.ts", | ||
"module": "./bi/helpers.js", | ||
"require": "./cjs/bi/helpers.js", | ||
@@ -109,2 +122,3 @@ "default": "./bi/helpers.js" | ||
"types": "./bi/min.d.ts", | ||
"module": "./bi/min.js", | ||
"require": "./cjs/bi/min.js", | ||
@@ -115,2 +129,3 @@ "default": "./bi/min.js" | ||
"types": "./bi/sqrt.d.ts", | ||
"module": "./bi/sqrt.js", | ||
"require": "./cjs/bi/sqrt.js", | ||
@@ -121,2 +136,3 @@ "default": "./bi/sqrt.js" | ||
"types": "./bi/toBigInt.d.ts", | ||
"module": "./bi/toBigInt.js", | ||
"require": "./cjs/bi/toBigInt.js", | ||
@@ -127,2 +143,3 @@ "default": "./bi/toBigInt.js" | ||
"types": "./bi/toHex.d.ts", | ||
"module": "./bi/toHex.js", | ||
"require": "./cjs/bi/toHex.js", | ||
@@ -133,2 +150,3 @@ "default": "./bi/toHex.js" | ||
"types": "./bi/toU8a.d.ts", | ||
"module": "./bi/toU8a.js", | ||
"require": "./cjs/bi/toU8a.js", | ||
@@ -139,2 +157,3 @@ "default": "./bi/toU8a.js" | ||
"types": "./bn/index.d.ts", | ||
"module": "./bn/index.js", | ||
"require": "./cjs/bn/index.js", | ||
@@ -145,2 +164,3 @@ "default": "./bn/index.js" | ||
"types": "./bn/bn.d.ts", | ||
"module": "./bn/bn.js", | ||
"require": "./cjs/bn/bn.js", | ||
@@ -151,2 +171,3 @@ "default": "./bn/bn.js" | ||
"types": "./bn/consts.d.ts", | ||
"module": "./bn/consts.js", | ||
"require": "./cjs/bn/consts.js", | ||
@@ -157,2 +178,3 @@ "default": "./bn/consts.js" | ||
"types": "./bn/fromHex.d.ts", | ||
"module": "./bn/fromHex.js", | ||
"require": "./cjs/bn/fromHex.js", | ||
@@ -163,2 +185,3 @@ "default": "./bn/fromHex.js" | ||
"types": "./bn/min.d.ts", | ||
"module": "./bn/min.js", | ||
"require": "./cjs/bn/min.js", | ||
@@ -169,2 +192,3 @@ "default": "./bn/min.js" | ||
"types": "./bn/sqrt.d.ts", | ||
"module": "./bn/sqrt.js", | ||
"require": "./cjs/bn/sqrt.js", | ||
@@ -175,2 +199,3 @@ "default": "./bn/sqrt.js" | ||
"types": "./bn/toBn.d.ts", | ||
"module": "./bn/toBn.js", | ||
"require": "./cjs/bn/toBn.js", | ||
@@ -181,2 +206,3 @@ "default": "./bn/toBn.js" | ||
"types": "./bn/toHex.d.ts", | ||
"module": "./bn/toHex.js", | ||
"require": "./cjs/bn/toHex.js", | ||
@@ -187,2 +213,3 @@ "default": "./bn/toHex.js" | ||
"types": "./bn/toU8a.d.ts", | ||
"module": "./bn/toU8a.js", | ||
"require": "./cjs/bn/toU8a.js", | ||
@@ -193,2 +220,3 @@ "default": "./bn/toU8a.js" | ||
"types": "./buffer/index.d.ts", | ||
"module": "./buffer/index.js", | ||
"require": "./cjs/buffer/index.js", | ||
@@ -199,2 +227,3 @@ "default": "./buffer/index.js" | ||
"types": "./buffer/toU8a.d.ts", | ||
"module": "./buffer/toU8a.js", | ||
"require": "./cjs/buffer/toU8a.js", | ||
@@ -205,2 +234,3 @@ "default": "./buffer/toU8a.js" | ||
"types": "./bundle.d.ts", | ||
"module": "./bundle.js", | ||
"require": "./cjs/bundle.js", | ||
@@ -211,2 +241,3 @@ "default": "./bundle.js" | ||
"types": "./compact/index.d.ts", | ||
"module": "./compact/index.js", | ||
"require": "./cjs/compact/index.js", | ||
@@ -217,2 +248,3 @@ "default": "./compact/index.js" | ||
"types": "./compact/addLength.d.ts", | ||
"module": "./compact/addLength.js", | ||
"require": "./cjs/compact/addLength.js", | ||
@@ -223,2 +255,3 @@ "default": "./compact/addLength.js" | ||
"types": "./compact/defaults.d.ts", | ||
"module": "./compact/defaults.js", | ||
"require": "./cjs/compact/defaults.js", | ||
@@ -229,2 +262,3 @@ "default": "./compact/defaults.js" | ||
"types": "./compact/fromU8a.d.ts", | ||
"module": "./compact/fromU8a.js", | ||
"require": "./cjs/compact/fromU8a.js", | ||
@@ -235,2 +269,3 @@ "default": "./compact/fromU8a.js" | ||
"types": "./compact/stripLength.d.ts", | ||
"module": "./compact/stripLength.js", | ||
"require": "./cjs/compact/stripLength.js", | ||
@@ -241,2 +276,3 @@ "default": "./compact/stripLength.js" | ||
"types": "./compact/toU8a.d.ts", | ||
"module": "./compact/toU8a.js", | ||
"require": "./cjs/compact/toU8a.js", | ||
@@ -247,2 +283,3 @@ "default": "./compact/toU8a.js" | ||
"types": "./compact/types.d.ts", | ||
"module": "./compact/types.js", | ||
"require": "./cjs/compact/types.js", | ||
@@ -253,2 +290,3 @@ "default": "./compact/types.js" | ||
"types": "./detectOther.d.ts", | ||
"module": "./detectOther.js", | ||
"require": "./cjs/detectOther.js", | ||
@@ -259,2 +297,3 @@ "default": "./detectOther.js" | ||
"types": "./detectPackage.d.ts", | ||
"module": "./detectPackage.js", | ||
"require": "./cjs/detectPackage.js", | ||
@@ -265,2 +304,3 @@ "default": "./detectPackage.js" | ||
"types": "./extractTime.d.ts", | ||
"module": "./extractTime.js", | ||
"require": "./cjs/extractTime.js", | ||
@@ -271,2 +311,3 @@ "default": "./extractTime.js" | ||
"types": "./float/index.d.ts", | ||
"module": "./float/index.js", | ||
"require": "./cjs/float/index.js", | ||
@@ -277,2 +318,3 @@ "default": "./float/index.js" | ||
"types": "./float/toU8a.d.ts", | ||
"module": "./float/toU8a.js", | ||
"require": "./cjs/float/toU8a.js", | ||
@@ -283,2 +325,3 @@ "default": "./float/toU8a.js" | ||
"types": "./format/index.d.ts", | ||
"module": "./format/index.js", | ||
"require": "./cjs/format/index.js", | ||
@@ -289,2 +332,3 @@ "default": "./format/index.js" | ||
"types": "./format/formatBalance.d.ts", | ||
"module": "./format/formatBalance.js", | ||
"require": "./cjs/format/formatBalance.js", | ||
@@ -295,2 +339,3 @@ "default": "./format/formatBalance.js" | ||
"types": "./format/formatDate.d.ts", | ||
"module": "./format/formatDate.js", | ||
"require": "./cjs/format/formatDate.js", | ||
@@ -301,2 +346,3 @@ "default": "./format/formatDate.js" | ||
"types": "./format/formatDecimal.d.ts", | ||
"module": "./format/formatDecimal.js", | ||
"require": "./cjs/format/formatDecimal.js", | ||
@@ -307,2 +353,3 @@ "default": "./format/formatDecimal.js" | ||
"types": "./format/formatElapsed.d.ts", | ||
"module": "./format/formatElapsed.js", | ||
"require": "./cjs/format/formatElapsed.js", | ||
@@ -313,2 +360,3 @@ "default": "./format/formatElapsed.js" | ||
"types": "./format/formatNumber.d.ts", | ||
"module": "./format/formatNumber.js", | ||
"require": "./cjs/format/formatNumber.js", | ||
@@ -319,2 +367,3 @@ "default": "./format/formatNumber.js" | ||
"types": "./format/getSeparator.d.ts", | ||
"module": "./format/getSeparator.js", | ||
"require": "./cjs/format/getSeparator.js", | ||
@@ -325,2 +374,3 @@ "default": "./format/getSeparator.js" | ||
"types": "./format/si.d.ts", | ||
"module": "./format/si.js", | ||
"require": "./cjs/format/si.js", | ||
@@ -331,2 +381,3 @@ "default": "./format/si.js" | ||
"types": "./has.d.ts", | ||
"module": "./has.js", | ||
"require": "./cjs/has.js", | ||
@@ -337,2 +388,3 @@ "default": "./has.js" | ||
"types": "./hex/index.d.ts", | ||
"module": "./hex/index.js", | ||
"require": "./cjs/hex/index.js", | ||
@@ -343,2 +395,3 @@ "default": "./hex/index.js" | ||
"types": "./hex/addPrefix.d.ts", | ||
"module": "./hex/addPrefix.js", | ||
"require": "./cjs/hex/addPrefix.js", | ||
@@ -349,2 +402,3 @@ "default": "./hex/addPrefix.js" | ||
"types": "./hex/fixLength.d.ts", | ||
"module": "./hex/fixLength.js", | ||
"require": "./cjs/hex/fixLength.js", | ||
@@ -355,2 +409,3 @@ "default": "./hex/fixLength.js" | ||
"types": "./hex/hasPrefix.d.ts", | ||
"module": "./hex/hasPrefix.js", | ||
"require": "./cjs/hex/hasPrefix.js", | ||
@@ -361,2 +416,3 @@ "default": "./hex/hasPrefix.js" | ||
"types": "./hex/stripPrefix.d.ts", | ||
"module": "./hex/stripPrefix.js", | ||
"require": "./cjs/hex/stripPrefix.js", | ||
@@ -367,2 +423,3 @@ "default": "./hex/stripPrefix.js" | ||
"types": "./hex/toBigInt.d.ts", | ||
"module": "./hex/toBigInt.js", | ||
"require": "./cjs/hex/toBigInt.js", | ||
@@ -373,2 +430,3 @@ "default": "./hex/toBigInt.js" | ||
"types": "./hex/toBn.d.ts", | ||
"module": "./hex/toBn.js", | ||
"require": "./cjs/hex/toBn.js", | ||
@@ -379,2 +437,3 @@ "default": "./hex/toBn.js" | ||
"types": "./hex/toNumber.d.ts", | ||
"module": "./hex/toNumber.js", | ||
"require": "./cjs/hex/toNumber.js", | ||
@@ -385,2 +444,3 @@ "default": "./hex/toNumber.js" | ||
"types": "./hex/toString.d.ts", | ||
"module": "./hex/toString.js", | ||
"require": "./cjs/hex/toString.js", | ||
@@ -391,2 +451,3 @@ "default": "./hex/toString.js" | ||
"types": "./hex/toU8a.d.ts", | ||
"module": "./hex/toU8a.js", | ||
"node": { | ||
@@ -401,2 +462,3 @@ "require": "./cjs/hex/toU8aBuffer.js", | ||
"types": "./hex/toU8aBuffer.d.ts", | ||
"module": "./hex/toU8aBuffer.js", | ||
"require": "./cjs/hex/toU8aBuffer.js", | ||
@@ -407,2 +469,3 @@ "default": "./hex/toU8aBuffer.js" | ||
"types": "./is/index.d.ts", | ||
"module": "./is/index.js", | ||
"require": "./cjs/is/index.js", | ||
@@ -413,2 +476,3 @@ "default": "./is/index.js" | ||
"types": "./is/array.d.ts", | ||
"module": "./is/array.js", | ||
"require": "./cjs/is/array.js", | ||
@@ -419,2 +483,3 @@ "default": "./is/array.js" | ||
"types": "./is/ascii.d.ts", | ||
"module": "./is/ascii.js", | ||
"require": "./cjs/is/ascii.js", | ||
@@ -425,2 +490,3 @@ "default": "./is/ascii.js" | ||
"types": "./is/bigInt.d.ts", | ||
"module": "./is/bigInt.js", | ||
"require": "./cjs/is/bigInt.js", | ||
@@ -431,2 +497,3 @@ "default": "./is/bigInt.js" | ||
"types": "./is/bn.d.ts", | ||
"module": "./is/bn.js", | ||
"require": "./cjs/is/bn.js", | ||
@@ -437,2 +504,3 @@ "default": "./is/bn.js" | ||
"types": "./is/boolean.d.ts", | ||
"module": "./is/boolean.js", | ||
"require": "./cjs/is/boolean.js", | ||
@@ -443,2 +511,3 @@ "default": "./is/boolean.js" | ||
"types": "./is/buffer.d.ts", | ||
"module": "./is/buffer.js", | ||
"require": "./cjs/is/buffer.js", | ||
@@ -449,2 +518,3 @@ "default": "./is/buffer.js" | ||
"types": "./is/childClass.d.ts", | ||
"module": "./is/childClass.js", | ||
"require": "./cjs/is/childClass.js", | ||
@@ -455,2 +525,3 @@ "default": "./is/childClass.js" | ||
"types": "./is/class.d.ts", | ||
"module": "./is/class.js", | ||
"require": "./cjs/is/class.js", | ||
@@ -461,2 +532,3 @@ "default": "./is/class.js" | ||
"types": "./is/codec.d.ts", | ||
"module": "./is/codec.js", | ||
"require": "./cjs/is/codec.js", | ||
@@ -467,2 +539,3 @@ "default": "./is/codec.js" | ||
"types": "./is/compact.d.ts", | ||
"module": "./is/compact.js", | ||
"require": "./cjs/is/compact.js", | ||
@@ -473,2 +546,3 @@ "default": "./is/compact.js" | ||
"types": "./is/error.d.ts", | ||
"module": "./is/error.js", | ||
"require": "./cjs/is/error.js", | ||
@@ -479,2 +553,3 @@ "default": "./is/error.js" | ||
"types": "./is/function.d.ts", | ||
"module": "./is/function.js", | ||
"require": "./cjs/is/function.js", | ||
@@ -485,2 +560,3 @@ "default": "./is/function.js" | ||
"types": "./is/helpers.d.ts", | ||
"module": "./is/helpers.js", | ||
"require": "./cjs/is/helpers.js", | ||
@@ -491,2 +567,3 @@ "default": "./is/helpers.js" | ||
"types": "./is/hex.d.ts", | ||
"module": "./is/hex.js", | ||
"require": "./cjs/is/hex.js", | ||
@@ -497,2 +574,3 @@ "default": "./is/hex.js" | ||
"types": "./is/instanceOf.d.ts", | ||
"module": "./is/instanceOf.js", | ||
"require": "./cjs/is/instanceOf.js", | ||
@@ -503,2 +581,3 @@ "default": "./is/instanceOf.js" | ||
"types": "./is/ip.d.ts", | ||
"module": "./is/ip.js", | ||
"require": "./cjs/is/ip.js", | ||
@@ -509,2 +588,3 @@ "default": "./is/ip.js" | ||
"types": "./is/jsonObject.d.ts", | ||
"module": "./is/jsonObject.js", | ||
"require": "./cjs/is/jsonObject.js", | ||
@@ -515,2 +595,3 @@ "default": "./is/jsonObject.js" | ||
"types": "./is/null.d.ts", | ||
"module": "./is/null.js", | ||
"require": "./cjs/is/null.js", | ||
@@ -521,2 +602,3 @@ "default": "./is/null.js" | ||
"types": "./is/number.d.ts", | ||
"module": "./is/number.js", | ||
"require": "./cjs/is/number.js", | ||
@@ -527,2 +609,3 @@ "default": "./is/number.js" | ||
"types": "./is/object.d.ts", | ||
"module": "./is/object.js", | ||
"require": "./cjs/is/object.js", | ||
@@ -533,2 +616,3 @@ "default": "./is/object.js" | ||
"types": "./is/observable.d.ts", | ||
"module": "./is/observable.js", | ||
"require": "./cjs/is/observable.js", | ||
@@ -539,2 +623,3 @@ "default": "./is/observable.js" | ||
"types": "./is/promise.d.ts", | ||
"module": "./is/promise.js", | ||
"require": "./cjs/is/promise.js", | ||
@@ -545,2 +630,3 @@ "default": "./is/promise.js" | ||
"types": "./is/string.d.ts", | ||
"module": "./is/string.js", | ||
"require": "./cjs/is/string.js", | ||
@@ -551,2 +637,3 @@ "default": "./is/string.js" | ||
"types": "./is/testChain.d.ts", | ||
"module": "./is/testChain.js", | ||
"require": "./cjs/is/testChain.js", | ||
@@ -557,2 +644,3 @@ "default": "./is/testChain.js" | ||
"types": "./is/toBigInt.d.ts", | ||
"module": "./is/toBigInt.js", | ||
"require": "./cjs/is/toBigInt.js", | ||
@@ -563,2 +651,3 @@ "default": "./is/toBigInt.js" | ||
"types": "./is/toBn.d.ts", | ||
"module": "./is/toBn.js", | ||
"require": "./cjs/is/toBn.js", | ||
@@ -569,2 +658,3 @@ "default": "./is/toBn.js" | ||
"types": "./is/u8a.d.ts", | ||
"module": "./is/u8a.js", | ||
"require": "./cjs/is/u8a.js", | ||
@@ -575,2 +665,3 @@ "default": "./is/u8a.js" | ||
"types": "./is/undefined.d.ts", | ||
"module": "./is/undefined.js", | ||
"require": "./cjs/is/undefined.js", | ||
@@ -581,2 +672,3 @@ "default": "./is/undefined.js" | ||
"types": "./is/utf8.d.ts", | ||
"module": "./is/utf8.js", | ||
"require": "./cjs/is/utf8.js", | ||
@@ -587,2 +679,3 @@ "default": "./is/utf8.js" | ||
"types": "./is/wasm.d.ts", | ||
"module": "./is/wasm.js", | ||
"require": "./cjs/is/wasm.js", | ||
@@ -593,2 +686,3 @@ "default": "./is/wasm.js" | ||
"types": "./lazy.d.ts", | ||
"module": "./lazy.js", | ||
"require": "./cjs/lazy.js", | ||
@@ -599,2 +693,3 @@ "default": "./lazy.js" | ||
"types": "./logger.d.ts", | ||
"module": "./logger.js", | ||
"require": "./cjs/logger.js", | ||
@@ -605,2 +700,3 @@ "default": "./logger.js" | ||
"types": "./memoize.d.ts", | ||
"module": "./memoize.js", | ||
"require": "./cjs/memoize.js", | ||
@@ -611,2 +707,3 @@ "default": "./memoize.js" | ||
"types": "./nextTick.d.ts", | ||
"module": "./nextTick.js", | ||
"require": "./cjs/nextTick.js", | ||
@@ -617,2 +714,3 @@ "default": "./nextTick.js" | ||
"types": "./noop.d.ts", | ||
"module": "./noop.js", | ||
"require": "./cjs/noop.js", | ||
@@ -623,2 +721,3 @@ "default": "./noop.js" | ||
"types": "./number/index.d.ts", | ||
"module": "./number/index.js", | ||
"require": "./cjs/number/index.js", | ||
@@ -629,2 +728,3 @@ "default": "./number/index.js" | ||
"types": "./number/toHex.d.ts", | ||
"module": "./number/toHex.js", | ||
"require": "./cjs/number/toHex.js", | ||
@@ -635,2 +735,3 @@ "default": "./number/toHex.js" | ||
"types": "./number/toU8a.d.ts", | ||
"module": "./number/toU8a.js", | ||
"require": "./cjs/number/toU8a.js", | ||
@@ -641,2 +742,3 @@ "default": "./number/toU8a.js" | ||
"types": "./object/index.d.ts", | ||
"module": "./object/index.js", | ||
"require": "./cjs/object/index.js", | ||
@@ -647,2 +749,3 @@ "default": "./object/index.js" | ||
"types": "./object/clear.d.ts", | ||
"module": "./object/clear.js", | ||
"require": "./cjs/object/clear.js", | ||
@@ -653,2 +756,3 @@ "default": "./object/clear.js" | ||
"types": "./object/copy.d.ts", | ||
"module": "./object/copy.js", | ||
"require": "./cjs/object/copy.js", | ||
@@ -659,2 +763,3 @@ "default": "./object/copy.js" | ||
"types": "./object/entries.d.ts", | ||
"module": "./object/entries.js", | ||
"require": "./cjs/object/entries.js", | ||
@@ -665,2 +770,3 @@ "default": "./object/entries.js" | ||
"types": "./object/keys.d.ts", | ||
"module": "./object/keys.js", | ||
"require": "./cjs/object/keys.js", | ||
@@ -671,2 +777,3 @@ "default": "./object/keys.js" | ||
"types": "./object/property.d.ts", | ||
"module": "./object/property.js", | ||
"require": "./cjs/object/property.js", | ||
@@ -677,2 +784,3 @@ "default": "./object/property.js" | ||
"types": "./object/spread.d.ts", | ||
"module": "./object/spread.js", | ||
"require": "./cjs/object/spread.js", | ||
@@ -683,2 +791,3 @@ "default": "./object/spread.js" | ||
"types": "./object/values.d.ts", | ||
"module": "./object/values.js", | ||
"require": "./cjs/object/values.js", | ||
@@ -693,2 +802,3 @@ "default": "./object/values.js" | ||
"types": "./packageInfo.d.ts", | ||
"module": "./packageInfo.js", | ||
"require": "./cjs/packageInfo.js", | ||
@@ -699,2 +809,3 @@ "default": "./packageInfo.js" | ||
"types": "./packageInfo.d.ts", | ||
"module": "./packageInfo.js", | ||
"require": "./cjs/packageInfo.js", | ||
@@ -705,2 +816,3 @@ "default": "./packageInfo.js" | ||
"types": "./promisify.d.ts", | ||
"module": "./promisify.js", | ||
"require": "./cjs/promisify.js", | ||
@@ -711,2 +823,3 @@ "default": "./promisify.js" | ||
"types": "./string/index.d.ts", | ||
"module": "./string/index.js", | ||
"require": "./cjs/string/index.js", | ||
@@ -717,2 +830,3 @@ "default": "./string/index.js" | ||
"types": "./string/camelCase.d.ts", | ||
"module": "./string/camelCase.js", | ||
"require": "./cjs/string/camelCase.js", | ||
@@ -723,2 +837,3 @@ "default": "./string/camelCase.js" | ||
"types": "./string/lowerFirst.d.ts", | ||
"module": "./string/lowerFirst.js", | ||
"require": "./cjs/string/lowerFirst.js", | ||
@@ -729,2 +844,3 @@ "default": "./string/lowerFirst.js" | ||
"types": "./string/shorten.d.ts", | ||
"module": "./string/shorten.js", | ||
"require": "./cjs/string/shorten.js", | ||
@@ -735,2 +851,3 @@ "default": "./string/shorten.js" | ||
"types": "./string/toHex.d.ts", | ||
"module": "./string/toHex.js", | ||
"require": "./cjs/string/toHex.js", | ||
@@ -741,2 +858,3 @@ "default": "./string/toHex.js" | ||
"types": "./string/toU8a.d.ts", | ||
"module": "./string/toU8a.js", | ||
"require": "./cjs/string/toU8a.js", | ||
@@ -747,2 +865,3 @@ "default": "./string/toU8a.js" | ||
"types": "./stringify.d.ts", | ||
"module": "./stringify.js", | ||
"require": "./cjs/stringify.js", | ||
@@ -753,2 +872,3 @@ "default": "./stringify.js" | ||
"types": "./types.d.ts", | ||
"module": "./types.js", | ||
"require": "./cjs/types.js", | ||
@@ -759,2 +879,3 @@ "default": "./types.js" | ||
"types": "./u8a/index.d.ts", | ||
"module": "./u8a/index.js", | ||
"require": "./cjs/u8a/index.js", | ||
@@ -765,2 +886,3 @@ "default": "./u8a/index.js" | ||
"types": "./u8a/cmp.d.ts", | ||
"module": "./u8a/cmp.js", | ||
"require": "./cjs/u8a/cmp.js", | ||
@@ -771,2 +893,3 @@ "default": "./u8a/cmp.js" | ||
"types": "./u8a/concat.d.ts", | ||
"module": "./u8a/concat.js", | ||
"require": "./cjs/u8a/concat.js", | ||
@@ -777,2 +900,3 @@ "default": "./u8a/concat.js" | ||
"types": "./u8a/concatBuffer.d.ts", | ||
"module": "./u8a/concatBuffer.js", | ||
"require": "./cjs/u8a/concatBuffer.js", | ||
@@ -783,2 +907,3 @@ "default": "./u8a/concatBuffer.js" | ||
"types": "./u8a/empty.d.ts", | ||
"module": "./u8a/empty.js", | ||
"require": "./cjs/u8a/empty.js", | ||
@@ -789,2 +914,3 @@ "default": "./u8a/empty.js" | ||
"types": "./u8a/eq.d.ts", | ||
"module": "./u8a/eq.js", | ||
"require": "./cjs/u8a/eq.js", | ||
@@ -795,2 +921,3 @@ "default": "./u8a/eq.js" | ||
"types": "./u8a/fixLength.d.ts", | ||
"module": "./u8a/fixLength.js", | ||
"require": "./cjs/u8a/fixLength.js", | ||
@@ -801,2 +928,3 @@ "default": "./u8a/fixLength.js" | ||
"types": "./u8a/sorted.d.ts", | ||
"module": "./u8a/sorted.js", | ||
"require": "./cjs/u8a/sorted.js", | ||
@@ -807,2 +935,3 @@ "default": "./u8a/sorted.js" | ||
"types": "./u8a/toBigInt.d.ts", | ||
"module": "./u8a/toBigInt.js", | ||
"require": "./cjs/u8a/toBigInt.js", | ||
@@ -813,2 +942,3 @@ "default": "./u8a/toBigInt.js" | ||
"types": "./u8a/toBn.d.ts", | ||
"module": "./u8a/toBn.js", | ||
"require": "./cjs/u8a/toBn.js", | ||
@@ -819,2 +949,3 @@ "default": "./u8a/toBn.js" | ||
"types": "./u8a/toBuffer.d.ts", | ||
"module": "./u8a/toBuffer.js", | ||
"require": "./cjs/u8a/toBuffer.js", | ||
@@ -825,2 +956,3 @@ "default": "./u8a/toBuffer.js" | ||
"types": "./u8a/toFloat.d.ts", | ||
"module": "./u8a/toFloat.js", | ||
"require": "./cjs/u8a/toFloat.js", | ||
@@ -831,2 +963,3 @@ "default": "./u8a/toFloat.js" | ||
"types": "./u8a/toHex.d.ts", | ||
"module": "./u8a/toHex.js", | ||
"node": { | ||
@@ -841,2 +974,3 @@ "require": "./cjs/u8a/toHexBuffer.js", | ||
"types": "./u8a/toHexBuffer.d.ts", | ||
"module": "./u8a/toHexBuffer.js", | ||
"require": "./cjs/u8a/toHexBuffer.js", | ||
@@ -847,2 +981,3 @@ "default": "./u8a/toHexBuffer.js" | ||
"types": "./u8a/toNumber.d.ts", | ||
"module": "./u8a/toNumber.js", | ||
"require": "./cjs/u8a/toNumber.js", | ||
@@ -853,2 +988,3 @@ "default": "./u8a/toNumber.js" | ||
"types": "./u8a/toString.d.ts", | ||
"module": "./u8a/toString.js", | ||
"require": "./cjs/u8a/toString.js", | ||
@@ -859,2 +995,3 @@ "default": "./u8a/toString.js" | ||
"types": "./u8a/toU8a.d.ts", | ||
"module": "./u8a/toU8a.js", | ||
"require": "./cjs/u8a/toU8a.js", | ||
@@ -865,2 +1002,3 @@ "default": "./u8a/toU8a.js" | ||
"types": "./u8a/wrap.d.ts", | ||
"module": "./u8a/wrap.js", | ||
"require": "./cjs/u8a/wrap.js", | ||
@@ -871,2 +1009,3 @@ "default": "./u8a/wrap.js" | ||
"types": "./versionDetect.d.ts", | ||
"module": "./versionDetect.js", | ||
"require": "./cjs/versionDetect.js", | ||
@@ -877,6 +1016,6 @@ "default": "./versionDetect.js" | ||
"dependencies": { | ||
"@polkadot/x-bigint": "12.1.2", | ||
"@polkadot/x-global": "12.1.2", | ||
"@polkadot/x-textdecoder": "12.1.2", | ||
"@polkadot/x-textencoder": "12.1.2", | ||
"@polkadot/x-bigint": "12.2.1", | ||
"@polkadot/x-global": "12.2.1", | ||
"@polkadot/x-textdecoder": "12.2.1", | ||
"@polkadot/x-textencoder": "12.2.1", | ||
"@types/bn.js": "^5.1.1", | ||
@@ -883,0 +1022,0 @@ "bn.js": "^5.2.1", |
@@ -1,1 +0,1 @@ | ||
export const packageInfo = { name: '@polkadot/util', path: (import.meta && import.meta.url) ? new URL(import.meta.url).pathname.substring(0, new URL(import.meta.url).pathname.lastIndexOf('/') + 1) : 'auto', type: 'esm', version: '12.1.2' }; | ||
export const packageInfo = { name: '@polkadot/util', path: (import.meta && import.meta.url) ? new URL(import.meta.url).pathname.substring(0, new URL(import.meta.url).pathname.lastIndexOf('/') + 1) : 'auto', type: 'esm', version: '12.2.1' }; |
/// <reference types="bn.js" /> | ||
import type { BN } from './bn/bn.js'; | ||
export interface Constructor<T extends object = object> { | ||
export interface Constructor<T = any> { | ||
prototype: T; | ||
@@ -5,0 +5,0 @@ new (...args: any[]): T; |
@@ -5,2 +5,3 @@ import { BigInt } from '@polkadot/x-bigint'; | ||
const U16_MAX = BigInt(256 * 256); | ||
const U64_MAX = BigInt('0x10000000000000000'); | ||
/** | ||
@@ -11,15 +12,24 @@ * @name u8aToBigInt | ||
export function u8aToBigInt(value, { isLe = true, isNegative = false } = {}) { | ||
if (!value || !value.length) { | ||
return BigInt(0); | ||
} | ||
// BE is not the optimal path - LE is the SCALE default | ||
const u8a = isLe | ||
? value | ||
: value.reverse(); | ||
const dvI = new DataView(u8a.buffer, u8a.byteOffset); | ||
: value.slice().reverse(); | ||
const count = u8a.length; | ||
const mod = count % 2; | ||
let result = BigInt(0); | ||
// This is mostly written for readability (with the single isNegative shortcut), | ||
// as opposed to performance, e.g. `u8aToBn` does loop unrolling, etc. | ||
if (isNegative) { | ||
switch (count) { | ||
case 0: | ||
return BigInt(0); | ||
case 1: | ||
return BigInt(((u8a[0] ^ 255) * -1) - 1); | ||
case 2: | ||
return BigInt((((u8a[0] + (u8a[1] << 8)) ^ 65535) * -1) - 1); | ||
case 4: | ||
return BigInt((((u8a[0] + (u8a[1] << 8) + (u8a[2] << 16) + (u8a[3] * 16777216)) ^ 4294967295) * -1) - 1); | ||
} | ||
const dvI = new DataView(u8a.buffer, u8a.byteOffset); | ||
if (count === 8) { | ||
return dvI.getBigInt64(0, true); | ||
} | ||
let result = BigInt(0); | ||
const mod = count % 2; | ||
for (let i = count - 2; i >= mod; i -= 2) { | ||
@@ -31,14 +41,32 @@ result = (result * U16_MAX) + BigInt(dvI.getUint16(i, true) ^ 0xffff); | ||
} | ||
return (result * -_1n) - _1n; | ||
} | ||
else { | ||
for (let i = count - 2; i >= mod; i -= 2) { | ||
result = (result * U16_MAX) + BigInt(dvI.getUint16(i, true)); | ||
switch (count) { | ||
case 0: | ||
return BigInt(0); | ||
case 1: | ||
return BigInt(u8a[0]); | ||
case 2: | ||
return BigInt(u8a[0] + (u8a[1] << 8)); | ||
case 4: | ||
return BigInt(u8a[0] + (u8a[1] << 8) + (u8a[2] << 16) + (u8a[3] * 16777216)); | ||
} | ||
const dvI = new DataView(u8a.buffer, u8a.byteOffset); | ||
switch (count) { | ||
case 8: | ||
return dvI.getBigUint64(0, true); | ||
case 16: | ||
return (dvI.getBigUint64(8, true) * U64_MAX) + dvI.getBigUint64(0, true); | ||
default: { | ||
let result = BigInt(0); | ||
const mod = count % 2; | ||
for (let i = count - 2; i >= mod; i -= 2) { | ||
result = (result * U16_MAX) + BigInt(dvI.getUint16(i, true)); | ||
} | ||
if (mod) { | ||
result = (result * U8_MAX) + BigInt(u8a[0]); | ||
} | ||
return result; | ||
} | ||
if (mod) { | ||
result = (result * U8_MAX) + BigInt(u8a[0]); | ||
} | ||
} | ||
return isNegative | ||
? ((result * -_1n) - _1n) | ||
: result; | ||
} |
124
u8a/toBn.js
@@ -24,49 +24,6 @@ import { BN } from '../bn/bn.js'; | ||
// here seems to be more efficient than passing the full array | ||
if (count <= 6) { | ||
if (isNegative) { | ||
let result = 0; | ||
if (isLe) { | ||
// Most common case i{8, 16, 32} default LE SCALE-encoded | ||
// For <= 32, we also optimize the xor to a single op | ||
// (see the comments around unrolling in the next section) | ||
switch (count) { | ||
case 0: | ||
return new BN(0); | ||
case 1: | ||
result = value[0] ^ 255; | ||
break; | ||
case 2: | ||
result = (value[0] + (value[1] << 8)) ^ 65535; | ||
break; | ||
case 3: | ||
result = (value[0] + (value[1] << 8) + (value[2] << 16)) ^ 16777215; | ||
break; | ||
case 4: | ||
// for the 3rd byte, we don't << 24 - since JS converts all bitwise operators to | ||
// 32-bit, in the case where the top-most bit is set this yields a negative value | ||
result = (value[0] + (value[1] << 8) + (value[2] << 16) + (value[3] * 16777216)) ^ 4294967295; | ||
break; | ||
case 5: | ||
result = ((value[0] + (value[1] << 8) + (value[2] << 16) + (value[3] * 16777216)) ^ 4294967295) + ((value[4] ^ 0xff) * 4294967296); | ||
break; | ||
default: // 6 | ||
result = ((value[0] + (value[1] << 8) + (value[2] << 16) + (value[3] * 16777216)) ^ 4294967295) + (((value[4] + (value[5] << 8)) ^ 65535) * 4294967296); | ||
break; | ||
} | ||
} | ||
else { | ||
for (let i = 0; i < count; i++) { | ||
result = (result * 256) + (value[i] ^ 0xff); | ||
} | ||
} | ||
return count | ||
? new BN((result * -1) - 1) | ||
: new BN(0); | ||
} | ||
else if (isLe) { | ||
// Most common case - u{8, 16, 32} default LE SCALE-encoded | ||
// | ||
// There are some slight benefits in unrolling this specific loop, | ||
// however it comes with diminishing returns since here the actual | ||
// `new BN` does seem to take up the bulk of the time | ||
if (isNegative) { | ||
if (isLe) { | ||
// Most common case i{8, 16, 32} default LE SCALE-encoded | ||
// For <= 32, we also optimize the xor to a single op | ||
switch (count) { | ||
@@ -76,28 +33,69 @@ case 0: | ||
case 1: | ||
return new BN(value[0]); | ||
return new BN(((value[0] ^ 255) * -1) - 1); | ||
case 2: | ||
return new BN(value[0] + (value[1] << 8)); | ||
return new BN((((value[0] + (value[1] << 8)) ^ 65535) * -1) - 1); | ||
case 3: | ||
return new BN(value[0] + (value[1] << 8) + (value[2] << 16)); | ||
return new BN((((value[0] + (value[1] << 8) + (value[2] << 16)) ^ 16777215) * -1) - 1); | ||
case 4: | ||
// for the 3rd byte, we don't << 24 - since JS converts all bitwise operators to | ||
// 32-bit, in the case where the top-most bit is set this yields a negative value | ||
return new BN(value[0] + (value[1] << 8) + (value[2] << 16) + (value[3] * 16777216)); | ||
return new BN((((value[0] + (value[1] << 8) + (value[2] << 16) + (value[3] * 16777216)) ^ 4294967295) * -1) - 1); | ||
case 5: | ||
return new BN(value[0] + (value[1] << 8) + (value[2] << 16) + ((value[3] + (value[4] << 8)) * 16777216)); | ||
default: // 6 | ||
return new BN(value[0] + (value[1] << 8) + (value[2] << 16) + ((value[3] + (value[4] << 8) + (value[5] << 16)) * 16777216)); | ||
return new BN(((((value[0] + (value[1] << 8) + (value[2] << 16) + (value[3] * 16777216)) ^ 4294967295) + ((value[4] ^ 0xff) * 4294967296)) * -1) - 1); | ||
case 6: | ||
return new BN(((((value[0] + (value[1] << 8) + (value[2] << 16) + (value[3] * 16777216)) ^ 4294967295) + (((value[4] + (value[5] << 8)) ^ 65535) * 4294967296)) * -1) - 1); | ||
default: | ||
return new BN(value, 'le').fromTwos(value.length * 8); | ||
} | ||
} | ||
else { | ||
let result = 0; | ||
for (let i = 0; i < count; i++) { | ||
result = (result * 256) + value[i]; | ||
} | ||
return new BN(result); | ||
if (count === 0) { | ||
return new BN(0); | ||
} | ||
else if (count > 6) { | ||
return new BN(value, 'be').fromTwos(value.length * 8); | ||
} | ||
let result = 0; | ||
for (let i = 0; i < count; i++) { | ||
result = (result * 256) + (value[i] ^ 0xff); | ||
} | ||
return new BN((result * -1) - 1); | ||
} | ||
return isNegative | ||
? new BN(value, isLe ? 'le' : 'be').fromTwos(value.length * 8) | ||
: new BN(value, isLe ? 'le' : 'be'); | ||
if (isLe) { | ||
// Most common case - u{8, 16, 32} default LE SCALE-encoded | ||
// | ||
// There are some slight benefits in unrolling this specific loop, | ||
// however it comes with diminishing returns since here the actual | ||
// `new BN` does seem to take up the bulk of the time | ||
switch (count) { | ||
case 0: | ||
return new BN(0); | ||
case 1: | ||
return new BN(value[0]); | ||
case 2: | ||
return new BN(value[0] + (value[1] << 8)); | ||
case 3: | ||
return new BN(value[0] + (value[1] << 8) + (value[2] << 16)); | ||
case 4: | ||
// for the 3rd byte, we don't << 24 - since JS converts all bitwise operators to | ||
// 32-bit, in the case where the top-most bit is set this yields a negative value | ||
return new BN(value[0] + (value[1] << 8) + (value[2] << 16) + (value[3] * 16777216)); | ||
case 5: | ||
return new BN(value[0] + (value[1] << 8) + (value[2] << 16) + ((value[3] + (value[4] << 8)) * 16777216)); | ||
case 6: | ||
return new BN(value[0] + (value[1] << 8) + (value[2] << 16) + ((value[3] + (value[4] << 8) + (value[5] << 16)) * 16777216)); | ||
default: | ||
return new BN(value, 'le'); | ||
} | ||
} | ||
if (count === 0) { | ||
return new BN(0); | ||
} | ||
else if (count > 6) { | ||
return new BN(value, 'be'); | ||
} | ||
let result = 0; | ||
for (let i = 0; i < count; i++) { | ||
result = (result * 256) + value[i]; | ||
} | ||
return new BN(result); | ||
} |
@@ -9,5 +9,5 @@ interface ToNumberOptions { | ||
* @name u8aToNumber | ||
* @summary Creates a number from a Uint8Array object. | ||
* @summary Creates a number from a Uint8Array object. This only operates on LE values as used in SCALE. | ||
*/ | ||
export declare function u8aToNumber(value: Uint8Array, { isNegative }?: ToNumberOptions): number; | ||
export {}; |
/** | ||
* @name u8aToNumber | ||
* @summary Creates a number from a Uint8Array object. | ||
* @summary Creates a number from a Uint8Array object. This only operates on LE values as used in SCALE. | ||
*/ | ||
@@ -8,3 +8,2 @@ export function u8aToNumber(value, { isNegative = false } = {}) { | ||
if (isNegative) { | ||
let result = 0; | ||
switch (count) { | ||
@@ -14,25 +13,18 @@ case 0: | ||
case 1: | ||
result = value[0] ^ 255; | ||
break; | ||
return (((value[0] ^ 255) * -1) - 1); | ||
case 2: | ||
result = (value[0] + (value[1] << 8)) ^ 65535; | ||
break; | ||
return ((((value[0] + (value[1] << 8)) ^ 65535) * -1) - 1); | ||
case 3: | ||
result = (value[0] + (value[1] << 8) + (value[2] << 16)) ^ 16777215; | ||
break; | ||
return ((((value[0] + (value[1] << 8) + (value[2] << 16)) ^ 16777215) * -1) - 1); | ||
case 4: | ||
// for the 3rd byte, we don't << 24 - since JS converts all bitwise operators to | ||
// 32-bit, in the case where the top-most bit is set this yields a negative value | ||
result = (value[0] + (value[1] << 8) + (value[2] << 16) + (value[3] * 16777216)) ^ 4294967295; | ||
break; | ||
return ((((value[0] + (value[1] << 8) + (value[2] << 16) + (value[3] * 16777216)) ^ 4294967295) * -1) - 1); | ||
case 5: | ||
result = ((value[0] + (value[1] << 8) + (value[2] << 16) + (value[3] * 16777216)) ^ 4294967295) + ((value[4] ^ 0xff) * 4294967296); | ||
break; | ||
return (((((value[0] + (value[1] << 8) + (value[2] << 16) + (value[3] * 16777216)) ^ 4294967295) + ((value[4] ^ 0xff) * 4294967296)) * -1) - 1); | ||
case 6: | ||
result = ((value[0] + (value[1] << 8) + (value[2] << 16) + (value[3] * 16777216)) ^ 4294967295) + (((value[4] + (value[5] << 8)) ^ 65535) * 4294967296); | ||
break; | ||
return (((((value[0] + (value[1] << 8) + (value[2] << 16) + (value[3] * 16777216)) ^ 4294967295) + (((value[4] + (value[5] << 8)) ^ 65535) * 4294967296)) * -1) - 1); | ||
default: | ||
throw new Error('Value more than 48-bits cannot be reliably converted'); | ||
} | ||
return (result * -1) - 1; | ||
} | ||
@@ -39,0 +31,0 @@ switch (count) { |
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
544248
14488
+ Added@polkadot/x-bigint@12.2.1(transitive)
+ Added@polkadot/x-global@12.2.1(transitive)
+ Added@polkadot/x-textdecoder@12.2.1(transitive)
+ Added@polkadot/x-textencoder@12.2.1(transitive)
- Removed@polkadot/x-bigint@12.1.2(transitive)
- Removed@polkadot/x-global@12.1.2(transitive)
- Removed@polkadot/x-textdecoder@12.1.2(transitive)
- Removed@polkadot/x-textencoder@12.1.2(transitive)
Updated@polkadot/x-bigint@12.2.1
Updated@polkadot/x-global@12.2.1