1- import { BufferFormat , ViewFormat } from './formats'
1+ import { BufferFormat , ViewFormat , UTF8Format } from './formats'
22
33/**
44 * Encodes {@link TIn} to {@link TFormat} and decodes
55 * {@link TFormat} to {@link TOut}.
66 */
7- export abstract class Encoding < TIn , TFormat , TOut > {
8- constructor ( options : EncodingOptions < TIn , TFormat , TOut > )
7+ export abstract class Encoding < TIn , TFormat , TOut > implements IEncoding < TIn , TFormat , TOut > {
8+ constructor ( options : IEncoding < TIn , TFormat , TOut > )
99
10- /** Encode data. */
1110 encode : ( data : TIn ) => TFormat
12-
13- /** Decode data. */
1411 decode : ( data : TFormat ) => TOut
15-
16- /** Unique name. */
1712 name : string
13+ format : 'buffer' | 'view' | 'utf8'
14+ createViewTranscoder ( ) : ViewFormat < TIn , TOut >
15+ createBufferTranscoder ( ) : BufferFormat < TIn , TOut >
16+ createUTF8Transcoder ( ) : UTF8Format < TIn , TOut >
1817
1918 /**
2019 * Common name, computed from {@link name}. If this encoding is a
@@ -23,53 +22,65 @@ export abstract class Encoding<TIn, TFormat, TOut> {
2322 * will equal {@link commonName}.
2423 */
2524 get commonName ( ) : string
25+ }
26+
27+ export interface IEncoding < TIn , TFormat , TOut > {
28+ /**
29+ * Encode data.
30+ */
31+ encode : ( data : TIn ) => TFormat
32+
33+ /**
34+ * Decode data.
35+ */
36+ decode : ( data : TFormat ) => TOut
2637
2738 /**
28- * Name of the (lower-level) encoding used by the return value of
39+ * Unique name.
40+ */
41+ name : string
42+
43+ /**
44+ * The name of the (lower-level) encoding used by the return value of
2945 * {@link encode}. One of 'buffer', 'view', 'utf8'.
3046 */
3147 format : 'buffer' | 'view' | 'utf8'
3248
3349 /**
34- * Create a new encoding that transcodes {@link TFormat} to a view.
50+ * Create a new encoding that transcodes {@link TFormat} from / to a view.
3551 */
36- createViewTranscoder ( ) : ViewFormat < TIn , TOut >
52+ createViewTranscoder ?: ( ( ) => ViewFormat < TIn , TOut > ) | undefined
3753
3854 /**
39- * Create a new encoding that transcodes {@link TFormat} to a buffer.
55+ * Create a new encoding that transcodes {@link TFormat} from / to a buffer.
4056 */
41- createBufferTranscoder ( ) : BufferFormat < TIn , TOut >
57+ createBufferTranscoder ?: ( ( ) => BufferFormat < TIn , TOut > ) | undefined
58+
59+ /**
60+ * Create a new encoding that transcodes {@link TFormat} from / to a UTF-8 string.
61+ */
62+ createUTF8Transcoder ?: ( ( ) => UTF8Format < TIn , TOut > ) | undefined
4263}
4364
44- // TODO: split into multiple interfaces (and then union those) in order to
45- // separate the main level-transcoder interface from compatibility options.
46- export interface EncodingOptions < TIn , TFormat , TOut > {
65+ export interface IExternalEncoding < TIn , TFormat , TOut > {
4766 /**
4867 * Encode data.
4968 */
50- encode ? : ( ( data : TIn ) => TFormat ) | undefined
69+ encode : ( data : TIn ) => TFormat
5170
5271 /**
5372 * Decode data.
5473 */
55- decode ? : ( ( data : TFormat ) => TOut ) | undefined
74+ decode : ( data : TFormat ) => TOut
5675
5776 /**
5877 * Unique name.
5978 */
6079 name ?: string | undefined
6180
62- /**
63- * The name of the (lower-level) encoding used by the return value of
64- * {@link encode}. One of 'buffer', 'view', 'utf8'. Defaults to 'buffer'
65- * if the {@link buffer} and {@link code} options are also undefined.
66- */
67- format ?: 'buffer' | 'view' | 'utf8' | undefined
68-
6981 /**
7082 * Legacy `level-codec` option that means the same as `format: 'buffer'`
71- * if true or `format: 'utf8'` if false. Used only when the {@link format} option
72- * is undefined.
83+ * if true or `format: 'utf8'` if false.
7384 */
7485 buffer ?: boolean | undefined
7586
@@ -80,13 +91,68 @@ export interface EncodingOptions<TIn, TFormat, TOut> {
8091 type ?: any
8192
8293 /**
83- * For compatibility with `multiformats`. If a number, then the encoding is
84- * assumed to have a {@link format} of 'view'. Used only when the {@link format}
85- * and {@link buffer} options are undefined.
94+ * To detect `multiformats`. If a number, then the encoding is
95+ * assumed to have a {@link format} of 'view'.
8696 * @see https://github.com/multiformats/js-multiformats/blob/master/src/codecs/interface.ts
8797 */
8898 code ?: any
89-
90- createViewTranscoder ?: ( ( ) => ViewFormat < TIn , TOut > ) | undefined
91- createBufferTranscoder ?: ( ( ) => BufferFormat < TIn , TOut > ) | undefined
9299}
100+
101+ /**
102+ * Names of built-in encodings.
103+ */
104+ export type KnownEncodingName = 'utf8' | 'buffer' | 'view' | 'json' | 'hex' | 'base64'
105+
106+ /**
107+ * One of the supported encoding interfaces.
108+ */
109+ export type MixedEncoding < TIn , TFormat , TOut > =
110+ IEncoding < TIn , TFormat , TOut > |
111+ IExternalEncoding < TIn , TFormat , TOut >
112+
113+ /**
114+ * Type utility to cast a built-in encoding identified by its name to an {@link Encoding}.
115+ */
116+ export type KnownEncoding < N extends KnownEncodingName , TFormat >
117+ = Encoding < KnownEncodingInput < N > , TFormat , KnownEncodingOutput < N > >
118+
119+ /**
120+ * Type utility to get the input type of a built-in encoding identified by its name.
121+ */
122+ export type KnownEncodingInput < N extends KnownEncodingName >
123+ = N extends 'utf8' ? string | Buffer | Uint8Array
124+ : N extends 'buffer' ? Buffer | Uint8Array | string
125+ : N extends 'view' ? Uint8Array | string
126+ : N extends 'json' ? any
127+ : N extends 'hex' ? Buffer | string
128+ : N extends 'base64' ? Buffer | string
129+ : never
130+
131+ /**
132+ * Type utility to get the output type of a built-in encoding identified by its name.
133+ */
134+ export type KnownEncodingOutput < N extends KnownEncodingName >
135+ = N extends 'utf8' ? string
136+ : N extends 'buffer' ? Buffer
137+ : N extends 'view' ? Uint8Array
138+ : N extends 'json' ? any
139+ : N extends 'hex' ? string
140+ : N extends 'base64' ? string
141+ : never
142+
143+ /**
144+ * Type utility to use a {@link MixedEncoding} with an untyped format.
145+ */
146+ export type PartialEncoding < TIn , TOut = TIn > = MixedEncoding < TIn , any , TOut >
147+
148+ /**
149+ * Type utility to use a {@link MixedEncoding} with an untyped format and output.
150+ * For when only the encoding side is needed.
151+ */
152+ export type PartialEncoder < TIn > = MixedEncoding < TIn , any , any >
153+
154+ /**
155+ * Type utility to use a {@link MixedEncoding} with an untyped input and format.
156+ * For when only the decoding side is needed.
157+ */
158+ export type PartialDecoder < TOut > = MixedEncoding < any , any , TOut >
0 commit comments