🛠 Extra utility codecs for purify-ts.
Install
npm install --save purify-ts-extra-codec
# or
yarn add purify-ts-extra-codec
API
interface codec alternative
Interface
Alternative implementation of Codec.interface
.
Original Codec.interface
returns optional field type to T | undefined
, so it should give undefined explicitly if declare value with type annotation.
const value: GetType<typeof MyCodec> = { optionalField: undefined }
Interface
allows to you to implicit undefined field value.
Implementation of Interface
is internally uses original Codec.interface
, it only changes returning type.
const ObjCodec = Interface({ str: string, opt: optional(string) });
ObjCodec.decode({ str: "foo" })
const obj: GetInterface<typeof ObjCodec> = { str: "foo" };
❤️ Support schema
.
String Codec Module
NonEmptyString
Ensure input string is non-empty.
NonEmptyString.decode("asdf");
NonEmptyString.decode("");
NonEmptyString.decode(1234);
❤️ Support schema
.
StringLengthRangedIn
Ensure length of input string is in range.
StringLengthRangedIn({ gt: 2, lte: 5 }).decode("asdf");
StringLengthRangedIn({ gte: 5, lte: 5 }).decode("asdf");
StringLengthRangedIn({ gte: 5, lte: 5 }).decode(1234);
❤️ Support schema
.
RegExpMatchedString
Ensure input string matched to RegExp.
RegExpMatchedString("\\w+").decode("asdf");
RegExpMatchedString("\\w+").decode("1234");
RegExpMatchedString("\\w+").decode(1234);
RegExpMatchedString(/\w+/).decode("asdf");
RegExpMatchedString(/\w+/).decode("1234");
RegExpMatchedString(/\w+/).decode(1234);
❤️ Support schema
.
FormattedStringFromDate
Convert Date instance into formatted string.
FormattedStringFromDate("yyyy_MM_dd").decode(new Date());
FormattedStringFromDate("yyyy_MM_dd").decode(new Date("InvalidDate"));
FormattedStringFromDate("yyyy_MM_dd").decode("asdf");
⚠️ No schema
support.
Number Codec Module
NumberRangedIn
Ensure input number is in range.
NumberRangedIn({ gt: 2, lte: 5 }).decode(3);
NumberRangedIn({ gte: 2, lt: 5 }).decode(0);
NumberRangedIn({ gt: 2, lte: 5 }).decode("a");
❤️ Support schema
.
NumberFromString
Convert string into number (if parsable).
NumberFromString.decode("-12.34");
NumberFromString.decode("Infinity");
NumberFromString.decode(1234);
❤️ Support schema
.
Integer
Ensure input number is integer.
Integer.decode(1234);
Integer.decode(12.34);
Integer.decode("1234");
❤️ Support schema
.
IntegerFromString
Convert string into integer number (if possible).
IntegerFromString.decode("1234");
IntegerFromString.decode("12.34");
IntegerFromString.decode(1234);
❤️ Support schema
.
Date Codec Module
DateFromAny
Convert any date parsable object into Date.
DateFromAny.decode("2020/01/01");
DateFromAny.decode(1577804400000);
DateFromAny.decode("today");
⚠️ No schema
support.
DateFromStringFormatOf
Convert formatted date string into Date.
DateFromStringFormatOf("yyyy_MM_dd").decode("2020_01_01");
DateFromStringFormatOf("yyyy_MM_dd").decode("2020");
DateFromStringFormatOf("yyyy_MM_dd").decode(new Date());
⚠️ No schema
support.
Json Codec Module
JsonFromString
Convert string into Json value.
JsonFromString(Codec.interface({ type: string })).decode(`{ "type": "hello" }`);
JsonFromString(Codec.interface({ type: string })).decode(`{}`);
JsonFromString(Codec.interface({ type: string })).decode(1234);
⚠️ No schema
support.
Codec Utility Module
withSchema
Utility for adding a schema after defined.
withSchema(
MyCodec,
(myCodecSchema) => ({
...myCodecSchema,
pattern: "^[a-fA-F\\d]{8}$"
})
);
extendCodec
Utility for type narrowing.
const ThreeLengthString = extendCodec(stringCodec, (str) =>
str.length === 3 ? Right(str) : Left(`${str} must have exact 3 length`)
);
ThreeLengthString.decode("123");
ThreeLengthString.decode("1");
ThreeLengthString.decode(1);
chainCodec
Utility for composing multiple Codecs.
This function accepts up to 9 Codecs.
⚠️ schema
only uses last one codec schema.
const ThreeDigitIntegerFromString = chainCodec(
ThreeLengthString,
NumberFromString,
Integer
);
ThreeDigitNumberFromString.decode("123");
ThreeDigitNumberFromString.decode("1.4");
ThreeDigitNumberFromString.decode(123);
sequenceWeak
sequenceWeak
accepts up to 9 Either values and combines to one Either tuple.
All of Either Left
type must be same, but Right
types may not be same.
It is useful to wrap up multiple Codec results.
Name comes from weak Right type constraint of Either.sequence
.
import { sequenceWeak } from "./sequenceWeak";
EitherAsync(async ({liftEither}) => {
const [str, { id }, int] = await liftEither(sequenceWeak(
string.decode(var1),
Codec.interface({ id: string }).decode(var2),
Integer.decode(var3)
));
})