<commit_before>import { TextEncoder as TextEncoderLegacy, TextDecoder as TextDecoderLegacy } from 'text-encoding';
import CharacterSetECI from './../common/CharacterSetECI';
import UnsupportedOperationException from '../UnsupportedOperationException';

/**
 * Responsible for en/decoding strings.
 */
export default class StringEncoding {

    /**
     * Decodes some Uint8Array to a string format.
     */
    public static decode(bytes: Uint8Array, encoding: string | CharacterSetECI): string {

        const encodingName = this.encodingName(encoding);

        // Node.js environment fallback.
        if (!StringEncoding.isBrowser()) {
            return new TextDecoderLegacy(encodingName).decode(bytes);
        }

        // TextDecoder not available
        if (typeof TextDecoder === 'undefined') {
            return this.decodeFallback(bytes, encodingName);
        }

        return new TextDecoder(encodingName).decode(bytes);
    }

    /**
     * Encodes some string into a Uint8Array.
     *
      * @todo natively support other string formats than UTF-8.
     */
    public static encode(s: string, encoding: string | CharacterSetECI): Uint8Array {

        const encodingName = this.encodingName(encoding);

        if (!StringEncoding.isBrowser()) {
            return new TextEncoderLegacy(encodingName, { NONSTANDARD_allowLegacyEncoding: true }).encode(s);
        }

        // TextEncoder only encodes to UTF8 by default as specified by encoding.spec.whatwg.org
        return new TextEncoder().encode(s);
    }

    private static isBrowser(): boolean {
        return typeof window !== 'undefined' && ({}).toString.call(window) === '[object Window]';
    }

    /**<commit_msg>avoid useless const definition<commit_after>import { TextEncoder as TextEncoderLegacy, TextDecoder as TextDecoderLegacy } from 'text-encoding';
import CharacterSetECI from './../common/CharacterSetECI';
import UnsupportedOperationException from '../UnsupportedOperationException';

/**
 * Responsible for en/decoding strings.
 */
export default class StringEncoding {

    /**
     * Decodes some Uint8Array to a string format.
     */
    public static decode(bytes: Uint8Array, encoding: string | CharacterSetECI): string {

        const encodingName = this.encodingName(encoding);

        // Node.js environment fallback.
        if (!StringEncoding.isBrowser()) {
            return new TextDecoderLegacy(encodingName).decode(bytes);
        }

        // TextDecoder not available
        if (typeof TextDecoder === 'undefined') {
            return this.decodeFallback(bytes, encodingName);
        }

        return new TextDecoder(encodingName).decode(bytes);
    }

    /**
     * Encodes some string into a Uint8Array.
     *
      * @todo natively support other string formats than UTF-8.
     */
    public static encode(s: string, encoding: string | CharacterSetECI): Uint8Array {


        // Uses `text-encoding` package.
        if (!StringEncoding.isBrowser()) {
            return new TextEncoderLegacy(this.encodingName(encoding), { NONSTANDARD_allowLegacyEncoding: true }).encode(s);
        }

        // TextEncoder only encodes to UTF8 by default as specified by encoding.spec.whatwg.org
        return new TextEncoder().encode(s);
    }

    private static isBrowser(): boolean {
        return typeof window !== 'undefined' && ({}).toString.call(window) === '[object Window]';
    }

    /**
{'fraction_non_alphanumeric': 0.10698626132709733, 'fraction_numerical': 0.0035077462730195848, 'mean_word_length': 2.906392694063927, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4800493', 'n_tokens_mistral': 906, 'n_tokens_neox': 870, 'n_words': 247}