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

styp

v0.0.2

Published

Algebraic Sum Types for Javascript (in beta)

Downloads

10

Readme

Styp (Sum TYPes) is written in Javascript, the library provides mechanisms for creating constructors and sum types or disjoint union types, the distribution size of the library is 611 bytes(Gzipped). This libary has been inspired by languages like Haskell, F# and OCaml which provide sum types natively. Styp has also taken inspiration from daggy(a library for creating sum types in Javascript).

Example Code

const { tagged, sum } = require("styp");

const Polar = tagged("Polar", ["r", "theta"]);

let p1 = Polar(3, 0.88);
console.log(p1.toString()); // -> Polar(3,0.88)

const Maybe = sum("Maybe", {
    Just: ["val"],
    Nothing: []
});

let test1 = Maybe.Just(10);

console.log(test1.toString()); // -> Maybe.Just(10)
console.log(Maybe.is(test1)); // -> true
console.log(Maybe.Just.is(test1)); // -> true
console.log(Maybe.Nothing.is(test1)); // -> false

let times = Maybe.Nothing
    .cata({
        Just: x => x.val,
        Nothing: () => 1
    });

console.log(times * 5); // -> 5

Installation

Node

npm i styp

Browser

<script src="https://unpkg.com/styp"></script>

Documentation

Underconstruction!

tagged(typename: String, fields: Array[String]) -> Function | Object

This function takes typename and fields as params and returns a constructor function for the specified data type. In case of an empty fields array this function will return an object which you can use like a type with only one value.

const { tagged } = require("styp");

const Point = tagged("Point", ["x","y"]);
const nil = tagged("nil", []); // -> my custom null type 

const p1 = Point(10,20);
const p2 = Point(5,5);

let temp = nil;
console.log(nil.is(temp));
console.log(temp.toString());

Every constructor functor has these methods: is, from,toString. The instance created by constructor has toString method and more methods can be added by the user more details are given below in the constructors section.

sum(typename: String, constructors: Object) -> Object

This function helps create a sum type (which is represented as an Object) it takes typename and an object which contains names of constructors and their fields(if any). The function returns an object which has all constructor functions (as specified in the passed constructors param).

const { sum } = require("styp");

const SafeDiv = sum("SafeDiv", [
    Succ:["v"],
    byZero: []
]);

const safelyDivide = (n,d) => d? SafeDiv.Succ(n/d): SafeDiv.byZero;

console.log(safelyDivide(10,2).toString());
console.log(safelyDivide(2,0).toString());

Constructors

Every constructor function/sum type object has the following methods defined -

  • toString

  • is

and

  • from (only defined on constructor functions)

{constructor}.toString() -> String

const { tagged } = require("styp");

const Point = sum("Point", {
    Cartesian: ["x","y","z"],
    Polar: ["r","theta"]
});

console.log(Point.toString()); // -> Point
console.log(Point.Cartesian.toString()); // -> Point.Cartesian
console.log(Point.Polar.toString()); // -> Point.Polar

{constructor}.is(obj: Object) -> Boolean

const { tagged } = require("styp");

const Point = tagged("Point", ["x","y"]);
let p1 = Point(2, 7);

console.log(Point.is(p1));  // -> true
console.log(Point.is({ random:true }));  // -> false

{constructor}.from(obj: Object) -> Object

const { tagged } = require("styp");

const Point = tagged("Point", ["x","y"]);
let p1 = Point.from({ x:10, y:3, extra:"anything" });

console.log(p1.toString()); // -> Point(10,3)

Instances

Every instance created by a constructor function is immutable. There is one predefined method toString. More methods can be added to the prototype of the constructor function.

const { tagged } = require("styp");

const Point = tagged("Point", ["x","y"]);

console.log(Point(5,5).toString()); // -> Point(5,5)

Point.prototype.scale = function(n) {
    return Point(this.x*n,this.y*n);
}

console.log(Point(5,5).scale(2).toString()); // -> Point(10,10)

There is special method accessible on instances of constructors belonging to sum types - cata

const { sum } = require("styp");

const Point = sum("Point", {
    Cartesian: ["x","y","z"],
    Polar: ["r","theta"]
});

let p1 = Point.Cartesian(2,3);
let p2 = Point.Polar(1,5);

let doSomething = (point) => point.cata({
                        Cartesian: c => c.x,
                        Polar: p => p.r
                    });

console.log(doSomething(p1)); // -> 2
console.log(doSomething(p2)); // -> 1