Comparing version 2.0.0-beta to 2.0.0
@@ -20,2 +20,3 @@ # Changelog | ||
- add `isType(x)` function | ||
- add `stringify(x)` function | ||
- **Breaking change** | ||
@@ -22,0 +23,0 @@ - numeric types on enums #93 (thanks @m0x72) |
@@ -208,3 +208,3 @@ # Setup | ||
// same as | ||
const Country = t.enums(['IT', 'US'], 'Country'); | ||
const Country = t.enums.of(['IT', 'US'], 'Country'); | ||
@@ -522,3 +522,3 @@ // same as | ||
### The `of(f, uncurried)` function | ||
### The `of(f: Function, curried?: boolean)` function | ||
@@ -562,3 +562,3 @@ ```js | ||
### The `is()` function | ||
### The `is(x: any)` function | ||
@@ -683,2 +683,2 @@ ```js | ||
...unless `override = true` | ||
...unless `override = true` |
141
index.js
'use strict'; | ||
function stringify(x) { | ||
try { // handle "Converting circular structure to JSON" error | ||
return JSON.stringify(x, null, 2); | ||
} catch (e) { | ||
return String(x); | ||
} | ||
} | ||
function isInstanceOf(x, constructor) { | ||
@@ -58,8 +66,8 @@ return x instanceof constructor; | ||
function create(type, value, mut) { | ||
function create(type, value) { | ||
if (isType(type)) { | ||
return isStruct(type) ? | ||
// for structs the new operator is allowed | ||
new type(value, mut) : | ||
type(value, mut); | ||
new type(value) : | ||
type(value); | ||
} | ||
@@ -69,3 +77,3 @@ | ||
// type should be a class constructor and value some instance, just check membership and return the value | ||
assert(isInstanceOf(value, type), 'Invalid argument value supplied to constructor ' + getFunctionName(type)); | ||
assert(isInstanceOf(value, type), 'The value ' + stringify(value) + ' is not an instance of ' + getFunctionName(type)); | ||
} | ||
@@ -106,3 +114,3 @@ | ||
if (process.env.NODE_ENV !== 'production') { | ||
assert(!target.hasOwnProperty(k), 'Cannot overwrite property ' + k); | ||
assert(!target.hasOwnProperty(k), 'Cannot overwrite property ' + k + ' in mixin(' + stringify(target) + ', ' + stringify(source) + ')'); | ||
} | ||
@@ -132,3 +140,3 @@ } | ||
if (process.env.NODE_ENV !== 'production') { | ||
assert(isObject(spec), 'Invalid argument spec supplied to update()'); | ||
assert(isObject(spec), 'Invalid argument spec = ' + stringify(spec) + ' supplied to function update(instance, spec): expected an object'); | ||
} | ||
@@ -140,5 +148,2 @@ | ||
if (update.commands.hasOwnProperty(k)) { | ||
if (process.env.NODE_ENV !== 'production') { | ||
assert(Object.keys(spec).length === 1, 'Invalid argument spec supplied to `update()`'); | ||
} | ||
return update.commands[k](spec[k], value); | ||
@@ -158,3 +163,3 @@ } | ||
if (process.env.NODE_ENV !== 'production') { | ||
assert(isFunction(f), 'Invalid argument f supplied to $apply command'); | ||
assert(isFunction(f), 'Invalid argument f supplied to immutability helper {"$apply": f}: expected a function'); | ||
} | ||
@@ -167,4 +172,4 @@ | ||
if (process.env.NODE_ENV !== 'production') { | ||
assert(isArray(elements), 'Invalid argument elements supplied to $push command'); | ||
assert(isArray(arr), 'Invalid argument arr supplied to $push command'); | ||
assert(isArray(elements), 'Invalid argument elements supplied to immutability helper {"$push": elements}: expected an array'); | ||
assert(isArray(arr), 'Invalid value supplied to immutability helper "$push": expected an array'); | ||
} | ||
@@ -177,4 +182,4 @@ | ||
if (process.env.NODE_ENV !== 'production') { | ||
assert(isArray(keys), 'Invalid argument keys supplied to $remove command'); | ||
assert(isObject(obj), 'Invalid argument obj supplied to $remove command'); | ||
assert(isArray(keys), 'Invalid argument keys supplied to immutability helper {"$remove": keys}: expected an array'); | ||
assert(isObject(obj), 'Invalid value supplied to immutability helper $remove: expected an object'); | ||
} | ||
@@ -193,4 +198,4 @@ | ||
if (process.env.NODE_ENV !== 'production') { | ||
assert(list(Arr).is(splices), 'Invalid argument splices supplied to $splice command'); | ||
assert(isArray(arr), 'Invalid argument arr supplied to $splice command'); | ||
assert(list(Arr).is(splices), 'Invalid argument splices supplied to immutability helper {"$splice": splices}: expected an array of arrays'); | ||
assert(isArray(arr), 'Invalid value supplied to immutability helper $splice: expected an array'); | ||
} | ||
@@ -206,6 +211,6 @@ | ||
if (process.env.NODE_ENV !== 'production') { | ||
assert(isObject(config), 'Invalid argument config supplied to $swap command'); | ||
assert(isNumber(config.from), 'Invalid argument config.from supplied to $swap command'); | ||
assert(isNumber(config.to), 'Invalid argument config.to supplied to $swap command'); | ||
assert(isArray(arr), 'Invalid argument arr supplied to $swap command'); | ||
assert(isObject(config), 'Invalid argument config supplied to immutability helper {"$swap": config}: expected an object'); | ||
assert(isNumber(config.from), 'Invalid argument config.from supplied to immutability helper {"$swap": config}: expected a number'); | ||
assert(isNumber(config.to), 'Invalid argument config.to supplied to immutability helper {"$swap": config}: expected a number'); | ||
assert(isArray(arr), 'Invalid value supplied to immutability helper $swap'); | ||
} | ||
@@ -221,4 +226,4 @@ | ||
if (process.env.NODE_ENV !== 'production') { | ||
assert(isArray(elements), 'Invalid argument elements supplied to $unshift command'); | ||
assert(isArray(arr), 'Invalid argument arr supplied to $unshift command'); | ||
assert(isArray(elements), 'Invalid argument elements supplied to immutability helper {"$unshift": elements}'); | ||
assert(isArray(arr), 'Invalid value supplied to immutability helper $unshift'); | ||
} | ||
@@ -236,4 +241,4 @@ | ||
if (process.env.NODE_ENV !== 'production') { | ||
assert(isString(name), 'Invalid argument name supplied to irreducible combinator'); | ||
assert(isFunction(predicate), 'Invalid argument predicate supplied to irreducible combinator'); | ||
assert(isString(name), 'Invalid argument name = ' + stringify(name) + ' supplied to irreducible(name, predicate)'); | ||
assert(isFunction(predicate), 'Invalid argument predicate supplied to irreducible(name, predicate)'); | ||
} | ||
@@ -245,3 +250,3 @@ | ||
forbidNewOperator(this, Irreducible); | ||
assert(predicate(value), 'Invalid argument value supplied to irreducible ' + name); | ||
assert(predicate(value), 'Invalid argument value = ' + stringify(value) + ' supplied to irreducible type ' + name); | ||
} | ||
@@ -297,4 +302,4 @@ | ||
if (process.env.NODE_ENV !== 'production') { | ||
assert(dict(Str, Func).is(props), 'Invalid argument props supplied to struct combinator'); | ||
assert(isTypeName(name), 'Invalid argument name supplied to struct combinator'); | ||
assert(dict(Str, Func).is(props), 'Invalid argument props = ' + stringify(props) + ' supplied to struct(props, name): expected a dictionary of types'); | ||
assert(isTypeName(name), 'Invalid argument name = ' + stringify(props) + ' supplied to struct(props, name): expected a string'); | ||
} | ||
@@ -315,3 +320,3 @@ | ||
if (process.env.NODE_ENV !== 'production') { | ||
assert(isObject(value), 'Invalid argument value supplied to struct ' + displayName); | ||
assert(isObject(value), 'Invalid argument value = ' + stringify(value) + ' supplied to struct ' + displayName + ': expected an object'); | ||
} | ||
@@ -359,3 +364,3 @@ | ||
if (process.env.NODE_ENV !== 'production') { | ||
assert(isStruct(x), 'Invalid argument structs[' + i + '] supplied to ' + displayName + '.extend()'); | ||
assert(isStruct(x), 'Invalid argument structs[' + i + '] = ' + stringify(structs[i]) + ' supplied to ' + displayName + '.extend(structs, name)'); | ||
} | ||
@@ -376,6 +381,4 @@ return x.meta.props; | ||
if (process.env.NODE_ENV !== 'production') { | ||
assert(isArray(types), 'Invalid argument types supplied to union combinator: must be an array'); | ||
assert(types.every(isFunction), 'Invalid argument types supplied to union combinator: at least one element is not a type not a constructor'); | ||
assert(types.length >= 2, 'Invalid argument types supplied to union combinator: provide at least two types'); | ||
assert(isTypeName(name), 'Invalid argument name supplied to union combinator'); | ||
assert(isArray(types) && types.every(isFunction) && types.length >= 2, 'Invalid argument types = ' + stringify(types) + ' supplied to union(types, name): expected an array of at least 2 types'); | ||
assert(isTypeName(name), 'Invalid argument name = ' + stringify(name) + ' supplied to union(types, name): expected a string'); | ||
} | ||
@@ -387,3 +390,3 @@ | ||
function Union(value, mut) { | ||
function Union(value) { | ||
@@ -401,3 +404,3 @@ if (process.env.NODE_ENV !== 'production') { | ||
return create(type, value, mut); | ||
return create(type, value); | ||
} | ||
@@ -433,3 +436,3 @@ | ||
if (process.env.NODE_ENV !== 'production') { | ||
assert(isFunction(type), 'Invalid argument type supplied to maybe combinator'); | ||
assert(isFunction(type), 'Invalid argument type = ' + stringify(type) + ' supplied to maybe(type, name): expected a type'); | ||
} | ||
@@ -442,3 +445,3 @@ | ||
if (process.env.NODE_ENV !== 'production') { | ||
assert(isTypeName(name), 'Invalid argument name supplied to maybe combinator'); | ||
assert(isTypeName(name), 'Invalid argument name = ' + stringify(name) + ' supplied to maybe(type, name): expected a string'); | ||
} | ||
@@ -448,7 +451,7 @@ | ||
function Maybe(value, mut) { | ||
function Maybe(value) { | ||
if (process.env.NODE_ENV !== 'production') { | ||
forbidNewOperator(this, Maybe); | ||
} | ||
return isNil(value) ? null : create(type, value, mut); | ||
return isNil(value) ? null : create(type, value); | ||
} | ||
@@ -474,4 +477,4 @@ | ||
if (process.env.NODE_ENV !== 'production') { | ||
assert(isObject(map), 'Invalid argument map supplied to enums combinator'); | ||
assert(isTypeName(name), 'Invalid argument name supplied to enums combinator'); | ||
assert(isObject(map), 'Invalid argument map = ' + stringify(map) + ' supplied to enums(map, name): expected a hash of strings / numbers'); | ||
assert(isTypeName(name), 'Invalid argument name = ' + stringify(name) + ' supplied to enums(map, name): expected a string'); | ||
} | ||
@@ -486,3 +489,3 @@ | ||
forbidNewOperator(this, Enums); | ||
assert(Enums.is(value), 'Invalid argument value supplied to enums ' + displayName + ', expected one of ' + JSON.stringify(Object.keys(map))); | ||
assert(Enums.is(value), 'Invalid argument value = ' + stringify(value) + ' supplied to enums ' + displayName + ': expected one of ' + stringify(Object.keys(map))); | ||
} | ||
@@ -519,5 +522,4 @@ return value; | ||
if (process.env.NODE_ENV !== 'production') { | ||
assert(isArray(types), 'Invalid argument types supplied to tuple combinator: must be an array'); | ||
assert(types.every(isFunction), 'Invalid argument types supplied to tuple combinator: at least one element is not a type not a constructor'); | ||
assert(isTypeName(name), 'Invalid argument name supplied to tuple combinator'); | ||
assert(isArray(types) && types.every(isFunction), 'Invalid argument types = ' + stringify(types) + ' supplied to tuple(types, name): expected an array of types'); | ||
assert(isTypeName(name), 'Invalid argument name = ' + stringify(name) + ' supplied to tuple(types, name): expected a string'); | ||
} | ||
@@ -538,3 +540,3 @@ | ||
if (process.env.NODE_ENV !== 'production') { | ||
assert(isArray(value) && value.length === types.length, 'Invalid argument value supplied to tuple ' + displayName + ', expected an array of length ' + types.length); | ||
assert(isArray(value) && value.length === types.length, 'Invalid argument value = ' + stringify(value) + ' supplied to tuple ' + displayName + ': expected an array of length ' + types.length); | ||
} | ||
@@ -588,5 +590,5 @@ | ||
if (process.env.NODE_ENV !== 'production') { | ||
assert(isFunction(type), 'Invalid argument type subtype combinator'); | ||
assert(isFunction(predicate), 'Invalid argument predicate supplied to subtype combinator'); | ||
assert(isTypeName(name), 'Invalid argument name supplied to subtype combinator'); | ||
assert(isFunction(type), 'Invalid argument type = ' + stringify(type) + ' supplied to subtype(type, predicate, name): expected a type'); | ||
assert(isFunction(predicate), 'Invalid argument predicate supplied to subtype(type, predicate, name): expected a function'); | ||
assert(isTypeName(name), 'Invalid argument name = ' + stringify(name) + ' supplied to subtype(type, predicate, name): expected a string'); | ||
} | ||
@@ -598,3 +600,3 @@ | ||
function Subtype(value, mut) { | ||
function Subtype(value) { | ||
@@ -605,6 +607,6 @@ if (process.env.NODE_ENV !== 'production') { | ||
var x = create(type, value, mut); | ||
var x = create(type, value); | ||
if (process.env.NODE_ENV !== 'production') { | ||
assert(predicate(x), 'Invalid argument value supplied to subtype ' + displayName); | ||
assert(predicate(x), 'Invalid argument value = ' + stringify(value) + ' supplied to subtype ' + displayName); | ||
} | ||
@@ -638,4 +640,4 @@ | ||
if (process.env.NODE_ENV !== 'production') { | ||
assert(isFunction(type), 'Invalid argument type supplied to list combinator'); | ||
assert(isTypeName(name), 'Invalid argument name supplied to list combinator'); | ||
assert(isFunction(type), 'Invalid argument type = ' + stringify(type) + ' supplied to list(type, name): expected a type'); | ||
assert(isTypeName(name), 'Invalid argument name = ' + stringify(name) + ' supplied to list(type, name): expected a string'); | ||
} | ||
@@ -653,6 +655,6 @@ | ||
function List(value, mut) { | ||
function List(value) { | ||
if (process.env.NODE_ENV !== 'production') { | ||
assert(isArray(value), 'Invalid argument value supplied to list ' + displayName); | ||
assert(isArray(value), 'Invalid argument value = ' + stringify(value) + ' supplied to list ' + displayName); | ||
} | ||
@@ -669,3 +671,3 @@ | ||
var actual = value[i]; | ||
arr.push(create(type, actual, mut)); | ||
arr.push(create(type, actual)); | ||
} | ||
@@ -702,5 +704,5 @@ | ||
if (process.env.NODE_ENV !== 'production') { | ||
assert(isFunction(domain), 'Invalid argument domain supplied to dict combinator'); | ||
assert(isFunction(codomain), 'Invalid argument codomain supplied to dict combinator'); | ||
assert(isTypeName(name), 'Invalid argument name supplied to dict combinator'); | ||
assert(isFunction(domain), 'Invalid argument domain = ' + stringify(domain) + ' supplied to dict(domain, codomain, name): expected a type'); | ||
assert(isFunction(codomain), 'Invalid argument codomain = ' + stringify(codomain) + ' supplied to dict(domain, codomain, name): expected a type'); | ||
assert(isTypeName(name), 'Invalid argument name = ' + stringify(name) + ' supplied to dict(domain, codomain, name): expected a string'); | ||
} | ||
@@ -723,6 +725,6 @@ | ||
function Dict(value, mut) { | ||
function Dict(value) { | ||
if (process.env.NODE_ENV !== 'production') { | ||
assert(isObject(value), 'Invalid argument value supplied to dict ' + displayName); | ||
assert(isObject(value), 'Invalid argument value = ' + stringify(value) + ' supplied to dict ' + displayName); | ||
} | ||
@@ -742,3 +744,3 @@ | ||
var actual = value[k]; | ||
obj[k] = create(codomain, actual, mut); | ||
obj[k] = create(codomain, actual); | ||
} | ||
@@ -783,5 +785,5 @@ } | ||
if (process.env.NODE_ENV !== 'production') { | ||
assert(list(Func).is(domain), 'Invalid argument domain supplied to func combinator'); | ||
assert(isFunction(codomain), 'Invalid argument codomain supplied to func combinator'); | ||
assert(isTypeName(name), 'Invalid argument name supplied to func combinator'); | ||
assert(list(Func).is(domain), 'Invalid argument domain = ' + stringify(domain) + ' supplied to func(domain, codomain, name): expected an array of types'); | ||
assert(isFunction(codomain), 'Invalid argument codomain = ' + stringify(codomain) + ' supplied to func(domain, codomain, name): expected a type'); | ||
assert(isTypeName(name), 'Invalid argument name = ' + stringify(name) + ' supplied to func(domain, codomain, name): expected a string'); | ||
} | ||
@@ -800,3 +802,3 @@ | ||
if (process.env.NODE_ENV !== 'production') { | ||
assert(FuncType.is(value), 'Invalid argument value supplied to func ' + displayName); | ||
assert(FuncType.is(value), 'Invalid argument value = ' + stringify(value) + ' supplied to func ' + displayName); | ||
} | ||
@@ -828,4 +830,4 @@ | ||
if (process.env.NODE_ENV !== 'production') { | ||
assert(isFunction(f), 'Invalid argument f supplied to func.of ' + displayName); | ||
assert(isNil(curried) || isBoolean(curried), 'Invalid argument curried supplied to func.of ' + displayName); | ||
assert(isFunction(f), 'Invalid argument f supplied to func.of ' + displayName + ': expected a function'); | ||
assert(isNil(curried) || isBoolean(curried), 'Invalid argument curried = ' + stringify(curried) + ' supplied to func.of ' + displayName + ': expected a boolean'); | ||
} | ||
@@ -874,2 +876,3 @@ | ||
mixin(exports, { | ||
stringify: stringify, | ||
is: is, | ||
@@ -876,0 +879,0 @@ isType: isType, |
{ | ||
"name": "tcomb", | ||
"version": "2.0.0-beta", | ||
"version": "2.0.0", | ||
"description": "Type checking for JavaScript", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -8,9 +8,32 @@ [![build status](https://img.shields.io/travis/gcanti/tcomb/master.svg?style=flat-square)](https://travis-ci.org/gcanti/tcomb) | ||
- **immutability**: instances are immutables in development mode | ||
- **speed**: asserts are active only in development mode and stripped in production code. | ||
- **DDD**: write complex domain models in a breeze and with a small code footprint | ||
- **debugging**: you can customize the behaviour when an assert fails leveraging the power of Chrome DevTools | ||
- **runtime type introspection**: every model written with tcomb is inspectable at runtime | ||
- **JSON**: encodes/decodes domain models to/from JSON for free | ||
1. **immutability**: instances are immutables (using `Object.freeze`) in development | ||
2. **speed**: asserts are active only in development mode and stripped in production code. | ||
3. **DDD**: write complex domain models in a breeze and with a small code footprint | ||
4. **debugging**: you can customize the behaviour when an assert fails leveraging the power of Chrome DevTools | ||
5. **runtime type introspection**: every model written with tcomb is inspectable at runtime | ||
6. **JSON**: encodes/decodes domain models to/from JSON for free | ||
# Quick example | ||
```js | ||
import t from 'tcomb'; | ||
// define a custom integer type | ||
const Integer = t.subtype(t.Num, n => n % 1 === 0); | ||
const Person = t.struct({ | ||
name: t.Str, // required string | ||
surname: t.maybe(t.Str), // optional string | ||
age: Integer, // required integer | ||
tags: t.list(t.Str) // a list of strings | ||
}); | ||
const person = new Person({ | ||
name: 'Giulio', | ||
surname: 'Canti', | ||
age: 41, | ||
tags: ['developer', 'rock climber'] | ||
}); | ||
``` | ||
# Documentation | ||
@@ -17,0 +40,0 @@ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
42565
667
0
50