Giter Site home page Giter Site logo

rafa2000 / tcomb Goto Github PK

View Code? Open in Web Editor NEW

This project forked from gcanti/tcomb

0.0 2.0 0.0 1.58 MB

Domain driven design and type checking for JavaScript

Home Page: https://gcanti.github.io/tcomb

License: MIT License

JavaScript 95.17% TypeScript 4.83%

tcomb's Introduction

build status dependency status npm downloads Join the chat at https://gitter.im/gcanti/tcomb

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

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'

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: 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'

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

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 informations 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 / deseralization

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

With babel-plugin-tcomb you can use type annotations:

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

TypeScript definition file

index.d.ts

Contributors

Similar projects

License

The MIT License (MIT)

tcomb's People

Contributors

bzalasky avatar gabro avatar gcanti avatar grahamlyus avatar grncdr avatar huevy avatar jayphelps avatar jgoux avatar kohei-takata avatar m0x72 avatar rafa2000 avatar utaal avatar

Watchers

 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.