This is an automated email from the ASF dual-hosted git repository. ptaylor pushed a commit to branch ARROW-12578 in repository https://gitbox.apache.org/repos/asf/arrow.git
commit 2315e1316941fc2f4c30b28b38dfc4d9d0585133 Author: Paul Taylor <paul.e.tay...@me.com> AuthorDate: Sat May 15 00:03:54 2021 -0500 always use the global TextDecoder and TextEncoder --- js/src/util/utf8.ts | 26 ++++---------------------- 1 file changed, 4 insertions(+), 22 deletions(-) diff --git a/js/src/util/utf8.ts b/js/src/util/utf8.ts index 4e04a8e..b592a07 100644 --- a/js/src/util/utf8.ts +++ b/js/src/util/utf8.ts @@ -21,28 +21,10 @@ import { TextEncoder as TextEncoderPolyfill, } from 'text-encoding-utf-8'; -/** @ignore @suppress {missingRequire} */ -const _Buffer = eval("typeof Buffer === 'function' ? Buffer : null"); +const decoder = new (typeof TextDecoder !== 'undefined' ? TextDecoder : TextDecoderPolyfill)('utf-8'); /** @ignore */ -const useNativeEncoders = typeof TextDecoder === 'function' && typeof TextEncoder === 'function'; +export const decodeUtf8 = (buffer?: ArrayBuffer | ArrayBufferView) => decoder.decode(buffer); +const encoder = new (typeof TextEncoder !== 'undefined' ? TextEncoder : TextEncoderPolyfill)(); /** @ignore */ -export const decodeUtf8 = ((TextDecoder) => { - if (useNativeEncoders || !_Buffer) { - const decoder = new TextDecoder('utf-8'); - return (buffer?: ArrayBuffer | ArrayBufferView) => decoder.decode(buffer); - } - return (input: ArrayBufferLike | ArrayBufferView) => { - const { buffer, byteOffset, length } = toUint8Array(input); - return _Buffer.from(buffer, byteOffset, length).toString(); - }; -})(typeof TextDecoder !== 'undefined' ? TextDecoder : TextDecoderPolyfill); - -/** @ignore */ -export const encodeUtf8 = ((TextEncoder) => { - if (useNativeEncoders || !_Buffer) { - const encoder = new TextEncoder(); - return (value?: string) => encoder.encode(value); - } - return (input = '') => toUint8Array(_Buffer.from(input, 'utf8')); -})(typeof TextEncoder !== 'undefined' ? TextEncoder : TextEncoderPolyfill); +export const encodeUtf8 = (value?: string) => encoder.encode(value);