
Security News
CISA Kills Off RSS Feeds for KEVs and Cyber Alerts
CISA is discontinuing official RSS support for KEV and cybersecurity alerts, shifting updates to email and social media, disrupting automation workflows.
purify-ts-extra-codec
Advanced tools
🛠 Extra utility codecs for [purify-ts](https://gigobyte.github.io/purify/).
🛠 Extra utility codecs for purify-ts.
npm install --save purify-ts-extra-codec
# or
yarn add purify-ts-extra-codec
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" }) // Right({ str: "foo" });
const obj: GetInterface<typeof ObjCodec> = { str: "foo" };
❤️ Support schema
.
Ensure input string is non-empty.
NonEmptyString.decode("asdf"); // Right("asdf")
NonEmptyString.decode(""); // Left("[error message]")
NonEmptyString.decode(1234); // Left("[error message]")
❤️ Support schema
.
Ensure length of input string is in range.
StringLengthRangedIn({ gt: 2, lte: 5 }).decode("asdf"); // Right("asdf")
StringLengthRangedIn({ gte: 5, lte: 5 }).decode("asdf"); // Left("[error message]")
StringLengthRangedIn({ gte: 5, lte: 5 }).decode(1234); // Left("[error message]")
❤️ Support schema
.
Ensure input string matched to RegExp.
RegExpMatchedString("\\w+").decode("asdf"); // Right("asdf")
RegExpMatchedString("\\w+").decode("1234"); // Left("[error message]")
RegExpMatchedString("\\w+").decode(1234); // Left("[error message]")
// those are deprecated interface (Non schema support)
RegExpMatchedString(/\w+/).decode("asdf"); // Right("asdf")
RegExpMatchedString(/\w+/).decode("1234"); // Left("[error message]")
RegExpMatchedString(/\w+/).decode(1234); // Left("[error message]")
❤️ Support schema
.
Convert Date instance into formatted string.
FormattedStringFromDate("yyyy_MM_dd").decode(new Date()); // Right("2020_01_01")
FormattedStringFromDate("yyyy_MM_dd").decode(new Date("InvalidDate")); // Left("[error message]")
FormattedStringFromDate("yyyy_MM_dd").decode("asdf"); // Left("[error message]")
⚠️ No schema
support.
Ensure input number is in range.
NumberRangedIn({ gt: 2, lte: 5 }).decode(3); // Right(3)
NumberRangedIn({ gte: 2, lt: 5 }).decode(0); // Left("[error message]")
NumberRangedIn({ gt: 2, lte: 5 }).decode("a"); // Left("[error message]")
❤️ Support schema
.
Convert string into number (if parsable).
NumberFromString.decode("-12.34"); // Right(-12.34)
NumberFromString.decode("Infinity"); // Left("[error message]")
NumberFromString.decode(1234); // Left("[error message]")
❤️ Support schema
.
Ensure input number is integer.
Integer.decode(1234); // Right(1234)
Integer.decode(12.34); // Left("[error message]")
Integer.decode("1234"); // Left("[error message]")
❤️ Support schema
.
Convert string into integer number (if possible).
IntegerFromString.decode("1234"); // Right(1234)
IntegerFromString.decode("12.34"); // Left("[error message]")
IntegerFromString.decode(1234); // Left("[error message]")
❤️ Support schema
.
Convert any date parsable object into Date.
DateFromAny.decode("2020/01/01"); // Right(Wed Jan 01 2020 00:00:00)
DateFromAny.decode(1577804400000); // Right(Wed Jan 01 2020 00:00:00)
DateFromAny.decode("today"); // Left("[error message]")
⚠️ No schema
support.
Convert formatted date string into Date.
DateFromStringFormatOf("yyyy_MM_dd").decode("2020_01_01"); // Right(Wed Jan 01 2020 00:00:00)
DateFromStringFormatOf("yyyy_MM_dd").decode("2020"); // Left("[error message]")
DateFromStringFormatOf("yyyy_MM_dd").decode(new Date()); // Left("[error message]")
⚠️ No schema
support.
Convert string into Json value.
JsonFromString(Codec.interface({ type: string })).decode(`{ "type": "hello" }`); // Right({ type: "hello" })
JsonFromString(Codec.interface({ type: string })).decode(`{}`); // Left("[error message]")
JsonFromString(Codec.interface({ type: string })).decode(1234); // Left("[error message]")
⚠️ No schema
support.
Utility for adding a schema after defined.
withSchema(
MyCodec,
(myCodecSchema) => ({
...myCodecSchema,
pattern: "^[a-fA-F\\d]{8}$"
})
);
Utility for type narrowing.
const ThreeLengthString = extendCodec(stringCodec, (str) =>
str.length === 3 ? Right(str) : Left(`${str} must have exact 3 length`)
);
ThreeLengthString.decode("123"); // Right("123");
ThreeLengthString.decode("1"); // Left("[error message]")
ThreeLengthString.decode(1); // Left("[error message]")
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"); // Right(123)
ThreeDigitNumberFromString.decode("1.4"); // Left("[error message]")
ThreeDigitNumberFromString.decode(123); // Left("[error message]")
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)
));
})
FAQs
🛠 Extra utility codecs for [purify-ts](https://gigobyte.github.io/purify/).
We found that purify-ts-extra-codec demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
CISA is discontinuing official RSS support for KEV and cybersecurity alerts, shifting updates to email and social media, disrupting automation workflows.
Security News
The MCP community is launching an official registry to standardize AI tool discovery and let agents dynamically find and install MCP servers.
Research
Security News
Socket uncovers an npm Trojan stealing crypto wallets and BullX credentials via obfuscated code and Telegram exfiltration.