Socket
Socket
Sign inDemoInstall

get-intrinsic

Package Overview
Dependencies
4
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

get-intrinsic

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


Version published
Maintainers
1
Weekly downloads
51,827,180
increased by1.4%
Install size
92.8 kB

Weekly downloads

Package description

What is get-intrinsic?

The get-intrinsic package is a utility that allows you to safely obtain references to ECMAScript language intrinsics without relying on the global namespace, which can be altered by other code. It helps in writing robust code that doesn't get affected by modifications to the global objects or functions.

What are get-intrinsic's main functionalities?

Getting intrinsic values

This feature allows you to get the original intrinsic value of Array.prototype.push, which can then be used to push elements to arrays without relying on Array.prototype.push being unmodified.

var getIntrinsic = require('get-intrinsic');
var ArrayPrototypePush = getIntrinsic('%Array.prototype.push%');
var anArray = [1, 2, 3];
ArrayPrototypePush(anArray, 4); // anArray becomes [1, 2, 3, 4]

Accessing deep intrinsics

This feature allows you to access deep intrinsic properties like Object.prototype.hasOwnProperty, which can be used to check for properties without relying on the original method being unaltered.

var getIntrinsic = require('get-intrinsic');
var hasOwn = getIntrinsic('%Object.prototype.hasOwnProperty%');
var hasDuck = hasOwn.call({ duck: 'quack' }, 'duck'); // hasDuck is true

Ensuring unmodified constructors

This feature allows you to use the original Array constructor to create new arrays, ensuring that the constructor has not been modified in the global scope.

var getIntrinsic = require('get-intrinsic');
var ArrayConstructor = getIntrinsic('%Array%');
var myArray = new ArrayConstructor(1, 2, 3); // myArray is [1, 2, 3]

Other packages similar to get-intrinsic

Readme

Source

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

var GetIntrinsic = require('get-intrinsic');
var assert = require('assert');

// static methods
assert.equal(GetIntrinsic('%Math.pow%'), Math.pow);
assert.equal(Math.pow(2, 3), 8);
assert.equal(GetIntrinsic('%Math.pow%')(2, 3), 8);
delete Math.pow;
assert.equal(GetIntrinsic('%Math.pow%')(2, 3), 8);

// instance methods
var arr = [1];
assert.equal(GetIntrinsic('%Array.prototype.push%'), Array.prototype.push);
assert.deepEqual(arr, [1]);

arr.push(2);
assert.deepEqual(arr, [1, 2]);

GetIntrinsic('%Array.prototype.push%').call(arr, 3);
assert.deepEqual(arr, [1, 2, 3]);

delete Array.prototype.push;
GetIntrinsic('%Array.prototype.push%').call(arr, 4);
assert.deepEqual(arr, [1, 2, 3, 4]);

// missing features
delete JSON.parse; // to simulate a real intrinsic that is missing in the environment
assert.throws(() => GetIntrinsic('%JSON.parse%'));
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.

Keywords

FAQs

Last updated on 14 May 2023

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc