diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index 088a423eb7..af326634b1 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -326,6 +326,7 @@ All definitions files include a header with the author and editors, so at some p * [Tags Manager](http://welldonethings.com/tags/manager) (by [Vincent Bortone](https://github.com/vbortone)) * [Teechart](http://www.steema.com) (by [Steema](http://www.steema.com)) * [text-buffer](https://github.com/atom/text-buffer) (by [vvakame](https://github.com/vvakame)) +* [text-encoding](https://github.com/inexorabletash/text-encoding) (by [MIZUNE Pine](https://github.com/pine613)) * [three.js](http://mrdoob.github.com/three.js/) (by [Kon](http://phyzkit.net/)) * [TimelineJS](https://github.com/NUKnightLab/TimelineJS) (by [Roland Zwaga](https://github.com/rolandzwaga)) * [timezonecomplete](https://github.com/SpiritIT/timezonecomplete) (by [Rogier Schouten](https://github.com/rogierschouten)) diff --git a/text-encoding/text-encoding-tests.ts b/text-encoding/text-encoding-tests.ts new file mode 100644 index 0000000000..e0274a04c0 --- /dev/null +++ b/text-encoding/text-encoding-tests.ts @@ -0,0 +1,60 @@ +/// + +function test_encoder() { + var text = "plain text"; + var uint8array: 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 = TextEncoder().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 + encoder.encode(); + encoder.encode(text); + encoder.encode(text, { stream: true }); +} + +function test_decoder() { + var text = "plain text"; + var uint8array: Uint8Array = TextEncoder().encode(text); + + // 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 = 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); + + // 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 + decoder.decode(); + decoder.decode(uint8array); + decoder.decode(uint8array, { stream: true }); +} \ No newline at end of file diff --git a/text-encoding/text-encoding.d.ts b/text-encoding/text-encoding.d.ts new file mode 100644 index 0000000000..574b25d716 --- /dev/null +++ b/text-encoding/text-encoding.d.ts @@ -0,0 +1,45 @@ +// Type definitions for text-encoding +// Project: https://github.com/inexorabletash/text-encoding +// Definitions by: MIZUNE Pine +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +declare module 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?: ArrayBufferView, options?: TextDecodeOptions): string; + } + + interface TextEncoder { + encoding: string; + encode(input?: string, options?: TextEncodeOptions): Uint8Array; + } + + interface TextEncodeOptions { + stream?: boolean; + } +} + +declare var TextDecoder: { + (label?: string, options?: TextEncoding.TextDecoderOptions): TextEncoding.TextDecoder; + new (label?: string, options?: TextEncoding.TextDecoderOptions): TextEncoding.TextDecoder; +}; + +declare var TextEncoder: { + (utfLabel?: string, options?: TextEncoding.TextEncoderOptions): TextEncoding.TextEncoder; + new (utfLabel?: string, options?: TextEncoding.TextEncoderOptions): TextEncoding.TextEncoder; +}; \ No newline at end of file