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

alge

Package Overview
Dependencies
Maintainers
1
Versions
71
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

alge - npm Package Compare versions

Comparing version 0.1.0-next.18 to 0.1.0-next.19

13

dist-cjs/Alge.d.ts
import { Initial } from './Builder';
import { z } from './lib/z';
import { OmitTag } from './Types';
import { SomeZodObject } from 'zod';
export declare type SomeVariant = object;
export declare type SomeVariantConstructorInput = Record<string, unknown>;
export declare type SomeCodecDefinition = {
encode: SomeEncoderDefinition;
decode: SomeDecoderDefinition;
};
export declare type SomeEncoderDefinition = (variant: SomeVariant) => string;
export declare type SomeDecoderDefinition = (encodedData: string, extensions: {
schema: SomeZodObject;
name: string;
[key: string]: unknown;
}) => null | SomeVariantConstructorInput;
/**

@@ -5,0 +18,0 @@ * Base properties of a data type.

10

dist-cjs/Alge.js

@@ -77,5 +77,7 @@ "use strict";

throw new Error(`Codec not implemented.`);
const data = v.codec.decode(value, v.extensions);
const data = v.codec.decode(value, { ...v.extensions, schema: v.schema, name: v.name });
if (data === null)
return null;
// TODO
// eslint-disable-next-line
return api.create(data);

@@ -98,3 +100,3 @@ },

}), r_1.r.indexBy(r_1.r.prop(`name`)));
const api = {
const adtApi = {
name,

@@ -142,3 +144,3 @@ schema: variants.length >= 2

decodeOrThrow: (value) => {
const data = api.decode(value);
const data = adtApi.decode(value);
if (data === null)

@@ -150,3 +152,3 @@ throw new Error(`Failed to decode value \`${value}\` into any of the variants for this ADT.`);

};
return api;
return adtApi;
},

@@ -153,0 +155,0 @@ };

14

dist-cjs/Builder.d.ts

@@ -37,3 +37,3 @@ /**

export interface PostVariantBuilder<ADT extends StoredADT, V extends StoredVariant, Vs extends StoredVariants> extends VariantRequired<ADT, [V, ...Vs]> {
codec(params: CodecParams<V>): PostCodecBuilder<ADT, StoredVariant.EnableCodec<V>, Vs>;
codec(definition: CodecDefiniton<V>): PostCodecBuilder<ADT, StoredVariant.EnableCodec<V>, Vs>;
/**

@@ -46,9 +46,13 @@ * Extend the ADT with new properties.

}
export interface CodecParams<V extends StoredVariant = StoredVariant> {
encode: Encoder<V>;
export interface CodecDefiniton<V extends StoredVariant = StoredVariant> {
encode: EncoderDefinition<V>;
decode: DecoderDefinition<V>;
}
export declare type Encoder<V extends StoredVariant> = (variant: StoredVariant.GetType<V>) => string;
export declare type EncoderDefinition<V extends StoredVariant> = (variant: StoredVariant.GetType<V>) => string;
export declare type Encoder<V extends StoredVariant> = EncoderDefinition<V>;
export declare type ADTEncoder<Vs extends StoredVariants> = (adt: StoredVariants.Union<Vs>) => string;
export declare type DecoderDefinition<V extends StoredVariant> = (encodedData: string, extensions: V[`extensions`]) => null | GetConstructorInput<V>;
export declare type DecoderDefinition<V extends StoredVariant> = (encodedData: string, extensions: V[`extensions`] & {
schema: StoredVariant.GetZodSchema<V>;
name: V[`name`];
}) => null | GetConstructorInput<V>;
export declare type Decoder<V extends StoredVariant> = (value: string) => null | StoredVariant.GetType<V>;

@@ -55,0 +59,0 @@ export declare type DecoderThatThrows<V extends StoredVariant> = (value: string) => StoredVariant.GetType<V>;

import { Initial } from './Builder';
import { z } from './lib/z';
import { OmitTag } from './Types';
import { SomeZodObject } from 'zod';
export declare type SomeVariant = object;
export declare type SomeVariantConstructorInput = Record<string, unknown>;
export declare type SomeCodecDefinition = {
encode: SomeEncoderDefinition;
decode: SomeDecoderDefinition;
};
export declare type SomeEncoderDefinition = (variant: SomeVariant) => string;
export declare type SomeDecoderDefinition = (encodedData: string, extensions: {
schema: SomeZodObject;
name: string;
[key: string]: unknown;
}) => null | SomeVariantConstructorInput;
/**

@@ -5,0 +18,0 @@ * Base properties of a data type.

@@ -73,5 +73,7 @@ import { Errors } from './Errors';

throw new Error(`Codec not implemented.`);
const data = v.codec.decode(value, v.extensions);
const data = v.codec.decode(value, { ...v.extensions, schema: v.schema, name: v.name });
if (data === null)
return null;
// TODO
// eslint-disable-next-line
return api.create(data);

@@ -94,3 +96,3 @@ },

}), r.indexBy(r.prop(`name`)));
const api = {
const adtApi = {
name,

@@ -138,3 +140,3 @@ schema: variants.length >= 2

decodeOrThrow: (value) => {
const data = api.decode(value);
const data = adtApi.decode(value);
if (data === null)

@@ -146,3 +148,3 @@ throw new Error(`Failed to decode value \`${value}\` into any of the variants for this ADT.`);

};
return api;
return adtApi;
},

@@ -149,0 +151,0 @@ };

@@ -37,3 +37,3 @@ /**

export interface PostVariantBuilder<ADT extends StoredADT, V extends StoredVariant, Vs extends StoredVariants> extends VariantRequired<ADT, [V, ...Vs]> {
codec(params: CodecParams<V>): PostCodecBuilder<ADT, StoredVariant.EnableCodec<V>, Vs>;
codec(definition: CodecDefiniton<V>): PostCodecBuilder<ADT, StoredVariant.EnableCodec<V>, Vs>;
/**

@@ -46,9 +46,13 @@ * Extend the ADT with new properties.

}
export interface CodecParams<V extends StoredVariant = StoredVariant> {
encode: Encoder<V>;
export interface CodecDefiniton<V extends StoredVariant = StoredVariant> {
encode: EncoderDefinition<V>;
decode: DecoderDefinition<V>;
}
export declare type Encoder<V extends StoredVariant> = (variant: StoredVariant.GetType<V>) => string;
export declare type EncoderDefinition<V extends StoredVariant> = (variant: StoredVariant.GetType<V>) => string;
export declare type Encoder<V extends StoredVariant> = EncoderDefinition<V>;
export declare type ADTEncoder<Vs extends StoredVariants> = (adt: StoredVariants.Union<Vs>) => string;
export declare type DecoderDefinition<V extends StoredVariant> = (encodedData: string, extensions: V[`extensions`]) => null | GetConstructorInput<V>;
export declare type DecoderDefinition<V extends StoredVariant> = (encodedData: string, extensions: V[`extensions`] & {
schema: StoredVariant.GetZodSchema<V>;
name: V[`name`];
}) => null | GetConstructorInput<V>;
export declare type Decoder<V extends StoredVariant> = (value: string) => null | StoredVariant.GetType<V>;

@@ -55,0 +59,0 @@ export declare type DecoderThatThrows<V extends StoredVariant> = (value: string) => StoredVariant.GetType<V>;

{
"name": "alge",
"version": "0.1.0-next.18",
"version": "0.1.0-next.19",
"repository": "git@github.com:jasonkuhrt/alge.git",

@@ -57,2 +57,4 @@ "author": "Jason Kuhrt",

"@types/node": "17.0.23",
"@types/semver": "7",
"@types/semver-utils": "1",
"@typescript-eslint/eslint-plugin": "5.20.0",

@@ -76,2 +78,4 @@ "@typescript-eslint/parser": "5.20.0",

"prettier": "2.6.2",
"semver": "7.3.7",
"semver-utils": "1.1.4",
"ts-jest": "27.1.4",

@@ -78,0 +82,0 @@ "ts-node": "10.7.0",

@@ -75,28 +75,3 @@ import { Alge } from '.'

.done()
it(`if not defined then variant API codec methods not available`, () => {
expectType<never>(A.M.encode)
//eslint-disable-next-line
expect(() => (A.M as any).encode()).toThrowErrorMatchingInlineSnapshot(`"Codec not implemented."`)
expectType<never>(A.M.decode)
//eslint-disable-next-line
expect(() => (A.M as any).decode()).toThrowErrorMatchingInlineSnapshot(`"Codec not implemented."`)
expectType<never>(A.M.decodeOrThrow)
//prettier-ignore
//eslint-disable-next-line
expect(() => (A.M as any).decodeOrThrow()).toThrowErrorMatchingInlineSnapshot(`"Codec not implemented."`)
})
it(`defines an encode and decode method`, () => {
const m = B.M.create({ m: `m` })
expect(B.M.encode(m)).toEqual(`m`)
const decodeResult = B.M.decode(`m`)
expectType<null | z.infer<typeof A.M.schema>>(decodeResult)
expect(B.M.decode(`m`)).toEqual(m)
expect(B.M.decode(``)).toEqual(null)
})
it(`.decodeOrThrow throws if decoding fails`, () => {
expect(() => B.M.decodeOrThrow(``)).toThrowErrorMatchingInlineSnapshot(
`"Failed to decode value \`\` into a A."`
)
})
const m = B.M.create({ m: `m` })
it(`cannot define codec multiple times in the chain`, () => {

@@ -122,2 +97,68 @@ // eslint-disable-next-line

})
describe(`Variant API`, () => {
it(`if not defined then variant API codec methods not available`, () => {
expectType<never>(A.M.encode)
//eslint-disable-next-line
expect(() => (A.M as any).encode()).toThrowErrorMatchingInlineSnapshot(`"Codec not implemented."`)
expectType<never>(A.M.decode)
//eslint-disable-next-line
expect(() => (A.M as any).decode()).toThrowErrorMatchingInlineSnapshot(`"Codec not implemented."`)
expectType<never>(A.M.decodeOrThrow)
//prettier-ignore
//eslint-disable-next-line
expect(() => (A.M as any).decodeOrThrow()).toThrowErrorMatchingInlineSnapshot(`"Codec not implemented."`)
})
describe(`.decode()`, () => {
it(`converts string into data or null on failure`, () => {
const decodeResult = B.M.decode(`m`)
expectType<null | z.infer<typeof A.M.schema>>(decodeResult)
expect(B.M.decode(`m`)).toEqual(m)
expect(B.M.decode(``)).toEqual(null)
})
it(`definition has access to the ADT schema`, () => {
const A = Alge.create(`A`)
.variant(`M`, { m: z.string() })
.codec({
encode: (data) => data.m,
decode: (value, { schema }) => {
expectType<typeof A.M.schema>(schema)
return schema.parse({ m: value, _tag: `M` })
},
})
.done()
expect(A.M.decode(`m`)).toEqual(A.M.create({ m: `m` }))
})
it(`definition has access to the ADT name`, () => {
const A = Alge.create(`A`)
.variant(`M`, { m: z.string() })
.codec({
encode: (data) => data.m,
decode: (_value, { name }) => {
expectType<typeof A.M.name>(name)
return { m: name }
},
})
.done()
expect(A.M.decode(`m`)).toEqual(A.M.create({ m: A.M.name }))
})
})
describe(`.encode()`, () => {
it(`converts data into string`, () => {
const m = B.M.create({ m: `m` })
const encodeResult = B.M.encode(m)
expectType<string>(encodeResult)
expect(encodeResult).toEqual(`m`)
})
})
describe(`.decodeOrThrow() `, () => {
it(`converts string into data or throws error on failure`, () => {
const decodeResult = B.M.decodeOrThrow(`m`)
expectType<z.infer<typeof A.M.schema>>(decodeResult)
expect(() => B.M.decodeOrThrow(``)).toThrowErrorMatchingInlineSnapshot(
`"Failed to decode value \`\` into a A."`
)
})
})
})
describe(`ADT API`, () => {

@@ -124,0 +165,0 @@ it(`If defined for every variant then an aggregate codec is available on the ADT`, () => {

@@ -1,2 +0,2 @@

import { CodecParams, ExtensionsBase, Initial, StoredVariant } from './Builder'
import { ExtensionsBase, Initial, StoredVariant } from './Builder'
import { Errors } from './Errors'

@@ -8,3 +8,20 @@ import { is } from './helpers'

import { OmitTag } from './Types'
import { SomeZodObject } from 'zod'
export type SomeVariant = object
export type SomeVariantConstructorInput = Record<string, unknown>
export type SomeCodecDefinition = {
encode: SomeEncoderDefinition
decode: SomeDecoderDefinition
}
export type SomeEncoderDefinition = (variant: SomeVariant) => string
export type SomeDecoderDefinition = (
encodedData: string,
extensions: { schema: SomeZodObject; name: string; [key: string]: unknown }
) => null | SomeVariantConstructorInput
/**

@@ -156,3 +173,3 @@ * Base properties of a data type.

type SomeVariantDef = Omit<StoredVariant, `codec` | `schema`> & {
codec?: CodecParams
codec?: SomeCodecDefinition
schema: z.SomeZodObject

@@ -187,3 +204,3 @@ }

},
codec: (codecDef: CodecParams) => {
codec: (codecDef: SomeCodecDefinition) => {
if (!currentVariant) throw new Error(`Define variant first.`)

@@ -216,4 +233,6 @@ if (currentVariant.codec) throw new Error(`Codec already defined.`)

if (!v.codec) throw new Error(`Codec not implemented.`)
const data = v.codec.decode(value, v.extensions)
const data = v.codec.decode(value, { ...v.extensions, schema: v.schema, name: v.name })
if (data === null) return null
// TODO
// eslint-disable-next-line
return api.create(data)

@@ -237,3 +256,3 @@ },

const api = {
const adtApi = {
name,

@@ -284,3 +303,3 @@ schema:

decodeOrThrow: (value: string) => {
const data = api.decode(value)
const data = adtApi.decode(value)
if (data === null)

@@ -293,3 +312,3 @@ throw new Error(`Failed to decode value \`${value}\` into any of the variants for this ADT.`)

return api
return adtApi
},

@@ -296,0 +315,0 @@ }

@@ -55,3 +55,3 @@ /**

extends VariantRequired<ADT, [V, ...Vs]> {
codec(params: CodecParams<V>): PostCodecBuilder<ADT, StoredVariant.EnableCodec<V>, Vs>
codec(definition: CodecDefiniton<V>): PostCodecBuilder<ADT, StoredVariant.EnableCodec<V>, Vs>
/**

@@ -67,9 +67,11 @@ * Extend the ADT with new properties.

export interface CodecParams<V extends StoredVariant = StoredVariant> {
encode: Encoder<V>
export interface CodecDefiniton<V extends StoredVariant = StoredVariant> {
encode: EncoderDefinition<V>
decode: DecoderDefinition<V>
}
export type Encoder<V extends StoredVariant> = (variant: StoredVariant.GetType<V>) => string
export type EncoderDefinition<V extends StoredVariant> = (variant: StoredVariant.GetType<V>) => string
export type Encoder<V extends StoredVariant> = EncoderDefinition<V>
export type ADTEncoder<Vs extends StoredVariants> = (adt: StoredVariants.Union<Vs>) => string

@@ -79,3 +81,3 @@

encodedData: string,
extensions: V[`extensions`]
extensions: V[`extensions`] & { schema: StoredVariant.GetZodSchema<V>; name: V[`name`] }
) => null | GetConstructorInput<V>

@@ -82,0 +84,0 @@

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

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