Update isHex check & toHex with shortening (#129)
* Update isHex check & toHex with shortening * Basic cleanups
This commit is contained in:
@@ -18,5 +18,5 @@ const isHex = require('../is/hex');
|
||||
*/
|
||||
module.exports = function hexHasPrefix (value: ?string): boolean {
|
||||
// flowlint-next-line sketchy-null-string:off
|
||||
return !!(value && isHex(value) && value.substr(0, 2) === '0x');
|
||||
return !!(value && isHex(value, -1, true) && value.substr(0, 2) === '0x');
|
||||
};
|
||||
|
||||
@@ -19,14 +19,14 @@ const HEX_REGEX = /^0x[a-fA-F0-9]+$/;
|
||||
isHex('0x1234'); // => true
|
||||
isHex('0x1234', 8); // => false
|
||||
*/
|
||||
module.exports = function isHex (value: mixed, bitLength: number = -1): boolean {
|
||||
// $FlowFixMe for the regex we have a string
|
||||
module.exports = function isHex (_value: mixed, bitLength: number = -1, ignoreLength: boolean = false): boolean {
|
||||
// flowlint-next-line unclear-type:off
|
||||
const value = ((_value: any): string);
|
||||
const isValidHex = value === '0x' || (isString(value) && HEX_REGEX.test(value));
|
||||
|
||||
if (isValidHex && bitLength !== -1) {
|
||||
// $FlowFixMe type is a string as checked above
|
||||
return value.length === (2 + bitLength / 4);
|
||||
return value.length === (2 + Math.ceil(bitLength / 4));
|
||||
}
|
||||
|
||||
return isValidHex;
|
||||
return isValidHex && (ignoreLength || (value.length % 2 === 0));
|
||||
};
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
const { isHex } = require('./index');
|
||||
|
||||
describe('isHex', () => {
|
||||
const test = '123abcd45';
|
||||
const test = '1234abcd';
|
||||
|
||||
it('returns true on 0x hex values', () => {
|
||||
expect(
|
||||
@@ -19,6 +19,12 @@ describe('isHex', () => {
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('returns false on hex values (non % 2)', () => {
|
||||
expect(
|
||||
isHex(`0x${test}0`)
|
||||
).toEqual(false);
|
||||
});
|
||||
|
||||
it('returns true on uppercase values', () => {
|
||||
expect(
|
||||
isHex(`0x${test.toUpperCase()}`)
|
||||
|
||||
@@ -13,7 +13,6 @@ const u8aFromString = require('./fromString');
|
||||
const u8aToBn = require('./toBn');
|
||||
const u8aToBuffer = require('./toBuffer');
|
||||
const u8aToHex = require('./toHex');
|
||||
const u8aToHexShort = require('./toHexShort');
|
||||
const u8aToString = require('./toString');
|
||||
const u8aToU8a = require('./toU8a');
|
||||
const u8aToUtf8 = require('./toUtf8');
|
||||
@@ -32,7 +31,6 @@ module.exports = {
|
||||
u8aToBn,
|
||||
u8aToBuffer,
|
||||
u8aToHex,
|
||||
u8aToHexShort,
|
||||
u8aToString,
|
||||
u8aToU8a,
|
||||
u8aToUtf8
|
||||
|
||||
@@ -14,12 +14,24 @@
|
||||
|
||||
u8aToHex(new Uint8Array([0x68, 0x65, 0x6c, 0x6c, 0xf])); // 0x68656c0f
|
||||
*/
|
||||
module.exports = function u8aToHex (value?: Uint8Array): string {
|
||||
module.exports = function u8aToHex (value?: Uint8Array, bitLength: number = -1, isPrefixed: boolean = true): string {
|
||||
const prefix = isPrefixed
|
||||
? '0x'
|
||||
: '';
|
||||
|
||||
if (!value || !value.length) {
|
||||
return '0x';
|
||||
return prefix;
|
||||
}
|
||||
|
||||
const byteLength = Math.ceil(bitLength / 8);
|
||||
|
||||
if (byteLength > 0 && value.length > byteLength) {
|
||||
const halfLength = Math.ceil(byteLength / 2);
|
||||
|
||||
return `${u8aToHex(value.subarray(0, halfLength), -1, isPrefixed)}…${u8aToHex(value.subarray(value.length - halfLength), -1, false)}`;
|
||||
}
|
||||
|
||||
return value.reduce((result, item) => {
|
||||
return result + ('0' + item.toString(16)).slice(-2);
|
||||
}, '0x');
|
||||
return result + `0${item.toString(16)}`.slice(-2);
|
||||
}, prefix);
|
||||
};
|
||||
|
||||
@@ -11,6 +11,12 @@ describe('u8aToHex', () => {
|
||||
).toEqual('0x');
|
||||
});
|
||||
|
||||
it('returns empty as "" (unprefixed)', () => {
|
||||
expect(
|
||||
u8aToHex('', -1, false)
|
||||
).toEqual('');
|
||||
});
|
||||
|
||||
it('returns the hex value for the array', () => {
|
||||
expect(
|
||||
u8aToHex(
|
||||
@@ -19,6 +25,15 @@ describe('u8aToHex', () => {
|
||||
).toEqual('0x80000a');
|
||||
});
|
||||
|
||||
it('returns the hex value for the array (unprefixed)', () => {
|
||||
expect(
|
||||
u8aToHex(
|
||||
new Uint8Array([128, 0, 10]),
|
||||
-1, false
|
||||
)
|
||||
).toEqual('80000a');
|
||||
});
|
||||
|
||||
it('handles starting zeros correctly', () => {
|
||||
expect(
|
||||
u8aToHex(
|
||||
@@ -26,4 +41,21 @@ describe('u8aToHex', () => {
|
||||
)
|
||||
).toEqual('0x0001000000000000');
|
||||
});
|
||||
|
||||
it('returns the hex value where allowed < max', () => {
|
||||
expect(
|
||||
u8aToHex(
|
||||
new Uint8Array([128, 0, 10, 11], 64)
|
||||
)
|
||||
).toEqual('0x80000a0b');
|
||||
});
|
||||
|
||||
it('returns the trimmed hex value where allowed >= max', () => {
|
||||
expect(
|
||||
u8aToHex(
|
||||
new Uint8Array([128, 0, 10, 11, 12, 13]),
|
||||
32
|
||||
)
|
||||
).toEqual('0x8000…0c0d');
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,31 +0,0 @@
|
||||
// Copyright 2017-2018 Jaco Greeff
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
// @flow
|
||||
|
||||
const u8aToHex = require('./toHex');
|
||||
|
||||
/**
|
||||
@name u8aToHexShort
|
||||
@signature u8aToHexShort (value?: UInt8Array, max: number = 16): string
|
||||
@summary Creates a hex string from a Uint8Array object with max characters.
|
||||
@description
|
||||
`UInt8Array` input values return the actual hex string shortened to the specified characters. `null` or `undefined` values returns an `0x` string.
|
||||
@example
|
||||
import { u8aToHexShort } from '@polkadot/util';
|
||||
|
||||
u8aToHexShort(new Uint8Array([0x68, 0x65, 0x6c, 0x6c, 0xf]), 4); // 0x68...0f
|
||||
*/
|
||||
module.exports = function u8aToHexShort (value?: Uint8Array, _max: number = 16): string {
|
||||
if (!value || !value.length) {
|
||||
return '0x';
|
||||
}
|
||||
|
||||
const max = _max / 2;
|
||||
|
||||
if (value.length <= max) {
|
||||
return u8aToHex(value);
|
||||
}
|
||||
|
||||
return `${u8aToHex(value.subarray(0, max))}…`;
|
||||
};
|
||||
@@ -1,33 +0,0 @@
|
||||
// Copyright 2017-2018 Jaco Greeff
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
const { u8aToHexShort } = require('./index');
|
||||
|
||||
describe('u8aToHexShort', () => {
|
||||
const input = new Uint8Array([1, 2, 3, 4, 5, 0, 6, 7, 8, 9]);
|
||||
|
||||
it('returns empty as 0x', () => {
|
||||
expect(
|
||||
u8aToHexShort()
|
||||
).toEqual('0x');
|
||||
});
|
||||
|
||||
it('returns the hex value for the array (max >= length)', () => {
|
||||
expect(
|
||||
u8aToHexShort(input, 20)
|
||||
).toEqual('0x01020304050006070809');
|
||||
});
|
||||
|
||||
it('returns the hex value for the array (max (default) < length)', () => {
|
||||
expect(
|
||||
u8aToHexShort(input)
|
||||
).toEqual('0x0102030405000607…');
|
||||
});
|
||||
|
||||
it('returns the hex value for the array (max < length)', () => {
|
||||
expect(
|
||||
u8aToHexShort(input, 10)
|
||||
).toEqual('0x0102030405…');
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user