From 58379cb18044fb284e89be52a47d4876725a3bff Mon Sep 17 00:00:00 2001 From: islishude Date: Sun, 13 May 2018 19:13:40 +0800 Subject: [PATCH 1/4] feat(node9&10): add `TextDecoder` and `TextEncoder` for `util` module --- types/node/index.d.ts | 33 +++++++++++++++++++++++++++++++++ types/node/node-tests.ts | 29 +++++++++++++++++++++++++++++ types/node/v9/index.d.ts | 33 +++++++++++++++++++++++++++++++++ types/node/v9/node-tests.ts | 29 +++++++++++++++++++++++++++++ 4 files changed, 124 insertions(+) diff --git a/types/node/index.d.ts b/types/node/index.d.ts index d228742629..09f8498359 100644 --- a/types/node/index.d.ts +++ b/types/node/index.d.ts @@ -23,6 +23,7 @@ // Mohsen Azimi // Hoàng Văn Khải // Alexander T. +// Lishude // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped /** inspector module types */ @@ -6220,6 +6221,38 @@ declare module "util" { export function isWeakSet(object: any): object is WeakSet; export function isWebAssemblyCompiledModule(object: any): boolean; } + + export class TextDecoder { + readonly encoding: string; + readonly fatal: boolean; + readonly ignoreBOM: boolean; + constructor( + encoding?: string, + options?: { fatal?: boolean; ignoreBOM?: boolean } + ); + decode( + input?: + | Int8Array + | Int16Array + | Int32Array + | Uint8Array + | Uint16Array + | Uint32Array + | Uint8ClampedArray + | Float32Array + | Float64Array + | DataView + | ArrayBuffer + | null, + options?: { stream?: boolean } + ): string; + } + + export class TextEncoder { + readonly encoding: string; + constructor(); + encode(input?: string): Uint8Array; + } } declare module "assert" { diff --git a/types/node/node-tests.ts b/types/node/node-tests.ts index d428cf8753..e5d7576fce 100644 --- a/types/node/node-tests.ts +++ b/types/node/node-tests.ts @@ -857,6 +857,35 @@ namespace util_tests { // util.isDeepStrictEqual util.isDeepStrictEqual({foo: 'bar'}, {foo: 'bar'}); + + // util.TextDecoder() + var td = new util.TextDecoder(); + new util.TextDecoder("utf-8"); + new util.TextDecoder("utf-8", { fatal: true }); + new util.TextDecoder("utf-8", { fatal: true, ignoreBOM: true }); + var ignoreBom: boolean = td.ignoreBOM; + var fatal: boolean = td.fatal; + var encoding: string = td.encoding; + td.decode(new Int8Array(1)); + td.decode(new Int16Array(1)); + td.decode(new Int32Array(1)); + td.decode(new Uint8Array(1)); + td.decode(new Uint16Array(1)); + td.decode(new Uint32Array(1)); + td.decode(new Uint8ClampedArray(1)); + td.decode(new Float32Array(1)); + td.decode(new Float64Array(1)); + td.decode(new DataView(new Int8Array(1).buffer)); + td.decode(new ArrayBuffer(1)); + td.decode(null); + td.decode(null, { stream: true }); + td.decode(new Int8Array(1), { stream: true }); + var decode: string = td.decode(new Int8Array(1)); + + // util.TextEncoder() + var te = new util.TextEncoder(); + var teEncoding: string = te.encoding; + var teEncodeRes: Uint8Array = te.encode("TextEncoder"); } } diff --git a/types/node/v9/index.d.ts b/types/node/v9/index.d.ts index 5a76e4cc45..65c55878cd 100644 --- a/types/node/v9/index.d.ts +++ b/types/node/v9/index.d.ts @@ -24,6 +24,7 @@ // Mohsen Azimi // Hoàng Văn Khải // Alexander T. +// Lishude // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped /** inspector module types */ @@ -5659,6 +5660,38 @@ declare module "util" { export namespace promisify { const custom: symbol; } + + export class TextDecoder { + readonly encoding: string; + readonly fatal: boolean; + readonly ignoreBOM: boolean; + constructor( + encoding?: string, + options?: { fatal?: boolean; ignoreBOM?: boolean } + ); + decode( + input?: + | Int8Array + | Int16Array + | Int32Array + | Uint8Array + | Uint16Array + | Uint32Array + | Uint8ClampedArray + | Float32Array + | Float64Array + | DataView + | ArrayBuffer + | null, + options?: { stream?: boolean } + ): string; + } + + export class TextEncoder { + readonly encoding: string; + constructor(); + encode(input?: string): Uint8Array; + } } declare module "assert" { diff --git a/types/node/v9/node-tests.ts b/types/node/v9/node-tests.ts index d6751541b9..b7c7471fe1 100644 --- a/types/node/v9/node-tests.ts +++ b/types/node/v9/node-tests.ts @@ -854,6 +854,35 @@ namespace util_tests { // util.isDeepStrictEqual util.isDeepStrictEqual({foo: 'bar'}, {foo: 'bar'}); + + // util.TextDecoder() + var td = new util.TextDecoder(); + new util.TextDecoder("utf-8"); + new util.TextDecoder("utf-8", { fatal: true }); + new util.TextDecoder("utf-8", { fatal: true, ignoreBOM: true }); + var ignoreBom: boolean = td.ignoreBOM; + var fatal: boolean = td.fatal; + var encoding: string = td.encoding; + td.decode(new Int8Array(1)); + td.decode(new Int16Array(1)); + td.decode(new Int32Array(1)); + td.decode(new Uint8Array(1)); + td.decode(new Uint16Array(1)); + td.decode(new Uint32Array(1)); + td.decode(new Uint8ClampedArray(1)); + td.decode(new Float32Array(1)); + td.decode(new Float64Array(1)); + td.decode(new DataView(new Int8Array(1).buffer)); + td.decode(new ArrayBuffer(1)); + td.decode(null); + td.decode(null, { stream: true }); + td.decode(new Int8Array(1), { stream: true }); + var decode: string = td.decode(new Int8Array(1)); + + // util.TextEncoder() + var te = new util.TextEncoder(); + var teEncoding: string = te.encoding; + var teEncodeRes: Uint8Array = te.encode("TextEncoder"); } } From fc2b440fc80f049792a9c76b41ef3059465d196b Mon Sep 17 00:00:00 2001 From: islishude Date: Sun, 13 May 2018 20:32:31 +0800 Subject: [PATCH 2/4] fix: fix ts version --- types/node/index.d.ts | 1 + types/node/v9/index.d.ts | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/types/node/index.d.ts b/types/node/index.d.ts index 09f8498359..f6b25c85c5 100644 --- a/types/node/index.d.ts +++ b/types/node/index.d.ts @@ -6222,6 +6222,7 @@ declare module "util" { export function isWebAssemblyCompiledModule(object: any): boolean; } + // TypeScript Version: 2.1 export class TextDecoder { readonly encoding: string; readonly fatal: boolean; diff --git a/types/node/v9/index.d.ts b/types/node/v9/index.d.ts index 65c55878cd..def77c2337 100644 --- a/types/node/v9/index.d.ts +++ b/types/node/v9/index.d.ts @@ -5660,7 +5660,7 @@ declare module "util" { export namespace promisify { const custom: symbol; } - + // TypeScript Version: 2.1 export class TextDecoder { readonly encoding: string; readonly fatal: boolean; From c31b37eb1699801d42bcc24f790858c6820798eb Mon Sep 17 00:00:00 2001 From: islishude Date: Mon, 14 May 2018 21:00:14 +0800 Subject: [PATCH 3/4] fix: fix typos --- types/node/index.d.ts | 2 +- types/node/v8/index.d.ts | 2 +- types/node/v9/index.d.ts | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/types/node/index.d.ts b/types/node/index.d.ts index f6b25c85c5..96416a4529 100644 --- a/types/node/index.d.ts +++ b/types/node/index.d.ts @@ -6233,7 +6233,7 @@ declare module "util" { ); decode( input?: - | Int8Array + Int8Array | Int16Array | Int32Array | Uint8Array diff --git a/types/node/v8/index.d.ts b/types/node/v8/index.d.ts index 45f1c85b64..32530306c2 100644 --- a/types/node/v8/index.d.ts +++ b/types/node/v8/index.d.ts @@ -5588,7 +5588,7 @@ declare module "util" { ); decode( input?: - | Int8Array + Int8Array | Int16Array | Int32Array | Uint8Array diff --git a/types/node/v9/index.d.ts b/types/node/v9/index.d.ts index def77c2337..cd8d58c6b9 100644 --- a/types/node/v9/index.d.ts +++ b/types/node/v9/index.d.ts @@ -5671,7 +5671,7 @@ declare module "util" { ); decode( input?: - | Int8Array + Int8Array | Int16Array | Int32Array | Uint8Array From ee6b647dbb7c2601fa25f5a4a11b2df58cd9e3fa Mon Sep 17 00:00:00 2001 From: islishude Date: Mon, 14 May 2018 21:15:04 +0800 Subject: [PATCH 4/4] chore: remove ts version statements --- types/node/index.d.ts | 1 - types/node/v9/index.d.ts | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/types/node/index.d.ts b/types/node/index.d.ts index 96416a4529..64834338b8 100644 --- a/types/node/index.d.ts +++ b/types/node/index.d.ts @@ -6222,7 +6222,6 @@ declare module "util" { export function isWebAssemblyCompiledModule(object: any): boolean; } - // TypeScript Version: 2.1 export class TextDecoder { readonly encoding: string; readonly fatal: boolean; diff --git a/types/node/v9/index.d.ts b/types/node/v9/index.d.ts index cd8d58c6b9..e7112daa25 100644 --- a/types/node/v9/index.d.ts +++ b/types/node/v9/index.d.ts @@ -5660,7 +5660,7 @@ declare module "util" { export namespace promisify { const custom: symbol; } - // TypeScript Version: 2.1 + export class TextDecoder { readonly encoding: string; readonly fatal: boolean;