UNPKG

indexed

Version:

database-like indexed array that always returns a new array

210 lines (186 loc) 5.9 kB
'use strict'; var _slicedToArray = (function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.addCollection = addCollection; exports.reindexCollections = reindexCollections; exports.getCollection = getCollection; exports.setInCollection = setInCollection; exports.copyCollection = copyCollection; exports.collectionToJson = collectionToJson; exports.collectionFromJson = collectionFromJson; exports.removeFromCollection = removeFromCollection; exports.iterateOverCollection = iterateOverCollection; var _utils = require('./utils'); var _constants = require('./constants'); function addCollection(indexes, propName) { if (!indexes) { return; } if (!indexes.has(COLLECTIONS_KEY)) { indexes.set(COLLECTIONS_KEY, new Map()); } indexes.get(COLLECTIONS_KEY).set(propName, new Map()); return indexes; } function reindexCollections(indexes, arr) { if (!indexes) { return; } if (!indexes.has(COLLECTIONS_KEY)) { return; } } function getCollection(indexes, propName) { if (!indexes) { return; } if (!indexes.has(COLLECTIONS_KEY)) { return; } if (!indexes.get(COLLECTIONS_KEY).has(propName)) { return; } return indexes.get(COLLECTIONS_KEY).get(propName); } function setInCollection(indexes, obj, index) { if (!indexes || !indexes.has(COLLECTIONS_KEY) || isInvalid(obj)) { return; } var collections = indexes.get(COLLECTIONS_KEY); var _iteratorNormalCompletion = true; var _didIteratorError = false; var _iteratorError = undefined; try { for (var _iterator = collections[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { var _step$value = _slicedToArray(_step.value, 2); var propName = _step$value[0]; var collection = _step$value[1]; if (propName in obj) { var propValue = obj[propName]; if (!collection.has(propValue)) { collection.set(propValue, []); } collection.get(propValue).push(index); } } } catch (err) { _didIteratorError = true; _iteratorError = err; } finally { try { if (!_iteratorNormalCompletion && _iterator.return) { _iterator.return(); } } finally { if (_didIteratorError) { throw _iteratorError; } } } } function copyCollection(indexes) { if (!indexes || !indexes.has(COLLECTIONS_KEY)) { return; } var newCollection = new Map(); indexes.get(COLLECTIONS_KEY).forEach(function (propName, collection) { var newcoll = new Map(); collection.forEach(function (propKey, indexesList) { newColl.set(propKey, indexesList.slice()); }); newCollection.set(propName, newColl); }); return newCollection; } function collectionToJson(indexes) { if (!indexes || !indexes.has(COLLECTIONS_KEY)) { return; } var newCollection = []; indexes.get(COLLECTIONS_KEY).forEach(function (propName, collection) { var newcoll = []; collection.forEach(function (propKey, indexesList) { newColl.push([propKey, indexesList.slice()]); }); newCollection.push([propName, newColl]); }); return newCollection; } function collectionFromJson(indexes, json) { var newCollection = new Map(); json.forEach(function (_ref) { var _ref2 = _slicedToArray(_ref, 2); var propName = _ref2[0]; var collection = _ref2[1]; var newcoll = new Map(); collection.forEach(function (_ref3) { var _ref4 = _slicedToArray(_ref3, 2); var propKey = _ref4[0]; var indexesList = _ref4[1]; newColl.set(propKey, indexesList.slice()); }); newCollection.set(propName, newColl); }); indexes.set(COLLECTIONS_KEY, newCollection); } function removeFromCollection(indexes, obj, index) { if (!indexes || !indexes.has(COLLECTIONS_KEY) || isInvalid(obj)) { return; } var collections = indexes.get(COLLECTIONS_KEY); var _iteratorNormalCompletion2 = true; var _didIteratorError2 = false; var _iteratorError2 = undefined; try { for (var _iterator2 = collections[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) { var _step2$value = _slicedToArray(_step2.value, 2); var propName = _step2$value[0]; var collection = _step2$value[1]; if (propName in obj) { var propValue = obj[propName]; if (collection.has(propValue)) { var indexesList = collection.get(propValue); var indexIndex = indexesList.indexOf(index); if (indexIndex >= 0) { indexesList.splice(indexIndex, 1); } } } } } catch (err) { _didIteratorError2 = true; _iteratorError2 = err; } finally { try { if (!_iteratorNormalCompletion2 && _iterator2.return) { _iterator2.return(); } } finally { if (_didIteratorError2) { throw _iteratorError2; } } } } function iterateOverCollection(indexes, propName, propValue, callback, thisArg) { if (!indexes || !indexes.has(COLLECTIONS_KEY)) { return; } var collections = indexes.get(COLLECTIONS_KEY); if (!collections.has(propName)) { return; } var collection = collections.get(propName); if (!collection.has(propValue)) { return; } var indexesList = collection.get(propValue); var length = indexesList.length; var i = 0; var result; while (i < length && result !== _constants.BREAK) { result = callback.call(thisArg, indexesList[i++]); } }