Comparing version 3.1.0 to 3.2.0
@@ -15,2 +15,10 @@ # Changelog | ||
# v3.2.0 | ||
- **New Feature** | ||
- `isSubsetOf` module, function for determining whether one type is compatible with another type (@R3D4C73D) | ||
- default props for structs (thanks @timoxley) | ||
- **Documentation** | ||
- global strict settings are deprecated (see https://github.com/gcanti/tcomb/issues/168#issuecomment-222422999) | ||
# v3.1.0 | ||
@@ -17,0 +25,0 @@ |
var isType = require('./isType'); | ||
var isStruct = require('./isStruct'); | ||
var getFunctionName = require('./getFunctionName'); | ||
@@ -11,3 +10,3 @@ var assert = require('./assert'); | ||
// for structs the new operator is allowed | ||
return isStruct(type) ? new type(value, path) : type(value, path); | ||
return type.meta.kind === 'struct' ? new type(value, path) : type(value, path); | ||
} | ||
@@ -14,0 +13,0 @@ |
@@ -8,28 +8,5 @@ var assert = require('./assert'); | ||
var isObject = require('./isObject'); | ||
var isType = require('./isType'); | ||
var refinement = require('./refinement'); | ||
var decompose = require('./decompose'); | ||
function isRefinement(type) { | ||
return isType(type) && type.meta.kind === 'subtype'; | ||
} | ||
function getPredicates(type) { | ||
return isRefinement(type) ? | ||
[type.meta.predicate].concat(getPredicates(type.meta.type)) : | ||
[]; | ||
} | ||
function getUnrefinedType(type) { | ||
return isRefinement(type) ? | ||
getUnrefinedType(type.meta.type) : | ||
type; | ||
} | ||
function decompose(type) { | ||
return { | ||
predicates: getPredicates(type), | ||
unrefinedType: getUnrefinedType(type) | ||
}; | ||
} | ||
function compose(predicates, unrefinedType) { | ||
@@ -45,2 +22,6 @@ return predicates.reduce(function (type, predicate) { | ||
function getDefaultProps(type) { | ||
return isObject(type) ? null : type.meta.defaultProps; | ||
} | ||
function pushAll(arr, elements) { | ||
@@ -50,6 +31,6 @@ Array.prototype.push.apply(arr, elements); | ||
function extend(combinator, mixins, name) { | ||
function extend(combinator, mixins, options) { | ||
if (process.env.NODE_ENV !== 'production') { | ||
assert(isFunction(combinator), function () { return 'Invalid argument combinator supplied to extend(combinator, mixins, name), expected a function'; }); | ||
assert(isArray(mixins), function () { return 'Invalid argument mixins supplied to extend(combinator, mixins, name), expected an array'; }); | ||
assert(isFunction(combinator), function () { return 'Invalid argument combinator supplied to extend(combinator, mixins, options), expected a function'; }); | ||
assert(isArray(mixins), function () { return 'Invalid argument mixins supplied to extend(combinator, mixins, options), expected an array'; }); | ||
} | ||
@@ -59,2 +40,3 @@ var props = {}; | ||
var predicates = []; | ||
var defaultProps = {}; | ||
mixins.forEach(function (x, i) { | ||
@@ -64,3 +46,3 @@ var decomposition = decompose(x); | ||
if (process.env.NODE_ENV !== 'production') { | ||
assert(isObject(unrefinedType) || isStruct(unrefinedType) || isInterface(unrefinedType), function () { return 'Invalid argument mixins[' + i + '] supplied to extend(combinator, mixins, name), expected an object, struct, interface or a refinement (of struct or interface)'; }); | ||
assert(isObject(unrefinedType) || isStruct(unrefinedType) || isInterface(unrefinedType), function () { return 'Invalid argument mixins[' + i + '] supplied to extend(combinator, mixins, options), expected an object, struct, interface or a refinement (of struct or interface)'; }); | ||
} | ||
@@ -70,4 +52,7 @@ pushAll(predicates, decomposition.predicates); | ||
mixin(prototype, unrefinedType.prototype); | ||
mixin(defaultProps, getDefaultProps(unrefinedType)); | ||
}); | ||
var result = compose(predicates, combinator(props, name)); | ||
options = combinator.getOptions(options); | ||
mixin(options.defaultProps, defaultProps); | ||
var result = compose(predicates, combinator(props, options)); | ||
mixin(result.prototype, prototype); | ||
@@ -74,0 +59,0 @@ return result; |
@@ -28,3 +28,3 @@ var isType = require('./isType'); | ||
body: function (x) { | ||
if (x.meta.kind === 'struct') { | ||
if (x.meta.kind === 'struct' || x.meta.kind === 'interface') { | ||
var props = Object.keys(x.meta.props).map(function (prop) { | ||
@@ -31,0 +31,0 @@ return ['li', listItemStyle, |
@@ -29,2 +29,5 @@ var assert = require('./assert'); | ||
} | ||
if (!options.hasOwnProperty('defaultProps')) { | ||
options.defaultProps = {}; | ||
} | ||
return options; | ||
@@ -38,2 +41,3 @@ } | ||
var strict = options.strict; | ||
var defaultProps = options.defaultProps; | ||
@@ -44,2 +48,3 @@ if (process.env.NODE_ENV !== 'production') { | ||
assert(isBoolean(strict), function () { return 'Invalid argument strict ' + assert.stringify(strict) + ' supplied to struct(props, [options]) combinator (expected a boolean)'; }); | ||
assert(isObject(defaultProps), function () { return 'Invalid argument defaultProps ' + assert.stringify(defaultProps) + ' supplied to struct(props, [options]) combinator (expected an object)'; }); | ||
} | ||
@@ -76,2 +81,6 @@ | ||
var actual = value[k]; | ||
// apply defaults | ||
if (actual === undefined) { | ||
actual = defaultProps[k]; | ||
} | ||
this[k] = create(expected, actual, ( process.env.NODE_ENV !== 'production' ? path.concat(k + ': ' + getTypeName(expected)) : null )); | ||
@@ -92,3 +101,4 @@ } | ||
identity: false, | ||
strict: strict | ||
strict: strict, | ||
defaultProps: defaultProps | ||
}; | ||
@@ -95,0 +105,0 @@ |
{ | ||
"name": "tcomb", | ||
"version": "3.1.0", | ||
"version": "3.2.0", | ||
"description": "Type checking and DDD for JavaScript", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -30,2 +30,7 @@ [![build status](https://img.shields.io/travis/gcanti/tcomb/master.svg?style=flat-square)](https://travis-ci.org/gcanti/tcomb) | ||
sum(1, 's'); // throws '[tcomb] Invalid value "s" supplied to Number' | ||
// using babel-plugin-tcomb | ||
function sum(a: t.Number, b: t.Number) { | ||
return a + b; | ||
} | ||
``` | ||
@@ -59,2 +64,6 @@ | ||
Chrome DevTools: | ||
![throws](docs/images/throws.png) | ||
# Docs | ||
@@ -61,0 +70,0 @@ |
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
86618
65
1899
237
70