Skip to content

Commit 3bd9daa

Browse files
committed
Initial implementation, tests, readme
1 parent b4cb4f2 commit 3bd9daa

21 files changed

+470
-4
lines changed

.eslintrc

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"root": true,
3+
4+
"extends": "@ljharb",
5+
6+
"rules": {
7+
"id-length": "off",
8+
"new-cap": ["error", {
9+
"capIsNewExceptions": [
10+
"CanonicalizeKeyedCollectionKey",
11+
],
12+
}]
13+
},
14+
}

.github/FUNDING.yml

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# These are supported funding model platforms
2+
3+
github: [ljharb]
4+
patreon: # Replace with a single Patreon username
5+
open_collective: # Replace with a single Open Collective username
6+
ko_fi: # Replace with a single Ko-fi username
7+
tidelift: npm/map.prototype.getorinsert
8+
community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry
9+
liberapay: # Replace with a single Liberapay username
10+
issuehunt: # Replace with a single IssueHunt username
11+
otechie: # Replace with a single Otechie username
12+
custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2']

.github/workflows/node-aught.yml

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
name: 'Tests: node.js < 10'
2+
3+
on: [pull_request, push]
4+
5+
jobs:
6+
tests:
7+
uses: ljharb/actions/.github/workflows/node.yml@main
8+
with:
9+
range: '< 10'
10+
type: minors
11+
command: npm run tests-only
12+
13+
node:
14+
name: 'node < 10'
15+
needs: [tests]
16+
runs-on: ubuntu-latest
17+
steps:
18+
- run: 'echo tests completed'

.github/workflows/node-pretest.yml

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
name: 'Tests: pretest/posttest'
2+
3+
on: [pull_request, push]
4+
5+
jobs:
6+
tests:
7+
uses: ljharb/actions/.github/workflows/pretest.yml@main

.github/workflows/node-tens.yml

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
name: 'Tests: node.js >= 10'
2+
3+
on: [pull_request, push]
4+
5+
jobs:
6+
tests:
7+
uses: ljharb/actions/.github/workflows/node.yml@main
8+
with:
9+
range: '>= 10'
10+
type: minors
11+
command: npm run tests-only
12+
13+
node:
14+
name: 'node >= 10'
15+
needs: [tests]
16+
runs-on: ubuntu-latest
17+
steps:
18+
- run: 'echo tests completed'

.github/workflows/rebase.yml

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name: Automatic Rebase
2+
3+
on: [pull_request_target]
4+
5+
jobs:
6+
_:
7+
uses: ljharb/actions/.github/workflows/rebase.yml@main
8+
secrets:
9+
token: ${{ secrets.GITHUB_TOKEN }}
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
name: Require “Allow Edits”
2+
3+
on: [pull_request_target]
4+
5+
jobs:
6+
_:
7+
name: "Require “Allow Edits”"
8+
9+
runs-on: ubuntu-latest
10+
11+
steps:
12+
- uses: ljharb/require-allow-edits@main

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -140,3 +140,4 @@ npm-shrinkwrap.json
140140
package-lock.json
141141
yarn.lock
142142

143+
.npmignore

.nycrc

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"all": true,
3+
"check-coverage": false,
4+
"reporter": ["text-summary", "text", "html", "json"],
5+
"lines": 86,
6+
"statements": 85.93,
7+
"functions": 82.43,
8+
"branches": 76.06,
9+
"exclude": [
10+
"coverage",
11+
"test"
12+
]
13+
}

README.md

+88-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,88 @@
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

aos/CanonicalizeKeyedCollectionKey.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
'use strict';
2+
3+
// https://tc39.es/ecma262/#sec-canonicalizekeyedcollectionkey
4+
5+
module.exports = function CanonicalizeKeyedCollectionKey(key) {
6+
return key === 0 ? +0 : key; // steps 1, 2
7+
};

auto.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
'use strict';
2+
3+
require('./shim')();

implementation.js

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
'use strict';
2+
3+
var callBind = require('call-bind');
4+
5+
var CanonicalizeKeyedCollectionKey = require('./aos/CanonicalizeKeyedCollectionKey');
6+
7+
var $Map = require('es-map/polyfill')();
8+
9+
var $mapHas = callBind($Map.prototype.has);
10+
var $mapGet = callBind($Map.prototype.get);
11+
var $mapSet = callBind($Map.prototype.set);
12+
13+
module.exports = function getOrInsert(key, value) {
14+
var M = this; // step 1
15+
16+
// 2. Perform ? RequireInternalSlot(M, [[MapData]]).
17+
var has = $mapHas(M); // step 2
18+
19+
// eslint-disable-next-line no-param-reassign
20+
key = CanonicalizeKeyedCollectionKey(key); // step 3
21+
22+
if (has) { // step 4
23+
return $mapGet(M, key); // step 4.a
24+
}
25+
26+
$mapSet(M, key, value); // step 5, 6
27+
28+
return value; // step 7
29+
};

