Adjust encoder/decoder fallbacks (#750)

* Adjust encoder/decoder fallbacks

* Dep-dup is functions

* Complexity cleanups
This commit is contained in:
Jaco Greeff
2020-11-08 11:20:22 +01:00
committed by GitHub
parent e1aa096cef
commit c67c1966d7
18 changed files with 118 additions and 78 deletions
+14 -6
View File
@@ -8,6 +8,18 @@ import bufferToU8a from '../buffer/toU8a';
import hexToU8a from '../hex/toU8a';
import stringToU8a from '../string/toU8a';
function convertArray (value: number[] | Uint8Array): Uint8Array {
return Array.isArray(value)
? Uint8Array.from(value)
: value;
}
function convertString (value: string): Uint8Array {
return isHex(value)
? hexToU8a(value)
: stringToU8a(value);
}
/**
* @name u8aToU8a
* @summary Creates a Uint8Array value from a Uint8Array, Buffer, string or hex input.
@@ -29,12 +41,8 @@ export default function u8aToU8a (value?: number[] | Buffer | Uint8Array | strin
} else if (isBuffer(value)) {
return bufferToU8a(value);
} else if (isString(value)) {
return isHex(value)
? hexToU8a(value)
: stringToU8a(value);
return convertString(value);
}
return Array.isArray(value)
? Uint8Array.from(value)
: value;
return convertArray(value);
}