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

super-ts

v1.11.1

Published

Typescript functional programming library inspired by Haskell and PureScript providing both runtime type checking and functional algebraic data types.

Downloads

116

Readme

🦸 λΔ super-ts

npm version

super-ts is a Typescript functional programming library inspired by Haskell and PureScript providing both runtime type checking and functional algebraic data types.

super-ts were built to unify concepts and tools of some wonderful libraries, such fp-ts, io-ts and runtypes.

Forwarding to be an all-in-one solution, super-ts is divided into two main categories: Algebraic Types & Runtime Types;

  • Runtime Types: static and runtime type checking
  • Algebraic Types: Monads, Functors, Semigroups and others.

Instalation

npm install super-ts will install super-ts for use.

Consuming

You can use super-ts in two flavors: CommonJS or ES Modules.

import { String } from "super-ts/cjs/runtime";
import { Either } from "super-ts/cjs/algebraic";
import { String } from "super-ts/esm/runtime";
import { Either } from "super-ts/esm/algebraic";

PS: If you are using the package with ES Modules over node you should run your application with the flag --experimental-specifier-resolution=node

ES Modules enables tree shaking to be possible using bundlers.

Δ Runtime Types

super-ts provides you with primitive types that can be safely type-checked at runtime with custom constraints and use this same schema as your Typescript static type signature. You can also provide custom runtime checks to build more advanced types that fit your use cases.

Δ Why is useful?

Let's suppose that you have the following static type schema on your Typescript project:

type League = "NFL" | "MLB" | "NBA" | "WNBA";

type Gender = "Male" | "Female" | "Other";

type Team = {
  name: string;
  yearFounded: number;
  league: League;
  type: "team";
};

type Player = {
  firstName: string;
  lastName: string;
  salaryOnTeam: [number, Team];
  age: number;
  isActive: boolean;
  teamsPlayed: Team[];
  gender: Gender;
  type: "player";
};

This works fine on TypeScript since you use this schema to type safe your application on compile time, but what about runtime? What happens if you receive this schema as a payload from some external source?

In this scenario your application is unsafe and you need to do a lot of validations to avoid runtime errors.

‌To avoid all this boring work you can use super-ts runtime types to define your schema keeping your application safe on runtime as well on compile time, re-using static types generated by super-ts.

Δ Example (Defining schema with super-ts)

In order to define the same schema as above using super-ts we do the following:

import {
  String,
  Number,
  Boolean,
  Array,
  Record,
  Literal,
  Tuple,
  Union,
} from "super-ts/esm/runtime";

const League = Union(
  Literal("NFL"),
  Literal("MLB"),
  Literal("NBA"),
  Literal("WNBA")
);

const Gender = Union(Literal("Male"), Literal("Female"), Literal("Other"));

const Team = Record({
  name: String,
  yearFounded: Number,
  league: League,
  type: Literal("team"),
});

const Player = Record({
  firstName: String,
  lastName: String,
  salaryOnTeam: Tuple(Number, Team),
  age: Number,
  isActive: Boolean,
  teamsPlayed: Array(Team),
  gender: Gender,
  type: Literal("player"),
});

When you define your schema using super-ts you can get Typescript static types using the custom TypeOf type.

import { TypeOf } from 'super-ts/esm/runtime'

type Player = TypeOf<typeof Player>;

// type Player = {
//    firstName: string;
//    lastName: string;
//    salaryOnTeam: [number, {
//        name: string;
//        yearFounded: number;
//        league: "NFL" | "MLB" | "NBA" | "WNBA";
//        type: "team";
//    }];
//    age: number;
//    isActive: boolean;
//    teamsPlayed: {
//        ...;
//    }[];
//    gender: "Male" | "Female" | "Other";
//    type: "player";
// }

Δ API

When you use the runtime types, we expose an API under the property so you can use functions available for the type.

check :: a -⁠> Resultλ InvalidCheck a

Takes an unknown payload and validates against the type. If the validation suceeds, we return an algebraic type called Resultλ of Sucess which contains the payload. If the check fails we return an Resultλ of Failure containing an NonEmptyArrayλ of InvalidCheck containing all the errors found on that payload.

Example

import { identity } from 'super-ts/common/identity';
import { Result } from 'super-ts/algebraic';

/** other imports .. */

/** above code .. */

const Team = Record({
  name: String,
  yearFounded: Number,
  league: League,
  type: Literal("team"),
});

const teamInvalidPayload = {
  name: null,
  yearFounded: "1974",
  league: "NFL",
  type: "team",
};

const isValidTeam = Team.Δ.check(teamInvalidPayload);

const isValidTeamRes = Result.λ.fold (identity, identity) (isValidTeam);


// isValidTeamRes = [
//    {
//        code: 'IS_STRING',
//        message: 'Expected string but found (null :: object)',
//        path: 'name'
//    },
//    {
//        code: 'IS_NUMBER',
//        message: 'Expected Number but found (1974 :: string)',
//        path: 'yearFounded'
//    }
//  ]

λ Algebraic Types

This documentation is working in progress.. 😅 🚧