Merge pull request #2 from Jessidhia/fix-utf-encoding

Fix encoding of Unicode keys greater than U+00FF
This commit is contained in:
Brian Vaughn
2019-02-19 08:56:21 -08:00
committed by GitHub
2 changed files with 11 additions and 14 deletions
+2
View File
@@ -566,6 +566,8 @@ export function attach(
if (key !== null) {
if (typeof key === 'number') {
// unreachable code?
// https://github.com/facebook/react/blob/master/packages/react/src/ReactElement.js#L187
encodedKey = new Uint8Array(1);
encodedKey[0] = key;
} else {
+9 -14
View File
@@ -51,20 +51,15 @@ export function getUID(): number {
return ++uidCounter;
}
export function utfDecodeString(array: Uint8Array): string {
let string = '';
const { length } = array;
for (let i = 0; i < length; i++) {
string += String.fromCharCode(array[i]);
}
return string;
export function utfDecodeString(array: Uint32Array): string {
return String.fromCodePoint(...array);
}
export function utfEncodeString(string: string): Uint8Array {
const array = new Uint8Array(string.length);
const { length } = string;
for (let i = 0; i < length; i++) {
array[i] = string.charCodeAt(i);
}
return array;
export function utfEncodeString(string: string): Uint32Array {
// $FlowFixMe Flow's Uint32Array.from's type definition is wrong; first argument of mapFn will be string
return Uint32Array.from(string, toCodePoint);
}
function toCodePoint(string: string) {
return string.codePointAt(0);
}