all files / src/ type.js

83.33% Statements 10/12
75% Branches 6/8
100% Functions 2/2
100% Lines 9/9
1 2 3 4 5 6 7 8 9 10 11 12 13 14     82×   74× 74×    
const types = 'Boolean Number String Function Array Date RegExp Object Error'.split(' ')
const class2type = {}
types.forEach(name => {
  class2type['[object ' + name + ']'] = name.toLowerCase()
})
 
export default function type (obj) {
  if (obj === null) {
    return obj + ''
  }
  return typeof obj === 'object' || typeof obj === 'function'
    ? class2type[Object.prototype.toString.call(obj)] || 'object' : typeof obj
}