Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

tiny-decoders

Package Overview
Dependencies
Maintainers
1
Versions
30
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

tiny-decoders - npm Package Compare versions

Comparing version 18.0.0 to 19.0.0

105

index.d.ts

@@ -1,3 +0,6 @@

export type Decoder<T> = (value: unknown) => DecoderResult<T>;
export type DecoderResult<T> = {
export type Codec<Decoded, Encoded = unknown> = {
decoder: (value: unknown) => DecoderResult<Decoded>;
encoder: (value: Decoded) => Encoded;
};
export type DecoderResult<Decoded> = {
tag: "DecoderError";

@@ -7,18 +10,20 @@ error: DecoderError;

tag: "Valid";
value: T;
value: Decoded;
};
export type Infer<T extends Decoder<any>> = Extract<ReturnType<T>, {
export type Infer<T extends Codec<any>> = Extract<ReturnType<T["decoder"]>, {
tag: "Valid";
}>["value"];
export type InferEncoded<T extends Codec<any>> = ReturnType<T["encoder"]>;
type Expand<T> = T extends infer O ? {
[K in keyof O]: O[K];
} : never;
export declare function boolean(value: unknown): DecoderResult<boolean>;
export declare function number(value: unknown): DecoderResult<number>;
export declare function string(value: unknown): DecoderResult<string>;
export declare function stringUnion<const T extends readonly [string, ...Array<string>]>(variants: T): Decoder<T[number]>;
export declare function array<T>(decoder: Decoder<T>): Decoder<Array<T>>;
export declare function record<T>(decoder: Decoder<T>): Decoder<Record<string, T>>;
type Field<Decoded, Meta extends FieldMeta> = Meta & {
decoder: Decoder<Decoded>;
export declare const unknown: Codec<unknown>;
export declare const boolean: Codec<boolean, boolean>;
export declare const number: Codec<number, number>;
export declare const string: Codec<string, string>;
export declare function stringUnion<const Variants extends readonly [string, ...Array<string>]>(variants: Variants): Codec<Variants[number], Variants[number]>;
export declare function array<DecodedItem, EncodedItem>(codec: Codec<DecodedItem, EncodedItem>): Codec<Array<DecodedItem>, Array<EncodedItem>>;
export declare function record<DecodedValue, EncodedValue>(codec: Codec<DecodedValue, EncodedValue>): Codec<Record<string, DecodedValue>, Record<string, EncodedValue>>;
type Field<Decoded, Encoded, Meta extends FieldMeta> = Meta & {
codec: Codec<Decoded, Encoded>;
};

@@ -33,4 +38,5 @@ type FieldMeta = {

};
type FieldsMapping = Record<string, Decoder<any> | Field<any, FieldMeta>>;
type InferField<T extends Decoder<any> | Field<any, FieldMeta>> = T extends Field<any, FieldMeta> ? Infer<T["decoder"]> : T extends Decoder<any> ? Infer<T> : never;
type FieldsMapping = Record<string, Codec<any> | Field<any, any, FieldMeta>>;
type InferField<T extends Codec<any> | Field<any, any, FieldMeta>> = T extends Field<any, any, FieldMeta> ? Infer<T["codec"]> : T extends Codec<any> ? Infer<T> : never;
type InferEncodedField<T extends Codec<any> | Field<any, any, FieldMeta>> = T extends Field<any, any, FieldMeta> ? InferEncoded<T["codec"]> : T extends Codec<any> ? InferEncoded<T> : never;
type InferFields<Mapping extends FieldsMapping> = Expand<{

@@ -45,8 +51,22 @@ [Key in keyof Mapping as Mapping[Key] extends {

}>;
type InferEncodedFields<Mapping extends FieldsMapping> = Expand<{
[Key in keyof Mapping as Mapping[Key] extends {
optional: true;
} ? never : Mapping[Key] extends {
renameFrom: infer Name;
} ? Name extends string ? Name : Key : Key]: InferEncodedField<Mapping[Key]>;
} & {
[Key in keyof Mapping as Mapping[Key] extends {
optional: true;
} ? Mapping[Key] extends {
renameFrom: infer Name;
} ? Name extends string ? Name : Key : Key : never]?: InferEncodedField<Mapping[Key]>;
}>;
export declare function fieldsAuto<Mapping extends FieldsMapping>(mapping: Mapping, { allowExtraFields }?: {
allowExtraFields?: boolean;
}): Decoder<InferFields<Mapping>>;
export declare function field<Decoded, const Meta extends Omit<FieldMeta, "tag">>(decoder: Decoder<Decoded>, meta: Meta): Field<Decoded, Meta>;
}): Codec<InferFields<Mapping>, InferEncodedFields<Mapping>>;
export declare function field<Decoded, Encoded, const Meta extends Omit<FieldMeta, "tag">>(codec: Codec<Decoded, Encoded>, meta: Meta): Field<Decoded, Encoded, Meta>;
type InferFieldsUnion<MappingsUnion extends FieldsMapping> = MappingsUnion extends any ? InferFields<MappingsUnion> : never;
type Variant<DecodedCommonField extends number | string | symbol> = Record<DecodedCommonField, Field<any, {
type InferEncodedFieldsUnion<MappingsUnion extends FieldsMapping> = MappingsUnion extends any ? InferEncodedFields<MappingsUnion> : never;
type Variant<DecodedCommonField extends number | string | symbol> = Record<DecodedCommonField, Field<any, any, {
tag: {

@@ -56,10 +76,13 @@ decoded: string;

};
}>> & Record<string, Decoder<any> | Field<any, FieldMeta>>;
}>> & Record<string, Codec<any> | Field<any, any, FieldMeta>>;
export declare function fieldsUnion<const DecodedCommonField extends keyof Variants[number], Variants extends readonly [
Variant<DecodedCommonField>,
...ReadonlyArray<Variant<DecodedCommonField>>
]>(decodedCommonField: DecodedCommonField, variants: Variants, { allowExtraFields }?: {
...Array<Variant<DecodedCommonField>>
]>(decodedCommonField: keyof InferEncodedFieldsUnion<Variants[number]> extends never ? [
"fieldsUnion variants must have a field in common, and their encoded field names must be the same",
never
] : DecodedCommonField, variants: Variants, { allowExtraFields }?: {
allowExtraFields?: boolean;
}): Decoder<InferFieldsUnion<Variants[number]>>;
export declare function tag<const Decoded extends string>(decoded: Decoded): Field<Decoded, {
}): Codec<InferFieldsUnion<Variants[number]>, InferEncodedFieldsUnion<Variants[number]>>;
export declare function tag<const Decoded extends string>(decoded: Decoded): Field<Decoded, Decoded, {
tag: {

@@ -72,3 +95,3 @@ decoded: string;

renameTagFrom: Encoded;
}): Field<Decoded, {
}): Field<Decoded, Encoded, {
tag: {

@@ -81,3 +104,3 @@ decoded: string;

renameFieldFrom: EncodedFieldName;
}): Field<Decoded, {
}): Field<Decoded, Decoded, {
renameFrom: EncodedFieldName;

@@ -92,3 +115,3 @@ tag: {

renameFieldFrom: EncodedFieldName;
}): Field<Decoded, {
}): Field<Decoded, Encoded, {
renameFrom: EncodedFieldName;

@@ -100,5 +123,13 @@ tag: {

}>;
export declare function tuple<T extends Array<unknown>>(mapping: [...{
[P in keyof T]: Decoder<T[P]>;
}]): Decoder<T>;
type InferTuple<Codecs extends ReadonlyArray<Codec<any>>> = [
...{
[P in keyof Codecs]: Infer<Codecs[P]>;
}
];
type InferEncodedTuple<Codecs extends ReadonlyArray<Codec<any>>> = [
...{
[P in keyof Codecs]: InferEncoded<Codecs[P]>;
}
];
export declare function tuple<const Codecs extends ReadonlyArray<Codec<any>>>(codecs: Codecs): Codec<InferTuple<Codecs>, InferEncodedTuple<Codecs>>;
type Multi<Types> = Types extends any ? Types extends "undefined" ? {

@@ -127,8 +158,14 @@ type: "undefined";

type MultiTypeName = "array" | "boolean" | "null" | "number" | "object" | "string" | "undefined";
export declare function multi<Types extends readonly [MultiTypeName, ...Array<MultiTypeName>]>(types: Types): Decoder<Multi<Types[number]>>;
export declare function recursive<T>(callback: () => Decoder<T>): Decoder<T>;
export declare function undefinedOr<T>(decoder: Decoder<T>): Decoder<T | undefined>;
export declare function nullable<T>(decoder: Decoder<T>): Decoder<T | null>;
export declare function map<T, U>(decoder: Decoder<T>, transform: (value: T) => U): Decoder<U>;
export declare function flatMap<T, U>(decoder: Decoder<T>, transform: (value: T) => DecoderResult<U>): Decoder<U>;
export declare function multi<Types extends readonly [MultiTypeName, ...Array<MultiTypeName>]>(types: Types): Codec<Multi<Types[number]>, Multi<Types[number]>["value"]>;
export declare function recursive<Decoded, Encoded>(callback: () => Codec<Decoded, Encoded>): Codec<Decoded, Encoded>;
export declare function undefinedOr<Decoded, Encoded>(codec: Codec<Decoded, Encoded>): Codec<Decoded | undefined, Encoded | undefined>;
export declare function nullable<Decoded, Encoded>(codec: Codec<Decoded, Encoded>): Codec<Decoded | null, Encoded | null>;
export declare function map<const Decoded, Encoded, NewDecoded>(codec: Codec<Decoded, Encoded>, transform: {
decoder: (value: Decoded) => NewDecoded;
encoder: (value: NewDecoded) => Readonly<Decoded>;
}): Codec<NewDecoded, Encoded>;
export declare function flatMap<const Decoded, Encoded, NewDecoded>(codec: Codec<Decoded, Encoded>, transform: {
decoder: (value: Decoded) => DecoderResult<NewDecoded>;
encoder: (value: NewDecoded) => Readonly<Decoded>;
}): Codec<NewDecoded, Encoded>;
export type DecoderError = {

@@ -135,0 +172,0 @@ path: Array<number | string>;

{
"name": "tiny-decoders",
"version": "18.0.0",
"version": "19.0.0",
"license": "MIT",

@@ -5,0 +5,0 @@ "author": "Simon Lydell",

# tiny-decoders [![minified size](https://img.shields.io/bundlephobia/min/tiny-decoders.svg)](https://bundlephobia.com/result?p=tiny-decoders)
Type-safe data decoding for the minimalist.
Type-safe data decoding and encoding for the minimalist.
**[➡️ Full readme](https://github.com/lydell/tiny-decoders/#readme)**

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc