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 new file mode 100644 index 0000000..fc24067 --- /dev/null +++ b/index.d.ts @@ -0,0 +1,102 @@ +// 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 = + | "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/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 new file mode 100644 index 0000000..a6ac372 --- /dev/null +++ b/tests/types/encoding-japanese-tests.cjs.ts @@ -0,0 +1,205 @@ +/// +import Encoding = require("../.."); + +// 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]; +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()(sjisArray1); +// or +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()(detected); +if (detected === "UTF8") { + // Encoding is UTF-8' +} + +const sjisArray3 = Encoding.convert(utf8Array, { + to: "SJIS", // to_encoding + from: "UTF8", // from_encoding +}); +expectType()(sjisArray3); + +const sjisArray4 = Encoding.convert("🐙", { + to: "SJIS", // to_encoding + from: "UTF8", // from_encoding + type: "string", + fallback: "html-entity", +}); +expectType()(sjisArray4); +// fallback: '🐙' + +const sjisArray5 = Encoding.convert("🐙", { + to: "SJIS", // to_encoding + from: "UTF8", // from_encoding + type: "string", + fallback: "ignore", +}); +expectType()(sjisArray5); +// fallback: '' + +const sjisArray6 = Encoding.convert("🐙", { + to: "SJIS", // to_encoding + from: "UTF8", // from_encoding + type: "string", + fallback: "error", +}); +expectType()(sjisArray6); +// 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) +}); +expectType()(unicodeString); + +const unicodeString2 = Encoding.convert(utf8String, { + to: "UNICODE", + from: "UTF8", // Unspecified type. Inferred from 1st argument +}); +expectType()(unicodeString2); + +const utf16Array = Encoding.convert(utf8Array, { + to: "UTF16", // to_encoding + from: "UTF8", // from_encoding + bom: true, // With BOM +}); +expectType()(utf16Array); + +const utf16leArray = Encoding.convert(utf8Array, { + to: "UTF16", // to_encoding + from: "UTF8", // from_encoding + bom: "LE", // With BOM (little-endian) +}); +expectType()(utf16leArray); + +const utf16beArray = Encoding.convert(utf8Array, { + to: "UTF16BE", + from: "UTF8", +}); +expectType()(utf16beArray); + +const utf16Array2 = Encoding.convert(utf8Array, { + to: "UTF16", // to_encoding + from: "UTF8", // from_encoding + type: "array", + bom: true, // With BOM +}); +expectType()(utf16Array2); + +const utf16Array3 = Encoding.convert(utf8Array, { + to: "UTF16", // to_encoding + from: "UTF8", // from_encoding + type: "arraybuffer", + bom: true, // With BOM +}); +expectType()(utf16Array3); + +const utf16Array4 = Encoding.convert(utf8Array, { + to: "UTF16", // to_encoding + from: "UTF8", // from_encoding + type: "string", + bom: true, // With BOM +}); +expectType()(utf16Array4); + +// Detect character encoding by automatic. (AUTO detect). +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()(isSJIS); +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()(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()(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()(encoded2); +// encoded2: 'grGC8YLJgr+CzQ==' + +const decoded2 = Encoding.base64Decode(encoded2); +expectType()(decoded2); +// decoded2: [130, 177, 130, 241, 130, 201, 130, 191, 130, 205] + +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 new file mode 100644 index 0000000..ecb5d2e --- /dev/null +++ b/tests/types/encoding-japanese-tests.global.ts @@ -0,0 +1,9 @@ +// 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()(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"] +}