TextEncoder allows non-Uint8Array Buffers (#596)

* TextEncoder allows non-Uint8Array Buffers

* Add eslintrc to eslintignore
This commit is contained in:
Jaco Greeff
2020-05-11 09:57:00 +02:00
committed by GitHub
parent d3583dc432
commit d1de30907f
17 changed files with 1106 additions and 953 deletions
+1 -1
View File
@@ -10,7 +10,7 @@ describe('bnToU8a', (): void => {
it('converts null values to 0x00', (): void => {
expect(
bnToU8a(null, -1, false)
).toEqual(new Uint8Array([]));
).toEqual(new Uint8Array());
});
it('converts null values to 0x00000000 (bitLength)', (): void => {
+1 -1
View File
@@ -43,7 +43,7 @@ function bnToU8a (value: BN | BigInt | number | null, arg1: number | Options = {
if (!value) {
return _options.bitLength === -1
? new Uint8Array([])
? new Uint8Array()
: new Uint8Array(byteLength);
}
+1 -1
View File
@@ -8,7 +8,7 @@ describe('bufferToU8a', (): void => {
it('returns an empty buffer when null provided', (): void => {
expect(
bufferToU8a(null)
).toEqual(new Uint8Array([]));
).toEqual(new Uint8Array());
});
it('returns a Uint8Buffer with the correct values', (): void => {
+1 -5
View File
@@ -17,9 +17,5 @@
* ```
*/
export default function bufferToU8a (buffer?: Buffer | number[] | null): Uint8Array {
if (!buffer) {
return new Uint8Array([]);
}
return new Uint8Array(buffer);
return new Uint8Array(buffer || []);
}
+1 -1
View File
@@ -23,7 +23,7 @@ import hexStripPrefix from './stripPrefix';
*/
export default function hexToU8a (_value?: string | null, bitLength = -1): Uint8Array {
if (!_value) {
return new Uint8Array([]);
return new Uint8Array();
}
assert(isHex(_value), `Expected hex value to convert, found '${_value}'`);
+1 -1
View File
@@ -19,7 +19,7 @@ describe('isUint8Array', (): void => {
it('returns true on Uint8Array values', (): void => {
expect(
isU8a(new Uint8Array([]))
isU8a(new Uint8Array())
).toEqual(true);
});
});
+4 -4
View File
@@ -8,25 +8,25 @@ describe('numberToU8a', (): void => {
it('converts 0 to empty', (): void => {
expect(
numberToU8a(0)
).toEqual(new Uint8Array([]));
).toEqual(new Uint8Array());
});
it('converts undefined to empty', (): void => {
expect(
numberToU8a()
).toEqual(new Uint8Array([]));
).toEqual(new Uint8Array());
});
it('converts null to empty', (): void => {
expect(
numberToU8a(null)
).toEqual(new Uint8Array([]));
).toEqual(new Uint8Array());
});
it('converts NaN to empty', (): void => {
expect(
numberToU8a(NaN)
).toEqual(new Uint8Array([]));
).toEqual(new Uint8Array());
});
it('converts values to the u8a', (): void => {
+1 -1
View File
@@ -21,7 +21,7 @@ import numberToHex from './toHex';
*/
export default function numberToU8a (value?: number | null, bitLength = -1): Uint8Array {
if (!value || isNaN(value)) {
return new Uint8Array([]);
return new Uint8Array();
}
return hexToU8a(
+16 -1
View File
@@ -4,8 +4,23 @@
if (typeof TextEncoder === 'undefined') {
try {
const UTE = require('util').TextEncoder;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(global as any).TextEncoder = require('util').TextEncoder;
(global as any).TextEncoder = class TextEncoder {
#encoder: TextEncoder;
constructor () {
this.#encoder = new UTE();
}
// For a Jest 26.0.1 environment, Buffer !== Uint8Array
encode (value: string): Uint8Array {
const encoded = this.#encoder.encode(value);
return Uint8Array.from(encoded);
}
};
} catch (error) {
// noop
}
+1 -1
View File
@@ -8,7 +8,7 @@ describe('stringToU8a', (): void => {
it('decodes to an empty string for undefined', (): void => {
expect(
stringToU8a()
).toEqual(new Uint8Array([]));
).toEqual(new Uint8Array());
});
it('encodes the string correctly', (): void => {
+1 -1
View File
@@ -39,5 +39,5 @@ try {
export default function stringToU8a (value?: string): Uint8Array {
return value
? encoder.encode(value)
: new Uint8Array([]);
: new Uint8Array();
}
+1 -1
View File
@@ -33,7 +33,7 @@ describe('u8aToBn', (): void => {
it('converts empty', (): void => {
expect(
u8aToBn(
new Uint8Array([]),
new Uint8Array(),
{ isLe: true }
).toString(16)
).toBe('0');
+1 -1
View File
@@ -13,7 +13,7 @@ describe('u8aToString', (): void => {
it('decodes to an empty string for empty buffer', (): void => {
expect(
u8aToString(new Uint8Array([]))
u8aToString(new Uint8Array())
).toEqual('');
});