forked from gcanti/tcomb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate.js
19 lines (16 loc) · 793 Bytes
/
create.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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;
};