diff --git a/src/types.js b/src/types.js index ddbf13d..5f885d0 100644 --- a/src/types.js +++ b/src/types.js @@ -26,9 +26,15 @@ module.exports = function enforce(type, value) { } default: { - if (value instanceof type) return + if (getName(value.constructor) === getName(type)) return } } - throw new TypeError('Expected ' + (type.name || type) + ', got ' + value) + throw new TypeError('Expected ' + (getName(type) || type) + ', got ' + value) +} + +function getName(fn) { + // Why not fn.name: https://kangax.github.io/compat-table/es6/#function_name_property + var match = fn.toString().match(/function (.*?)\(/) + return match ? match[1] : null } diff --git a/test/types.js b/test/types.js index 4b6c123..fe1d149 100644 --- a/test/types.js +++ b/test/types.js @@ -1,7 +1,7 @@ var assert = require('assert') var enforceType = require('../src/types') -function CustomType() {} +function CustomType() { return "ensure non-greedy match".toUpperCase() } var types = ['Array', 'Boolean', 'Buffer', 'Number', 'String', CustomType] var values = [[], true, new Buffer(1), 1234, 'foobar', new CustomType()]