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