From 548030f4f9b38e0dff21fa2c60df3a09bc4632cc Mon Sep 17 00:00:00 2001 From: fengkx Date: Wed, 1 Apr 2020 07:06:17 +0800 Subject: [PATCH] types: add types of whatwg-encoding (#43328) * types: add types of whatwg-encoding * rename to tests --- types/whatwg-encoding/index.d.ts | 10 ++++++++++ types/whatwg-encoding/tsconfig.json | 16 ++++++++++++++++ types/whatwg-encoding/tslint.json | 1 + types/whatwg-encoding/whatwg-encoding-tests.ts | 17 +++++++++++++++++ 4 files changed, 44 insertions(+) create mode 100644 types/whatwg-encoding/index.d.ts create mode 100644 types/whatwg-encoding/tsconfig.json create mode 100644 types/whatwg-encoding/tslint.json create mode 100644 types/whatwg-encoding/whatwg-encoding-tests.ts diff --git a/types/whatwg-encoding/index.d.ts b/types/whatwg-encoding/index.d.ts new file mode 100644 index 0000000000..c31916ceb4 --- /dev/null +++ b/types/whatwg-encoding/index.d.ts @@ -0,0 +1,10 @@ +// Type definitions for whatwg-encoding 1.0 +// Project: https://github.com/jsdom/whatwg-encoding +// Definitions by: fengkx +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +/// +export function decode(buffer: Buffer, fallbackEncodingName: string): string; +export function labelToName(label: string): string | null; +export function isSupported(name: string): boolean; +export function getBOMEncoding(buffer: Buffer): string | null; diff --git a/types/whatwg-encoding/tsconfig.json b/types/whatwg-encoding/tsconfig.json new file mode 100644 index 0000000000..09835ba487 --- /dev/null +++ b/types/whatwg-encoding/tsconfig.json @@ -0,0 +1,16 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": ["es6"], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "baseUrl": "../", + "typeRoots": ["../"], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": ["index.d.ts", "whatwg-encoding-tests.ts"] +} diff --git a/types/whatwg-encoding/tslint.json b/types/whatwg-encoding/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/whatwg-encoding/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" } diff --git a/types/whatwg-encoding/whatwg-encoding-tests.ts b/types/whatwg-encoding/whatwg-encoding-tests.ts new file mode 100644 index 0000000000..fe08ba76da --- /dev/null +++ b/types/whatwg-encoding/whatwg-encoding-tests.ts @@ -0,0 +1,17 @@ +import whatwgEncoding = require('whatwg-encoding'); + +console.assert(whatwgEncoding.labelToName('latin1') === 'windows-1252'); +console.assert(whatwgEncoding.labelToName(' CYRILLic ') === 'ISO-8859-5'); + +console.assert(whatwgEncoding.isSupported('IBM866')); + +// Not supported by the Encoding Standard +console.assert(!whatwgEncoding.isSupported('UTF-32')); + +// In the Encoding Standard, but this package can't decode it +console.assert(!whatwgEncoding.isSupported('x-mac-cyrillic')); + +console.assert(whatwgEncoding.getBOMEncoding(Buffer.from([0xfe, 0xff])) === 'UTF-16BE'); +console.assert(whatwgEncoding.getBOMEncoding(Buffer.from([0x48, 0x69])) === null); + +console.assert(whatwgEncoding.decode(Buffer.from([0x48, 0x69]), 'UTF-8') === 'Hi');