[d565449] | 1 | # which-boxed-primitive <sup>[![Version Badge][2]][1]</sup>
|
---|
| 2 |
|
---|
| 3 | [![dependency status][5]][6]
|
---|
| 4 | [![dev dependency status][7]][8]
|
---|
| 5 | [![License][license-image]][license-url]
|
---|
| 6 | [![Downloads][downloads-image]][downloads-url]
|
---|
| 7 |
|
---|
| 8 | [![npm badge][11]][1]
|
---|
| 9 |
|
---|
| 10 | 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.
|
---|
| 11 |
|
---|
| 12 | ## Example
|
---|
| 13 |
|
---|
| 14 | ```js
|
---|
| 15 | var whichBoxedPrimitive = require('which-boxed-primitive');
|
---|
| 16 | var assert = require('assert');
|
---|
| 17 |
|
---|
| 18 | // unboxed primitives return `null`
|
---|
| 19 | // boxed primitives return the builtin constructor name
|
---|
| 20 |
|
---|
| 21 | assert.equal(whichBoxedPrimitive(undefined), null);
|
---|
| 22 | assert.equal(whichBoxedPrimitive(null), null);
|
---|
| 23 |
|
---|
| 24 | assert.equal(whichBoxedPrimitive(false), null);
|
---|
| 25 | assert.equal(whichBoxedPrimitive(true), null);
|
---|
| 26 | assert.equal(whichBoxedPrimitive(new Boolean(false)), 'Boolean');
|
---|
| 27 | assert.equal(whichBoxedPrimitive(new Boolean(true)), 'Boolean');
|
---|
| 28 |
|
---|
| 29 | assert.equal(whichBoxedPrimitive(42), null);
|
---|
| 30 | assert.equal(whichBoxedPrimitive(NaN), null);
|
---|
| 31 | assert.equal(whichBoxedPrimitive(Infinity), null);
|
---|
| 32 | assert.equal(whichBoxedPrimitive(new Number(42)), 'Number');
|
---|
| 33 | assert.equal(whichBoxedPrimitive(new Number(NaN)), 'Number');
|
---|
| 34 | assert.equal(whichBoxedPrimitive(new Number(Infinity)), 'Number');
|
---|
| 35 |
|
---|
| 36 | assert.equal(whichBoxedPrimitive(''), null);
|
---|
| 37 | assert.equal(whichBoxedPrimitive('foo'), null);
|
---|
| 38 | assert.equal(whichBoxedPrimitive(new String('')), 'String');
|
---|
| 39 | assert.equal(whichBoxedPrimitive(new String('foo')), 'String');
|
---|
| 40 |
|
---|
| 41 | assert.equal(whichBoxedPrimitive(Symbol()), null);
|
---|
| 42 | assert.equal(whichBoxedPrimitive(Object(Symbol()), 'Symbol');
|
---|
| 43 |
|
---|
| 44 | assert.equal(whichBoxedPrimitive(42n), null);
|
---|
| 45 | assert.equal(whichBoxedPrimitive(Object(42n), 'BigInt');
|
---|
| 46 |
|
---|
| 47 | // non-boxed-primitive objects return `undefined`
|
---|
| 48 | assert.equal(whichBoxedPrimitive([]), undefined);
|
---|
| 49 | assert.equal(whichBoxedPrimitive({}), undefined);
|
---|
| 50 | assert.equal(whichBoxedPrimitive(/a/g), undefined);
|
---|
| 51 | assert.equal(whichBoxedPrimitive(new RegExp('a', 'g')), undefined);
|
---|
| 52 | assert.equal(whichBoxedPrimitive(new Date()), undefined);
|
---|
| 53 | assert.equal(whichBoxedPrimitive(function () {}), undefined);
|
---|
| 54 | assert.equal(whichBoxedPrimitive(function* () {}), undefined);
|
---|
| 55 | assert.equal(whichBoxedPrimitive(x => x * x), undefined);
|
---|
| 56 | assert.equal(whichBoxedPrimitive([]), undefined);
|
---|
| 57 |
|
---|
| 58 | ```
|
---|
| 59 |
|
---|
| 60 | ## Tests
|
---|
| 61 | Simply clone the repo, `npm install`, and run `npm test`
|
---|
| 62 |
|
---|
| 63 | [1]: https://npmjs.org/package/which-boxed-primitive
|
---|
| 64 | [2]: https://versionbadg.es/inspect-js/which-boxed-primitive.svg
|
---|
| 65 | [5]: https://david-dm.org/inspect-js/which-boxed-primitive.svg
|
---|
| 66 | [6]: https://david-dm.org/inspect-js/which-boxed-primitive
|
---|
| 67 | [7]: https://david-dm.org/inspect-js/which-boxed-primitive/dev-status.svg
|
---|
| 68 | [8]: https://david-dm.org/inspect-js/which-boxed-primitive#info=devDependencies
|
---|
| 69 | [11]: https://nodei.co/npm/which-boxed-primitive.png?downloads=true&stars=true
|
---|
| 70 | [license-image]: https://img.shields.io/npm/l/which-boxed-primitive.svg
|
---|
| 71 | [license-url]: LICENSE
|
---|
| 72 | [downloads-image]: https://img.shields.io/npm/dm/which-boxed-primitive.svg
|
---|
| 73 | [downloads-url]: https://npm-stat.com/charts.html?package=which-boxed-primitive
|
---|