npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

purify-ts-extra-codec

v0.6.0

Published

🛠 Extra utility codecs for [purify-ts](https://gigobyte.github.io/purify/).

Downloads

1,687

Readme

purify-ts-extra-codec

🛠 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" }) // Right({ str: "foo" });
const obj: GetInterface<typeof ObjCodec> = { str: "foo" };

❤️ Support schema.

String Codec Module

NonEmptyString

Ensure input string is non-empty.

NonEmptyString.decode("asdf"); // Right("asdf")
NonEmptyString.decode(""); // Left("[error message]")
NonEmptyString.decode(1234); // Left("[error message]")

❤️ Support schema.

StringLengthRangedIn

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.

RegExpMatchedString

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.

FormattedStringFromDate

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.

Number Codec Module

NumberRangedIn

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.

NumberFromString

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.

Integer

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.

IntegerFromString

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.

Date Codec Module

DateFromAny

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.

DateFromStringFormatOf

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.

Json Codec Module

JsonFromString

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.

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"); // Right("123");
ThreeLengthString.decode("1"); // Left("[error message]")
ThreeLengthString.decode(1); // Left("[error message]")

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"); // Right(123)
ThreeDigitNumberFromString.decode("1.4"); // Left("[error message]")
ThreeDigitNumberFromString.decode(123); // Left("[error message]")

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)
  ));
})