Add pair to/fromJson (#123)

This commit is contained in:
Jaco Greeff
2018-05-08 12:46:04 +02:00
committed by GitHub
parent 46706bc950
commit f8e07bada9
26 changed files with 282 additions and 161 deletions
+4 -2
View File
@@ -9,7 +9,7 @@ const HEX_REGEX = /^0x[a-fA-F0-9]+$/;
/**
@name isHex
@signature isHex (value: string, bitLength: number = -1): boolean
@signature isHex (value: mixed, bitLength: number = -1): boolean
@summary Tests for a hex string.
@description
Checks to see if the input value is a `0x` prefixed hex string. Optionally (`bitLength` !== -1) checks to see if the bitLength is correct.
@@ -19,10 +19,12 @@ const HEX_REGEX = /^0x[a-fA-F0-9]+$/;
isHex('0x1234'); // => true
isHex('0x1234', 8); // => false
*/
module.exports = function isHex (value: string, bitLength: number = -1): boolean {
module.exports = function isHex (value: mixed, bitLength: number = -1): boolean {
// $FlowFixMe for the regex we have a 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);
}
+6 -2
View File
@@ -16,7 +16,7 @@
u8aFixLength('0x12', 16) // => 0x0012
u8aFixLength('0x1234', 8) // => 0x12
*/
module.exports = function u8aFixLength (value: Uint8Array, bitLength: number = -1): Uint8Array {
module.exports = function u8aFixLength (value: Uint8Array, bitLength: number = -1, atStart?: boolean = false): Uint8Array {
const byteLength = Math.ceil(bitLength / 8);
if (bitLength === -1 || value.length === byteLength) {
@@ -29,7 +29,11 @@ module.exports = function u8aFixLength (value: Uint8Array, bitLength: number = -
const result = new Uint8Array(byteLength);
result.set(value, byteLength - value.length);
if (atStart) {
result.set(value, 0);
} else {
result.set(value, byteLength - value.length);
}
return result;
};