0853708949424d0e80bdffb25f94646aa271e83581c0195691f6f72d3b7db55542890270ed40109d9dcecc347e5e5926ffa16e463ac365cc175a0b6f8dc293 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. # which-builtin-type <sup>[![Version Badge][npm-version-svg]][package-url]</sup>
  2. [![github actions][actions-image]][actions-url]
  3. [![coverage][codecov-image]][codecov-url]
  4. [![License][license-image]][license-url]
  5. [![Downloads][downloads-image]][downloads-url]
  6. [![npm badge][npm-badge-png]][package-url]
  7. What is the type of this builtin JavaScript value? Works cross-realm, without `instanceof`, and can not be fooled by a `constructor` property.
  8. ## Example
  9. ```js
  10. var whichBuiltinType = require('which-builtin-type');
  11. var assert = require('assert');
  12. assert.equal(undefined, whichBuiltinType(undefined));
  13. assert.equal(null, whichBuiltinType(null));
  14. assert.equal('Boolean', whichBuiltinType(false));
  15. assert.equal('Boolean', whichBuiltinType(true));
  16. assert.equal('Array', whichBuiltinType([]));
  17. assert.equal('Object', whichBuiltinType({}));
  18. assert.equal('RegExp', whichBuiltinType(/a/g));
  19. assert.equal('RegExp', whichBuiltinType(new RegExp('a', 'g')));
  20. assert.equal('Date', whichBuiltinType(new Date()));
  21. assert.equal('Number', whichBuiltinType(42));
  22. assert.equal('Number', whichBuiltinType(NaN));
  23. assert.equal('Number', whichBuiltinType(Infinity));
  24. assert.equal('Number', whichBuiltinType(new Number(42)));
  25. assert.equal('String', whichBuiltinType('foo'));
  26. assert.equal('String', whichBuiltinType(Object('foo')));
  27. assert.equal('Function', whichBuiltinType(function () {}));
  28. assert.equal('GeneratorFunction', whichBuiltinType(function* () {}));
  29. assert.equal('Function', whichBuiltinType(x => x * x));
  30. assert.equal('Array', whichBuiltinType([]));
  31. assert.equal('Int8Array', whichBuiltinType(new Int8Array()));
  32. assert.equal('Uint8Array', whichBuiltinType(new Uint8Array()));
  33. assert.equal('Uint8ClampedArray', whichBuiltinType(new Uint8ClampedArray()));
  34. assert.equal('Int16Array', whichBuiltinType(new Int16Array()));
  35. assert.equal('Uint16Array', whichBuiltinType(new Uint16Array()));
  36. assert.equal('Int32Array', whichBuiltinType(new Int32Array()));
  37. assert.equal('Uint32Array', whichBuiltinType(new Uint32Array()));
  38. assert.equal('Float32Array', whichBuiltinType(new Float32Array()));
  39. assert.equal('Float64Array', whichBuiltinType(new Float64Array()));
  40. assert.equal('BigInt64Array', whichBuiltinType(new BigInt64Array()));
  41. assert.equal('BigUint64Array', whichBuiltinType(new BigUint64Array()));
  42. ```
  43. ## Tests
  44. Simply clone the repo, `npm install`, and run `npm test`
  45. [package-url]: https://npmjs.org/package/which-builtin-type
  46. [npm-version-svg]: https://versionbadg.es/inspect-js/which-builtin-type.svg
  47. [deps-svg]: https://david-dm.org/inspect-js/which-builtin-type.svg
  48. [deps-url]: https://david-dm.org/inspect-js/which-builtin-type
  49. [dev-deps-svg]: https://david-dm.org/inspect-js/which-builtin-type/dev-status.svg
  50. [dev-deps-url]: https://david-dm.org/inspect-js/which-builtin-type#info=devDependencies
  51. [npm-badge-png]: https://which-builtin-type/which-builtin-type.png?downloads=true&stars=true
  52. [license-image]: https://img.shields.io/npm/l/which-builtin-type.svg
  53. [license-url]: LICENSE
  54. [downloads-image]: https://img.shields.io/npm/dm/which-builtin-type.svg
  55. [downloads-url]: https://npm-stat.com/charts.html?package=which-builtin-type
  56. [codecov-image]: https://codecov.io/gh/inspect-js/which-builtin-type/branch/main/graphs/badge.svg
  57. [codecov-url]: https://app.codecov.io/gh/inspect-js/which-builtin-type/
  58. [actions-image]: https://img.shields.io/endpoint?url=https://github-actions-badge-u3jn4tfpocch.runkit.sh/inspect-js/which-builtin-type
  59. [actions-url]: https://github.com/inspect-js/which-builtin-type/actions