React native support, strengthen TextEncoder/Decoder (#273)
This commit is contained in:
@@ -35,11 +35,14 @@
|
||||
"@types/ip-regex": "^2.0.0",
|
||||
"@types/xxhashjs": "^0.1.1",
|
||||
"bn.js": "^4.11.8",
|
||||
"buffer": "^5.2.1",
|
||||
"camelcase": "^5.0.0",
|
||||
"chalk": "^2.4.1",
|
||||
"core-js": "^2.5.7",
|
||||
"deasync": "^0.1.14",
|
||||
"ip-regex": "^3.0.0",
|
||||
"moment": "^2.22.2"
|
||||
"moment": "^2.22.2",
|
||||
"stream": "^0.0.2",
|
||||
"util": "^0.11.1"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,17 +5,9 @@
|
||||
// tslint:disable-next-line
|
||||
if (typeof TextDecoder === 'undefined') {
|
||||
try {
|
||||
// @ts-ignore For the Node.js case
|
||||
// @ts-ignore
|
||||
global.TextDecoder = require('util').TextDecoder;
|
||||
} catch (error) {
|
||||
// @ts-ignore For the old browser case
|
||||
global.TextDecoder = {
|
||||
// Very naive ascii-only decoder
|
||||
decode: (u8a: Uint8Array) => {
|
||||
return u8a.reduce((str, code) => {
|
||||
return str + String.fromCharCode(code);
|
||||
}, '');
|
||||
}
|
||||
};
|
||||
// noop
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,18 +8,6 @@ if (typeof TextEncoder === 'undefined') {
|
||||
// @ts-ignore For the Node.js case
|
||||
global.TextEncoder = require('util').TextEncoder;
|
||||
} catch (error) {
|
||||
// @ts-ignore For the old browser case
|
||||
global.TextEncoder = {
|
||||
// Very naive ascii-only encoder
|
||||
encode: (str: string) => {
|
||||
const u8a = new Uint8Array(str.length);
|
||||
|
||||
for (let i = 0; i < str.length; i++) {
|
||||
u8a[i] = str.charCodeAt(i);
|
||||
}
|
||||
|
||||
return u8a;
|
||||
}
|
||||
};
|
||||
// noop
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,7 +4,13 @@
|
||||
|
||||
import './polyfill/textEncoder';
|
||||
|
||||
const encoder = new TextEncoder();
|
||||
let encoder: TextEncoder | undefined;
|
||||
|
||||
try {
|
||||
encoder = new TextEncoder();
|
||||
} catch (error) {
|
||||
// noop
|
||||
}
|
||||
|
||||
/**
|
||||
* @name stringToU8a
|
||||
@@ -24,7 +30,15 @@ const encoder = new TextEncoder();
|
||||
export default function stringToU8a (value?: string): Uint8Array {
|
||||
if (!value) {
|
||||
return new Uint8Array([]);
|
||||
} else if (encoder) {
|
||||
return encoder.encode(value);
|
||||
}
|
||||
|
||||
return encoder.encode(value);
|
||||
const u8a = new Uint8Array(value.length);
|
||||
|
||||
for (let i = 0; i < value.length; i++) {
|
||||
u8a[i] = value.charCodeAt(i);
|
||||
}
|
||||
|
||||
return u8a;
|
||||
}
|
||||
|
||||
@@ -4,7 +4,13 @@
|
||||
|
||||
import '../string/polyfill/textDecoder';
|
||||
|
||||
const decoder = new TextDecoder('utf-8');
|
||||
let decoder: TextDecoder | undefined;
|
||||
|
||||
try {
|
||||
decoder = new TextDecoder('utf-8');
|
||||
} catch (error) {
|
||||
// ignore
|
||||
}
|
||||
|
||||
/**
|
||||
* @name u8aToString
|
||||
@@ -24,7 +30,11 @@ const decoder = new TextDecoder('utf-8');
|
||||
export default function u8aToString (value?: Uint8Array): string {
|
||||
if (!value || !value.length) {
|
||||
return '';
|
||||
} else if (decoder) {
|
||||
return decoder.decode(value);
|
||||
}
|
||||
|
||||
return decoder.decode(value);
|
||||
return value.reduce((str, code) => {
|
||||
return str + String.fromCharCode(code);
|
||||
}, '');
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user