19 lines
793 B
JavaScript
19 lines
793 B
JavaScript
var isType = require('./isType');
|
|
var getFunctionName = require('./getFunctionName');
|
|
var assert = require('./assert');
|
|
var stringify = require('./stringify');
|
|
|
|
// creates an instance of a type, handling the optional new operator
|
|
module.exports = function create(type, value, path) {
|
|
if (isType(type)) {
|
|
return !type.meta.identity && typeof value === 'object' && value !== null ? new type(value, path): type(value, path);
|
|
}
|
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
// here type should be a class constructor and value some instance, just check membership and return the value
|
|
path = path || [getFunctionName(type)];
|
|
assert(value instanceof type, function () { return 'Invalid value ' + stringify(value) + ' supplied to ' + path.join('/'); });
|
|
}
|
|
|
|
return value;
|
|
}; |