[d565449] | 1 | /**
|
---|
| 2 | * A collection of shims that provide minimal functionality of the ES6 collections.
|
---|
| 3 | *
|
---|
| 4 | * These implementations are not meant to be used outside of the ResizeObserver
|
---|
| 5 | * modules as they cover only a limited range of use cases.
|
---|
| 6 | */
|
---|
| 7 | /* eslint-disable require-jsdoc, valid-jsdoc */
|
---|
| 8 | const MapShim = (() => {
|
---|
| 9 | if (typeof Map !== 'undefined') {
|
---|
| 10 | return Map;
|
---|
| 11 | }
|
---|
| 12 |
|
---|
| 13 | /**
|
---|
| 14 | * Returns index in provided array that matches the specified key.
|
---|
| 15 | *
|
---|
| 16 | * @param {Array<Array>} arr
|
---|
| 17 | * @param {*} key
|
---|
| 18 | * @returns {number}
|
---|
| 19 | */
|
---|
| 20 | function getIndex(arr, key) {
|
---|
| 21 | let result = -1;
|
---|
| 22 |
|
---|
| 23 | arr.some((entry, index) => {
|
---|
| 24 | if (entry[0] === key) {
|
---|
| 25 | result = index;
|
---|
| 26 |
|
---|
| 27 | return true;
|
---|
| 28 | }
|
---|
| 29 |
|
---|
| 30 | return false;
|
---|
| 31 | });
|
---|
| 32 |
|
---|
| 33 | return result;
|
---|
| 34 | }
|
---|
| 35 |
|
---|
| 36 | return class {
|
---|
| 37 | constructor() {
|
---|
| 38 | this.__entries__ = [];
|
---|
| 39 | }
|
---|
| 40 |
|
---|
| 41 | /**
|
---|
| 42 | * @returns {boolean}
|
---|
| 43 | */
|
---|
| 44 | get size() {
|
---|
| 45 | return this.__entries__.length;
|
---|
| 46 | }
|
---|
| 47 |
|
---|
| 48 | /**
|
---|
| 49 | * @param {*} key
|
---|
| 50 | * @returns {*}
|
---|
| 51 | */
|
---|
| 52 | get(key) {
|
---|
| 53 | const index = getIndex(this.__entries__, key);
|
---|
| 54 | const entry = this.__entries__[index];
|
---|
| 55 |
|
---|
| 56 | return entry && entry[1];
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | /**
|
---|
| 60 | * @param {*} key
|
---|
| 61 | * @param {*} value
|
---|
| 62 | * @returns {void}
|
---|
| 63 | */
|
---|
| 64 | set(key, value) {
|
---|
| 65 | const index = getIndex(this.__entries__, key);
|
---|
| 66 |
|
---|
| 67 | if (~index) {
|
---|
| 68 | this.__entries__[index][1] = value;
|
---|
| 69 | } else {
|
---|
| 70 | this.__entries__.push([key, value]);
|
---|
| 71 | }
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | /**
|
---|
| 75 | * @param {*} key
|
---|
| 76 | * @returns {void}
|
---|
| 77 | */
|
---|
| 78 | delete(key) {
|
---|
| 79 | const entries = this.__entries__;
|
---|
| 80 | const index = getIndex(entries, key);
|
---|
| 81 |
|
---|
| 82 | if (~index) {
|
---|
| 83 | entries.splice(index, 1);
|
---|
| 84 | }
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | /**
|
---|
| 88 | * @param {*} key
|
---|
| 89 | * @returns {void}
|
---|
| 90 | */
|
---|
| 91 | has(key) {
|
---|
| 92 | return !!~getIndex(this.__entries__, key);
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | /**
|
---|
| 96 | * @returns {void}
|
---|
| 97 | */
|
---|
| 98 | clear() {
|
---|
| 99 | this.__entries__.splice(0);
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | /**
|
---|
| 103 | * @param {Function} callback
|
---|
| 104 | * @param {*} [ctx=null]
|
---|
| 105 | * @returns {void}
|
---|
| 106 | */
|
---|
| 107 | forEach(callback, ctx = null) {
|
---|
| 108 | for (const entry of this.__entries__) {
|
---|
| 109 | callback.call(ctx, entry[1], entry[0]);
|
---|
| 110 | }
|
---|
| 111 | }
|
---|
| 112 | };
|
---|
| 113 | })();
|
---|
| 114 |
|
---|
| 115 | export {MapShim as Map};
|
---|