Created
January 19, 2017 18:30
-
-
Save olange/514325f6fe3d89610d224d731a2def9e to your computer and use it in GitHub Desktop.
Yet another type detective – CoffeeScript version of the type `component/type` NPM package
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# A less-broken `typeof` function. CoffeeScript version of the | |
# `component/type` NPM package [1]. | |
# | |
# See also: | |
# | |
# [1] https://github.com/component/type/blob/master/index.js | |
# and https://gist.github.com/olange/2aa1abe1001c92bde78be1c84d7f1261 | |
exp.type = (val) -> | |
toString = Object.prototype.toString | |
isBuffer = (obj) -> | |
# // code borrowed from https://github.com/feross/is-buffer/blob/master/index.js | |
return !!( obj? and \ | |
(obj._isBuffer \ # For Safari 5-7 (missing Object.prototype.constructor) | |
or( obj.constructor \ | |
and typeof obj.constructor.isBuffer is "function" \ | |
and obj.constructor.isBuffer( obj)))) | |
switch toString.call(val) | |
when "[object Date]" then return "date" | |
when "[object RegExp]" then return "regexp" | |
when "[object Arguments]" then return "arguments" | |
when "[object Array]" then return "array" | |
when "[object Error]" then return "error" | |
return "null" if val is null | |
return "undefined" if val is undefined | |
return "nan" if val isnt val | |
return "element" if val? and val.nodeType is 1 | |
return "buffer" if isBuffer( val) | |
val = if val.valueOf? then val.valueOf() \ | |
else Object.prototype.valueOf.apply(val) | |
return typeof val |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment