项目作者: ljharb

项目描述 :
Get and robustly cache all JS language-level intrinsics at first require time.
高级语言: JavaScript
项目地址: git://github.com/ljharb/get-intrinsic.git
创建时间: 2020-10-30T03:37:22Z
项目社区:https://github.com/ljharb/get-intrinsic

开源协议:MIT License

下载


get-intrinsic Version Badge

github actions
coverage
dependency status
dev dependency status
License
Downloads

npm badge

Get and robustly cache all JS language-level intrinsics at first require time.

See the syntax described in the JS spec for reference.

Example

  1. var GetIntrinsic = require('get-intrinsic');
  2. var assert = require('assert');
  3. // static methods
  4. assert.equal(GetIntrinsic('%Math.pow%'), Math.pow);
  5. assert.equal(Math.pow(2, 3), 8);
  6. assert.equal(GetIntrinsic('%Math.pow%')(2, 3), 8);
  7. delete Math.pow;
  8. assert.equal(GetIntrinsic('%Math.pow%')(2, 3), 8);
  9. // instance methods
  10. var arr = [1];
  11. assert.equal(GetIntrinsic('%Array.prototype.push%'), Array.prototype.push);
  12. assert.deepEqual(arr, [1]);
  13. arr.push(2);
  14. assert.deepEqual(arr, [1, 2]);
  15. GetIntrinsic('%Array.prototype.push%').call(arr, 3);
  16. assert.deepEqual(arr, [1, 2, 3]);
  17. delete Array.prototype.push;
  18. GetIntrinsic('%Array.prototype.push%').call(arr, 4);
  19. assert.deepEqual(arr, [1, 2, 3, 4]);
  20. // missing features
  21. delete JSON.parse; // to simulate a real intrinsic that is missing in the environment
  22. assert.throws(() => GetIntrinsic('%JSON.parse%'));
  23. assert.equal(undefined, GetIntrinsic('%JSON.parse%', true));

Tests

Simply clone the repo, npm install, and run npm test

Security

Please email @ljharb or see https://tidelift.com/security if you have a potential security vulnerability to report.