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