* Adjust encoder/decoder fallbacks * Dep-dup is functions * Complexity cleanups
24 lines
592 B
TypeScript
24 lines
592 B
TypeScript
// Copyright 2017-2020 @polkadot/x-textencoder authors & contributors
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
import { TextEncoder as NodeTextEncoder } from 'util';
|
|
|
|
class NodeFallback {
|
|
#encoder: NodeTextEncoder;
|
|
|
|
constructor () {
|
|
this.#encoder = new NodeTextEncoder();
|
|
}
|
|
|
|
// For a Jest 26.0.1 environment, Buffer !== Uint8Array
|
|
encode (value: string): Uint8Array {
|
|
const encoded = this.#encoder.encode(value);
|
|
|
|
return Uint8Array.from(encoded);
|
|
}
|
|
}
|
|
|
|
export default typeof TextEncoder === 'undefined'
|
|
? NodeFallback as unknown as typeof TextEncoder
|
|
: TextEncoder;
|