first commit
This commit is contained in:
13
node_modules/internmap/LICENSE
generated
vendored
Normal file
13
node_modules/internmap/LICENSE
generated
vendored
Normal file
@@ -0,0 +1,13 @@
|
||||
Copyright 2021 Mike Bostock
|
||||
|
||||
Permission to use, copy, modify, and/or distribute this software for any purpose
|
||||
with or without fee is hereby granted, provided that the above copyright notice
|
||||
and this permission notice appear in all copies.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
||||
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
||||
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
|
||||
OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
|
||||
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
|
||||
THIS SOFTWARE.
|
||||
94
node_modules/internmap/README.md
generated
vendored
Normal file
94
node_modules/internmap/README.md
generated
vendored
Normal file
@@ -0,0 +1,94 @@
|
||||
# InternMap
|
||||
|
||||
*For live examples, see https://observablehq.com/@mbostock/internmap.*
|
||||
|
||||
If you use dates as keys in a JavaScript Map (or as values in a Set), you may be surprised that it won’t work as you expect.
|
||||
|
||||
```js
|
||||
dateMap = new Map([
|
||||
[new Date(Date.UTC(2001, 0, 1)), "red"],
|
||||
[new Date(Date.UTC(2001, 0, 1)), "green"] // distinct key!
|
||||
])
|
||||
```
|
||||
```js
|
||||
dateMap.get(new Date(Date.UTC(2001, 0, 1))) // undefined!
|
||||
```
|
||||
|
||||
That’s because Map uses the [SameValueZero algorithm](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Equality_comparisons_and_sameness) to determine key equality: for two dates to be considered the same, they must be the same instance (the same object), not just the same moment in time. This is true of the equality operator, too.
|
||||
|
||||
```js
|
||||
{
|
||||
const date1 = new Date(Date.UTC(2001, 0, 1));
|
||||
const date2 = new Date(Date.UTC(2001, 0, 1));
|
||||
return date1 === date2; // false!
|
||||
}
|
||||
```
|
||||
|
||||
You can avoid this issue by using primitive values such as numbers or strings as keys instead. But it’s tedious and easy to forget to coerce types. (You’ll also need to do the inverse type conversion when pulling keys out of the map, say when using *map*.keys or *map*.entries, or when iterating over the map. The inverse above is new Date(*key*). Also, if you forget to coerce your key to a number when using *map*.get, it’s easy not to notice because the map won’t throw an error; it’ll simply return undefined.)
|
||||
|
||||
```js
|
||||
numberMap = new Map([[978307200000, "red"]])
|
||||
numberMap.get(978307200000) // "red"
|
||||
numberMap.get(new Date(978307200000)) // undefined; oops!
|
||||
```
|
||||
|
||||
Wouldn’t it be easier if Map and Set “just worked” with dates? Or with any object that supports [*object*.valueOf](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/valueOf)?
|
||||
|
||||
Enter **InternMap**. [*Interning*](https://en.wikipedia.org/wiki/String_interning) refers to storing only one copy of each distinct key. An InternMap considers two Date instances representing the same moment to be equal, storing only the first instance.
|
||||
|
||||
```js
|
||||
map = new InternMap([
|
||||
[new Date(Date.UTC(2001, 0, 1)), "red"],
|
||||
[new Date(Date.UTC(2001, 0, 1)), "green"] // replaces previous entry
|
||||
])
|
||||
```
|
||||
```js
|
||||
map.get(new Date(Date.UTC(2001, 0, 1))) // "green"
|
||||
```
|
||||
```js
|
||||
[...map.keys()] // [2001-01-01]
|
||||
```
|
||||
|
||||
InternMap extends Map, so you can simply drop it in whenever you’d prefer this behavior to the SameValueZero algorithm. Because InternMap calls *object*.valueOf only for non-primitive keys, note that you can pass primitive keys, too.
|
||||
|
||||
```js
|
||||
map.get(978307200000) // "green"; this works too!
|
||||
```
|
||||
|
||||
InternMap keeps only the first distinct key according to its associated primitive value. Avoid adding keys to the map with inconsistent types.
|
||||
|
||||
```js
|
||||
map2 = new InternMap([
|
||||
[978307200000, "red"], // danger!
|
||||
[new Date(Date.UTC(2001, 0, 1)), "blue"]
|
||||
])
|
||||
```
|
||||
```js
|
||||
map2.get(new Date(Date.UTC(2001, 0, 1))) // "blue"; this still works…
|
||||
```
|
||||
```js
|
||||
[...map2.keys()] // [978307200000]; but the key isn’t a Date
|
||||
```
|
||||
|
||||
While InternMap uses *object*.valueOf by default to compute the intern key, you can pass a key function as a second argument to the constructor to change the behavior. For example, if you use JSON.stringify, you can use arrays as compound keys (assuming that the array elements can be serialized to JSON).
|
||||
|
||||
```js
|
||||
map3 = new InternMap([
|
||||
[["foo", "bar"], 1],
|
||||
[["foo", "baz"], 2],
|
||||
[["goo", "bee"], 3]
|
||||
], JSON.stringify)
|
||||
```
|
||||
```js
|
||||
map3.get(["foo", "baz"]) // 2
|
||||
```
|
||||
|
||||
There’s an **InternSet** class, too.
|
||||
|
||||
```js
|
||||
set = new InternSet([
|
||||
new Date(Date.UTC(2000, 0, 1)),
|
||||
new Date(Date.UTC(2001, 0, 1)),
|
||||
new Date(Date.UTC(2001, 0, 1))
|
||||
])
|
||||
```
|
||||
75
node_modules/internmap/dist/internmap.js
generated
vendored
Normal file
75
node_modules/internmap/dist/internmap.js
generated
vendored
Normal file
@@ -0,0 +1,75 @@
|
||||
// https://github.com/mbostock/internmap/ v2.0.3 Copyright 2021 Mike Bostock
|
||||
(function (global, factory) {
|
||||
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
|
||||
typeof define === 'function' && define.amd ? define(['exports'], factory) :
|
||||
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.internmap = {}));
|
||||
}(this, (function (exports) { 'use strict';
|
||||
|
||||
class InternMap extends Map {
|
||||
constructor(entries, key = keyof) {
|
||||
super();
|
||||
Object.defineProperties(this, {_intern: {value: new Map()}, _key: {value: key}});
|
||||
if (entries != null) for (const [key, value] of entries) this.set(key, value);
|
||||
}
|
||||
get(key) {
|
||||
return super.get(intern_get(this, key));
|
||||
}
|
||||
has(key) {
|
||||
return super.has(intern_get(this, key));
|
||||
}
|
||||
set(key, value) {
|
||||
return super.set(intern_set(this, key), value);
|
||||
}
|
||||
delete(key) {
|
||||
return super.delete(intern_delete(this, key));
|
||||
}
|
||||
}
|
||||
|
||||
class InternSet extends Set {
|
||||
constructor(values, key = keyof) {
|
||||
super();
|
||||
Object.defineProperties(this, {_intern: {value: new Map()}, _key: {value: key}});
|
||||
if (values != null) for (const value of values) this.add(value);
|
||||
}
|
||||
has(value) {
|
||||
return super.has(intern_get(this, value));
|
||||
}
|
||||
add(value) {
|
||||
return super.add(intern_set(this, value));
|
||||
}
|
||||
delete(value) {
|
||||
return super.delete(intern_delete(this, value));
|
||||
}
|
||||
}
|
||||
|
||||
function intern_get({_intern, _key}, value) {
|
||||
const key = _key(value);
|
||||
return _intern.has(key) ? _intern.get(key) : value;
|
||||
}
|
||||
|
||||
function intern_set({_intern, _key}, value) {
|
||||
const key = _key(value);
|
||||
if (_intern.has(key)) return _intern.get(key);
|
||||
_intern.set(key, value);
|
||||
return value;
|
||||
}
|
||||
|
||||
function intern_delete({_intern, _key}, value) {
|
||||
const key = _key(value);
|
||||
if (_intern.has(key)) {
|
||||
value = _intern.get(key);
|
||||
_intern.delete(key);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
function keyof(value) {
|
||||
return value !== null && typeof value === "object" ? value.valueOf() : value;
|
||||
}
|
||||
|
||||
exports.InternMap = InternMap;
|
||||
exports.InternSet = InternSet;
|
||||
|
||||
Object.defineProperty(exports, '__esModule', { value: true });
|
||||
|
||||
})));
|
||||
2
node_modules/internmap/dist/internmap.min.js
generated
vendored
Normal file
2
node_modules/internmap/dist/internmap.min.js
generated
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
// https://github.com/mbostock/internmap/ v2.0.3 Copyright 2021 Mike Bostock
|
||||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).internmap={})}(this,(function(e){"use strict";class InternMap extends Map{constructor(e,t=r){if(super(),Object.defineProperties(this,{_intern:{value:new Map},_key:{value:t}}),null!=e)for(const[t,n]of e)this.set(t,n)}get(e){return super.get(t(this,e))}has(e){return super.has(t(this,e))}set(e,t){return super.set(n(this,e),t)}delete(e){return super.delete(s(this,e))}}class InternSet extends Set{constructor(e,t=r){if(super(),Object.defineProperties(this,{_intern:{value:new Map},_key:{value:t}}),null!=e)for(const t of e)this.add(t)}has(e){return super.has(t(this,e))}add(e){return super.add(n(this,e))}delete(e){return super.delete(s(this,e))}}function t({_intern:e,_key:t},n){const s=t(n);return e.has(s)?e.get(s):n}function n({_intern:e,_key:t},n){const s=t(n);return e.has(s)?e.get(s):(e.set(s,n),n)}function s({_intern:e,_key:t},n){const s=t(n);return e.has(s)&&(n=e.get(s),e.delete(s)),n}function r(e){return null!==e&&"object"==typeof e?e.valueOf():e}e.InternMap=InternMap,e.InternSet=InternSet,Object.defineProperty(e,"__esModule",{value:!0})}));
|
||||
43
node_modules/internmap/package.json
generated
vendored
Normal file
43
node_modules/internmap/package.json
generated
vendored
Normal file
@@ -0,0 +1,43 @@
|
||||
{
|
||||
"name": "internmap",
|
||||
"version": "2.0.3",
|
||||
"description": "Map and Set with automatic key interning",
|
||||
"homepage": "https://github.com/mbostock/internmap/",
|
||||
"license": "ISC",
|
||||
"author": {
|
||||
"name": "Mike Bostock",
|
||||
"url": "https://bost.ocks.org/mike"
|
||||
},
|
||||
"type": "module",
|
||||
"main": "src/index.js",
|
||||
"module": "src/index.js",
|
||||
"jsdelivr": "dist/internmap.min.js",
|
||||
"unpkg": "dist/internmap.min.js",
|
||||
"exports": {
|
||||
"umd": "./dist/internmap.min.js",
|
||||
"default": "./src/index.js"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/mbostock/internmap.git"
|
||||
},
|
||||
"files": [
|
||||
"dist/**/*.js",
|
||||
"src/**/*.js"
|
||||
],
|
||||
"scripts": {
|
||||
"test": "mocha 'test/**/*-test.js' && eslint src test",
|
||||
"prepublishOnly": "rm -rf dist && yarn test && rollup -c",
|
||||
"postpublish": "git push && git push --tags"
|
||||
},
|
||||
"sideEffects": false,
|
||||
"devDependencies": {
|
||||
"eslint": "^7.32.0",
|
||||
"mocha": "^9.1.1",
|
||||
"rollup": "^2.56.3",
|
||||
"rollup-plugin-terser": "^7.0.2"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=12"
|
||||
}
|
||||
}
|
||||
61
node_modules/internmap/src/index.js
generated
vendored
Normal file
61
node_modules/internmap/src/index.js
generated
vendored
Normal file
@@ -0,0 +1,61 @@
|
||||
export class InternMap extends Map {
|
||||
constructor(entries, key = keyof) {
|
||||
super();
|
||||
Object.defineProperties(this, {_intern: {value: new Map()}, _key: {value: key}});
|
||||
if (entries != null) for (const [key, value] of entries) this.set(key, value);
|
||||
}
|
||||
get(key) {
|
||||
return super.get(intern_get(this, key));
|
||||
}
|
||||
has(key) {
|
||||
return super.has(intern_get(this, key));
|
||||
}
|
||||
set(key, value) {
|
||||
return super.set(intern_set(this, key), value);
|
||||
}
|
||||
delete(key) {
|
||||
return super.delete(intern_delete(this, key));
|
||||
}
|
||||
}
|
||||
|
||||
export class InternSet extends Set {
|
||||
constructor(values, key = keyof) {
|
||||
super();
|
||||
Object.defineProperties(this, {_intern: {value: new Map()}, _key: {value: key}});
|
||||
if (values != null) for (const value of values) this.add(value);
|
||||
}
|
||||
has(value) {
|
||||
return super.has(intern_get(this, value));
|
||||
}
|
||||
add(value) {
|
||||
return super.add(intern_set(this, value));
|
||||
}
|
||||
delete(value) {
|
||||
return super.delete(intern_delete(this, value));
|
||||
}
|
||||
}
|
||||
|
||||
function intern_get({_intern, _key}, value) {
|
||||
const key = _key(value);
|
||||
return _intern.has(key) ? _intern.get(key) : value;
|
||||
}
|
||||
|
||||
function intern_set({_intern, _key}, value) {
|
||||
const key = _key(value);
|
||||
if (_intern.has(key)) return _intern.get(key);
|
||||
_intern.set(key, value);
|
||||
return value;
|
||||
}
|
||||
|
||||
function intern_delete({_intern, _key}, value) {
|
||||
const key = _key(value);
|
||||
if (_intern.has(key)) {
|
||||
value = _intern.get(key);
|
||||
_intern.delete(key);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
function keyof(value) {
|
||||
return value !== null && typeof value === "object" ? value.valueOf() : value;
|
||||
}
|
||||
Reference in New Issue
Block a user