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

notaryjs

v1.0.0

Published

Small library providing function type checking at runtime.

Downloads

10

Readme

npm version CircleCI build

notaryjs

Library allowing type checking of functions at runtime, using haskell-like function signatures.

Install

npm install --save notaryjs

Usage

Basic: type signatures

import { sign } from 'notaryjs'

const addNumbers = sign('number -> number -> number', (n, m) => n + m)

// The following call will work seamlessly.
const one = addNumbers(.5, .5)

// The following call will throw an error, warning about a signature violation.
const two = addNumbers('one', 1)

Advanced: type classes

import { notary } from 'notaryjs'

const sign = notary({
  int: i => typeof i === 'number' && i % 1 === 0
})

const addIntegers = sign('int i => i -> i -> i', (i, j) => i + j)

// The following call will work seamlessly.
const five = addIntegers(1, 4)

// The following call will throw an error, warning about signature violation.
const six = addIntegers(1.5, 4.5)

API

notary(typeClasses)

Create a sign function, passing in an object containing all the type classes that will be used in signature constraints. The returned sign function is described later.

The first parameter must contain one key for each type class. Their values can either be:

  • a function, which will be used to test whether a value matches the type class. It's first argument will be the value to test, and must return a truthy value if the test is passed, and a falsy value otherwise. Example:
const sign = notary({
    letter: le => typeof le === 'string' && le.length === 1
})
  • another object. In this case, values will match the type class if all the properties in the object are defined for them (or in their protoype chain). Example:
const sign = notary({
    point: {
      x: 'x coordinate of the point',
      y: 'y coordinate of the point'
    }
})

sign(signature, function)

Creates a typed function. It's first argument is a string containing a haskell-like type signature, the second one is the function to be typed. It returns a function just like the one passed in, only this one will throw errors whenever the signature is violated. It can be used straight from the library for basic signatures, or previous creation with notary() for class-constrained signatures.

Valid signatures must have the following structure:

[class constraints => ] parameter-type-1 -> parameter-type-2 -> ... -> return type

where class constraints are a list or type class, type variable pairs:

type-class-1 type-variable-1, type-class-2 type-variable-2, ...

Allowed types are:

  • string
  • number
  • boolean
  • function
  • symbol
  • object
  • homogenous arrays: [[string]], [number], ...
  • type variables (their name must not match any other type)

Examples:

const basicSign = notary()

const words = basicSign(
  'string -> [string]',
  str => str.split(' ').filter( w => w !== '' ).map( w => w.trim() )
)

const advancedSign = notary({
  functor: { fmap: '' }
})

const apply = advancedSign(
  'functor f => function -> f -> f',
  (callback, f) => f.fmap(callback)
)

Testing

In order to test the library, just run npm test. Make sure you have installed all dependencies by running npm install first.