1 | # get-intrinsic <sup>[![Version Badge][npm-version-svg]][package-url]</sup>
|
---|
2 |
|
---|
3 | [![dependency status][deps-svg]][deps-url]
|
---|
4 | [![dev dependency status][dev-deps-svg]][dev-deps-url]
|
---|
5 | [![License][license-image]][license-url]
|
---|
6 | [![Downloads][downloads-image]][downloads-url]
|
---|
7 |
|
---|
8 | [![npm badge][npm-badge-png]][package-url]
|
---|
9 |
|
---|
10 | Get and robustly cache all JS language-level intrinsics at first require time.
|
---|
11 |
|
---|
12 | See the syntax described [in the JS spec](https://tc39.es/ecma262/#sec-well-known-intrinsic-objects) for reference.
|
---|
13 |
|
---|
14 | ## Example
|
---|
15 |
|
---|
16 | ```js
|
---|
17 | var GetIntrinsic = require('get-intrinsic');
|
---|
18 | var assert = require('assert');
|
---|
19 |
|
---|
20 | // static methods
|
---|
21 | assert.equal(GetIntrinsic('%Math.pow%'), Math.pow);
|
---|
22 | assert.equal(Math.pow(2, 3), 8);
|
---|
23 | assert.equal(GetIntrinsic('%Math.pow%')(2, 3), 8);
|
---|
24 | delete Math.pow;
|
---|
25 | assert.equal(GetIntrinsic('%Math.pow%')(2, 3), 8);
|
---|
26 |
|
---|
27 | // instance methods
|
---|
28 | var arr = [1];
|
---|
29 | assert.equal(GetIntrinsic('%Array.prototype.push%'), Array.prototype.push);
|
---|
30 | assert.deepEqual(arr, [1]);
|
---|
31 |
|
---|
32 | arr.push(2);
|
---|
33 | assert.deepEqual(arr, [1, 2]);
|
---|
34 |
|
---|
35 | GetIntrinsic('%Array.prototype.push%').call(arr, 3);
|
---|
36 | assert.deepEqual(arr, [1, 2, 3]);
|
---|
37 |
|
---|
38 | delete Array.prototype.push;
|
---|
39 | GetIntrinsic('%Array.prototype.push%').call(arr, 4);
|
---|
40 | assert.deepEqual(arr, [1, 2, 3, 4]);
|
---|
41 |
|
---|
42 | // missing features
|
---|
43 | delete JSON.parse; // to simulate a real intrinsic that is missing in the environment
|
---|
44 | assert.throws(() => GetIntrinsic('%JSON.parse%'));
|
---|
45 | assert.equal(undefined, GetIntrinsic('%JSON.parse%', true));
|
---|
46 | ```
|
---|
47 |
|
---|
48 | ## Tests
|
---|
49 | Simply clone the repo, `npm install`, and run `npm test`
|
---|
50 |
|
---|
51 | ## Security
|
---|
52 |
|
---|
53 | Please email [@ljharb](https://github.com/ljharb) or see https://tidelift.com/security if you have a potential security vulnerability to report.
|
---|
54 |
|
---|
55 | [package-url]: https://npmjs.org/package/get-intrinsic
|
---|
56 | [npm-version-svg]: http://versionbadg.es/ljharb/get-intrinsic.svg
|
---|
57 | [deps-svg]: https://david-dm.org/ljharb/get-intrinsic.svg
|
---|
58 | [deps-url]: https://david-dm.org/ljharb/get-intrinsic
|
---|
59 | [dev-deps-svg]: https://david-dm.org/ljharb/get-intrinsic/dev-status.svg
|
---|
60 | [dev-deps-url]: https://david-dm.org/ljharb/get-intrinsic#info=devDependencies
|
---|
61 | [npm-badge-png]: https://nodei.co/npm/get-intrinsic.png?downloads=true&stars=true
|
---|
62 | [license-image]: https://img.shields.io/npm/l/get-intrinsic.svg
|
---|
63 | [license-url]: LICENSE
|
---|
64 | [downloads-image]: https://img.shields.io/npm/dm/get-intrinsic.svg
|
---|
65 | [downloads-url]: https://npm-stat.com/charts.html?package=get-intrinsic
|
---|