Code Monkey home page Code Monkey logo

sarcastic's Introduction

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

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, pkgShape, 'pkg');
}

let pkg = assertPkg(require('./package.json'));
// type:
//   {
//     name: string,
//     version: string,
//     private: boolean | null,
//     scripts: { [key: string]: string } | null,
//     bin: string | Array<string> | null
//   }

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.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
  }
}

sarcastic's People

Contributors

jamiebuilds avatar aulisius avatar

Watchers

James Cloos avatar David Buchan-Swanson avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.