indexed
Version:
database-like indexed array that always returns a new array
329 lines (280 loc) • 10.1 kB
JavaScript
'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"); } }; })();
var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.methods = exports.Indexed = undefined;
exports.IndexedFactory = IndexedFactory;
exports.shouldReindex = shouldReindex;
exports.Props = Props;
var _methods = require('./methods');
var _constants = require('./constants');
var _arrayIndexes = require('./array-indexes');
var _initializersMethods = require('./initializersMethods');
var _initializersMethods2 = _interopRequireDefault(_initializersMethods);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function IndexedFactory(arr, indexes, initializer, factory) {
return new Indexed(arr, indexes, initializer, factory);
}
function shouldReindex(props, arr, indexes) {
return arr && arr.length && props.indexes !== indexes;
}
function Props(arr, indexes, initializer, factory) {
return new PropsClass(arr, indexes, initializer, factory);
}
var PropsClass = (function () {
function PropsClass(arr, indexes, initializer, factory) {
_classCallCheck(this, PropsClass);
this.arr = arr || [];
this.indexes = (0, _arrayIndexes.createIndexes)(indexes);
this.collections = [];
this.lastIndexes = [];
this.mutate = false;
this.factory = factory;
this.initializer = initializer || false;
this.currentIndex = 0;
}
_createClass(PropsClass, [{
key: 'chain',
value: function chain() {
this.mutate = true;
}
}, {
key: 'value',
value: function value() {
this.mutate = false;
return this.arr;
}
}, {
key: 'doMutate',
value: function doMutate(_mutate) {
this.mutate = _mutate;
}
}, {
key: 'toJson',
value: function toJson() {
return {
indexes: (0, _arrayIndexes.indexesToJson)(this.indexes),
items: this.arr
};
}
}, {
key: 'getIterator',
value: function getIterator(key) {
var _ref;
var iterator = this.indexes && this.indexes.has(key) ? this.indexes.get(key).entries() : null;
if (!iterator) {
throw new Error('index `' + key + '` does not exist');
}
var arr = this.arr;
function next() {
var answer = iterator.next();
if (answer.done) {
return answer;
}
var _answer$value = _slicedToArray(answer.value, 2);
var key = _answer$value[0];
var index = _answer$value[1];
return { value: [index, arr[index]] };
}
function forEach(fn, thisArg) {
var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;
try {
for (var _iterator = iterator[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var _step$value = _slicedToArray(_step.value, 2);
var _key = _step$value[0];
var index = _step$value[1];
var answer = fn.call(thisArg, arr[index], index);
if (answer === _constants.BREAK) {
return;
}
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator.return) {
_iterator.return();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}
}
function map(fn, thisArg) {
var result = [];
var _iteratorNormalCompletion2 = true;
var _didIteratorError2 = false;
var _iteratorError2 = undefined;
try {
for (var _iterator2 = iterator[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
var _step2$value = _slicedToArray(_step2.value, 2);
var _key2 = _step2$value[0];
var index = _step2$value[1];
var answer = fn.call(thisArg, arr[index], index);
if (answer === _constants.BREAK) {
return result;
}
if (answer === _constants.SKIP) {
continue;
}
result.push(answer);
}
} catch (err) {
_didIteratorError2 = true;
_iteratorError2 = err;
} finally {
try {
if (!_iteratorNormalCompletion2 && _iterator2.return) {
_iterator2.return();
}
} finally {
if (_didIteratorError2) {
throw _iteratorError2;
}
}
}
return result;
}
return _ref = {}, _defineProperty(_ref, Symbol.iterator, function () {
return this;
}), _defineProperty(_ref, 'next', next), _defineProperty(_ref, 'map', map), _defineProperty(_ref, 'forEach', forEach), _ref;
}
}]);
return PropsClass;
})();
var Indexed = (function () {
function Indexed(arr, indexes, initializer, factory) {
_classCallCheck(this, Indexed);
this.__indexedProps = Props(arr, indexes, initializer, factory || IndexedFactory);
if (shouldReindex(this.__indexedProps, arr, indexes)) {
this.reindex();
}
}
_createClass(Indexed, [{
key: 'chain',
value: function chain() {
this.__indexedProps.chain();
return this;
}
}, {
key: 'value',
value: function value() {
return this.__indexedProps.value();
}
}, {
key: 'mutate',
value: function mutate(_mutate) {
if (arguments.length) {
this.__indexedProps.doMutate(_mutate);
return this;
}
return this.__indexedProps.mutate;
}
}, {
key: 'toJson',
value: function toJson() {
return this.__indexedProps.toJson();
}
}, {
key: 'initializer',
value: function initializer(_initializer) {
if (arguments.length) {
if (typeof _initializer !== 'function') {
throw new Error('initializer must be a function');
}
this.__indexedProps.initializer = _initializer;
return this;
}
return this.__indexedProps.initializer;
}
}, {
key: 'size',
value: function size() {
return this.__indexedProps.arr.length;
}
}, {
key: 'getIterator',
value: function getIterator(key) {
return this.__indexedProps.getIterator(key);
}
}, {
key: 'lastIndexes',
value: function lastIndexes() {
return this.__indexedProps.lastIndexes;
}
}]);
return Indexed;
})();
exports.Indexed = Indexed;
Indexed.factory = IndexedFactory;
_constants.regularMethods.forEach(function (name) {
if (!(name in _constants.arrProto)) {
return;
}
Indexed.prototype[name] = function () {
var arr = this.__indexedProps.arr;
for (var _len = arguments.length, args = Array(_len), _key3 = 0; _key3 < _len; _key3++) {
args[_key3] = arguments[_key3];
}
return _constants.arrProto[name].apply(arr, args);
};
Indexed.prototype[name].nativeArrayMethod = true;
});
_methods.indexesMethodsKeys.forEach(function (name) {
Indexed.prototype[name] = function () {
var _indexedProps = this.__indexedProps;
var indexes = _indexedProps.indexes;
var arr = _indexedProps.arr;
for (var _len2 = arguments.length, args = Array(_len2), _key4 = 0; _key4 < _len2; _key4++) {
args[_key4] = arguments[_key4];
}
return _methods.indexesMethods[name].apply(_methods.indexesMethods, [indexes, arr].concat(args));
};
});
_methods.immutableMethodsKeys.forEach(function (name) {
Indexed.prototype[name] = function () {
var arr = this.__indexedProps.arr;
for (var _len3 = arguments.length, args = Array(_len3), _key5 = 0; _key5 < _len3; _key5++) {
args[_key5] = arguments[_key5];
}
return _methods.immutableMethods[name].apply(_methods.immutableMethods, [this.__indexedProps, arr].concat(args));
};
});
_methods.mutableMethodsKeys.forEach(function (name) {
Indexed.prototype[name] = function () {
var props = this.__indexedProps;
var arr = props.arr;
var factory = props.factory;
var initializer = props.initializer;
var mutate = props.mutate;
for (var _len4 = arguments.length, args = Array(_len4), _key6 = 0; _key6 < _len4; _key6++) {
args[_key6] = arguments[_key6];
}
var _ref2 = initializer && name in _initializersMethods2.default ? _initializersMethods2.default[name].apply(_initializersMethods2.default, [initializer, props, arr].concat(args)) : _methods.mutableMethods[name].apply(_methods.mutableMethods, [props, arr].concat(args));
var _ref3 = _slicedToArray(_ref2, 2);
var newIndexes = _ref3[0];
var newArr = _ref3[1];
if (mutate) {
this.__indexedProps.indexes = newIndexes;
this.__indexedProps.arr = newArr;
return this;
}
var ret = factory(newArr, newIndexes, initializer, factory);
if (props.lastIndexes.length) {
ret.__indexedProps.lastIndexes = props.lastIndexes;
}
return ret;
};
});
var methods = exports.methods = Object.getOwnPropertyNames(Indexed.prototype).filter(function (name) {
return name !== 'constructor';
});