Skip to content

Commit 7e897a5

Browse files
committed
Merge pull request #289 from weilu/loose-instanceof
loose instanceof: check constructor function name instead
2 parents 1063087 + b55b10c commit 7e897a5

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

src/types.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,15 @@ module.exports = function enforce(type, value) {
2626
}
2727

2828
default: {
29-
if (value instanceof type) return
29+
if (getName(value.constructor) === getName(type)) return
3030
}
3131
}
3232

33-
throw new TypeError('Expected ' + (type.name || type) + ', got ' + value)
33+
throw new TypeError('Expected ' + (getName(type) || type) + ', got ' + value)
34+
}
35+
36+
function getName(fn) {
37+
// Why not fn.name: https://kangax.github.io/compat-table/es6/#function_name_property
38+
var match = fn.toString().match(/function (.*?)\(/)
39+
return match ? match[1] : null
3440
}

test/types.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
var assert = require('assert')
22
var enforceType = require('../src/types')
33

4-
function CustomType() {}
4+
function CustomType() { return "ensure non-greedy match".toUpperCase() }
55

66
var types = ['Array', 'Boolean', 'Buffer', 'Number', 'String', CustomType]
77
var values = [[], true, new Buffer(1), 1234, 'foobar', new CustomType()]

0 commit comments

Comments
 (0)