index.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
'use strict';
2+
3+
var callBind = require('call-bind');
4+
5+
var getPolyfill = require('./polyfill');
6+
7+
module.exports = callBind(getPolyfill());

package.json

+56-2
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,24 @@
55
"main": "index.js",
66
"exports": {
77
".": "./index.js",
8+
"./auto": "./auto.js",
9+
"./polyfill": "./polyfill.js",
10+
"./implementation": "./implementation.js",
11+
"./shim": "./shim.js",
812
"./package.json": "./package.json"
913
},
1014
"scripts": {
11-
"test": "echo \"Error: no test specified\" && exit 1"
15+
"prepack": "npmignore --auto --commentLines=autogenerated",
16+
"prepublish": "not-in-publish || npm run prepublishOnly",
17+
"prepublishOnly": "safe-publish-latest",
18+
"pretest": "npm run --silent lint",
19+
"test": "npm run tests-only",
20+
"posttest": "npx npm@\">= 10.2\" audit --production",
21+
"tests-only": "nyc tape 'test/**/*.js'",
22+
"lint": "eslint --ext=js,mjs .",
23+
"postlint": "es-shim-api && evalmd README.md",
24+
"version": "auto-changelog && git add CHANGELOG.md",
25+
"postversion": "auto-changelog && git add CHANGELOG.md && git commit --no-edit --amend && git tag -f \"v$(node -e \"console.log(require('./package.json').version)\")\""
1226
},
1327
"repository": {
1428
"type": "git",
@@ -32,5 +46,45 @@
3246
"bugs": {
3347
"url": "https://github.com/es-shims/Map.prototype.getOrInsert/issues"
3448
},
35-
"homepage": "https://github.com/es-shims/Map.prototype.getOrInsert#readme"
49+
"homepage": "https://github.com/es-shims/Map.prototype.getOrInsert#readme",
50+
"devDependencies": {
51+
"@es-shims/api": "^3.0.2",
52+
"@ljharb/eslint-config": "^21.1.1",
53+
"auto-changelog": "^2.5.0",
54+
"es-value-fixtures": "^1.7.1",
55+
"eslint": "=8.8.0",
56+
"evalmd": "^0.0.19",
57+
"for-each": "^0.3.5",
58+
"functions-have-names": "^1.2.3",
59+
"in-publish": "^2.0.1",
60+
"npmignore": "^0.3.1",
61+
"nyc": "^10.3.2",
62+
"object-inspect": "^1.13.4",
63+
"safe-publish-latest": "^2.0.0",
64+
"tape": "^5.9.0"
65+
},
66+
"auto-changelog": {
67+
"output": "CHANGELOG.md",
68+
"template": "keepachangelog",
69+
"unreleased": false,
70+
"commitLimit": false,
71+
"backfillLimit": false,
72+
"hideCredit": true
73+
},
74+
"publishConfig": {
75+
"ignore": [
76+
".github/workflows"
77+
]
78+
},
79+
"engines": {
80+
"node": ">= 0.4"
81+
},
82+
"testling": {
83+
"files": "test/shimmed.js"
84+
},
85+
"dependencies": {
86+
"call-bind": "^1.0.8",
87+
"define-properties": "^1.2.1",
88+
"es-map": "^2.0.1"
89+
}
3690
}

polyfill.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
'use strict';
2+
3+
var implementation = require('./implementation');
4+
5+
module.exports = function getPolyfill() {
6+
return (typeof Map === 'function' && Map.prototype.getOrInsert) || implementation;
7+
};

shim.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
'use strict';
2+
3+
var define = require('define-properties');
4+
5+
var getPolyfill = require('./polyfill');
6+
var getMap = require('es-map/polyfill');
7+
8+
module.exports = function shim() {
9+
var polyfill = getPolyfill();
10+
11+
var $Map = getMap();
12+
define(
13+
$Map.prototype,
14+
{ getOrInsert: polyfill },
15+
{ getOrInsert: function () { return $Map.prototype.getOrInsert !== polyfill; } }
16+
);
17+
if (typeof Map === 'function' && Map !== $Map) {
18+
define(
19+
Map,
20+
{ getOrInsert: polyfill },
21+
{ getOrInsert: function () { return Map.prototype.getOrInsert !== polyfill; } }
22+
);
23+
}
24+
25+
return polyfill;
26+
};

test/implementation.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
'use strict';
2+
3+
var callBind = require('call-bind');
4+
var test = require('tape');
5+
6+
var implementation = require('../implementation');
7+
var runTests = require('./tests');
8+
9+
test('as a function', function (t) {
10+
runTests(callBind(implementation), t);
11+
12+
t.end();
13+
});

test/index.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
'use strict';
2+
3+
var test = require('tape');
4+
5+
var index = require('../');
6+
var runTests = require('./tests');
7+
8+
test('as a function', function (t) {
9+
runTests(index, t);
10+
11+
t.end();
12+
});

0 commit comments

Comments
 (0)