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

@blacklane/sarcastic

v1.5.1

Published

Cast unknown values to typed values

Downloads

4

Readme

Sarcastic

Cast unknown values to typed values

  • Asserts that a value matches the defined type
  • Returns a typed value
  • Copies the value for type safety

Install

yarn add sarcastic

Usage

const is = require('sarcastic');

const PKG_SHAPE = is.shape({
  name: is.string,
  version: is.string,
  private: is.default(is.boolean, false),
  scripts: is.maybe(is.objectOf(is.string)),
  bin: is.maybe(is.either(is.string, is.arrayOf(is.string))),
});

let pkg = is(require('./package.json'), PKG_SHAPE);
// {
//   name: "sarcastic",
//   version: "1.0.0",
//   private: false,
//   scripts: { "test": "ava" },
//   bin: null
// }

With strict typing:

import is, { type AssertionType } from 'sarcastic';

const PKG_SHAPE = is.shape({
  name: is.string,
  version: is.string,
  private: is.maybe(is.boolean),
  scripts: is.maybe(is.objectOf(is.string)),
  bin: is.maybe(is.either(is.string, is.arrayOf(is.string))),
});

type PkgShape = AssertionType<typeof PKG_SHAPE>;

function assertPkg(pkg: mixed): PkgShape {
  return is(pkg, PKG_SHAPE, 'pkg');
}

let pkg = assertPkg(require('./package.json'));

API

is(val, assertion, name?)

is(true, is.boolean);
is(true, is.boolean, "example");

is.boolean

is(true, is.boolean); // returns true
is(false, is.boolean); // returns false
is(42, is.boolean); // throws instanceof is.AssertionError

is.number

is(42, is.number); // returns 42
is(NaN, is.number); // returns NaN
is(true, is.number); // throws instanceof is.AssertionError

is.string

is("", is.string), ""); // returns ""
is("hi", is.string); // returns "hi"
is(true, is.string); // throws instanceof is.AssertionError

is.array

is([], is.array); // returns []
is([1, 2, 3], is.array); // returns [1, 2, 3]
is({}, is.array); // throws instanceof is.AssertionError

is.func

is(() => {}, is.func); // returns () => {}
is({}, is.func); // throws instanceof is.AssertionError
is(/regex/, is.func); // throws instanceof is.AssertionError

is.object

is({}, is.object); // returns {}
is({ foo: true }, is.object); // returns { foo: true }
is([], is.object); // throws instanceof is.AssertionError
is(null, is.object); // throws instanceof is.AssertionError

is.arrayOf(assertion)

is([], is.arrayOf(is.number)); // returns []
is([1, 2, 3], is.arrayOf(is.number)); // returns [1, 2, 3]
is({}, is.arrayOf(is.number)); // throws instanceof is.AssertionError
is(["hi"], is.arrayOf(is.number)); // throws instanceof is.AssertionError

is.arrayish(assertion)

is(1, is.arrayish(is.number)); // returns [1]
is([], is.arrayish(is.number)); // returns []
is([1, 2, 3], is.arrayish(is.number)); // returns [1, 2, 3]
is("hi", is.arrayish(is.number)); // throws instanceof is.AssertionError
is({}, is.arrayish(is.number)); // throws instanceof is.AssertionError
is(["hi"], is.arrayish(is.number)); // throws instanceof is.AssertionError

is.objectOf(assertion)

is({}, is.objectOf(is.boolean)); // returns {}
is({ foo: true }, is.objectOf(is.boolean)); // returns { foo: true }
is([], is.objectOf(is.boolean)); // throws instanceof is.AssertionError
is(null, is.objectOf(is.boolean)); // throws instanceof is.AssertionError
is({ foo: 42 }, is.objectOf(is.boolean)); // throws instanceof is.AssertionError

is.shape({ [key: string]: assertion })

let myShape = is.shape({ foo: is.boolean });

is({ foo: true }, myShape); // returns { foo: true
is({ foo: true, bar: false }, myShape); // returns { foo: true }
is([], myShape); // throws instanceof is.AssertionError
is(null, myShape); // throws instanceof is.AssertionError
is({ foo: 42 }, myShape); // throws instanceof is.AssertionError

is.maybe(assertion)

is(undefined, is.maybe(is.boolean)); // returns null
is(null, is.maybe(is.boolean)); // returns null
is(true, is.maybe(is.boolean)); // returns true
is(42, is.maybe(is.boolean)); // throws instanceof is.AssertionError

is.default(assertion, defaultValue)

is(undefined, is.default(is.number, 42)); // returns 42
is(null, is.default(is.number, 42)); // returns 42
is(3.14, is.default(is.number, 42)); // returns 3.14
is("hi", is.default(is.number, 42)); // throws instanceof is.AssertionError

is.either(assertionA, assertionB)

is(true, is.either(is.boolean, is.string)); // returns true
is("hi", is.either(is.boolean, is.string)); // returns "hi"
is(42, is.either(is.boolean, is.string)); // throws instanceof is.AssertionError

is.AssertionError

try {
  is(true, is.number);
} catch (err) {
  if (err instanceof is.AssertionError) {
    // an assertion error
  } else {
    // some other unexpected error
  }
}

is.literal(stringLiteral)

is('a', is.literal('a')) // returns 'a'
is(42, is.literal('a')) // throws instanceof is.AssertionError

With strict typing:

let literalAssetion = is.literal<'a'>('a');
// or if syntax above doesn't work due to JSX, Prettier, ESLint etc
import { type Assertion } from 'sarcastic';
let literalAssetion: Assertion<'a'> = is.literal('a');

is('a', literalAssetion) // returns 'a'
is(42, is.literal<'a'>('a')) // throws instanceof is.AssertionError

is.literals(arrayOfStringLiterals)

let literalsAssertion = is.literals([
  'a', 'b', 'c'
]);
is('a', literalsAssertion) // returns 'a'
is('b', literalsAssertion) // returns 'b'
is('c', literalsAssertion) // returns 'c'
is(42, literalsAssertion) // throws instanceof is.AssertionError

With strict typing:

let literalsAssertion = is.literals<'a'|'b'|'c'>([
  'a', 'b', 'c'
]);
// or if syntax above doesn't work due to JSX, Prettier, ESLint etc
import { type Assertion } from 'sarcastic';
let literalsAssertion: Assertion<'a'|'b'|'c'> = is.literals<'a'|'b'|'c'>([
  'a', 'b', 'c'
]);
is('a', literalsAssertion) // returns 'a'
is('b', literalsAssertion) // returns 'b'
is('c', literalsAssertion) // returns 'c'
is(42, literalsAssertion) // throws instanceof is.AssertionError