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

tcomb

v3.2.29

Published

Type checking and DDD for JavaScript

Downloads

6,366,864

Readme

build status dependency status npm downloads

"Si vis pacem, para bellum" - (Vegetius 5th century)

tcomb is a library for Node.js and the browser which allows you to check the types of JavaScript values at runtime with a simple and concise syntax. It's great for Domain Driven Design and for adding safety to your internal code.

TypeScript / Flowtype users

You may want to check out io-ts

IMPORTANT: Running in production

tcomb is supposed to be used in development and is disabled in production. If you want type checks in production you may use

Setup

npm install tcomb --save

Code example

A type-checked function:

import t from 'tcomb';

function sum(a, b) {
  t.Number(a);
  t.Number(b);
  return a + b;
}

sum(1, 's'); // throws '[tcomb] Invalid value "s" supplied to Number'

// using babel-plugin-tcomb
function sum(a: number, b: number) {
  return a + b;
}

A user defined type:

const Integer = t.refinement(t.Number, (n) => n % 1 === 0, 'Integer');

A type-checked class:

const Person = t.struct({
  name: t.String,              // required string
  surname: t.maybe(t.String),  // optional string
  age: t.Integer,                // required integer
  tags: t.list(t.String)       // a list of strings
}, 'Person');

// methods are defined as usual
Person.prototype.getFullName = function () {
  return `${this.name} ${this.surname}`;
};

const person = Person({
  surname: 'Canti'
}); // throws '[tcomb] Invalid value undefined supplied to Person/name: String'

Chrome DevTools:

throws

Docs

Features

Lightweight

3KB gzipped, no dependencies.

Type safety

All models defined with tcomb are type-checked.

Note. Instances are not boxed, this means that tcomb works great with lodash, Ramda, etc. And you can of course use them as props to React components.

Based on set theory

Domain Driven Design

Write complex domain models in a breeze and with a small code footprint. Supported types / combinators:

  • user defined types
  • structs
  • lists
  • enums
  • refinements
  • unions
  • intersections
  • the option type
  • tuples
  • dictionaries
  • functions
  • recursive and mutually recursive types
  • interfaces

Immutability and immutability helpers

Instances are immutable using Object.freeze. This means you can use standard JavaScript objects and arrays. You don't have to change how you normally code. You can update an immutable instance with the provided update(instance, spec) function:

const person2 = Person.update(person, {
  name: { $set: 'Guido' }
});

where spec is an object containing commands. The following commands are compatible with the Facebook Immutability Helpers:

  • $push
  • $unshift
  • $splice
  • $set
  • $apply
  • $merge

See Updating immutable instances for details.

Speed

Object.freeze calls and asserts are executed only in development and stripped out in production (using process.env.NODE_ENV !== 'production' tests).

Runtime type introspection

All models are inspectable at runtime. You can read and reuse the information stored in your types (in the meta static member). See The meta object in the docs for details.

Libraries exploiting tcomb's RTI:

Easy JSON serialization / deserialization

Encodes / decodes your domain models to / from JSON for free.

Debugging with Chrome DevTools

You can customize the behavior when an assert fails leveraging the power of Chrome DevTools.

// use the default...
t.fail = function fail(message) {
  throw new TypeError('[tcomb] ' + message); // set "Pause on exceptions" on the "Sources" panel for a great DX
};

// .. or define your own behavior
t.fail = function fail(message) {
  console.error(message);
};

Pattern matching

const result = t.match(1,
  t.String, () => 'a string',
  t.Number, () => 'a number'
);

console.log(result); // => 'a number'

Babel plugin

Using babel-plugin-tcomb you can also write (Flow compatible) type annotations:

function sum(a: number, b: number): number {
  return a + b;
}

TypeScript definition file

index.d.ts

Contributors

How to Build a standalone bundle

git clone [email protected]:gcanti/tcomb.git
cd tcomb
npm install
npm run dist

Will output 2 files:

  • dist/tcomb.js (development)
  • dist/tcomb.min.js (production) Object.freeze calls and asserts stripped out

Related libraries

Similar projects

License

The MIT License (MIT)