diff --git a/types/text-encoding-utf-8/index.d.ts b/types/text-encoding-utf-8/index.d.ts new file mode 100644 index 0000000000..30f3728250 --- /dev/null +++ b/types/text-encoding-utf-8/index.d.ts @@ -0,0 +1,56 @@ +// Type definitions for text-encoding-utf-8 1.0 +// Project: https://github.com/arv/text-encoding-utf-8 +// Definitions by: Paul Taylor +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +declare namespace TextEncoding { + interface TextDecoderOptions { + fatal?: boolean; + ignoreBOM?: boolean; + } + + interface TextDecodeOptions { + stream?: boolean; + } + + interface TextEncoderOptions { + NONSTANDARD_allowLegacyEncoding?: boolean; + } + + interface TextDecoder { + encoding: string; + fatal: boolean; + ignoreBOM: boolean; + decode(input?: ArrayBuffer | ArrayBufferView, options?: TextDecodeOptions): string; + } + + interface TextEncoder { + encoding: string; + encode(input?: string, options?: TextEncodeOptions): Uint8Array; + } + + interface TextEncodeOptions { + stream?: boolean; + } + + interface TextEncoderStatic { + (utfLabel?: string, options?: TextEncoderOptions): TextEncoder; + new (utfLabel?: string, options?: TextEncoderOptions): TextEncoder; + } + + interface TextDecoderStatic { + (label?: string, options?: TextDecoderOptions): TextDecoder; + new (label?: string, options?: TextDecoderOptions): TextDecoder; + } + + interface TextEncodingStatic { + TextEncoder: TextEncoderStatic; + TextDecoder: TextDecoderStatic; + } +} + +declare var TextDecoder: TextEncoding.TextDecoderStatic; + +declare var TextEncoder: TextEncoding.TextEncoderStatic; + +declare var TextEncoding: TextEncoding.TextEncodingStatic; diff --git a/types/text-encoding-utf-8/text-encoding-utf-8-tests.ts b/types/text-encoding-utf-8/text-encoding-utf-8-tests.ts new file mode 100644 index 0000000000..7ebfb04055 --- /dev/null +++ b/types/text-encoding-utf-8/text-encoding-utf-8-tests.ts @@ -0,0 +1,50 @@ +function test_encoder() { + const text = "plain text"; + let uint8array: Uint8Array; + + // constructor + uint8array = new TextEncoder().encode(text); + uint8array = new TextEncoder('utf-8').encode(text); + + uint8array = TextEncoder().encode(text); + uint8array = TextEncoder('utf-8').encode(text); + + // attributes + const encoder = new TextEncoder(); + encoder.encoding = 'utf-8'; + const encoding: string = encoder.encoding; + + // methods + encoder.encode(); + encoder.encode(text); +} + +function test_decoder() { + let text = "plain text"; + const uint8array: Uint8Array = TextEncoder().encode(text); + const arrayBuffer: ArrayBuffer = uint8array.buffer; + + // constructor + text = new TextDecoder().decode(uint8array); + text = new TextDecoder('utf-8').decode(uint8array); + + text = TextDecoder().decode(uint8array); + text = TextDecoder('utf-8').decode(uint8array); + + // attributes + const decoder = new TextDecoder(); + + decoder.encoding = 'utf-8'; + const encoding: string = decoder.encoding; + + decoder.fatal = true; + const fatal: boolean = decoder.fatal; + + decoder.ignoreBOM = true; + const ignoreBOM: boolean = decoder.ignoreBOM; + + // methods + decoder.decode(); + decoder.decode(uint8array); + decoder.decode(arrayBuffer); +} diff --git a/types/text-encoding-utf-8/tsconfig.json b/types/text-encoding-utf-8/tsconfig.json new file mode 100644 index 0000000000..f9d410c8c5 --- /dev/null +++ b/types/text-encoding-utf-8/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "text-encoding-utf-8-tests.ts" + ] +} \ No newline at end of file diff --git a/types/text-encoding-utf-8/tslint.json b/types/text-encoding-utf-8/tslint.json new file mode 100644 index 0000000000..f93cf8562a --- /dev/null +++ b/types/text-encoding-utf-8/tslint.json @@ -0,0 +1,3 @@ +{ + "extends": "dtslint/dt.json" +}