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