|
1 |
| -# Map.prototype.getOrInsert |
2 |
| -ES Proposal spec-compliant shim for Map.prototype.getOrInsert |
| 1 | +# map.prototype.getorinsert <sup>[![Version Badge][npm-version-svg]][package-url]</sup> |
| 2 | + |
| 3 | +[![github actions][actions-image]][actions-url] |
| 4 | +[![coverage][codecov-image]][codecov-url] |
| 5 | +[![License][license-image]][license-url] |
| 6 | +[![Downloads][downloads-image]][downloads-url] |
| 7 | + |
| 8 | +[![npm badge][npm-badge-png]][package-url] |
| 9 | + |
| 10 | +An ESnext spec-compliant `Map.prototype.getOrInsert` shim/polyfill/replacement that works as far down as ES3. |
| 11 | + |
| 12 | +This package implements the [es-shim API](https://github.com/es-shims/api) v3 interface. It works in an ES3-supported environment and complies with the proposed [spec](https://tc39.github.io/proposal-array-grouping/). |
| 13 | + |
| 14 | +## Getting started |
| 15 | + |
| 16 | +```sh |
| 17 | +npm install --save map.prototype.getorinsert |
| 18 | +``` |
| 19 | + |
| 20 | +## Usage/Examples |
| 21 | + |
| 22 | +```js |
| 23 | +var getOrInsert = require('map.prototype.getorinsert'); |
| 24 | +var assert = require('assert'); |
| 25 | + |
| 26 | +var map = new Map(); |
| 27 | +var key = {}; |
| 28 | +var value = {}; |
| 29 | + |
| 30 | +assert.equal(map.has(key), false); |
| 31 | +assert.equal(getOrInsert(map, key, value), value); |
| 32 | +assert.equal(map.has(key), true); |
| 33 | +``` |
| 34 | + |
| 35 | +```js |
| 36 | +var getPolyfill = require('map.prototype.getorinsert/polyfill'); |
| 37 | +var shim = require('map.prototype.getorinsert/shim'); |
| 38 | +var assert = require('assert'); |
| 39 | +/* when Map.prototype.getOrInsert is not present */ |
| 40 | +delete Map.prototype.getOrInsert; |
| 41 | +var shimmed = shim(); |
| 42 | + |
| 43 | +assert.equal(shimmed, getPolyfill()); |
| 44 | + |
| 45 | +var map = new Map(); |
| 46 | +var key = {}; |
| 47 | +var value = {}; |
| 48 | + |
| 49 | +assert.equal(map.has(key), false); |
| 50 | +assert.equal(map.getOrInsert(key, value), value); |
| 51 | +assert.equal(map.has(key), true); |
| 52 | +``` |
| 53 | + |
| 54 | +```js |
| 55 | +var shim = require('map.prototype.getorinsert/shim'); |
| 56 | +var assert = require('assert'); |
| 57 | +/* when Map.prototype.getOrInsert is present */ |
| 58 | +var shimmed = shim(); |
| 59 | + |
| 60 | +assert.equal(shimmed, Map.prototype.getOrInsert); |
| 61 | + |
| 62 | +var map = new Map(); |
| 63 | +var key = {}; |
| 64 | +var value = {}; |
| 65 | + |
| 66 | +assert.equal(map.has(key), false); |
| 67 | +assert.equal(map.getOrInsert(key, value), value); |
| 68 | +assert.equal(map.has(key), true); |
| 69 | +``` |
| 70 | + |
| 71 | +## Tests |
| 72 | +Simply clone the repo, `npm install`, and run `npm test` |
| 73 | + |
| 74 | +[package-url]: https://npmjs.org/package/map.prototype.getorinsert |
| 75 | +[npm-version-svg]: https://versionbadg.es/es-shims/Map.prototype.getOrInsert.svg |
| 76 | +[deps-svg]: https://david-dm.org/es-shims/Map.prototype.getOrInsert.svg |
| 77 | +[deps-url]: https://david-dm.org/es-shims/Map.prototype.getOrInsert |
| 78 | +[dev-deps-svg]: https://david-dm.org/es-shims/Map.prototype.getOrInsert/dev-status.svg |
| 79 | +[dev-deps-url]: https://david-dm.org/es-shims/Map.prototype.getOrInsert#info=devDependencies |
| 80 | +[npm-badge-png]: https://nodei.co/npm/map.prototype.getorinsert.png?downloads=true&stars=true |
| 81 | +[license-image]: https://img.shields.io/npm/l/map.prototype.getorinsert.svg |
| 82 | +[license-url]: LICENSE |
| 83 | +[downloads-image]: https://img.shields.io/npm/dm/map.prototype.getorinsert.svg |
| 84 | +[downloads-url]: https://npm-stat.com/charts.html?package=map.prototype.getorinsert |
| 85 | +[codecov-image]: https://codecov.io/gh/es-shims/Map.prototype.getOrInsert/branch/main/graphs/badge.svg |
| 86 | +[codecov-url]: https://app.codecov.io/gh/es-shims/Map.prototype.getOrInsert/ |
| 87 | +[actions-image]: https://img.shields.io/endpoint?url=https://github-actions-badge-u3jn4tfpocch.runkit.sh/es-shims/Map.prototype.getOrInsert |
| 88 | +[actions-url]: https://github.com/es-shims/Map.prototype.getOrInsert/actions |
0 commit comments