项目作者: alexindigo

项目描述 :
Detects precise type of objects like `Array()`, `new Number(1)`, `new Boolean(true)`, etc
高级语言: JavaScript
项目地址: git://github.com/alexindigo/precise-typeof.git
创建时间: 2016-01-18T07:55:10Z
项目社区:https://github.com/alexindigo/precise-typeof

开源协议:MIT License

下载


precise-typeof NPM Module

Better typeof. Detects real type of the objects like Array(), new Number(1), new Boolean(true), etc.

PhantomJS Build
Linux Build
MacOS Build
Windows Build

Coverage Status
Dependency Status
MIT license

compression size
precise-typeof.js 1.69 kB
precise-typeof.min.js 809 B
precise-typeof.min.js.gz 391 B

Install

  1. $ yarn add precise-typeof

or

  1. $ npm install --save precise-typeof

Examples

  1. var typeOf = require('precise-typeof');
  2. typeOf({}); // -> 'object'
  3. typeOf(new function(){}); // -> 'object'
  4. typeOf([]); // -> 'array'
  5. typeOf(25); // -> 'number'
  6. typeOf(Infinity); // -> 'number'
  7. typeOf('ABC'); // -> 'string'
  8. typeOf(function(){}); // -> 'function'
  9. typeOf(Math.sin); // -> 'function'
  10. typeOf(undefined); // -> 'undefined'
  11. typeOf(true); // -> 'boolean'
  12. typeOf(null); // -> 'null'
  13. typeOf(NaN); // -> 'nan'
  14. // object values
  15. typeOf(new Object()); // -> 'object'
  16. typeOf(new Array()); // -> 'array'
  17. typeOf(new Number(5)); // -> 'number'
  18. typeOf(new Number(Infinity)); // -> 'number'
  19. typeOf(new String('ABC')); // -> 'string'
  20. typeOf(new Function('a', 'b', 'return a + b')); // -> 'function'
  21. typeOf(new Boolean()); // -> 'boolean'
  22. typeOf(new Number('blabla')); // -> 'nan'
  23. // instances
  24. typeOf(new function Moment(){}); // -> 'object'
  25. // special objects
  26. typeOf(/s/); // -> 'regexp'
  27. typeOf(new Date()); // -> 'date'
  28. typeOf(Math); // -> 'math'
  29. typeOf(new Error()); // -> 'error'
  30. typeOf(arguments); // -> 'arguments'
  31. // node
  32. typeOf(global); // -> 'global'
  33. typeOf(process); // -> 'process'
  34. typeOf(Buffer('B')); // -> 'buffer'
  35. typeOf(new Buffer(2)); // -> 'buffer'
  36. typeOf(Buffer([62, 64, 66])); // -> 'buffer'
  37. // es6
  38. typeOf(Symbol('A')); // -> 'symbol'
  39. // browser
  40. typeOf(window); // -> 'global'
  41. typeOf(document); // -> 'html'
  42. typeOf(document.body); // -> 'html'
  43. typeOf(document.getElementsByTagName('html')[0]); // -> 'html'
  44. typeOf(document.getElementsByTagName('div')); // -> 'html'
  45. typeOf(document.createElement('a')); // -> 'html'
  46. typeOf(document.createTextNode('Abcd')); // -> 'text'
  47. typeOf(document.createComment('abcd')); // -> 'comment'
  48. typeOf(document.createEvent('Event')); // -> 'event'
  49. typeOf(document.createEvent('UIEvents')); // -> 'event'
  50. typeOf(document.createEvent('HTMLEvents')); // -> 'event'
  51. typeOf(document.createEvent('MouseEvents')); // -> 'event'

{ pojoOnly: true }

With pojoOnly flag it only reports Plain-Old Javascript Objects as object,
and reports “instances” by their constructor names (e.g. Moment for moment instance).
In case if object was created from the nameless function, it will be reported as unknown.

  1. var typeOf = require('precise-typeof');
  2. // reported differently with `{pojoOnly: true}`
  3. typeOf(new function Moment(){}, {pojoOnly: true}); // -> 'Moment'
  4. typeOf(new function ABC(){}, {pojoOnly: true}); // -> 'ABC'
  5. typeOf(new function(){}, {pojoOnly: true}); // -> 'unknown'
  6. // same with or without `{pojoOnly: true}`
  7. typeOf({}, {pojoOnly: true}); // -> 'object'
  8. typeOf([], {pojoOnly: true}); // -> 'array'
  9. typeOf(25, {pojoOnly: true}); // -> 'number'
  10. typeOf(Infinity, {pojoOnly: true}); // -> 'number'
  11. typeOf('ABC', {pojoOnly: true}); // -> 'string'
  12. typeOf(function(){}, {pojoOnly: true}); // -> 'function'
  13. typeOf(Math.sin, {pojoOnly: true}); // -> 'function'
  14. typeOf(undefined, {pojoOnly: true}); // -> 'undefined'
  15. typeOf(true, {pojoOnly: true}); // -> 'boolean'
  16. typeOf(null, {pojoOnly: true}); // -> 'null'
  17. typeOf(NaN, {pojoOnly: true}); // -> 'nan'
  18. typeOf(new Object(), {pojoOnly: true}); // -> 'object'
  19. typeOf(new Array(), {pojoOnly: true}); // -> 'array'
  20. typeOf(new Number(5), {pojoOnly: true}); // -> 'number'
  21. typeOf(new Number(Infinity), {pojoOnly: true}); // -> 'number'
  22. typeOf(new String('ABC'), {pojoOnly: true}); // -> 'string'
  23. typeOf(new Function(), {pojoOnly: true}); // -> 'function'
  24. typeOf(new Boolean(), {pojoOnly: true}); // -> 'boolean'
  25. typeOf(new Number('blabla'), {pojoOnly: true}); // -> 'nan'

License

Precise-TypeOf is released under the MIT license.