Skip to content

ES Proposal spec-compliant shim for WeakMap.prototype.getOrInsert

License

Notifications You must be signed in to change notification settings

es-shims/WeakMap.prototype.getOrInsert

Repository files navigation

weakmap.prototype.getorinsert Version Badge

github actions coverage License Downloads

npm badge

An ESnext spec-compliant WeakMap.prototype.getOrInsert 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.getorinsert

Usage/Examples

var getOrInsert = require('weakmap.prototype.getorinsert');
var assert = require('assert');

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

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

assert.equal(shimmed, getPolyfill());

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

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

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

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

assert.equal(map.has(key), false);
assert.equal(map.getOrInsert(key, value), value);
assert.equal(map.has(key), true);

Tests

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

About

ES Proposal spec-compliant shim for WeakMap.prototype.getOrInsert

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks