Comparing version 2.3.0 to 2.4.0
@@ -15,2 +15,10 @@ # Changelog | ||
## v2.4.0 | ||
- **New Feature** | ||
- unions | ||
- added `update` function, #127 | ||
- the default `dispatch` implementation now handles unions of unions, #126 | ||
- show the offended union type in error messages | ||
## v2.3.0 | ||
@@ -17,0 +25,0 @@ |
@@ -639,3 +639,3 @@ # Setup | ||
### Removing a value form a dict | ||
### Removing a value from a dict | ||
@@ -758,2 +758,2 @@ ```js | ||
t.stringify = String; | ||
``` | ||
``` |
32
index.js
@@ -0,1 +1,9 @@ | ||
/*! @preserve | ||
* | ||
* The MIT License (MIT) | ||
* | ||
* Copyright (c) 2014 Giulio Canti | ||
* | ||
*/ | ||
'use strict'; | ||
@@ -56,2 +64,6 @@ | ||
function isUnion(x) { | ||
return isType(x) && (x.meta.kind === 'union'); | ||
} | ||
function isTypeName(name) { | ||
@@ -396,2 +408,3 @@ return isNil(name) || isString(name); | ||
assert(isType(type), function () { return 'Invalid value ' + exports.stringify(value) + ' supplied to ' + path.join('/'); }); | ||
path[path.length - 1] += '(' + getTypeName(type) + ')'; | ||
} | ||
@@ -418,8 +431,19 @@ | ||
for (var i = 0, len = types.length; i < len; i++ ) { | ||
if (is(x, types[i])) { | ||
return types[i]; | ||
var type = types[i]; | ||
if (isUnion(type)) { | ||
var t = type.dispatch(x); | ||
if (!isNil(t)) { | ||
return t; | ||
} | ||
} | ||
else if (is(x, type)) { | ||
return type; | ||
} | ||
} | ||
}; | ||
Union.update = function (instance, spec) { | ||
return Union(exports.update(instance, spec)); | ||
}; | ||
return Union; | ||
@@ -704,4 +728,4 @@ } | ||
if (process.env.NODE_ENV !== 'production') { | ||
assert(isArray(value), function () { return 'Invalid value ' + exports.stringify(value) + ' supplied to ' + displayName + ' (expected an array of ' + typeNameCache + ')'; }); | ||
path = path || [displayName]; | ||
assert(isArray(value), function () { return 'Invalid value ' + exports.stringify(value) + ' supplied to ' + path.join('/') + ' (expected an array of ' + typeNameCache + ')'; }); | ||
} | ||
@@ -778,4 +802,4 @@ | ||
if (process.env.NODE_ENV !== 'production') { | ||
assert(isObject(value), function () { return 'Invalid value ' + exports.stringify(value) + ' supplied to ' + displayName; }); | ||
path = path || [displayName]; | ||
assert(isObject(value), function () { return 'Invalid value ' + exports.stringify(value) + ' supplied to ' + path.join('/'); }); | ||
} | ||
@@ -782,0 +806,0 @@ |
{ | ||
"name": "tcomb", | ||
"version": "2.3.0", | ||
"version": "2.4.0", | ||
"description": "Type checking and DDD for JavaScript", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -16,3 +16,3 @@ [![build status](https://img.shields.io/travis/gcanti/tcomb/master.svg?style=flat-square)](https://travis-ci.org/gcanti/tcomb) | ||
// a user defined type | ||
var Integer = t.subtype(t.Number, function (n) => { return n % 1 === 0; }); | ||
var Integer = t.subtype(t.Number, function (n) => { return n % 1 === 0; }, 'Integer'); // <= give a name for better debug messages | ||
@@ -25,3 +25,3 @@ // a struct | ||
tags: t.list(t.String) // a list of strings | ||
}); | ||
}, 'Person'); // <= give a name for better debug messages | ||
@@ -86,3 +86,3 @@ // methods are defined as usual | ||
```js | ||
[tcomb] Invalid argument value = undefined supplied to irreducible type Number | ||
[tcomb] Invalid value undefined supplied to Person/age: Number | ||
``` | ||
@@ -89,0 +89,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
56721
793