Re-use pre-define BN_{BE, LE}_* constants (#1175)
* Re-use pre-define BN_{BE, LE}_* constants
* ordering update
* bnMax/bnMin opt
* u8aToHex flatten
This commit is contained in:
@@ -15,4 +15,16 @@ describe('bnMax', (): void => {
|
||||
bnMax(new BN(2), new BN(3), new BN(1))
|
||||
).toEqual(new BN(3));
|
||||
});
|
||||
|
||||
it('returns a single item', (): void => {
|
||||
expect(
|
||||
bnMax(new BN(1))
|
||||
).toEqual(new BN(1));
|
||||
});
|
||||
|
||||
it('fails when no items are available', (): void => {
|
||||
expect(
|
||||
() => bnMax()
|
||||
).toThrow(/Must provide one or more BN arguments/);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,9 +1,20 @@
|
||||
// Copyright 2017-2021 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { BN } from './bn';
|
||||
import { assert } from '../assert';
|
||||
import { BN } from './bn';
|
||||
|
||||
import { checkMaxMin } from './util';
|
||||
export function checkMaxMin (type: 'max' | 'min', items: BN[]): BN {
|
||||
assert(items.length >= 1, 'Must provide one or more BN arguments');
|
||||
|
||||
let result = items[0];
|
||||
|
||||
for (let i = 1; i < items.length; i++) {
|
||||
result = BN[type](result, items[i]);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @name bnMax
|
||||
|
||||
@@ -9,4 +9,10 @@ describe('bnMin', (): void => {
|
||||
bnMin(new BN(2), new BN(1), new BN(3))
|
||||
).toEqual(new BN(1));
|
||||
});
|
||||
|
||||
it('returns a single item', (): void => {
|
||||
expect(
|
||||
bnMin(new BN(1))
|
||||
).toEqual(new BN(1));
|
||||
});
|
||||
});
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
|
||||
import type { BN } from './bn';
|
||||
|
||||
import { checkMaxMin } from './util';
|
||||
import { checkMaxMin } from './max';
|
||||
|
||||
/**
|
||||
* @name bnMin
|
||||
|
||||
@@ -9,12 +9,12 @@ import { objectSpread } from '../object/spread';
|
||||
import { u8aToHex } from '../u8a';
|
||||
import { bnToU8a } from './toU8a';
|
||||
|
||||
const ZERO_STR = '0x00';
|
||||
|
||||
interface Options extends ToBnOptions {
|
||||
bitLength?: number;
|
||||
}
|
||||
|
||||
const ZERO_STR = '0x00';
|
||||
|
||||
/**
|
||||
* @name bnToHex
|
||||
* @summary Creates a hex value from a BN.js bignumber object.
|
||||
|
||||
@@ -1,17 +0,0 @@
|
||||
// Copyright 2017-2021 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { assert } from '../assert';
|
||||
import { BN } from './bn';
|
||||
|
||||
export function checkMaxMin (type: 'max' | 'min', items: BN[]): BN {
|
||||
assert(items.length >= 1, 'Must provide one or more BN arguments');
|
||||
|
||||
let result = items[0];
|
||||
|
||||
for (const v of items) {
|
||||
result = BN[type](result, v);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
@@ -10,7 +10,7 @@ type HexByte = `${HexDigit}${HexDigit}`;
|
||||
const ALPHABET = arrayRange(256).map((n) => n.toString(16).padStart(2, '0') as HexByte);
|
||||
|
||||
/** @internal */
|
||||
function extract (value: Uint8Array): string {
|
||||
function hex (value: Uint8Array): string {
|
||||
const result = new Array<HexByte>(value.length);
|
||||
|
||||
for (let i = 0; i < value.length; i++) {
|
||||
@@ -20,20 +20,6 @@ function extract (value: Uint8Array): string {
|
||||
return result.join('');
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
function unprefixed (value: Uint8Array, bitLength = -1): string {
|
||||
const byteLength = Math.ceil(bitLength / 8);
|
||||
|
||||
return (byteLength > 0 && value.length > byteLength)
|
||||
? trim(value, Math.ceil(byteLength / 2))
|
||||
: extract(value);
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
function trim (value: Uint8Array, halfLength: number): string {
|
||||
return `${unprefixed(value.subarray(0, halfLength))}…${unprefixed(value.subarray(value.length - halfLength))}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* @name u8aToHex
|
||||
* @summary Creates a hex string from a Uint8Array object.
|
||||
@@ -49,9 +35,13 @@ function trim (value: Uint8Array, halfLength: number): string {
|
||||
* ```
|
||||
*/
|
||||
export function u8aToHex (value?: Uint8Array | null, bitLength = -1, isPrefixed = true): HexString {
|
||||
const length = Math.ceil(bitLength / 8);
|
||||
|
||||
return `${isPrefixed ? '0x' : ''}${
|
||||
!value || !value.length
|
||||
? ''
|
||||
: unprefixed(value, bitLength)
|
||||
: (length > 0 && value.length > length)
|
||||
? `${hex(value.subarray(0, length / 2))}…${hex(value.subarray(value.length - length / 2))}`
|
||||
: hex(value)
|
||||
}` as HexString;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user