npm stats
  • Search
  • About
  • Repo
  • Sponsor
  • more
    • Search
    • About
    • Repo
    • Sponsor

Made by Antonio Ramirez

weakmap.prototype.getorinsertcomputed

1.0.0

@ljharb

npmHomeRepoSnykSocket
Downloads:102
$ npm install weakmap.prototype.getorinsertcomputed
DailyWeeklyMonthlyYearly

weakmap.prototype.getorinsertcomputed Version Badge

github actions coverage License Downloads

npm badge

An ESnext spec-compliant WeakMap.prototype.getOrInsertComputed shim/polyfill/replacement that works as far down as ES3.

This package implements the es-shim API v3 interface. It works in an ES3-supported environment and complies with the proposed spec.

Getting started

npm install --save weakmap.prototype.getorinsertcomputed

Usage/Examples

var getOrInsertComputed = require('weakmap.prototype.getorinsertcomputed');
var assert = require('assert');

var map = new WeakMap();
var key = {};
var value = {};

assert.equal(map.has(key), false);
assert.equal(getOrInsertComputed(map, key, () => value), value);
assert.equal(map.has(key), true);
var getPolyfill = require('weakmap.prototype.getorinsertcomputed/polyfill');
var shim = require('weakmap.prototype.getorinsertcomputed/shim');
var assert = require('assert');
/* when WeakMap.prototype.getOrInsertComputed is not present */
delete WeakMap.prototype.getOrInsertComputed;
var shimmed = shim();

assert.equal(shimmed, getPolyfill());

var map = new WeakMap();
var key = {};
var value = {};

assert.equal(map.has(key), false);
assert.equal(map.getOrInsertComputed(key, () => value), value);
assert.equal(map.has(key), true);
var shim = require('weakmap.prototype.getorinsertcomputed/shim');
var assert = require('assert');
/* when WeakMap.prototype.getOrInsertComputed is present */
var shimmed = shim();

assert.equal(shimmed, WeakMap.prototype.getOrInsertComputed);

var map = new WeakMap();
var key = {};
var value = {};

assert.equal(map.has(key), false);
assert.equal(map.getOrInsertComputed(key, () => value), value);
assert.equal(map.has(key), true);

Tests

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