UNPKG

pouchdb-collections

Version:

Map and Set shims for PouchDB

103 lines (92 loc) 2.51 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); function mangle(key) { return '$' + key; } function unmangle(key) { return key.substring(1); } function Map$1() { this._store = {}; } Map$1.prototype.get = function (key) { var mangled = mangle(key); return this._store[mangled]; }; Map$1.prototype.set = function (key, value) { var mangled = mangle(key); this._store[mangled] = value; return true; }; Map$1.prototype.has = function (key) { var mangled = mangle(key); return mangled in this._store; }; Map$1.prototype.keys = function () { return Object.keys(this._store).map(k => unmangle(k)); }; Map$1.prototype.delete = function (key) { var mangled = mangle(key); var res = mangled in this._store; delete this._store[mangled]; return res; }; Map$1.prototype.forEach = function (cb) { var keys = Object.keys(this._store); for (var i = 0, len = keys.length; i < len; i++) { var key = keys[i]; var value = this._store[key]; key = unmangle(key); cb(value, key); } }; Object.defineProperty(Map$1.prototype, 'size', { get: function () { return Object.keys(this._store).length; } }); function Set$1(array) { this._store = new Map$1(); // init with an array if (array && Array.isArray(array)) { for (var i = 0, len = array.length; i < len; i++) { this.add(array[i]); } } } Set$1.prototype.add = function (key) { return this._store.set(key, true); }; Set$1.prototype.has = function (key) { return this._store.has(key); }; Set$1.prototype.forEach = function (cb) { this._store.forEach(function (value, key) { cb(key); }); }; Object.defineProperty(Set$1.prototype, 'size', { get: function () { return this._store.size; } }); // Based on https://kangax.github.io/compat-table/es6/ we can sniff out // incomplete Map/Set implementations which would otherwise cause our tests to fail. // Notably they fail in IE11 and iOS 8.4, which this prevents. function supportsMapAndSet() { if (typeof Symbol === 'undefined' || typeof Map === 'undefined' || typeof Set === 'undefined') { return false; } var prop = Object.getOwnPropertyDescriptor(Map, Symbol.species); return prop && 'get' in prop && Map[Symbol.species] === Map; } // based on https://github.com/montagejs/collections { if (supportsMapAndSet()) { // prefer built-in Map/Set exports.Set = Set; exports.Map = Map; } else { // fall back to our polyfill exports.Set = Set$1; exports.Map = Map$1; } }