-
Notifications
You must be signed in to change notification settings - Fork 292
improve typescript typings #330
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
plbstl
wants to merge
34
commits into
pillarjs:master
Choose a base branch
from
plbstl:typedefs/improve-typings
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
34 commits
Select commit
Hold shift + click to select a range
8cef837
add jsDocs
plbstl f74482d
separate encoding `Options` to `DecodeOptions` and `EncodeOptions`
plbstl 2cc3a01
add typedefs for all supported encodings
plbstl 318accc
add more exhaustive lists of supported encodings
plbstl 55dd479
add script to auto-generate `iconv-lite` typedefs
plbstl c8daf93
add `typegen` script to package.json
plbstl d4df2ec
add manual edits warning to generated `index.d.ts`
plbstl ed4d59c
move `generate-typings` script to `generation` folder
plbstl b8fc5b6
remove "declare module" from typings
plbstl 429c890
make sure typings correctly describes usage in both cjs and esm
plbstl 60d80da
process all encodings
plbstl aee31d5
typings: add legacy aliases (`toEncoding`, `fromEncoding`), `enableSt…
plbstl f4e3513
typings: add `supportsStreams` property
plbstl a9a5bdc
add jsDocs to legacy aliases
plbstl 88e6647
small doc change
plbstl 17d9b17
small js docs change
plbstl a2bf322
improve var names and docs
plbstl 72e0fa4
mirror the lib's `_canonicalizeEncoding` logic
plbstl ed46ed8
change placeholder for `SupportedEncoding` type
plbstl ed3f39e
improve docs
plbstl f66e8ec
improve docs
plbstl a773a6f
refactor: separate generated encodings into a dedicated file
plbstl 088e997
remove unused `index-template.d.ts`
plbstl f54b92a
Merge branch 'master' of github.com:ashtuchkin/iconv-lite into typede…
bjohansebas c4d3a80
test: add typescript test
bjohansebas 37f734a
fixup install node types
bjohansebas 4977980
fixup ci
bjohansebas ea6d519
fix imports and type name
plbstl a68d5de
fix typescript tests
plbstl d578371
fix eslint issues
plbstl 41a4833
allow CJS imports from node10
plbstl 9234c9d
improve `@ts-expect-error` comment
plbstl 3bc5c76
correct `@ts-expect-error` comment
plbstl 1d22041
fix typedefs
plbstl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,4 +10,5 @@ performance | |
.nyc_output | ||
eslint.config.js | ||
*.tgz | ||
.git-blame-ignore-revs | ||
.git-blame-ignore-revs | ||
tsconfig.json |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,124 +1,131 @@ | ||
/*--------------------------------------------------------------------------------------------- | ||
/* --------------------------------------------------------------------------------------------- | ||
* REQUIREMENT: This definition is dependent on the @types/node definition. | ||
* | ||
* Install with `npm install @types/node --save-dev` | ||
* | ||
* This file provides detailed typings for the public API of iconv-lite | ||
*--------------------------------------------------------------------------------------------*/ | ||
*-------------------------------------------------------------------------------------------- */ | ||
|
||
import type { SupportedEncoding } from "./encodings"; | ||
import type { Encodings } from "../types/encodings" | ||
|
||
// --- Options --- | ||
|
||
export interface DecodeOptions { | ||
/** Strip the byte order mark (BOM) from the input, when decoding. @default true */ | ||
stripBOM?: boolean; | ||
/** Override the default endianness for `UTF-16` and `UTF-32` decodings. */ | ||
defaultEncoding?: "utf16be" | "utf32be"; | ||
} | ||
declare namespace iconv { | ||
plbstl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
export interface DecodeOptions { | ||
/** Strip the byte order mark (BOM) from the input, when decoding. @default true */ | ||
stripBOM?: boolean; | ||
/** Override the default endianness for `UTF-16` and `UTF-32` decodings. */ | ||
defaultEncoding?: "utf16be" | "utf32be"; | ||
} | ||
|
||
export interface EncodeOptions { | ||
/** Add a byte order mark (BOM) to the output, when encoding. @default false */ | ||
addBOM?: boolean; | ||
/** Override the default endianness for `UTF-32` encoding. */ | ||
defaultEncoding?: "utf32be"; | ||
} | ||
export interface EncodeOptions { | ||
/** Add a byte order mark (BOM) to the output, when encoding. @default false */ | ||
addBOM?: boolean; | ||
/** Override the default endianness for `UTF-32` encoding. */ | ||
defaultEncoding?: "utf32be"; | ||
} | ||
|
||
// --- Return values --- | ||
// --- Return values --- | ||
|
||
export interface EncoderStream { | ||
write(str: string): Buffer; | ||
end(): Buffer | undefined; | ||
} | ||
export interface EncoderStream { | ||
write(str: string): Buffer; | ||
end(): Buffer | undefined; | ||
} | ||
|
||
export interface DecoderStream { | ||
write(buf: Buffer): string; | ||
end(): string | undefined; | ||
} | ||
export interface DecoderStream { | ||
write(buf: Buffer): string; | ||
end(): string | undefined; | ||
} | ||
|
||
export interface Codec { | ||
encoder: new (options?: EncodeOptions, codec?: any) => EncoderStream; | ||
decoder: new (options?: DecodeOptions, codec?: any) => DecoderStream; | ||
[key: string]: any; | ||
} | ||
export interface Codec { | ||
encoder: new (options?: EncodeOptions, codec?: any) => EncoderStream; | ||
decoder: new (options?: DecodeOptions, codec?: any) => DecoderStream; | ||
[key: string]: any; | ||
} | ||
|
||
declare const iconv: { | ||
// --- Basic API --- | ||
const iconv: { | ||
// --- Basic API --- | ||
|
||
/** Encodes a `string` into a `Buffer`, using the provided `encoding`. */ | ||
encode(content: string, encoding: SupportedEncoding, options?: EncodeOptions): Buffer; | ||
/** Encodes a `string` into a `Buffer`, using the provided `encoding`. */ | ||
encode(content: string, encoding: Encodings, options?: EncodeOptions): Buffer; | ||
|
||
/** Decodes a `Buffer` into a `string`, using the provided `encoding`. */ | ||
decode(buffer: Buffer | Uint8Array, encoding: SupportedEncoding, options?: DecodeOptions): string; | ||
/** Decodes a `Buffer` into a `string`, using the provided `encoding`. */ | ||
decode(buffer: Buffer | Uint8Array, encoding: Encodings, options?: DecodeOptions): string; | ||
|
||
/** Checks if a given encoding is supported by `iconv-lite`. */ | ||
encodingExists(encoding: string): encoding is SupportedEncoding; | ||
/** Checks if a given encoding is supported by `iconv-lite`. */ | ||
encodingExists(encoding: string): encoding is Encodings; | ||
|
||
// --- Legacy aliases --- | ||
// --- Legacy aliases --- | ||
|
||
/** Legacy alias for {@link iconv.encode}. */ | ||
toEncoding: typeof iconv.encode; | ||
/** Legacy alias for {@link iconv.encode}. */ | ||
toEncoding: typeof iconv.encode; | ||
|
||
/** Legacy alias for {@link iconv.decode}. */ | ||
fromEncoding: typeof iconv.decode; | ||
/** Legacy alias for {@link iconv.decode}. */ | ||
fromEncoding: typeof iconv.decode; | ||
|
||
// --- Stream API --- | ||
// --- Stream API --- | ||
|
||
/** Creates a stream that decodes binary data from a given `encoding` into strings. */ | ||
decodeStream(encoding: SupportedEncoding, options?: DecodeOptions): NodeJS.ReadWriteStream; | ||
/** Creates a stream that decodes binary data from a given `encoding` into strings. */ | ||
decodeStream(encoding: Encodings, options?: DecodeOptions): NodeJS.ReadWriteStream; | ||
|
||
/** Creates a stream that encodes strings into binary data in a given `encoding`. */ | ||
encodeStream(encoding: SupportedEncoding, options?: EncodeOptions): NodeJS.ReadWriteStream; | ||
/** Creates a stream that encodes strings into binary data in a given `encoding`. */ | ||
encodeStream(encoding: Encodings, options?: EncodeOptions): NodeJS.ReadWriteStream; | ||
|
||
/** | ||
* Explicitly enable Streaming API in browser environments by passing in: | ||
* ```js | ||
* require('stream') | ||
* ``` | ||
* @example iconv.enableStreamingAPI(require('stream')); | ||
*/ | ||
enableStreamingAPI(stream_module: any): void; | ||
/** | ||
* Explicitly enable Streaming API in browser environments by passing in: | ||
* ```js | ||
* require('stream') | ||
* ``` | ||
* @example iconv.enableStreamingAPI(require('stream')); | ||
*/ | ||
enableStreamingAPI(stream_module: any): void; | ||
|
||
// --- Low-level stream APIs --- | ||
// --- Low-level stream APIs --- | ||
|
||
/** Creates and returns a low-level encoder stream. */ | ||
getEncoder(encoding: SupportedEncoding, options?: EncodeOptions): EncoderStream; | ||
/** Creates and returns a low-level encoder stream. */ | ||
getEncoder(encoding: Encodings, options?: EncodeOptions): EncoderStream; | ||
|
||
/** Creates and returns a low-level decoder stream. */ | ||
getDecoder(encoding: SupportedEncoding, options?: DecodeOptions): DecoderStream; | ||
/** Creates and returns a low-level decoder stream. */ | ||
getDecoder(encoding: Encodings, options?: DecodeOptions): DecoderStream; | ||
|
||
/** | ||
* Returns a codec object for the given `encoding`. | ||
* @throws If the `encoding` is not recognized. | ||
*/ | ||
getCodec(encoding: SupportedEncoding): Codec; | ||
/** | ||
* Returns a codec object for the given `encoding`. | ||
* @throws If the `encoding` is not recognized. | ||
*/ | ||
getCodec(encoding: Encodings): Codec; | ||
|
||
/** Strips all non-alphanumeric characters and appended year from `encoding`. */ | ||
_canonicalizeEncoding(encoding: SupportedEncoding): string; | ||
/** Strips all non-alphanumeric characters and appended year from `encoding`. */ | ||
_canonicalizeEncoding(encoding: Encodings): string; | ||
|
||
// --- Properties --- | ||
// --- Properties --- | ||
|
||
/** A cache of all loaded encoding definitions. */ | ||
encodings: Record< | ||
SupportedEncoding, | ||
| string | ||
| { | ||
/** A cache of all loaded encoding definitions. */ | ||
encodings: Record< | ||
Encodings, | ||
| string | ||
| { | ||
type: string; | ||
[key: string]: any; | ||
} | ||
> | null; | ||
> | null; | ||
|
||
/** A cache of initialized codec objects. */ | ||
_codecDataCache: Record<string, Codec>; | ||
/** A cache of initialized codec objects. */ | ||
_codecDataCache: Record<string, Codec>; | ||
|
||
/** The character used for untranslatable `Unicode` characters. @default "�" */ | ||
defaultCharUnicode: string; | ||
/** The character used for untranslatable `Unicode` characters. @default "�" */ | ||
defaultCharUnicode: string; | ||
|
||
/** The character used for untranslatable `single-byte` characters. @default "?" */ | ||
defaultCharSingleByte: string; | ||
/** The character used for untranslatable `single-byte` characters. @default "?" */ | ||
defaultCharSingleByte: string; | ||
|
||
/** @readonly Whether or not, Streaming API is enabled. */ | ||
readonly supportsStreams: boolean; | ||
}; | ||
/** @readonly Whether or not, Streaming API is enabled. */ | ||
readonly supportsStreams: boolean; | ||
} | ||
|
||
export default iconv; | ||
export type { | ||
iconv, | ||
Encodings | ||
} | ||
export { iconv as default } | ||
} | ||
export = iconv |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import iconv from "../../lib" | ||
import { expectTypeOf } from "expect-type" | ||
|
||
expectTypeOf(iconv._canonicalizeEncoding).toBeFunction() | ||
expectTypeOf(iconv.encode).toBeFunction() | ||
expectTypeOf(iconv.decode).toBeFunction() | ||
expectTypeOf(iconv.encodingExists).toBeFunction() | ||
expectTypeOf(iconv.toEncoding).toBeFunction() | ||
expectTypeOf(iconv.enableStreamingAPI).toBeFunction() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
import iconv, { iconv as iconvLite, Codec, DecodeOptions, DecoderStream, EncodeOptions, EncoderStream, Encodings } from "../../lib" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"compilerOptions": { | ||
"target": "es5", | ||
"lib": [ "es2015" ], | ||
"module": "commonjs", | ||
"noEmit": true, | ||
"strict": true | ||
}, | ||
"include": [ | ||
"./test/types/**/*.ts", | ||
"./**/*.d.ts" | ||
] | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.