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

refinement-ts

v0.0.1

Published

Combinators for composing strongly typed predicates: refinements and definements.

Downloads

3

Readme

refinement-ts

Combinators for composing strongly typed predicates: refinements and definements.

The basic operations of and, or and not have been implemented. Feature requests are welcome.

Quick Start

Installation

Install from NPM with yarn add refinement-ts or npm install refinement-ts --save, dependant on your package manager of choice.

Example

import { refinement as RF } from "refinement-ts";

const isString = (a: unknown): a is string => typeof a === "string";
const isNumber = (a: unknown): a is number => typeof a === "number";
const isShrek = (a: unknown): a is "Shrek" => a === "Shrek";

// RF.Refinement is here only to show what it's doing.
// types will be infered from usage.

// result type has been narrowed to `"Shrek"`
// because type `string & "Shrek"` is type `"Shrek"`
const result1: RF.Refinement<unknown, "Shrek"> = RF.and(isString, isShrek);

// result type has been widened to `string`
// because type `string | "Shrek"` is type `string`

const result2: RF.Refinement<unknown, string> = RF.or(isString, isShrek);

// tserror.
// becuase type `string & number` is type `never`.
const result3: RF.Refinement<unknown, string> = RF.and(isString, isNumber);

// result type is widened to `string | number`
// because type `string | number` is type `string | number`.

const result4: RF.Refinement<unknown, string> = RF.or(isString, isNumber);

Refinement

Here is the signature for a refinement:

export interface Refinement<A, B extends A> {
  (a: A): a is B;
}

A refinement is a predicate with a type guard, which can be read about from the typescript handbook.

I saw the name in fp-ts and felt it is fitting.

Definement

Here is the signature for a definement:

export interface Definement<T, U> {
  <A extends T>(a: A): a is A extends U ? A : never;
}

This is just like a refinement, but uses a generic as the input value. This is constrained by the input type T.

This is very special because it can take any compatible or incompatible return value and refine it in real time.

My experience indicates these are useful when refining different types of objects.

What's next?

Glad you asked.

Tests

These should be easy, but I haven't done them. If you're new to testing and want to give it a go, I welcome PR's.

Catalogue

I'd love to see a catalogue of refinements and definements for most cases. Writing refinements and definements is extremely verbose in Typescript.

Constructors

I'd like to see a constructor that build a Definement and a Refinement out of thin air, to save some time.