web 3d图形渲染器
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

73 lines
3.0 KiB

  1. # which-boxed-primitive <sup>[![Version Badge][2]][1]</sup>
  2. [![dependency status][5]][6]
  3. [![dev dependency status][7]][8]
  4. [![License][license-image]][license-url]
  5. [![Downloads][downloads-image]][downloads-url]
  6. [![npm badge][11]][1]
  7. Which kind of boxed JS primitive is this? This module works cross-realm/iframe, does not depend on `instanceof` or mutable properties, and works despite ES6 Symbol.toStringTag.
  8. ## Example
  9. ```js
  10. var whichBoxedPrimitive = require('which-boxed-primitive');
  11. var assert = require('assert');
  12. // unboxed primitives return `null`
  13. // boxed primitives return the builtin constructor name
  14. assert.equal(whichBoxedPrimitive(undefined), null);
  15. assert.equal(whichBoxedPrimitive(null), null);
  16. assert.equal(whichBoxedPrimitive(false), null);
  17. assert.equal(whichBoxedPrimitive(true), null);
  18. assert.equal(whichBoxedPrimitive(new Boolean(false)), 'Boolean');
  19. assert.equal(whichBoxedPrimitive(new Boolean(true)), 'Boolean');
  20. assert.equal(whichBoxedPrimitive(42), null);
  21. assert.equal(whichBoxedPrimitive(NaN), null);
  22. assert.equal(whichBoxedPrimitive(Infinity), null);
  23. assert.equal(whichBoxedPrimitive(new Number(42)), 'Number');
  24. assert.equal(whichBoxedPrimitive(new Number(NaN)), 'Number');
  25. assert.equal(whichBoxedPrimitive(new Number(Infinity)), 'Number');
  26. assert.equal(whichBoxedPrimitive(''), null);
  27. assert.equal(whichBoxedPrimitive('foo'), null);
  28. assert.equal(whichBoxedPrimitive(new String('')), 'String');
  29. assert.equal(whichBoxedPrimitive(new String('foo')), 'String');
  30. assert.equal(whichBoxedPrimitive(Symbol()), null);
  31. assert.equal(whichBoxedPrimitive(Object(Symbol()), 'Symbol');
  32. assert.equal(whichBoxedPrimitive(42n), null);
  33. assert.equal(whichBoxedPrimitive(Object(42n), 'BigInt');
  34. // non-boxed-primitive objects return `undefined`
  35. assert.equal(whichBoxedPrimitive([]), undefined);
  36. assert.equal(whichBoxedPrimitive({}), undefined);
  37. assert.equal(whichBoxedPrimitive(/a/g), undefined);
  38. assert.equal(whichBoxedPrimitive(new RegExp('a', 'g')), undefined);
  39. assert.equal(whichBoxedPrimitive(new Date()), undefined);
  40. assert.equal(whichBoxedPrimitive(function () {}), undefined);
  41. assert.equal(whichBoxedPrimitive(function* () {}), undefined);
  42. assert.equal(whichBoxedPrimitive(x => x * x), undefined);
  43. assert.equal(whichBoxedPrimitive([]), undefined);
  44. ```
  45. ## Tests
  46. Simply clone the repo, `npm install`, and run `npm test`
  47. [1]: https://npmjs.org/package/which-boxed-primitive
  48. [2]: https://versionbadg.es/inspect-js/which-boxed-primitive.svg
  49. [5]: https://david-dm.org/inspect-js/which-boxed-primitive.svg
  50. [6]: https://david-dm.org/inspect-js/which-boxed-primitive
  51. [7]: https://david-dm.org/inspect-js/which-boxed-primitive/dev-status.svg
  52. [8]: https://david-dm.org/inspect-js/which-boxed-primitive#info=devDependencies
  53. [11]: https://nodei.co/npm/which-boxed-primitive.png?downloads=true&stars=true
  54. [license-image]: https://img.shields.io/npm/l/which-boxed-primitive.svg
  55. [license-url]: LICENSE
  56. [downloads-image]: https://img.shields.io/npm/dm/which-boxed-primitive.svg
  57. [downloads-url]: https://npm-stat.com/charts.html?package=which-boxed-primitive