diff --git a/types/text-encoding/index.d.ts b/types/text-encoding/index.d.ts index bda902dace..4038f805d2 100644 --- a/types/text-encoding/index.d.ts +++ b/types/text-encoding/index.d.ts @@ -13,11 +13,28 @@ declare namespace TextEncoding { (utfLabel?: string, options?: TextEncoderOptions): TextEncoder; new (utfLabel?: string, options?: TextEncoderOptions): TextEncoder; } - export const TextEncoder: TextEncoder; - export const TextDecoder: TextDecoder; + + export var TextEncoder: { + new (utfLabel?: string, options?: TextEncoderOptions): TextEncoder; + (utfLabel?: string, options?: TextEncoderOptions): TextEncoder; + encoding: string; + }; + + export var TextDecoder: { + (label?: string, options?: TextDecoderOptions): TextDecoder; + new (label?: string, options?: TextDecoderOptions): TextDecoder; + encoding: string; + }; } -declare var TextEncoding: TextEncoding.TextEncodingStatic; +interface TextEncodeOptions { + stream?: boolean; +} + +interface TextEncoder { + readonly encoding: string; + encode(input?: string, options?: TextEncodeOptions): Uint8Array; +} declare module "text-encoding" { export = TextEncoding; diff --git a/types/text-encoding/text-encoding-tests.ts b/types/text-encoding/text-encoding-tests.ts index 914f680bd3..6b2bb13017 100644 --- a/types/text-encoding/text-encoding-tests.ts +++ b/types/text-encoding/text-encoding-tests.ts @@ -1,21 +1,24 @@ - +import { TextEncoder, TextDecoder } from "text-encoding"; function test_encoder() { var text = "plain text"; - var uint8array: Uint8Array; + var uint8array = new Uint8Array([]); // constructor uint8array = new TextEncoder().encode(text); - uint8array = new TextEncoder('utf-8').encode(text); - uint8array = new TextEncoder('windows-1252', { NONSTANDARD_allowLegacyEncoding: true }).encode(text); + uint8array = new TextEncoder("utf-8").encode(text); + uint8array = new TextEncoder("windows-1252", { + NONSTANDARD_allowLegacyEncoding: true + }).encode(text); uint8array = TextEncoder().encode(text); - uint8array = TextEncoder('utf-8').encode(text); - uint8array = TextEncoder('windows-1252', { NONSTANDARD_allowLegacyEncoding: true }).encode(text); + uint8array = TextEncoder("utf-8").encode(text); + uint8array = TextEncoder("windows-1252", { + NONSTANDARD_allowLegacyEncoding: true + }).encode(text); // attributes var encoder = new TextEncoder(); - encoder.encoding = 'utf-8'; var encoding: string = encoder.encoding; // methods @@ -27,31 +30,30 @@ function test_encoder() { function test_decoder() { var text = "plain text"; var uint8array: Uint8Array = TextEncoder().encode(text); - var arrayBuffer: ArrayBuffer = uint8array.buffer + var arrayBuffer: ArrayBuffer = uint8array.buffer; // constructor text = new TextDecoder().decode(uint8array); - text = new TextDecoder('utf-8').decode(uint8array); - text = new TextDecoder('windows-1252', {}).decode(uint8array); - text = new TextDecoder('windows-1252', { fatal: true }).decode(uint8array); - text = new TextDecoder('windows-1252', { ignoreBOM: true }).decode(uint8array); + text = new TextDecoder("utf-8").decode(uint8array); + text = new TextDecoder("windows-1252", {}).decode(uint8array); + text = new TextDecoder("windows-1252", { fatal: true }).decode(uint8array); + text = new TextDecoder("windows-1252", { ignoreBOM: true }).decode( + uint8array + ); text = TextDecoder().decode(uint8array); - text = TextDecoder('utf-8').decode(uint8array); - text = TextDecoder('windows-1252', {}).decode(uint8array); - text = TextDecoder('windows-1252', { fatal: true }).decode(uint8array); - text = TextDecoder('windows-1252', { ignoreBOM: true }).decode(uint8array); + text = TextDecoder("utf-8").decode(uint8array); + text = TextDecoder("windows-1252", {}).decode(uint8array); + text = TextDecoder("windows-1252", { fatal: true }).decode(uint8array); + text = TextDecoder("windows-1252", { ignoreBOM: true }).decode(uint8array); // attributes var decoder = new TextDecoder(); - decoder.encoding = 'utf-8'; var encoding: string = decoder.encoding; - decoder.fatal = true; var fatal: boolean = decoder.fatal; - decoder.ignoreBOM = true; var ignoreBOM: boolean = decoder.ignoreBOM; // methods