7752261fd487d362a006576916f64aae22c6c6a1521718995d2fd94afee5d3190415467801ba9f5c0234b0f1430549b6fe1b7069a6019d363154fb4e733010 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. # which-collection <sup>[![Version Badge][2]][1]</sup>
  2. [![github actions][actions-image]][actions-url]
  3. [![coverage][codecov-image]][codecov-url]
  4. [![dependency status][5]][6]
  5. [![dev dependency status][7]][8]
  6. [![License][license-image]][license-url]
  7. [![Downloads][downloads-image]][downloads-url]
  8. [![npm badge][11]][1]
  9. Which kind of Collection (Map, Set, WeakMap, WeakSet) is this JavaScript value? Works cross-realm, without `instanceof`, and despite Symbol.toStringTag.
  10. ## Example
  11. ```js
  12. var whichCollection = require('which-collection');
  13. var assert = require('assert');
  14. assert.equal(false, whichCollection(undefined));
  15. assert.equal(false, whichCollection(null));
  16. assert.equal(false, whichCollection(false));
  17. assert.equal(false, whichCollection(true));
  18. assert.equal(false, whichCollection([]));
  19. assert.equal(false, whichCollection({}));
  20. assert.equal(false, whichCollection(/a/g));
  21. assert.equal(false, whichCollection(new RegExp('a', 'g')));
  22. assert.equal(false, whichCollection(new Date()));
  23. assert.equal(false, whichCollection(42));
  24. assert.equal(false, whichCollection(NaN));
  25. assert.equal(false, whichCollection(Infinity));
  26. assert.equal(false, whichCollection(new Number(42)));
  27. assert.equal(false, whichCollection(42n));
  28. assert.equal(false, whichCollection(Object(42n)));
  29. assert.equal(false, whichCollection('foo'));
  30. assert.equal(false, whichCollection(Object('foo')));
  31. assert.equal(false, whichCollection(function () {}));
  32. assert.equal(false, whichCollection(function* () {}));
  33. assert.equal(false, whichCollection(x => x * x));
  34. assert.equal(false, whichCollection([]));
  35. assert.equal('Map', whichCollection(new Map()));
  36. assert.equal('Set', whichCollection(new Set()));
  37. assert.equal('WeakMap', whichCollection(new WeakMap()));
  38. assert.equal('WeakSet', whichCollection(new WeakSet()));
  39. ```
  40. ## Tests
  41. Simply clone the repo, `npm install`, and run `npm test`
  42. [1]: https://npmjs.org/package/which-collection
  43. [2]: https://versionbadg.es/inspect-js/which-collection.svg
  44. [5]: https://david-dm.org/inspect-js/which-collection.svg
  45. [6]: https://david-dm.org/inspect-js/which-collection
  46. [7]: https://david-dm.org/inspect-js/which-collection/dev-status.svg
  47. [8]: https://david-dm.org/inspect-js/which-collection#info=devDependencies
  48. [11]: https://nodei.co/npm/which-collection.png?downloads=true&stars=true
  49. [license-image]: https://img.shields.io/npm/l/which-collection.svg
  50. [license-url]: LICENSE
  51. [downloads-image]: https://img.shields.io/npm/dm/which-collection.svg
  52. [downloads-url]: https://npm-stat.com/charts.html?package=which-collection
  53. [codecov-image]: https://codecov.io/gh/inspect-js/which-collection/branch/main/graphs/badge.svg
  54. [codecov-url]: https://app.codecov.io/gh/inspect-js/which-collection/
  55. [actions-image]: https://img.shields.io/endpoint?url=https://github-actions-badge-u3jn4tfpocch.runkit.sh/inspect-js/which-collection
  56. [actions-url]: https://github.com/inspect-js/which-collection/actions