types: add types of whatwg-encoding (#43328)

* types: add types of whatwg-encoding

* rename to tests
This commit is contained in:
fengkx 2020-04-01 07:06:17 +08:00 committed by GitHub
parent 7b906f2a33
commit 548030f4f9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 44 additions and 0 deletions

10
types/whatwg-encoding/index.d.ts vendored Normal file
View File

@ -0,0 +1,10 @@
// Type definitions for whatwg-encoding 1.0
// Project: https://github.com/jsdom/whatwg-encoding
// Definitions by: fengkx <https://github.com/fengkx>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
/// <reference types="node" />
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;

View File

@ -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"]
}

View File

@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }

View File

@ -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');