@polkadot/util
Advanced tools
Comparing version 9.1.2-10 to 9.1.2-11
@@ -10,8 +10,2 @@ // Copyright 2017-2022 @polkadot/util authors & contributors | ||
function createEmpty({ | ||
bitLength = 0 | ||
}) { | ||
return bitLength === -1 ? new Uint8Array() : new Uint8Array(Math.ceil(bitLength / 8)); | ||
} | ||
function toU8a(value, { | ||
@@ -57,3 +51,3 @@ isLe, | ||
if (valueBi === _0n) { | ||
return createEmpty(opts); | ||
return opts.bitLength === -1 ? new Uint8Array() : new Uint8Array(Math.ceil((opts.bitLength || 0) / 8)); | ||
} | ||
@@ -60,0 +54,0 @@ |
@@ -27,3 +27,3 @@ // Copyright 2017-2022 @polkadot/util authors & contributors | ||
export function bnToBn(value) { | ||
return BN.isBN(value) ? value : !value ? new BN(0) : isHex(value) ? hexToBn(value.toString()) : isBigInt(value) ? new BN(value.toString()) : isToBn(value) ? value.toBn() : isToBigInt(value) ? new BN(value.toBigInt().toString()) : new BN(value); | ||
return value ? BN.isBN(value) ? value : isHex(value) ? hexToBn(value.toString()) : isBigInt(value) ? new BN(value.toString()) : isToBn(value) ? value.toBn() : isToBigInt(value) ? new BN(value.toBigInt().toString()) : new BN(value) : new BN(0); | ||
} |
@@ -20,3 +20,4 @@ /// <reference types="bn.js" /> | ||
declare function bnToHex<ExtToBn extends ToBn>(value?: ExtToBn | BN | bigint | number | null, options?: NumberOptions): HexString; | ||
/** @deprecated Use bnToHex (value?: ExtToBn | BN | bigint | number | null, options?: NumberOptions) */ | ||
declare function bnToHex<ExtToBn extends ToBn>(value?: ExtToBn | BN | bigint | number | null, bitLength?: number, isLe?: boolean): HexString; | ||
export { bnToHex }; |
@@ -29,2 +29,3 @@ // Copyright 2017-2022 @polkadot/util authors & contributors | ||
/** @deprecated Use bnToHex (value?: ExtToBn | BN | bigint | number | null, options?: NumberOptions) */ | ||
function bnToHex(value, arg1 = DEFAULT_OPTS, arg2) { | ||
@@ -31,0 +32,0 @@ return !value ? ZERO_STR : u8aToHex(bnToU8a(value, objectSpread( // We spread here, the default for hex values is BE (JSONRPC via substrate) |
@@ -11,16 +11,2 @@ // Copyright 2017-2022 @polkadot/util authors & contributors | ||
}; | ||
function createEmpty(byteLength, options) { | ||
return options.bitLength === -1 ? new Uint8Array() : new Uint8Array(byteLength); | ||
} | ||
function createValue(valueBn, byteLength, { | ||
isLe, | ||
isNegative | ||
}) { | ||
const output = new Uint8Array(byteLength); | ||
const bn = isNegative ? valueBn.toTwos(byteLength * 8) : valueBn; | ||
output.set(bn.toArray(isLe ? 'le' : 'be', byteLength), 0); | ||
return output; | ||
} | ||
/** | ||
@@ -41,6 +27,9 @@ * @name bnToU8a | ||
/** @deprecated Use bnToU8a(value?: ExtToBn | BN | bigint | number | null, options?: NumberOptions) */ | ||
function bnToU8a(value, arg1 = DEFAULT_OPTS, arg2) { | ||
const options = objectSpread({ | ||
const { | ||
bitLength, | ||
isLe, | ||
isNegative | ||
} = objectSpread({ | ||
bitLength: -1, | ||
@@ -54,6 +43,14 @@ isLe: true, | ||
const valueBn = bnToBn(value); | ||
const byteLength = options.bitLength === -1 ? Math.ceil(valueBn.bitLength() / 8) : Math.ceil((options.bitLength || 0) / 8); | ||
return value ? createValue(valueBn, byteLength, options) : createEmpty(byteLength, options); | ||
const byteLength = bitLength === -1 ? Math.ceil(valueBn.bitLength() / 8) : Math.ceil((bitLength || 0) / 8); | ||
if (!value) { | ||
return bitLength === -1 ? new Uint8Array() : new Uint8Array(byteLength); | ||
} | ||
const output = new Uint8Array(byteLength); | ||
const bn = isNegative ? valueBn.toTwos(byteLength * 8) : valueBn; | ||
output.set(bn.toArray(isLe ? 'le' : 'be', byteLength), 0); | ||
return output; | ||
} | ||
export { bnToU8a }; |
@@ -21,14 +21,7 @@ "use strict"; | ||
function createEmpty(_ref) { | ||
function toU8a(value, _ref) { | ||
let { | ||
bitLength = 0 | ||
} = _ref; | ||
return bitLength === -1 ? new Uint8Array() : new Uint8Array(Math.ceil(bitLength / 8)); | ||
} | ||
function toU8a(value, _ref2) { | ||
let { | ||
isLe, | ||
isNegative | ||
} = _ref2; | ||
} = _ref; | ||
const arr = []; | ||
@@ -70,3 +63,3 @@ | ||
if (valueBi === _consts._0n) { | ||
return createEmpty(opts); | ||
return opts.bitLength === -1 ? new Uint8Array() : new Uint8Array(Math.ceil((opts.bitLength || 0) / 8)); | ||
} | ||
@@ -73,0 +66,0 @@ |
@@ -40,3 +40,3 @@ "use strict"; | ||
function bnToBn(value) { | ||
return _bn.BN.isBN(value) ? value : !value ? new _bn.BN(0) : (0, _hex.isHex)(value) ? (0, _toBn.hexToBn)(value.toString()) : (0, _bigInt.isBigInt)(value) ? new _bn.BN(value.toString()) : (0, _toBn2.isToBn)(value) ? value.toBn() : (0, _toBigInt.isToBigInt)(value) ? new _bn.BN(value.toBigInt().toString()) : new _bn.BN(value); | ||
return value ? _bn.BN.isBN(value) ? value : (0, _hex.isHex)(value) ? (0, _toBn.hexToBn)(value.toString()) : (0, _bigInt.isBigInt)(value) ? new _bn.BN(value.toString()) : (0, _toBn2.isToBn)(value) ? value.toBn() : (0, _toBigInt.isToBigInt)(value) ? new _bn.BN(value.toBigInt().toString()) : new _bn.BN(value) : new _bn.BN(0); | ||
} |
@@ -40,2 +40,3 @@ "use strict"; | ||
/** @deprecated Use bnToHex (value?: ExtToBn | BN | bigint | number | null, options?: NumberOptions) */ | ||
function bnToHex(value) { | ||
@@ -42,0 +43,0 @@ let arg1 = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : DEFAULT_OPTS; |
@@ -21,17 +21,2 @@ "use strict"; | ||
}; | ||
function createEmpty(byteLength, options) { | ||
return options.bitLength === -1 ? new Uint8Array() : new Uint8Array(byteLength); | ||
} | ||
function createValue(valueBn, byteLength, _ref) { | ||
let { | ||
isLe, | ||
isNegative | ||
} = _ref; | ||
const output = new Uint8Array(byteLength); | ||
const bn = isNegative ? valueBn.toTwos(byteLength * 8) : valueBn; | ||
output.set(bn.toArray(isLe ? 'le' : 'be', byteLength), 0); | ||
return output; | ||
} | ||
/** | ||
@@ -52,3 +37,2 @@ * @name bnToU8a | ||
/** @deprecated Use bnToU8a(value?: ExtToBn | BN | bigint | number | null, options?: NumberOptions) */ | ||
@@ -58,3 +42,7 @@ function bnToU8a(value) { | ||
let arg2 = arguments.length > 2 ? arguments[2] : undefined; | ||
const options = (0, _spread.objectSpread)({ | ||
const { | ||
bitLength, | ||
isLe, | ||
isNegative | ||
} = (0, _spread.objectSpread)({ | ||
bitLength: -1, | ||
@@ -68,4 +56,12 @@ isLe: true, | ||
const valueBn = (0, _toBn.bnToBn)(value); | ||
const byteLength = options.bitLength === -1 ? Math.ceil(valueBn.bitLength() / 8) : Math.ceil((options.bitLength || 0) / 8); | ||
return value ? createValue(valueBn, byteLength, options) : createEmpty(byteLength, options); | ||
const byteLength = bitLength === -1 ? Math.ceil(valueBn.bitLength() / 8) : Math.ceil((bitLength || 0) / 8); | ||
if (!value) { | ||
return bitLength === -1 ? new Uint8Array() : new Uint8Array(byteLength); | ||
} | ||
const output = new Uint8Array(byteLength); | ||
const bn = isNegative ? valueBn.toTwos(byteLength * 8) : valueBn; | ||
output.set(bn.toArray(isLe ? 'le' : 'be', byteLength), 0); | ||
return output; | ||
} |
@@ -31,16 +31,16 @@ "use strict"; | ||
const u8a = (0, _u8a.u8aToU8a)(input); | ||
const flag = u8a[0] & 0b11; | ||
const flag = u8a[0] & 0b11; // The u8a is manually converted here, it is 2x faster that doing an | ||
// additional call to u8aToBn | ||
if (flag === 0b00) { | ||
return [1, new _bn.BN(u8a[0]).ishrn(2)]; | ||
return [1, new _bn.BN(u8a[0] >>> 2)]; | ||
} else if (flag === 0b01) { | ||
return [2, (0, _u8a.u8aToBn)(u8a.subarray(0, 2), true).ishrn(2)]; | ||
return [2, new _bn.BN(u8a[0] + u8a[1] * 0x100 >>> 2)]; | ||
} else if (flag === 0b10) { | ||
return [4, (0, _u8a.u8aToBn)(u8a.subarray(0, 4), true).ishrn(2)]; | ||
} | ||
return [4, new _bn.BN(u8a[0] + u8a[1] * 0x100 + u8a[2] * 0x10000 + u8a[3] * 0x1000000 >>> 2)]; | ||
} // add 5 to shifted (4 for base length, 1 for this byte) | ||
const offset = new _bn.BN(u8a[0]).ishrn(2) // clear flag | ||
.iadd(_bn.BN_FIVE) // add 4 for base length, 1 for this byte | ||
.toNumber(); | ||
return [offset, (0, _u8a.u8aToBn)(u8a.subarray(1, offset), true)]; | ||
const offset = (u8a[0] >>> 2) + 5; | ||
return [offset, (0, _u8a.u8aToBn)(u8a.subarray(1, offset))]; | ||
} |
@@ -21,2 +21,9 @@ "use strict"; | ||
const MAX_U32 = _bn.BN_TWO.pow(new _bn.BN(32 - 2)).isub(_bn.BN_ONE); | ||
const BL_16 = { | ||
bitLength: 16 | ||
}; | ||
const BL_32 = { | ||
bitLength: 32 | ||
}; | ||
/** | ||
@@ -35,3 +42,2 @@ * @name compactToU8a | ||
function compactToU8a(value) { | ||
@@ -43,5 +49,5 @@ const bn = (0, _bn.bnToBn)(value); | ||
} else if (bn.lte(MAX_U16)) { | ||
return (0, _bn.bnToU8a)(bn.shln(2).iadd(_bn.BN_ONE), 16, true); | ||
return (0, _bn.bnToU8a)(bn.shln(2).iadd(_bn.BN_ONE), BL_16); | ||
} else if (bn.lte(MAX_U32)) { | ||
return (0, _bn.bnToU8a)(bn.shln(2).iadd(_bn.BN_TWO), 32, true); | ||
return (0, _bn.bnToU8a)(bn.shln(2).iadd(_bn.BN_TWO), BL_32); | ||
} | ||
@@ -48,0 +54,0 @@ |
@@ -14,4 +14,4 @@ "use strict"; | ||
type: 'cjs', | ||
version: '9.1.2-10' | ||
version: '9.1.2-11' | ||
}; | ||
exports.packageInfo = packageInfo; |
@@ -37,3 +37,3 @@ "use strict"; | ||
function u8aToU8a(value) { | ||
return !value ? new Uint8Array() : Array.isArray(value) || (0, _buffer.isBuffer)(value) ? new Uint8Array(value) : (0, _u8a.isU8a)(value) ? value : (0, _hex.isHex)(value) ? (0, _toU8a.hexToU8a)(value) : (0, _toU8a2.stringToU8a)(value); | ||
return value ? Array.isArray(value) || (0, _buffer.isBuffer)(value) ? new Uint8Array(value) : (0, _u8a.isU8a)(value) ? value : (0, _hex.isHex)(value) ? (0, _toU8a.hexToU8a)(value) : (0, _toU8a2.stringToU8a)(value) : new Uint8Array(); | ||
} |
// Copyright 2017-2022 @polkadot/util authors & contributors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
import { BN, BN_FIVE } from "../bn/index.js"; | ||
import { BN } from "../bn/index.js"; | ||
import { u8aToBn, u8aToU8a } from "../u8a/index.js"; | ||
@@ -22,16 +22,16 @@ /** | ||
const u8a = u8aToU8a(input); | ||
const flag = u8a[0] & 0b11; | ||
const flag = u8a[0] & 0b11; // The u8a is manually converted here, it is 2x faster that doing an | ||
// additional call to u8aToBn | ||
if (flag === 0b00) { | ||
return [1, new BN(u8a[0]).ishrn(2)]; | ||
return [1, new BN(u8a[0] >>> 2)]; | ||
} else if (flag === 0b01) { | ||
return [2, u8aToBn(u8a.subarray(0, 2), true).ishrn(2)]; | ||
return [2, new BN(u8a[0] + u8a[1] * 0x100 >>> 2)]; | ||
} else if (flag === 0b10) { | ||
return [4, u8aToBn(u8a.subarray(0, 4), true).ishrn(2)]; | ||
} | ||
return [4, new BN(u8a[0] + u8a[1] * 0x100 + u8a[2] * 0x10000 + u8a[3] * 0x1000000 >>> 2)]; | ||
} // add 5 to shifted (4 for base length, 1 for this byte) | ||
const offset = new BN(u8a[0]).ishrn(2) // clear flag | ||
.iadd(BN_FIVE) // add 4 for base length, 1 for this byte | ||
.toNumber(); | ||
return [offset, u8aToBn(u8a.subarray(1, offset), true)]; | ||
const offset = (u8a[0] >>> 2) + 5; | ||
return [offset, u8aToBn(u8a.subarray(1, offset))]; | ||
} |
@@ -9,2 +9,8 @@ // Copyright 2017-2022 @polkadot/util authors & contributors | ||
const MAX_U32 = BN_TWO.pow(new BN(32 - 2)).isub(BN_ONE); | ||
const BL_16 = { | ||
bitLength: 16 | ||
}; | ||
const BL_32 = { | ||
bitLength: 32 | ||
}; | ||
/** | ||
@@ -29,5 +35,5 @@ * @name compactToU8a | ||
} else if (bn.lte(MAX_U16)) { | ||
return bnToU8a(bn.shln(2).iadd(BN_ONE), 16, true); | ||
return bnToU8a(bn.shln(2).iadd(BN_ONE), BL_16); | ||
} else if (bn.lte(MAX_U32)) { | ||
return bnToU8a(bn.shln(2).iadd(BN_TWO), 32, true); | ||
return bnToU8a(bn.shln(2).iadd(BN_TWO), BL_32); | ||
} | ||
@@ -34,0 +40,0 @@ |
@@ -23,3 +23,3 @@ { | ||
"type": "module", | ||
"version": "9.1.2-10", | ||
"version": "9.1.2-11", | ||
"main": "./cjs/index.js", | ||
@@ -693,6 +693,6 @@ "module": "./index.js", | ||
"@babel/runtime": "^7.17.9", | ||
"@polkadot/x-bigint": "9.1.2-10", | ||
"@polkadot/x-global": "9.1.2-10", | ||
"@polkadot/x-textdecoder": "9.1.2-10", | ||
"@polkadot/x-textencoder": "9.1.2-10", | ||
"@polkadot/x-bigint": "9.1.2-11", | ||
"@polkadot/x-global": "9.1.2-11", | ||
"@polkadot/x-textdecoder": "9.1.2-11", | ||
"@polkadot/x-textencoder": "9.1.2-11", | ||
"@types/bn.js": "^5.1.0", | ||
@@ -699,0 +699,0 @@ "bn.js": "^5.2.0", |
@@ -8,3 +8,3 @@ // Copyright 2017-2022 @polkadot/util authors & contributors | ||
type: 'esm', | ||
version: '9.1.2-10' | ||
version: '9.1.2-11' | ||
}; |
@@ -25,3 +25,3 @@ // Copyright 2017-2022 @polkadot/util authors & contributors | ||
export function u8aToU8a(value) { | ||
return !value ? new Uint8Array() : Array.isArray(value) || isBuffer(value) ? new Uint8Array(value) : isU8a(value) ? value : isHex(value) ? hexToU8a(value) : stringToU8a(value); | ||
return value ? Array.isArray(value) || isBuffer(value) ? new Uint8Array(value) : isU8a(value) ? value : isHex(value) ? hexToU8a(value) : stringToU8a(value) : new Uint8Array(); | ||
} |
Sorry, the diff of this file is too big to display
492556
14189
+ Added@polkadot/x-bigint@9.1.2-11(transitive)
+ Added@polkadot/x-global@9.1.2-11(transitive)
+ Added@polkadot/x-textdecoder@9.1.2-11(transitive)
+ Added@polkadot/x-textencoder@9.1.2-11(transitive)
- Removed@polkadot/x-bigint@9.1.2-10(transitive)
- Removed@polkadot/x-global@9.1.2-10(transitive)
- Removed@polkadot/x-textdecoder@9.1.2-10(transitive)
- Removed@polkadot/x-textencoder@9.1.2-10(transitive)
Updated@polkadot/x-bigint@9.1.2-11
Updated@polkadot/x-global@9.1.2-11