UNPKG

testem

Version:

Test'em 'scripts! Javascript Unit testing made easy.

35 lines (25 loc) 784 B
/* isa.js ====== This is an `isa()` function that does simple and elegant type checking. Examples: isa([], Array) // true isa(function(){}, Function) // true isa("abc", String) // true */ 'use strict'; const toString = Object.prototype.toString; function typeName(ctr) { return ctr.name || String(ctr).match(/function (.{1,})\(/)[1]; } function objTypeName(obj) { return toString.call(obj).match(/^\[object (.*)\]$/)[1]; } module.exports = function isa(obj, type) { if (obj === null || obj === undefined) { return false; } return obj instanceof type || // the straight-forward case obj.constructor === type || // .constructor check to catch the primitives case objTypeName(obj) === typeName(type); // name-based check for the cross window case };