99 * This file provides detailed typings for the public API of iconv-lite
1010 *-------------------------------------------------------------------------------------------- */
1111
12+ import type Stream = require( "stream" )
1213import type { Encoding } from "../types/encodings"
1314
14- // --- Options ---
15-
1615declare namespace iconv {
1716 export interface DecodeOptions {
18- /** Strip the byte order mark (BOM) from the input, when decoding. @default true */
17+ /**
18+ * Strip the Byte Order Mark (BOM) from the input,
19+ * when decoding, if the codec is BOM-aware. @default true
20+ */
1921 stripBOM ?: boolean ;
2022 /** Override the default endianness for `UTF-16` and `UTF-32` decodings. */
2123 defaultEncoding ?: "utf16be" | "utf32be" ;
2224 }
2325
2426 export interface EncodeOptions {
25- /** Add a byte order mark (BOM) to the output, when encoding. @default false */
27+ /**
28+ * Add a Byte Order Mark (BOM) to the output, when encoding,
29+ * if the codec is BOM-aware. @default false
30+ */
2631 addBOM ?: boolean ;
2732 /** Override the default endianness for `UTF-32` encoding. */
2833 defaultEncoding ?: "utf32be" ;
2934 }
3035
31- // --- Return values ---
32-
3336 export interface EncoderStream {
3437 write ( str : string ) : Buffer ;
3538 end ( ) : Buffer | undefined ;
@@ -41,91 +44,80 @@ declare namespace iconv {
4144 }
4245
4346 export interface Codec {
44- encoder : new ( options ?: EncodeOptions , codec ?: any ) => EncoderStream ;
45- decoder : new ( options ?: DecodeOptions , codec ?: any ) => DecoderStream ;
47+ encoder : new ( options ?: EncodeOptions , codec ?: Codec ) => EncoderStream ;
48+ decoder : new ( options ?: DecodeOptions , codec ?: Codec ) => DecoderStream ;
49+ bomAware ?: boolean ;
4650 [ key : string ] : any ;
4751 }
4852
49- const iconv : {
50- // --- Basic API ---
53+ /** Encodes a `string` into a `Buffer`, using the provided `encoding`. */
54+ export function encode ( content : string , encoding : Encoding , options ?: EncodeOptions ) : Buffer
5155
52- /** Encodes a `string ` into a `Buffer `, using the provided `encoding`. */
53- encode ( content : string , encoding : Encoding , options ?: EncodeOptions ) : Buffer ;
56+ /** Decodes a `Buffer ` into a `string `, using the provided `encoding`. */
57+ export function decode ( buffer : Buffer | Uint8Array , encoding : Encoding , options ?: DecodeOptions ) : string
5458
55- /** Decodes a `Buffer` into a `string`, using the provided `encoding `. */
56- decode ( buffer : Buffer | Uint8Array , encoding : Encoding , options ?: DecodeOptions ) : string ;
59+ /** Checks if a given encoding is supported by `iconv-lite `. */
60+ export function encodingExists ( encoding : string ) : encoding is Encoding
5761
58- /** Checks if a given encoding is supported by ` iconv-lite` . */
59- encodingExists ( encoding : string ) : encoding is Encoding ;
62+ /** Legacy alias for { @link iconv.encode} . */
63+ export const toEncoding : typeof iconv . encode
6064
61- // --- Legacy aliases ---
65+ /** Legacy alias for {@link iconv.decode}. */
66+ export const fromEncoding : typeof iconv . decode
6267
63- /** Legacy alias for { @link iconv.encode} . */
64- toEncoding : typeof iconv . encode ;
68+ /** Creates a stream that decodes binary data from a given `encoding` into strings . */
69+ export function decodeStream ( encoding : Encoding , options ?: DecodeOptions ) : NodeJS . ReadWriteStream
6570
66- /** Legacy alias for { @link iconv.decode} . */
67- fromEncoding : typeof iconv . decode ;
71+ /** Creates a stream that encodes strings into binary data in a given `encoding` . */
72+ export function encodeStream ( encoding : Encoding , options ?: EncodeOptions ) : NodeJS . ReadWriteStream
6873
69- // --- Stream API ---
74+ /**
75+ * Explicitly enable Streaming API in browser environments by passing in:
76+ * ```js
77+ * require('stream')
78+ * ```
79+ * @example iconv.enableStreamingAPI(require('stream'));
80+ */
81+ export function enableStreamingAPI ( stream_module : { Transform : typeof Stream . Transform } ) : void
7082
71- /** Creates a stream that decodes binary data from a given `encoding` into strings . */
72- decodeStream ( encoding : Encoding , options ?: DecodeOptions ) : NodeJS . ReadWriteStream ;
83+ /** Creates and returns a low-level encoder stream . */
84+ export function getEncoder ( encoding : Encoding , options ?: EncodeOptions ) : EncoderStream
7385
74- /** Creates a stream that encodes strings into binary data in a given `encoding` . */
75- encodeStream ( encoding : Encoding , options ?: EncodeOptions ) : NodeJS . ReadWriteStream ;
86+ /** Creates and returns a low-level decoder stream . */
87+ export function getDecoder ( encoding : Encoding , options ?: DecodeOptions ) : DecoderStream
7688
77- /**
78- * Explicitly enable Streaming API in browser environments by passing in:
79- * ```js
80- * require('stream')
81- * ```
82- * @example iconv.enableStreamingAPI(require('stream'));
83- */
84- enableStreamingAPI ( stream_module : any ) : void ;
89+ /**
90+ * Returns a codec object for the given `encoding`.
91+ * @throws If the `encoding` is not recognized.
92+ */
93+ export function getCodec ( encoding : Encoding ) : Codec
8594
86- // --- Low-level stream APIs ---
95+ /** Strips all non-alphanumeric characters and appended year from `encoding`. */
96+ export function _canonicalizeEncoding ( encoding : Encoding ) : string
8797
88- /** Creates and returns a low-level encoder stream. */
89- getEncoder ( encoding : Encoding , options ?: EncodeOptions ) : EncoderStream ;
98+ /** A cache of all loaded encoding definitions. */
99+ export let encodings : Record <
100+ Encoding ,
101+ | string
102+ | {
103+ type : string ;
104+ [ key : string ] : any ;
105+ }
106+ > | null
90107
91- /** Creates and returns a low-level decoder stream . */
92- getDecoder ( encoding : Encoding , options ?: DecodeOptions ) : DecoderStream ;
108+ /** A cache of initialized codec objects . */
109+ export let _codecDataCache : Record < string , Codec >
93110
94- /**
95- * Returns a codec object for the given `encoding`.
96- * @throws If the `encoding` is not recognized.
97- */
98- getCodec ( encoding : Encoding ) : Codec ;
99-
100- /** Strips all non-alphanumeric characters and appended year from `encoding`. */
101- _canonicalizeEncoding ( encoding : Encoding ) : string ;
102-
103- // --- Properties ---
104-
105- /** A cache of all loaded encoding definitions. */
106- encodings : Record <
107- Encoding ,
108- | string
109- | {
110- type : string ;
111- [ key : string ] : any ;
112- }
113- > | null ;
111+ /** The character used for untranslatable `Unicode` characters. @default "�" */
112+ export let defaultCharUnicode : string
114113
115- /** A cache of initialized codec objects. */
116- _codecDataCache : Record < string , Codec > ;
114+ /** The character used for untranslatable `single-byte` characters. @default "?" */
115+ export let defaultCharSingleByte : string
117116
118- /** The character used for untranslatable `Unicode` characters. @default "�" */
119- defaultCharUnicode : string ;
120-
121- /** The character used for untranslatable `single-byte` characters. @default "?" */
122- defaultCharSingleByte : string ;
123-
124- /** @readonly Whether or not, Streaming API is enabled. */
125- readonly supportsStreams : boolean ;
126- }
117+ /** @readonly Whether or not, Streaming API is enabled. */
118+ export const supportsStreams : boolean
127119
128120 export type { iconv as Iconv , Encoding }
129- export { iconv as default }
130121}
122+
131123export = iconv
0 commit comments