Skip to content

Commit 25c4cde

Browse files
authored
v1.8.3 pretty types, fix malloc (#17)
1 parent bab9bca commit 25c4cde

11 files changed

Lines changed: 261 additions & 241 deletions

File tree

dist/index.cjs

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

dist/index.cjs.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.d.ts

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,10 @@ export type ValueTypes = {
115115
[Type.RegExp]: RegExp;
116116
[Type.Date]: Date;
117117
};
118+
/** https://www.totaltypescript.com/concepts/the-prettify-helper */
119+
export type Pretty<T> = T extends ArrayBuffer | ArrayBufferView | Date | RegExp | Uint8Array ? T : T extends Array<infer U> ? Array<Pretty<U>> : T extends object ? {
120+
[K in keyof T]: Pretty<T[K]>;
121+
} & unknown : T;
118122
/** @throws any error too */
119123
export type ValidationFn<T> = (x: T) => undefined | boolean | Error;
120124
export type TransformFn<T> = (x: T) => T;
@@ -152,41 +156,42 @@ export type FieldDefinition = keyof ValueTypes | [
152156
/**
153157
* The resulting type of the decoded data, based on the encoder definition.
154158
*/
155-
export type InferredDecodedType<EncoderType extends EncoderDefinition> = {
159+
export type RawDecodedType<EncoderType extends EncoderDefinition> = {
156160
[EKey in keyof EncoderType as EncoderType[EKey] extends MaybeType<any> ? never : EKey]: EncoderType[EKey] extends keyof ValueTypes ? ValueTypes[EncoderType[EKey]] : EncoderType[EKey] extends [
157161
keyof ValueTypes
158-
] ? Array<ValueTypes[EncoderType[EKey][0]]> : EncoderType[EKey] extends EncoderDefinition ? InferredDecodedType<EncoderType[EKey]> : EncoderType[EKey] extends [
162+
] ? Array<ValueTypes[EncoderType[EKey][0]]> : EncoderType[EKey] extends EncoderDefinition ? RawDecodedType<EncoderType[EKey]> : EncoderType[EKey] extends [
159163
EncoderDefinition
160-
] ? Array<InferredDecodedType<EncoderType[EKey][number]>> : never;
164+
] ? Array<RawDecodedType<EncoderType[EKey][number]>> : never;
161165
} & {
162166
[EKey in keyof EncoderType as EncoderType[EKey] extends MaybeType<any> ? EKey : never]?: EncoderType[EKey] extends MaybeType<infer OptionalValue extends keyof ValueTypes> ? ValueTypes[OptionalValue] | undefined : EncoderType[EKey] extends MaybeType<infer OptionalValue extends [
163167
keyof ValueTypes
164-
]> ? Array<ValueTypes[OptionalValue[0]]> | undefined : EncoderType[EKey] extends MaybeType<infer OptionalValue extends EncoderDefinition> ? InferredDecodedType<OptionalValue> | undefined : never;
168+
]> ? Array<ValueTypes[OptionalValue[0]]> | undefined : EncoderType[EKey] extends MaybeType<infer OptionalValue extends EncoderDefinition> ? RawDecodedType<OptionalValue> | undefined : never;
165169
};
166-
export type InferredTransformConfig<EncoderType extends EncoderDefinition> = {
170+
export type DecodedType<EncoderType extends EncoderDefinition> = Pretty<RawDecodedType<EncoderType>>;
171+
export type TransformConfig<EncoderType extends EncoderDefinition> = {
167172
[EKey in keyof EncoderType]?: EncoderType[EKey] extends keyof ValueTypes ? Transforms<ValueTypes[EncoderType[EKey]]> : EncoderType[EKey] extends [
168173
keyof ValueTypes
169-
] ? Transforms<ValueTypes[EncoderType[EKey][0]]> : EncoderType[EKey] extends EncoderDefinition ? InferredTransformConfig<EncoderType[EKey]> : EncoderType[EKey] extends [
174+
] ? Transforms<ValueTypes[EncoderType[EKey][0]]> : EncoderType[EKey] extends EncoderDefinition ? TransformConfig<EncoderType[EKey]> : EncoderType[EKey] extends [
170175
EncoderDefinition
171-
] ? InferredTransformConfig<EncoderType[EKey][number]> : EncoderType[EKey] extends MaybeType<infer OptionalValue extends keyof ValueTypes> ? Transforms<ValueTypes[OptionalValue]> : EncoderType[EKey] extends MaybeType<infer OptionalValue extends [
176+
] ? TransformConfig<EncoderType[EKey][number]> : EncoderType[EKey] extends MaybeType<infer OptionalValue extends keyof ValueTypes> ? Transforms<ValueTypes[OptionalValue]> : EncoderType[EKey] extends MaybeType<infer OptionalValue extends [
172177
keyof ValueTypes
173-
]> ? Transforms<ValueTypes[OptionalValue[0]]> : EncoderType[EKey] extends MaybeType<infer OptionalValue extends EncoderDefinition> ? InferredTransformConfig<OptionalValue> | undefined : never;
178+
]> ? Transforms<ValueTypes[OptionalValue[0]]> : EncoderType[EKey] extends MaybeType<infer OptionalValue extends EncoderDefinition> ? TransformConfig<OptionalValue> | undefined : never;
174179
};
175-
export type InferredValidationConfig<EncoderType extends EncoderDefinition> = {
180+
export type ValidationConfig<EncoderType extends EncoderDefinition> = {
176181
[EKey in keyof EncoderType]?: EncoderType[EKey] extends keyof ValueTypes ? ValidationFn<ValueTypes[EncoderType[EKey]]> : EncoderType[EKey] extends [
177182
keyof ValueTypes
178-
] ? ValidationFn<ValueTypes[EncoderType[EKey][0]]> : EncoderType[EKey] extends EncoderDefinition ? InferredValidationConfig<EncoderType[EKey]> : EncoderType[EKey] extends [
183+
] ? ValidationFn<ValueTypes[EncoderType[EKey][0]]> : EncoderType[EKey] extends EncoderDefinition ? ValidationConfig<EncoderType[EKey]> : EncoderType[EKey] extends [
179184
EncoderDefinition
180-
] ? InferredValidationConfig<EncoderType[EKey][number]> : EncoderType[EKey] extends MaybeType<infer OptionalValue extends keyof ValueTypes> ? ValidationFn<ValueTypes[OptionalValue]> : EncoderType[EKey] extends MaybeType<infer OptionalValue extends [
185+
] ? ValidationConfig<EncoderType[EKey][number]> : EncoderType[EKey] extends MaybeType<infer OptionalValue extends keyof ValueTypes> ? ValidationFn<ValueTypes[OptionalValue]> : EncoderType[EKey] extends MaybeType<infer OptionalValue extends [
181186
keyof ValueTypes
182-
]> ? ValidationFn<ValueTypes[OptionalValue[0]]> : EncoderType[EKey] extends MaybeType<infer OptionalValue extends EncoderDefinition> ? InferredValidationConfig<OptionalValue> | undefined : never;
187+
]> ? ValidationFn<ValueTypes[OptionalValue[0]]> : EncoderType[EKey] extends MaybeType<infer OptionalValue extends EncoderDefinition> ? ValidationConfig<OptionalValue> | undefined : never;
183188
};
184189
export type FormatHeader = string | number;
185190
/**
186-
* Decoded object types for a given binary format.
187-
* @example let onData = (data: Decoded<typeof MyBufferFormat>) => {...};
191+
* Utility to get the decoded type of a buffer format
192+
* @example type Format = Decoded<typeof MyBufferFormat>
188193
*/
189-
export type Decoded<FromBufferFormat> = FromBufferFormat extends BufferFormat<infer EncoderType, any> ? InferredDecodedType<EncoderType> : never;
194+
export type Decoded<TBufferFormat> = TBufferFormat extends BufferFormat<infer Format, any> ? DecodedType<Format> : never;
190195
/**
191196
* Defines a format for encoding/decoding binary buffers.
192197
*
@@ -198,7 +203,7 @@ export type Decoded<FromBufferFormat> = FromBufferFormat extends BufferFormat<in
198203
* const MyFormat = defineFormat(1234, { ... });
199204
* const MyFormat = defineFormat(null, { ... });
200205
*/
201-
export declare function defineFormat<T extends EncoderDefinition, HeaderType extends string | number = number>(def: T): BufferFormat<T, HeaderType>;
206+
export declare function defineFormat<T extends EncoderDefinition, HeaderType extends FormatHeader = number>(def: T): BufferFormat<T, HeaderType>;
202207
/**
203208
* Defines a format for encoding/decoding binary buffers.
204209
*
@@ -210,7 +215,7 @@ export declare function defineFormat<T extends EncoderDefinition, HeaderType ext
210215
* const MyFormat = defineFormat(1234, { ... });
211216
* const MyFormat = defineFormat(null, { ... });
212217
*/
213-
export declare function defineFormat<T extends EncoderDefinition, HeaderType extends string | number = number>(h: HeaderType | null, def: T): BufferFormat<T, HeaderType>;
218+
export declare function defineFormat<T extends EncoderDefinition, HeaderType extends FormatHeader = number>(h: HeaderType | null, def: T): BufferFormat<T, HeaderType>;
214219
/**
215220
* BufferFormat is a utility class for encoding and decoding binary data based
216221
* on a provided encoding format.
@@ -257,23 +262,23 @@ export declare class BufferFormat<EncoderType extends EncoderDefinition, HeaderT
257262
* @returns a copy of encoded bytes
258263
* @throws if fails to encode value to schema
259264
*/
260-
encode<DecodedType extends InferredDecodedType<EncoderType>>(data: DecodedType, preserveBytes?: boolean): Uint8Array;
265+
encode<TDecodedType extends DecodedType<EncoderType>>(data: TDecodedType, preserveBytes?: boolean): Uint8Array;
261266
/**
262267
* Decode binary data to an object.
263268
* @throws if fails to decode bytes to schema.
264269
*/
265-
decode<DecodedType = InferredDecodedType<EncoderType>>(bytes: Uint8Array | ArrayBufferView | ArrayBuffer, decodeInto?: Partial<DecodedType>): DecodedType;
270+
decode<TDecodedType = DecodedType<EncoderType>>(bytes: Uint8Array | ArrayBufferView | ArrayBuffer, decodeInto?: Partial<TDecodedType>): TDecodedType;
266271
/**
267272
* Set additional transform functions to apply before encoding and after decoding.
268273
*/
269-
setTransforms(transforms: InferredTransformConfig<EncoderType> | Transforms<any>): this;
274+
setTransforms(transforms: TransformConfig<EncoderType> | Transforms<any>): this;
270275
/**
271276
* Set additional validation rules which are applied on encode() and decode().
272277
*
273278
* - Validation functions should throw an error, return an error, or return boolean false.
274279
* - Anything else is treated as successfully passing validation.
275280
*/
276-
setValidation(validations: InferredValidationConfig<EncoderType> | ValidationFn<any>): this;
281+
setValidation(validations: ValidationConfig<EncoderType> | ValidationFn<any>): this;
277282
private _$processValidation;
278283
}
279284
export type AnyFormat = BufferFormat<any, any>;
@@ -301,7 +306,7 @@ export declare class BufferParser {
301306
/**
302307
* Register a format handler.
303308
*/
304-
on<EncoderType extends EncoderDefinition, DecodedType = InferredDecodedType<EncoderType>>(format: BufferFormat<EncoderType, string | number>, callback: (data: DecodedType) => any, { decodeInPlace, }?: {
309+
on<EncoderType extends EncoderDefinition, TDecodedType = DecodedType<EncoderType>>(format: BufferFormat<EncoderType, string | number>, callback: (data: TDecodedType) => any, { decodeInPlace, }?: {
305310
decodeInPlace?: boolean;
306311
}): this;
307312
/** Register a format (or formats) that are recognized. */

0 commit comments

Comments
 (0)