From c7e645a1f9140380a9978c2c71d632798c974502 Mon Sep 17 00:00:00 2001 From: polygonplanet Date: Tue, 8 Sep 2026 23:47:21 +0900 Subject: [PATCH 1/3] Import TypeScript declarations and tests from DefinitelyTyped --- index.d.ts | 97 +++++++++ tests/types/encoding-japanese-tests.cjs.ts | 196 ++++++++++++++++++ tests/types/encoding-japanese-tests.global.ts | 10 + 3 files changed, 303 insertions(+) create mode 100644 index.d.ts create mode 100644 tests/types/encoding-japanese-tests.cjs.ts create mode 100644 tests/types/encoding-japanese-tests.global.ts diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 0000000..59191e8 --- /dev/null +++ b/index.d.ts @@ -0,0 +1,97 @@ +export as namespace Encoding; + +export type Encoding = + | "UTF32" + | "UTF16" + | "UTF16BE" + | "UTF16LE" + | "BINARY" + | "ASCII" + | "JIS" + | "UTF8" + | "EUCJP" + | "SJIS" + | "UNICODE" + | "AUTO"; +type IntArrayType = + | readonly number[] + | Uint8Array + | Uint16Array + | Uint32Array + | Int8Array + | Int16Array + | Int32Array; +type EncodingDetection = Encoding | false; + +export type ConvertOptions = + | ConvertStringOptions + | ConvertArrayBufferOptions + | ConvertArrayOptions + | ConvertUnknownOptions; + +export interface ConvertStringOptions { + to: Encoding; + from?: Encoding | undefined; + type: "string"; + fallback?: "html-entity" | "html-entity-hex" | "ignore" | "error"; + bom?: boolean | string | undefined; +} + +export interface ConvertArrayBufferOptions { + to: Encoding; + from?: Encoding | undefined; + type: "arraybuffer"; + fallback?: "html-entity" | "html-entity-hex" | "ignore" | "error"; + bom?: boolean | string | undefined; +} + +export interface ConvertArrayOptions { + to: Encoding; + from?: Encoding | undefined; + type: "array"; + fallback?: "html-entity" | "html-entity-hex" | "ignore" | "error"; + bom?: boolean | string | undefined; +} + +export interface ConvertUnknownOptions { + to: Encoding; + from?: Encoding | undefined; + fallback?: "html-entity" | "html-entity-hex" | "ignore" | "error"; + bom?: boolean | string | undefined; +} + +export function detect(data: IntArrayType | string, encodings?: Encoding | Encoding[]): EncodingDetection; +export function convert(data: IntArrayType, to: Encoding, from?: Encoding): number[]; +export function convert(data: string, to: Encoding, from?: Encoding): string; +export function convert(data: IntArrayType | string, options: ConvertStringOptions): string; +export function convert(data: IntArrayType | string, options: ConvertArrayBufferOptions): Uint16Array; +export function convert(data: IntArrayType | string, options: ConvertArrayOptions): number[]; +export function convert(data: string, options: ConvertUnknownOptions): string; +export function convert(data: IntArrayType, options: ConvertUnknownOptions): number[]; +export function urlEncode(data: IntArrayType): string; +export function urlDecode(data: string): number[]; +export function base64Encode(data: IntArrayType): string; +export function base64Decode(data: string): number[]; +export function codeToString(data: IntArrayType): string; +export function stringToCode(data: string): number[]; +export function toHankakuCase(data: readonly number[]): number[]; +export function toHankakuCase(data: string): string; +export function toZenkakuCase(data: readonly number[]): number[]; +export function toZenkakuCase(data: string): string; +export function toHiraganaCase(data: readonly number[]): number[]; +export function toHiraganaCase(data: string): string; +export function toKatakanaCase(data: readonly number[]): number[]; +export function toKatakanaCase(data: string): string; +export function toHankanaCase(data: readonly number[]): number[]; +export function toHankanaCase(data: string): string; +export function toZenkanaCase(data: readonly number[]): number[]; +export function toZenkanaCase(data: string): string; +export function toHankakuSpace(data: readonly number[]): number[]; +export function toHankakuSpace(data: string): string; +export function toZenkakuSpace(data: readonly number[]): number[]; +export function toZenkakuSpace(data: string): string; + +export const version: string; +export const orders: string[]; + +export {}; diff --git a/tests/types/encoding-japanese-tests.cjs.ts b/tests/types/encoding-japanese-tests.cjs.ts new file mode 100644 index 0000000..06d09e7 --- /dev/null +++ b/tests/types/encoding-japanese-tests.cjs.ts @@ -0,0 +1,196 @@ +/// +import Encoding = require("encoding-japanese"); + +// Convert character encoding to Shift_JIS from UTF-8. +const utf8Array_1 = new Uint8Array([1, 2, 3]); +const utf8Array_2 = [1, 2, 3]; +const utf8Array_3 = Buffer.from([1, 2, 3]); +const utf8Array_4 = new Uint16Array([1, 2, 3]); +const utf8Array_5 = new Uint32Array([1, 2, 3]); +const utf8Array_6 = new Int16Array([1, 2, 3]); +const utf8Array_7 = new Int32Array([1, 2, 3]); +const utf8Array_8 = new Int8Array([1, 2, 3]); +const utf8Array_9: readonly number[] = [1, 2, 3]; +Encoding.convert(utf8Array_1, "SJIS", "UTF8"); // $ExpectType number[] +Encoding.convert(utf8Array_2, "UTF16", "UTF8"); // $ExpectType number[] +Encoding.convert(utf8Array_3, "UTF16LE", "UTF8"); // $ExpectType number[] +Encoding.convert(utf8Array_4, "UTF16BE", "UTF8"); // $ExpectType number[] +Encoding.convert(utf8Array_5, "BINARY", "UTF8"); // $ExpectType number[] +Encoding.convert(utf8Array_6, "UTF32", "UTF8"); // $ExpectType number[] +Encoding.convert(utf8Array_7, "AUTO", "UTF8"); // $ExpectType number[] +Encoding.convert(utf8Array_8, "ASCII", "UTF8"); // $ExpectType number[] +Encoding.convert(utf8Array_9, "JIS", "UTF8"); // $ExpectType number[] +Encoding.convert("string value", "UTF16", "UTF8"); // $ExpectType string + +// Convert character encoding by automatic detection (AUTO detect). +const utf8Array = utf8Array_1; +const sjisArray1 = Encoding.convert(utf8Array, "SJIS"); // $ExpectType number[] +// or +const sjisArray2 = Encoding.convert(utf8Array, "SJIS", "AUTO"); // $ExpectType number[] + +// Detect the character encoding. +// The return value be one of the "Available Encodings" below. +const detected = Encoding.detect(utf8Array); // $ExpectType EncodingDetection +if (detected === "UTF8") { + // Encoding is UTF-8' +} + +const sjisArray3 = Encoding.convert(utf8Array, { + to: "SJIS", // to_encoding + from: "UTF8", // from_encoding +}); +sjisArray3; // $ExpectType number[] + +const sjisArray4 = Encoding.convert("🐙", { + to: "SJIS", // to_encoding + from: "UTF8", // from_encoding + type: "string", + fallback: "html-entity", +}); +sjisArray4; // $ExpectType string +// fallback: '🐙' + +const sjisArray5 = Encoding.convert("🐙", { + to: "SJIS", // to_encoding + from: "UTF8", // from_encoding + type: "string", + fallback: "ignore", +}); +sjisArray5; // $ExpectType string +// fallback: '' + +const sjisArray6 = Encoding.convert("🐙", { + to: "SJIS", // to_encoding + from: "UTF8", // from_encoding + type: "string", + fallback: "error", +}); +sjisArray6; // $ExpectType string +// throws error: `Character cannot be represented` + +const utf8String = "ã\u0081\u0093ã\u0082\u0093ã\u0081«ã\u0081¡ã\u0081¯"; +const unicodeString = Encoding.convert(utf8String, { + to: "UNICODE", + from: "UTF8", + type: "string", // Specify 'string' type. (Return as string) +}); +unicodeString; // $ExpectType string + +const unicodeString2 = Encoding.convert(utf8String, { + to: "UNICODE", + from: "UTF8", // Unspecified type. Inferred from 1st argument +}); +unicodeString2; // $ExpectType string + +const utf16Array = Encoding.convert(utf8Array, { + to: "UTF16", // to_encoding + from: "UTF8", // from_encoding + bom: true, // With BOM +}); +utf16Array; // $ExpectType number[] + +const utf16leArray = Encoding.convert(utf8Array, { + to: "UTF16", // to_encoding + from: "UTF8", // from_encoding + bom: "LE", // With BOM (little-endian) +}); +utf16leArray; // $ExpectType number[] + +const utf16beArray = Encoding.convert(utf8Array, { + to: "UTF16BE", + from: "UTF8", +}); +utf16beArray; // $ExpectType number[] + +const utf16Array2 = Encoding.convert(utf8Array, { + to: "UTF16", // to_encoding + from: "UTF8", // from_encoding + type: "array", + bom: true, // With BOM +}); +utf16Array2; // $ExpectType number[] + +const utf16Array3 = Encoding.convert(utf8Array, { + to: "UTF16", // to_encoding + from: "UTF8", // from_encoding + type: "arraybuffer", + bom: true, // With BOM +}); +utf16Array3; // $ExpectType Uint16Array || Uint16Array + +const utf16Array4 = Encoding.convert(utf8Array, { + to: "UTF16", // to_encoding + from: "UTF8", // from_encoding + type: "string", + bom: true, // With BOM +}); +utf16Array4; // $ExpectType string + +// Detect character encoding by automatic. (AUTO detect). +const detected2 = Encoding.detect(utf8Array); // $ExpectType EncodingDetection +if (detected2 === "UTF8") { + // Encoding is UTF-8' +} + +// Detect character encoding by specific encoding name. +const isSJIS = Encoding.detect(sjisArray1, "SJIS"); // $ExpectType EncodingDetection +if (isSJIS) { + // Encoding is SJIS' +} + +const sjisArray7 = [ + 130, + 177, + 130, + 241, + 130, + 201, + 130, + 191, + 130, + 205, + 129, + 65, + 130, + 217, + 130, + 176, + 129, + 153, + 130, + 210, + 130, + 230, +]; + +const encoded = Encoding.urlEncode(sjisArray7); // $ExpectType string +// encoded: '%82%B1%82%F1%82%C9%82%BF%82%CD%81A%82%D9%82%B0%81%99%82%D2%82%E6' + +const decoded = Encoding.urlDecode(encoded); // $ExpectType number[] +// decoded: [ +// 130, 177, 130, 241, 130, 201, 130, 191, 130, 205, 129, +// 65, 130, 217, 130, 176, 129, 153, 130, 210, 130, 230 +// ] + +const sjisArray8 = [130, 177, 130, 241, 130, 201, 130, 191, 130, 205]; +const encoded2 = Encoding.base64Encode(sjisArray8); // $ExpectType string +// encoded2: 'grGC8YLJgr+CzQ==' + +const decoded2 = Encoding.base64Decode(encoded2); // $ExpectType number[] +// decoded2: [130, 177, 130, 241, 130, 201, 130, 191, 130, 205] + +Encoding.version; // $ExpectType string +Encoding.orders; // $ExpectType string[] + +Encoding.toZenkakuCase([1, 2, 3]); // $ExpectType number[] +Encoding.toZenkakuCase("abcdef"); // $ExpectType string +Encoding.toHankakuCase([1, 2, 3]); // $ExpectType number[] +Encoding.toHankakuCase("ABCDEF"); // $ExpectType string +Encoding.toZenkanaCase([1, 2, 3]); // $ExpectType number[] +Encoding.toZenkanaCase("アイウエオ"); // $ExpectType string +Encoding.toHankanaCase([1, 2, 3]); // $ExpectType number[] +Encoding.toHankanaCase("アイウエオ"); // $ExpectType string +Encoding.toZenkakuSpace([1, 2, 3]); // $ExpectType number[] +Encoding.toZenkakuSpace(" "); // $ExpectType string +Encoding.toHankakuSpace([1, 2, 3]); // $ExpectType number[] +Encoding.toHankakuSpace("\u{3000}"); // $ExpectType string diff --git a/tests/types/encoding-japanese-tests.global.ts b/tests/types/encoding-japanese-tests.global.ts new file mode 100644 index 0000000..6856788 --- /dev/null +++ b/tests/types/encoding-japanese-tests.global.ts @@ -0,0 +1,10 @@ +// just verify we can use exported namespace + +const utf8String = "ã\u0081\u0093ã\u0082\u0093ã\u0081«ã\u0081¡ã\u0081¯"; +const unicodeString = Encoding.convert(utf8String, { + to: "UNICODE", + from: "UTF8", + type: "string", +}); +// $ExpectType string +unicodeString; // こんにちは From a90f8f94095725ef35282e9f8ce2989d64db72a7 Mon Sep 17 00:00:00 2001 From: polygonplanet Date: Thu, 10 Sep 2026 02:52:21 +0900 Subject: [PATCH 2/3] test(types): update type tests to run with tsc - Add "types": "index.d.ts" to package.json - Add "test:types" script to package.json - Add typescript to devDependencies --- package-lock.json | 577 ++++++++++++++++++ package.json | 7 +- tests/types/assertions.d.ts | 19 + tests/types/assertions.test.ts | 21 + tests/types/encoding-japanese-tests.cjs.ts | 103 ++-- tests/types/encoding-japanese-tests.global.ts | 3 +- tests/types/tsconfig.json | 3 + tsconfig.types.json | 15 + 8 files changed, 698 insertions(+), 50 deletions(-) create mode 100644 tests/types/assertions.d.ts create mode 100644 tests/types/assertions.test.ts create mode 100644 tests/types/tsconfig.json create mode 100644 tsconfig.types.json diff --git a/package-lock.json b/package-lock.json index d098114..99f083a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11,12 +11,14 @@ "devDependencies": { "@eslint/js": "^10.0.1", "@stylistic/eslint-plugin": "^5.10.0", + "@types/node": "^26.5.0", "bannerify": "^1.0.1", "browserify": "^17.0.1", "eslint": "^10.9.1", "globals": "^17.12.0", "mocha": "^11.8.0", "package-json-versionify": "^1.0.4", + "typescript": "^7.0.2", "uglify-js": "^3.19.3", "watchify": "^4.0.0" }, @@ -426,6 +428,16 @@ "dev": true, "license": "MIT" }, + "node_modules/@types/node": { + "version": "26.5.0", + "resolved": "https://registry.npmjs.org/@types/node/-/node-26.5.0.tgz", + "integrity": "sha512-dVSGpriSoCgz8WnDNTuSSuSv1PC/ALXihO4ulRZt7Md8k9mlbdin3lGOcDE8SnWOgf513ByWlXd7BK4azmyg/A==", + "dev": true, + "license": "MIT", + "dependencies": { + "undici-types": "~8.9.0" + } + }, "node_modules/@typescript-eslint/types": { "version": "8.69.0", "resolved": "https://registry.npmjs.org/@typescript-eslint/types/-/types-8.69.0.tgz", @@ -440,6 +452,346 @@ "url": "https://opencollective.com/typescript-eslint" } }, + "node_modules/@typescript/typescript-aix-ppc64": { + "version": "7.0.2", + "resolved": "https://registry.npmjs.org/@typescript/typescript-aix-ppc64/-/typescript-aix-ppc64-7.0.2.tgz", + "integrity": "sha512-MTKKkWB7p/0E9xi1d1tHtZ5PiLkGEMIq88pK2CubZjOsLtYTLqhgIgi6zepFa+9GHZ6h05NMCkQxGKiPXMxXtQ==", + "cpu": [ + "ppc64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "aix" + ], + "engines": { + "node": ">=16.20.0" + } + }, + "node_modules/@typescript/typescript-darwin-arm64": { + "version": "7.0.2", + "resolved": "https://registry.npmjs.org/@typescript/typescript-darwin-arm64/-/typescript-darwin-arm64-7.0.2.tgz", + "integrity": "sha512-gowzar9MwS/aRWp6f3a4KUqzRjAZjOsmGNCM6LcTgXum+dBfgsBVMN+AgvOCCbguXyick6LJhpBszxMebJ8syA==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": ">=16.20.0" + } + }, + "node_modules/@typescript/typescript-darwin-x64": { + "version": "7.0.2", + "resolved": "https://registry.npmjs.org/@typescript/typescript-darwin-x64/-/typescript-darwin-x64-7.0.2.tgz", + "integrity": "sha512-SZ9xZInqApNlNGc9s0W1VSsktYSOe9cFqNOIqmN1Gs8SmkjKZYFt017G4VwPxASInODuAdbTW7sXiFUf893RgA==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": ">=16.20.0" + } + }, + "node_modules/@typescript/typescript-freebsd-arm64": { + "version": "7.0.2", + "resolved": "https://registry.npmjs.org/@typescript/typescript-freebsd-arm64/-/typescript-freebsd-arm64-7.0.2.tgz", + "integrity": "sha512-W5NH4y/J0plIIS5b2xvTEkU7JFxyqdMAOgf+Ilhl0vHQXKO5dZoxd+C/jEtq56c4F3wk71RB4BMRQ2XdI+bwYQ==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "freebsd" + ], + "engines": { + "node": ">=16.20.0" + } + }, + "node_modules/@typescript/typescript-freebsd-x64": { + "version": "7.0.2", + "resolved": "https://registry.npmjs.org/@typescript/typescript-freebsd-x64/-/typescript-freebsd-x64-7.0.2.tgz", + "integrity": "sha512-UMGDx5sTpzNw3WiPebH7l90IWfJggEd+egHt/q6p7/Cm3zqoV7VxkGXt+3DxPIw8CcmvAB0j3sVVfbhX+M4Tpw==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "freebsd" + ], + "engines": { + "node": ">=16.20.0" + } + }, + "node_modules/@typescript/typescript-linux-arm": { + "version": "7.0.2", + "resolved": "https://registry.npmjs.org/@typescript/typescript-linux-arm/-/typescript-linux-arm-7.0.2.tgz", + "integrity": "sha512-gffT3xPz9sR7j/YJExkyPntrI0P2EP9XbOyWzth2/Gs0RstK+90RBcO0ncXoXy/beYll1SXw846Nf2zdnEz0QQ==", + "cpu": [ + "arm" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=16.20.0" + } + }, + "node_modules/@typescript/typescript-linux-arm64": { + "version": "7.0.2", + "resolved": "https://registry.npmjs.org/@typescript/typescript-linux-arm64/-/typescript-linux-arm64-7.0.2.tgz", + "integrity": "sha512-Qh4eU4/y3yDjnfjjyPYihMj5/ODIlmt+Bzu17OI+fiSRDW57QmU5SiN63exPRNJPKUzcc1INa1NXdrJ+MqHjUQ==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=16.20.0" + } + }, + "node_modules/@typescript/typescript-linux-loong64": { + "version": "7.0.2", + "resolved": "https://registry.npmjs.org/@typescript/typescript-linux-loong64/-/typescript-linux-loong64-7.0.2.tgz", + "integrity": "sha512-uEHck9i8hoAzXPiYRib1O7miOnz23SxIeVl6F4LXox+qov1K35jHcEW6VHKvZI+pyvl7fZEP4MCU5LYvIq1GuQ==", + "cpu": [ + "loong64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=16.20.0" + } + }, + "node_modules/@typescript/typescript-linux-mips64el": { + "version": "7.0.2", + "resolved": "https://registry.npmjs.org/@typescript/typescript-linux-mips64el/-/typescript-linux-mips64el-7.0.2.tgz", + "integrity": "sha512-R4KvAMnE43W5Qeqb0Ly56O3mWMWIAgsMyz36DCaycd5nbg/9kzm0liw3JocfRqyJY0KPmzFjbswozXyW0DnIYA==", + "cpu": [ + "mips64el" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=16.20.0" + } + }, + "node_modules/@typescript/typescript-linux-ppc64": { + "version": "7.0.2", + "resolved": "https://registry.npmjs.org/@typescript/typescript-linux-ppc64/-/typescript-linux-ppc64-7.0.2.tgz", + "integrity": "sha512-DORx5b3sd/4S7eayxm4FQv+A7CrkUIGRaHiwI8oiHTAI1fAPWhF4J0vAlkC8biAlHSVVwxMQ3tjZ2/DVbnQiiA==", + "cpu": [ + "ppc64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=16.20.0" + } + }, + "node_modules/@typescript/typescript-linux-riscv64": { + "version": "7.0.2", + "resolved": "https://registry.npmjs.org/@typescript/typescript-linux-riscv64/-/typescript-linux-riscv64-7.0.2.tgz", + "integrity": "sha512-wf0jqEDOjrPRnKwYRyyJDRo11KMbvMFrU+q4zqKyChODBzvlkbhNQfKvLxQCcwTpdDaXSHZTVuh0JoCrKCUMHQ==", + "cpu": [ + "riscv64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=16.20.0" + } + }, + "node_modules/@typescript/typescript-linux-s390x": { + "version": "7.0.2", + "resolved": "https://registry.npmjs.org/@typescript/typescript-linux-s390x/-/typescript-linux-s390x-7.0.2.tgz", + "integrity": "sha512-IkwJc3L7yhytWd/ewjyxNDfOmswCm9GWMJT/ue/dU4aZNbwZeYAetq42VyLmsmSjvoX7z74X6ZaYCtzAr0EuGw==", + "cpu": [ + "s390x" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=16.20.0" + } + }, + "node_modules/@typescript/typescript-linux-x64": { + "version": "7.0.2", + "resolved": "https://registry.npmjs.org/@typescript/typescript-linux-x64/-/typescript-linux-x64-7.0.2.tgz", + "integrity": "sha512-EYdf2cNg7rgCWJnxCdJ+F3V39O8ihb37eHAu1LK8oAFizgTQbPOK7zHHXbPt8rX24COqODXeI3sIf0fCXG7H/A==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=16.20.0" + } + }, + "node_modules/@typescript/typescript-netbsd-arm64": { + "version": "7.0.2", + "resolved": "https://registry.npmjs.org/@typescript/typescript-netbsd-arm64/-/typescript-netbsd-arm64-7.0.2.tgz", + "integrity": "sha512-+polYF4MF04aPpO5FTkHran9yUQDSXqy5GiSDKpsll5jy3l3+g9QLhpf39T+ePtefhXLOGrLl0QIjkQP6VnelA==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "netbsd" + ], + "engines": { + "node": ">=16.20.0" + } + }, + "node_modules/@typescript/typescript-netbsd-x64": { + "version": "7.0.2", + "resolved": "https://registry.npmjs.org/@typescript/typescript-netbsd-x64/-/typescript-netbsd-x64-7.0.2.tgz", + "integrity": "sha512-8YIT0EHM/3dq10ZOVF/A7pc/YSMtbcecct4rWtexrnSCHOPcpC2KTLXfTCR6vDpnSiY12heNb1GiN/wu+T/FyA==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "netbsd" + ], + "engines": { + "node": ">=16.20.0" + } + }, + "node_modules/@typescript/typescript-openbsd-arm64": { + "version": "7.0.2", + "resolved": "https://registry.npmjs.org/@typescript/typescript-openbsd-arm64/-/typescript-openbsd-arm64-7.0.2.tgz", + "integrity": "sha512-APT8+ClYnuYm1u9+kgGXoMj2VzWzcymwh2gNSQVySHfkRDGOTVkoWLjCmOQSaO+PoqQ57B0flRp9SA+7GnnkzQ==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "openbsd" + ], + "engines": { + "node": ">=16.20.0" + } + }, + "node_modules/@typescript/typescript-openbsd-x64": { + "version": "7.0.2", + "resolved": "https://registry.npmjs.org/@typescript/typescript-openbsd-x64/-/typescript-openbsd-x64-7.0.2.tgz", + "integrity": "sha512-yX7s+Q0Dln0Dt9tEzZsAjXXR/+ytBM7AlglaqyeMPxQszJ1JhlJdZ6jLA+IzldHtflX81em7lDao1xXu+aRRkg==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "openbsd" + ], + "engines": { + "node": ">=16.20.0" + } + }, + "node_modules/@typescript/typescript-sunos-x64": { + "version": "7.0.2", + "resolved": "https://registry.npmjs.org/@typescript/typescript-sunos-x64/-/typescript-sunos-x64-7.0.2.tgz", + "integrity": "sha512-dLJDGaLZ1D4HPQn62u1n8mBDkJREwMsAkCdkwd4Ieqw+x3TUyTsqY0YiBCtE6H6OzzgGk3iuZ3vFWRS+E8/d1g==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "sunos" + ], + "engines": { + "node": ">=16.20.0" + } + }, + "node_modules/@typescript/typescript-win32-arm64": { + "version": "7.0.2", + "resolved": "https://registry.npmjs.org/@typescript/typescript-win32-arm64/-/typescript-win32-arm64-7.0.2.tgz", + "integrity": "sha512-Gyl1Vy6OsWesLzmq+EP0Fb7b4Nid5232AvcA2SFcdYreldpNtYFFofPjnt62y9hQy7VTaZp65ICJjuAQRaVcIQ==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">=16.20.0" + } + }, + "node_modules/@typescript/typescript-win32-x64": { + "version": "7.0.2", + "resolved": "https://registry.npmjs.org/@typescript/typescript-win32-x64/-/typescript-win32-x64-7.0.2.tgz", + "integrity": "sha512-0BQ3HkAHHlKLSp1qRvf3SUhGpGsDuhB/jgFw75guyqbxJqEaS0Cw/VFO8i2nHglJUzQCRtMMR/IBAKE3ETMC4g==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">=16.20.0" + } + }, "node_modules/acorn": { "version": "7.4.1", "resolved": "https://registry.npmjs.org/acorn/-/acorn-7.4.1.tgz", @@ -4170,6 +4522,41 @@ "integrity": "sha1-hnrHTjhkGHsdPUfZlqeOxciDB3c=", "dev": true }, + "node_modules/typescript": { + "version": "7.0.2", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-7.0.2.tgz", + "integrity": "sha512-8FYau96o3NKOhbjKi/qNvG/W5jhzxkbdm5sj9AbZ/5T5sWqn3hJgLfGx27sRKZWTvyzCP8dLRBTf5tBTSRVUNA==", + "dev": true, + "license": "Apache-2.0", + "bin": { + "tsc": "bin/tsc" + }, + "engines": { + "node": ">=16.20.0" + }, + "optionalDependencies": { + "@typescript/typescript-aix-ppc64": "7.0.2", + "@typescript/typescript-darwin-arm64": "7.0.2", + "@typescript/typescript-darwin-x64": "7.0.2", + "@typescript/typescript-freebsd-arm64": "7.0.2", + "@typescript/typescript-freebsd-x64": "7.0.2", + "@typescript/typescript-linux-arm": "7.0.2", + "@typescript/typescript-linux-arm64": "7.0.2", + "@typescript/typescript-linux-loong64": "7.0.2", + "@typescript/typescript-linux-mips64el": "7.0.2", + "@typescript/typescript-linux-ppc64": "7.0.2", + "@typescript/typescript-linux-riscv64": "7.0.2", + "@typescript/typescript-linux-s390x": "7.0.2", + "@typescript/typescript-linux-x64": "7.0.2", + "@typescript/typescript-netbsd-arm64": "7.0.2", + "@typescript/typescript-netbsd-x64": "7.0.2", + "@typescript/typescript-openbsd-arm64": "7.0.2", + "@typescript/typescript-openbsd-x64": "7.0.2", + "@typescript/typescript-sunos-x64": "7.0.2", + "@typescript/typescript-win32-arm64": "7.0.2", + "@typescript/typescript-win32-x64": "7.0.2" + } + }, "node_modules/uglify-js": { "version": "3.19.3", "resolved": "https://registry.npmjs.org/uglify-js/-/uglify-js-3.19.3.tgz", @@ -4208,6 +4595,13 @@ "undeclared-identifiers": "bin.js" } }, + "node_modules/undici-types": { + "version": "8.9.0", + "resolved": "https://registry.npmjs.org/undici-types/-/undici-types-8.9.0.tgz", + "integrity": "sha512-KTDyRTYX8sWmKXAikPHHSyc63CRPETMctyjKFupcC6OBLXT3xsN0e9aF7m+mIXutFWpUXuedtowG7iLOzp0kQg==", + "dev": true, + "license": "MIT" + }, "node_modules/uri-js": { "version": "4.4.1", "resolved": "https://registry.npmjs.org/uri-js/-/uri-js-4.4.1.tgz", @@ -4756,12 +5150,161 @@ "integrity": "sha512-5+fP8P8MFNC+AyZCDxrB2pkZFPGzqQWUzpSeuuVLvm8VMcorNYavBqoFcxK8bQz4Qsbn4oUEEem4wDLfcysGHA==", "dev": true }, + "@types/node": { + "version": "26.5.0", + "resolved": "https://registry.npmjs.org/@types/node/-/node-26.5.0.tgz", + "integrity": "sha512-dVSGpriSoCgz8WnDNTuSSuSv1PC/ALXihO4ulRZt7Md8k9mlbdin3lGOcDE8SnWOgf513ByWlXd7BK4azmyg/A==", + "dev": true, + "requires": { + "undici-types": "~8.9.0" + } + }, "@typescript-eslint/types": { "version": "8.69.0", "resolved": "https://registry.npmjs.org/@typescript-eslint/types/-/types-8.69.0.tgz", "integrity": "sha512-K3VrubUPhlo9VDBS6QdI8YB5j7ClpqLRdefcz6PFrhnwicehBweqQ9Evhl4l+FYz0HdDmMqIiSX0aldGRYtDCA==", "dev": true }, + "@typescript/typescript-aix-ppc64": { + "version": "7.0.2", + "resolved": "https://registry.npmjs.org/@typescript/typescript-aix-ppc64/-/typescript-aix-ppc64-7.0.2.tgz", + "integrity": "sha512-MTKKkWB7p/0E9xi1d1tHtZ5PiLkGEMIq88pK2CubZjOsLtYTLqhgIgi6zepFa+9GHZ6h05NMCkQxGKiPXMxXtQ==", + "dev": true, + "optional": true + }, + "@typescript/typescript-darwin-arm64": { + "version": "7.0.2", + "resolved": "https://registry.npmjs.org/@typescript/typescript-darwin-arm64/-/typescript-darwin-arm64-7.0.2.tgz", + "integrity": "sha512-gowzar9MwS/aRWp6f3a4KUqzRjAZjOsmGNCM6LcTgXum+dBfgsBVMN+AgvOCCbguXyick6LJhpBszxMebJ8syA==", + "dev": true, + "optional": true + }, + "@typescript/typescript-darwin-x64": { + "version": "7.0.2", + "resolved": "https://registry.npmjs.org/@typescript/typescript-darwin-x64/-/typescript-darwin-x64-7.0.2.tgz", + "integrity": "sha512-SZ9xZInqApNlNGc9s0W1VSsktYSOe9cFqNOIqmN1Gs8SmkjKZYFt017G4VwPxASInODuAdbTW7sXiFUf893RgA==", + "dev": true, + "optional": true + }, + "@typescript/typescript-freebsd-arm64": { + "version": "7.0.2", + "resolved": "https://registry.npmjs.org/@typescript/typescript-freebsd-arm64/-/typescript-freebsd-arm64-7.0.2.tgz", + "integrity": "sha512-W5NH4y/J0plIIS5b2xvTEkU7JFxyqdMAOgf+Ilhl0vHQXKO5dZoxd+C/jEtq56c4F3wk71RB4BMRQ2XdI+bwYQ==", + "dev": true, + "optional": true + }, + "@typescript/typescript-freebsd-x64": { + "version": "7.0.2", + "resolved": "https://registry.npmjs.org/@typescript/typescript-freebsd-x64/-/typescript-freebsd-x64-7.0.2.tgz", + "integrity": "sha512-UMGDx5sTpzNw3WiPebH7l90IWfJggEd+egHt/q6p7/Cm3zqoV7VxkGXt+3DxPIw8CcmvAB0j3sVVfbhX+M4Tpw==", + "dev": true, + "optional": true + }, + "@typescript/typescript-linux-arm": { + "version": "7.0.2", + "resolved": "https://registry.npmjs.org/@typescript/typescript-linux-arm/-/typescript-linux-arm-7.0.2.tgz", + "integrity": "sha512-gffT3xPz9sR7j/YJExkyPntrI0P2EP9XbOyWzth2/Gs0RstK+90RBcO0ncXoXy/beYll1SXw846Nf2zdnEz0QQ==", + "dev": true, + "optional": true + }, + "@typescript/typescript-linux-arm64": { + "version": "7.0.2", + "resolved": "https://registry.npmjs.org/@typescript/typescript-linux-arm64/-/typescript-linux-arm64-7.0.2.tgz", + "integrity": "sha512-Qh4eU4/y3yDjnfjjyPYihMj5/ODIlmt+Bzu17OI+fiSRDW57QmU5SiN63exPRNJPKUzcc1INa1NXdrJ+MqHjUQ==", + "dev": true, + "optional": true + }, + "@typescript/typescript-linux-loong64": { + "version": "7.0.2", + "resolved": "https://registry.npmjs.org/@typescript/typescript-linux-loong64/-/typescript-linux-loong64-7.0.2.tgz", + "integrity": "sha512-uEHck9i8hoAzXPiYRib1O7miOnz23SxIeVl6F4LXox+qov1K35jHcEW6VHKvZI+pyvl7fZEP4MCU5LYvIq1GuQ==", + "dev": true, + "optional": true + }, + "@typescript/typescript-linux-mips64el": { + "version": "7.0.2", + "resolved": "https://registry.npmjs.org/@typescript/typescript-linux-mips64el/-/typescript-linux-mips64el-7.0.2.tgz", + "integrity": "sha512-R4KvAMnE43W5Qeqb0Ly56O3mWMWIAgsMyz36DCaycd5nbg/9kzm0liw3JocfRqyJY0KPmzFjbswozXyW0DnIYA==", + "dev": true, + "optional": true + }, + "@typescript/typescript-linux-ppc64": { + "version": "7.0.2", + "resolved": "https://registry.npmjs.org/@typescript/typescript-linux-ppc64/-/typescript-linux-ppc64-7.0.2.tgz", + "integrity": "sha512-DORx5b3sd/4S7eayxm4FQv+A7CrkUIGRaHiwI8oiHTAI1fAPWhF4J0vAlkC8biAlHSVVwxMQ3tjZ2/DVbnQiiA==", + "dev": true, + "optional": true + }, + "@typescript/typescript-linux-riscv64": { + "version": "7.0.2", + "resolved": "https://registry.npmjs.org/@typescript/typescript-linux-riscv64/-/typescript-linux-riscv64-7.0.2.tgz", + "integrity": "sha512-wf0jqEDOjrPRnKwYRyyJDRo11KMbvMFrU+q4zqKyChODBzvlkbhNQfKvLxQCcwTpdDaXSHZTVuh0JoCrKCUMHQ==", + "dev": true, + "optional": true + }, + "@typescript/typescript-linux-s390x": { + "version": "7.0.2", + "resolved": "https://registry.npmjs.org/@typescript/typescript-linux-s390x/-/typescript-linux-s390x-7.0.2.tgz", + "integrity": "sha512-IkwJc3L7yhytWd/ewjyxNDfOmswCm9GWMJT/ue/dU4aZNbwZeYAetq42VyLmsmSjvoX7z74X6ZaYCtzAr0EuGw==", + "dev": true, + "optional": true + }, + "@typescript/typescript-linux-x64": { + "version": "7.0.2", + "resolved": "https://registry.npmjs.org/@typescript/typescript-linux-x64/-/typescript-linux-x64-7.0.2.tgz", + "integrity": "sha512-EYdf2cNg7rgCWJnxCdJ+F3V39O8ihb37eHAu1LK8oAFizgTQbPOK7zHHXbPt8rX24COqODXeI3sIf0fCXG7H/A==", + "dev": true, + "optional": true + }, + "@typescript/typescript-netbsd-arm64": { + "version": "7.0.2", + "resolved": "https://registry.npmjs.org/@typescript/typescript-netbsd-arm64/-/typescript-netbsd-arm64-7.0.2.tgz", + "integrity": "sha512-+polYF4MF04aPpO5FTkHran9yUQDSXqy5GiSDKpsll5jy3l3+g9QLhpf39T+ePtefhXLOGrLl0QIjkQP6VnelA==", + "dev": true, + "optional": true + }, + "@typescript/typescript-netbsd-x64": { + "version": "7.0.2", + "resolved": "https://registry.npmjs.org/@typescript/typescript-netbsd-x64/-/typescript-netbsd-x64-7.0.2.tgz", + "integrity": "sha512-8YIT0EHM/3dq10ZOVF/A7pc/YSMtbcecct4rWtexrnSCHOPcpC2KTLXfTCR6vDpnSiY12heNb1GiN/wu+T/FyA==", + "dev": true, + "optional": true + }, + "@typescript/typescript-openbsd-arm64": { + "version": "7.0.2", + "resolved": "https://registry.npmjs.org/@typescript/typescript-openbsd-arm64/-/typescript-openbsd-arm64-7.0.2.tgz", + "integrity": "sha512-APT8+ClYnuYm1u9+kgGXoMj2VzWzcymwh2gNSQVySHfkRDGOTVkoWLjCmOQSaO+PoqQ57B0flRp9SA+7GnnkzQ==", + "dev": true, + "optional": true + }, + "@typescript/typescript-openbsd-x64": { + "version": "7.0.2", + "resolved": "https://registry.npmjs.org/@typescript/typescript-openbsd-x64/-/typescript-openbsd-x64-7.0.2.tgz", + "integrity": "sha512-yX7s+Q0Dln0Dt9tEzZsAjXXR/+ytBM7AlglaqyeMPxQszJ1JhlJdZ6jLA+IzldHtflX81em7lDao1xXu+aRRkg==", + "dev": true, + "optional": true + }, + "@typescript/typescript-sunos-x64": { + "version": "7.0.2", + "resolved": "https://registry.npmjs.org/@typescript/typescript-sunos-x64/-/typescript-sunos-x64-7.0.2.tgz", + "integrity": "sha512-dLJDGaLZ1D4HPQn62u1n8mBDkJREwMsAkCdkwd4Ieqw+x3TUyTsqY0YiBCtE6H6OzzgGk3iuZ3vFWRS+E8/d1g==", + "dev": true, + "optional": true + }, + "@typescript/typescript-win32-arm64": { + "version": "7.0.2", + "resolved": "https://registry.npmjs.org/@typescript/typescript-win32-arm64/-/typescript-win32-arm64-7.0.2.tgz", + "integrity": "sha512-Gyl1Vy6OsWesLzmq+EP0Fb7b4Nid5232AvcA2SFcdYreldpNtYFFofPjnt62y9hQy7VTaZp65ICJjuAQRaVcIQ==", + "dev": true, + "optional": true + }, + "@typescript/typescript-win32-x64": { + "version": "7.0.2", + "resolved": "https://registry.npmjs.org/@typescript/typescript-win32-x64/-/typescript-win32-x64-7.0.2.tgz", + "integrity": "sha512-0BQ3HkAHHlKLSp1qRvf3SUhGpGsDuhB/jgFw75guyqbxJqEaS0Cw/VFO8i2nHglJUzQCRtMMR/IBAKE3ETMC4g==", + "dev": true, + "optional": true + }, "acorn": { "version": "7.4.1", "resolved": "https://registry.npmjs.org/acorn/-/acorn-7.4.1.tgz", @@ -7748,6 +8291,34 @@ "integrity": "sha1-hnrHTjhkGHsdPUfZlqeOxciDB3c=", "dev": true }, + "typescript": { + "version": "7.0.2", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-7.0.2.tgz", + "integrity": "sha512-8FYau96o3NKOhbjKi/qNvG/W5jhzxkbdm5sj9AbZ/5T5sWqn3hJgLfGx27sRKZWTvyzCP8dLRBTf5tBTSRVUNA==", + "dev": true, + "requires": { + "@typescript/typescript-aix-ppc64": "7.0.2", + "@typescript/typescript-darwin-arm64": "7.0.2", + "@typescript/typescript-darwin-x64": "7.0.2", + "@typescript/typescript-freebsd-arm64": "7.0.2", + "@typescript/typescript-freebsd-x64": "7.0.2", + "@typescript/typescript-linux-arm": "7.0.2", + "@typescript/typescript-linux-arm64": "7.0.2", + "@typescript/typescript-linux-loong64": "7.0.2", + "@typescript/typescript-linux-mips64el": "7.0.2", + "@typescript/typescript-linux-ppc64": "7.0.2", + "@typescript/typescript-linux-riscv64": "7.0.2", + "@typescript/typescript-linux-s390x": "7.0.2", + "@typescript/typescript-linux-x64": "7.0.2", + "@typescript/typescript-netbsd-arm64": "7.0.2", + "@typescript/typescript-netbsd-x64": "7.0.2", + "@typescript/typescript-openbsd-arm64": "7.0.2", + "@typescript/typescript-openbsd-x64": "7.0.2", + "@typescript/typescript-sunos-x64": "7.0.2", + "@typescript/typescript-win32-arm64": "7.0.2", + "@typescript/typescript-win32-x64": "7.0.2" + } + }, "uglify-js": { "version": "3.19.3", "resolved": "https://registry.npmjs.org/uglify-js/-/uglify-js-3.19.3.tgz", @@ -7773,6 +8344,12 @@ "xtend": "^4.0.1" } }, + "undici-types": { + "version": "8.9.0", + "resolved": "https://registry.npmjs.org/undici-types/-/undici-types-8.9.0.tgz", + "integrity": "sha512-KTDyRTYX8sWmKXAikPHHSyc63CRPETMctyjKFupcC6OBLXT3xsN0e9aF7m+mIXutFWpUXuedtowG7iLOzp0kQg==", + "dev": true + }, "uri-js": { "version": "4.4.1", "resolved": "https://registry.npmjs.org/uri-js/-/uri-js-4.4.1.tgz", diff --git a/package.json b/package.json index 878411b..82eed92 100644 --- a/package.json +++ b/package.json @@ -3,9 +3,11 @@ "version": "2.3.0", "description": "Convert and detect character encoding in JavaScript", "main": "src/index.js", + "types": "index.d.ts", "files": [ "encoding.js", "encoding.min.js", + "index.d.ts", "src/*" ], "scripts": { @@ -13,7 +15,8 @@ "compile": "browserify src/index.js -o encoding.js -s Encoding -p [ bannerify --file src/banner.js ] --no-bundle-external --bare", "minify": "uglifyjs encoding.js -o encoding.min.js --comments -c -m -b ascii_only=true,beautify=false", "lint": "eslint .", - "test": "npm run build && mocha tests/test", + "test": "npm run build && npm run test:types && mocha tests/test", + "test:types": "tsc --project tsconfig.types.json", "watch": "watchify src/index.js -o encoding.js -s Encoding -p [ bannerify --file src/banner.js ] --no-bundle-external --bare --poll=300 -v" }, "engines": { @@ -54,12 +57,14 @@ "devDependencies": { "@eslint/js": "^10.0.1", "@stylistic/eslint-plugin": "^5.10.0", + "@types/node": "^26.5.0", "bannerify": "^1.0.1", "browserify": "^17.0.1", "eslint": "^10.9.1", "globals": "^17.12.0", "mocha": "^11.8.0", "package-json-versionify": "^1.0.4", + "typescript": "^7.0.2", "uglify-js": "^3.19.3", "watchify": "^4.0.0" }, diff --git a/tests/types/assertions.d.ts b/tests/types/assertions.d.ts new file mode 100644 index 0000000..ed4cfec --- /dev/null +++ b/tests/types/assertions.d.ts @@ -0,0 +1,19 @@ +/** + * Checks whether type A is equal to type B + * See also: https://github.com/Microsoft/TypeScript/issues/27024#issuecomment-421529650 + */ +type Equals = + (() => T extends A ? 1 : 2) extends + (() => T extends B ? 1 : 2) ? true : false; + +/** + * Raises a type error if Actual type is not equal to Expected type + * + * @example + * expectType()("test"); // Passes if the value is of type string + */ +declare function expectType(): ( + value: Actual, + // Raise a type error by requiring an impossible second argument + ...notEqual: Equals extends true ? [] : [never] +) => void; diff --git a/tests/types/assertions.test.ts b/tests/types/assertions.test.ts new file mode 100644 index 0000000..719f946 --- /dev/null +++ b/tests/types/assertions.test.ts @@ -0,0 +1,21 @@ +export {}; + +declare const stringType: string; +declare const anyType: any; +declare const unionType: string | number; +declare const neverType: never; + +expectType()(stringType); + +// @ts-expect-error +expectType()(stringType); +// @ts-expect-error +expectType()(anyType); +// @ts-expect-error +expectType()(unionType); +// @ts-expect-error +expectType()(stringType); +// @ts-expect-error +expectType()(neverType); +// @ts-expect-error +expectType()(stringType); diff --git a/tests/types/encoding-japanese-tests.cjs.ts b/tests/types/encoding-japanese-tests.cjs.ts index 06d09e7..a6ac372 100644 --- a/tests/types/encoding-japanese-tests.cjs.ts +++ b/tests/types/encoding-japanese-tests.cjs.ts @@ -1,5 +1,5 @@ /// -import Encoding = require("encoding-japanese"); +import Encoding = require("../.."); // Convert character encoding to Shift_JIS from UTF-8. const utf8Array_1 = new Uint8Array([1, 2, 3]); @@ -11,26 +11,29 @@ const utf8Array_6 = new Int16Array([1, 2, 3]); const utf8Array_7 = new Int32Array([1, 2, 3]); const utf8Array_8 = new Int8Array([1, 2, 3]); const utf8Array_9: readonly number[] = [1, 2, 3]; -Encoding.convert(utf8Array_1, "SJIS", "UTF8"); // $ExpectType number[] -Encoding.convert(utf8Array_2, "UTF16", "UTF8"); // $ExpectType number[] -Encoding.convert(utf8Array_3, "UTF16LE", "UTF8"); // $ExpectType number[] -Encoding.convert(utf8Array_4, "UTF16BE", "UTF8"); // $ExpectType number[] -Encoding.convert(utf8Array_5, "BINARY", "UTF8"); // $ExpectType number[] -Encoding.convert(utf8Array_6, "UTF32", "UTF8"); // $ExpectType number[] -Encoding.convert(utf8Array_7, "AUTO", "UTF8"); // $ExpectType number[] -Encoding.convert(utf8Array_8, "ASCII", "UTF8"); // $ExpectType number[] -Encoding.convert(utf8Array_9, "JIS", "UTF8"); // $ExpectType number[] -Encoding.convert("string value", "UTF16", "UTF8"); // $ExpectType string +expectType()(Encoding.convert(utf8Array_1, "SJIS", "UTF8")); +expectType()(Encoding.convert(utf8Array_2, "UTF16", "UTF8")); +expectType()(Encoding.convert(utf8Array_3, "UTF16LE", "UTF8")); +expectType()(Encoding.convert(utf8Array_4, "UTF16BE", "UTF8")); +expectType()(Encoding.convert(utf8Array_5, "BINARY", "UTF8")); +expectType()(Encoding.convert(utf8Array_6, "UTF32", "UTF8")); +expectType()(Encoding.convert(utf8Array_7, "AUTO", "UTF8")); +expectType()(Encoding.convert(utf8Array_8, "ASCII", "UTF8")); +expectType()(Encoding.convert(utf8Array_9, "JIS", "UTF8")); +expectType()(Encoding.convert("string value", "UTF16", "UTF8")); // Convert character encoding by automatic detection (AUTO detect). const utf8Array = utf8Array_1; -const sjisArray1 = Encoding.convert(utf8Array, "SJIS"); // $ExpectType number[] +const sjisArray1 = Encoding.convert(utf8Array, "SJIS"); +expectType()(sjisArray1); // or -const sjisArray2 = Encoding.convert(utf8Array, "SJIS", "AUTO"); // $ExpectType number[] +const sjisArray2 = Encoding.convert(utf8Array, "SJIS", "AUTO"); +expectType()(sjisArray2); // Detect the character encoding. // The return value be one of the "Available Encodings" below. -const detected = Encoding.detect(utf8Array); // $ExpectType EncodingDetection +const detected = Encoding.detect(utf8Array); +expectType()(detected); if (detected === "UTF8") { // Encoding is UTF-8' } @@ -39,7 +42,7 @@ const sjisArray3 = Encoding.convert(utf8Array, { to: "SJIS", // to_encoding from: "UTF8", // from_encoding }); -sjisArray3; // $ExpectType number[] +expectType()(sjisArray3); const sjisArray4 = Encoding.convert("🐙", { to: "SJIS", // to_encoding @@ -47,7 +50,7 @@ const sjisArray4 = Encoding.convert("🐙", { type: "string", fallback: "html-entity", }); -sjisArray4; // $ExpectType string +expectType()(sjisArray4); // fallback: '🐙' const sjisArray5 = Encoding.convert("🐙", { @@ -56,7 +59,7 @@ const sjisArray5 = Encoding.convert("🐙", { type: "string", fallback: "ignore", }); -sjisArray5; // $ExpectType string +expectType()(sjisArray5); // fallback: '' const sjisArray6 = Encoding.convert("🐙", { @@ -65,7 +68,7 @@ const sjisArray6 = Encoding.convert("🐙", { type: "string", fallback: "error", }); -sjisArray6; // $ExpectType string +expectType()(sjisArray6); // throws error: `Character cannot be represented` const utf8String = "ã\u0081\u0093ã\u0082\u0093ã\u0081«ã\u0081¡ã\u0081¯"; @@ -74,33 +77,33 @@ const unicodeString = Encoding.convert(utf8String, { from: "UTF8", type: "string", // Specify 'string' type. (Return as string) }); -unicodeString; // $ExpectType string +expectType()(unicodeString); const unicodeString2 = Encoding.convert(utf8String, { to: "UNICODE", from: "UTF8", // Unspecified type. Inferred from 1st argument }); -unicodeString2; // $ExpectType string +expectType()(unicodeString2); const utf16Array = Encoding.convert(utf8Array, { to: "UTF16", // to_encoding from: "UTF8", // from_encoding bom: true, // With BOM }); -utf16Array; // $ExpectType number[] +expectType()(utf16Array); const utf16leArray = Encoding.convert(utf8Array, { to: "UTF16", // to_encoding from: "UTF8", // from_encoding bom: "LE", // With BOM (little-endian) }); -utf16leArray; // $ExpectType number[] +expectType()(utf16leArray); const utf16beArray = Encoding.convert(utf8Array, { to: "UTF16BE", from: "UTF8", }); -utf16beArray; // $ExpectType number[] +expectType()(utf16beArray); const utf16Array2 = Encoding.convert(utf8Array, { to: "UTF16", // to_encoding @@ -108,7 +111,7 @@ const utf16Array2 = Encoding.convert(utf8Array, { type: "array", bom: true, // With BOM }); -utf16Array2; // $ExpectType number[] +expectType()(utf16Array2); const utf16Array3 = Encoding.convert(utf8Array, { to: "UTF16", // to_encoding @@ -116,7 +119,7 @@ const utf16Array3 = Encoding.convert(utf8Array, { type: "arraybuffer", bom: true, // With BOM }); -utf16Array3; // $ExpectType Uint16Array || Uint16Array +expectType()(utf16Array3); const utf16Array4 = Encoding.convert(utf8Array, { to: "UTF16", // to_encoding @@ -124,16 +127,18 @@ const utf16Array4 = Encoding.convert(utf8Array, { type: "string", bom: true, // With BOM }); -utf16Array4; // $ExpectType string +expectType()(utf16Array4); // Detect character encoding by automatic. (AUTO detect). -const detected2 = Encoding.detect(utf8Array); // $ExpectType EncodingDetection +const detected2 = Encoding.detect(utf8Array); +expectType()(detected2); if (detected2 === "UTF8") { // Encoding is UTF-8' } // Detect character encoding by specific encoding name. -const isSJIS = Encoding.detect(sjisArray1, "SJIS"); // $ExpectType EncodingDetection +const isSJIS = Encoding.detect(sjisArray1, "SJIS"); +expectType()(isSJIS); if (isSJIS) { // Encoding is SJIS' } @@ -163,34 +168,38 @@ const sjisArray7 = [ 230, ]; -const encoded = Encoding.urlEncode(sjisArray7); // $ExpectType string +const encoded = Encoding.urlEncode(sjisArray7); +expectType()(encoded); // encoded: '%82%B1%82%F1%82%C9%82%BF%82%CD%81A%82%D9%82%B0%81%99%82%D2%82%E6' -const decoded = Encoding.urlDecode(encoded); // $ExpectType number[] +const decoded = Encoding.urlDecode(encoded); +expectType()(decoded); // decoded: [ // 130, 177, 130, 241, 130, 201, 130, 191, 130, 205, 129, // 65, 130, 217, 130, 176, 129, 153, 130, 210, 130, 230 // ] const sjisArray8 = [130, 177, 130, 241, 130, 201, 130, 191, 130, 205]; -const encoded2 = Encoding.base64Encode(sjisArray8); // $ExpectType string +const encoded2 = Encoding.base64Encode(sjisArray8); +expectType()(encoded2); // encoded2: 'grGC8YLJgr+CzQ==' -const decoded2 = Encoding.base64Decode(encoded2); // $ExpectType number[] +const decoded2 = Encoding.base64Decode(encoded2); +expectType()(decoded2); // decoded2: [130, 177, 130, 241, 130, 201, 130, 191, 130, 205] -Encoding.version; // $ExpectType string -Encoding.orders; // $ExpectType string[] - -Encoding.toZenkakuCase([1, 2, 3]); // $ExpectType number[] -Encoding.toZenkakuCase("abcdef"); // $ExpectType string -Encoding.toHankakuCase([1, 2, 3]); // $ExpectType number[] -Encoding.toHankakuCase("ABCDEF"); // $ExpectType string -Encoding.toZenkanaCase([1, 2, 3]); // $ExpectType number[] -Encoding.toZenkanaCase("アイウエオ"); // $ExpectType string -Encoding.toHankanaCase([1, 2, 3]); // $ExpectType number[] -Encoding.toHankanaCase("アイウエオ"); // $ExpectType string -Encoding.toZenkakuSpace([1, 2, 3]); // $ExpectType number[] -Encoding.toZenkakuSpace(" "); // $ExpectType string -Encoding.toHankakuSpace([1, 2, 3]); // $ExpectType number[] -Encoding.toHankakuSpace("\u{3000}"); // $ExpectType string +expectType()(Encoding.version); +expectType()(Encoding.orders); + +expectType()(Encoding.toZenkakuCase([1, 2, 3])); +expectType()(Encoding.toZenkakuCase("abcdef")); +expectType()(Encoding.toHankakuCase([1, 2, 3])); +expectType()(Encoding.toHankakuCase("ABCDEF")); +expectType()(Encoding.toZenkanaCase([1, 2, 3])); +expectType()(Encoding.toZenkanaCase("アイウエオ")); +expectType()(Encoding.toHankanaCase([1, 2, 3])); +expectType()(Encoding.toHankanaCase("アイウエオ")); +expectType()(Encoding.toZenkakuSpace([1, 2, 3])); +expectType()(Encoding.toZenkakuSpace(" ")); +expectType()(Encoding.toHankakuSpace([1, 2, 3])); +expectType()(Encoding.toHankakuSpace("\u{3000}")); diff --git a/tests/types/encoding-japanese-tests.global.ts b/tests/types/encoding-japanese-tests.global.ts index 6856788..ecb5d2e 100644 --- a/tests/types/encoding-japanese-tests.global.ts +++ b/tests/types/encoding-japanese-tests.global.ts @@ -6,5 +6,4 @@ const unicodeString = Encoding.convert(utf8String, { from: "UTF8", type: "string", }); -// $ExpectType string -unicodeString; // こんにちは +expectType()(unicodeString); // こんにちは diff --git a/tests/types/tsconfig.json b/tests/types/tsconfig.json new file mode 100644 index 0000000..92fa841 --- /dev/null +++ b/tests/types/tsconfig.json @@ -0,0 +1,3 @@ +{ + "extends": "../../tsconfig.types.json" +} diff --git a/tsconfig.types.json b/tsconfig.types.json new file mode 100644 index 0000000..7e47b0f --- /dev/null +++ b/tsconfig.types.json @@ -0,0 +1,15 @@ +{ + "compilerOptions": { + "target": "ES2022", + "module": "Node18", + "moduleResolution": "Node16", + "moduleDetection": "legacy", + "lib": ["ES2022"], + "types": [], + "strict": true, + "noEmit": true, + "skipLibCheck": false, + "forceConsistentCasingInFileNames": true + }, + "include": ["index.d.ts", "tests/types/**/*.ts"] +} From dec226e441ee0a0274a1d8358727182f8cc557d4 Mon Sep 17 00:00:00 2001 From: polygonplanet Date: Fri, 11 Sep 2026 00:46:46 +0900 Subject: [PATCH 3/3] docs(license): add third-party notice for DefinitelyTyped type definitions --- LICENSE | 26 +++++++++++++++++++++++++- index.d.ts | 5 +++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/LICENSE b/LICENSE index 3244f30..e2a356d 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2012 polygonplanet +Copyright (c) 2012-present polygonplanet Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal @@ -19,3 +19,27 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +----- + +Third-Party Licenses and Credits + +TypeScript declarations and type tests are based on the type definitions for +encoding-japanese from DefinitelyTyped, licensed under the MIT License. + +Upstream package: @types/encoding-japanese 2.2.1 + +The LICENSE distributed with @types/encoding-japanese states: + + Copyright (c) Microsoft Corporation. + +The LICENSE in the DefinitelyTyped repository states: + + Copyrights are respective of each contributor listed at the beginning of + each definition file. + +The contributors credited in the upstream package are: + rhysd (https://github.com/rhysd) + Piotr Błażejewicz (https://github.com/peterblazejewicz) + +The MIT License terms stated above apply equally to these files. diff --git a/index.d.ts b/index.d.ts index 59191e8..fc24067 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,3 +1,8 @@ +// TypeScript declarations originally from DefinitelyTyped +// Upstream package: @types/encoding-japanese 2.2.1 +// Thanks to the DefinitelyTyped contributors +// Licensed under the MIT License. See the LICENSE file + export as namespace Encoding; export type Encoding =