|
@ -16,9 +16,9 @@ const { always, isNonZeroFalsy, stringify, areNumbers } = require('./utils'); |
|
|
class InfiniteListItem { |
|
|
class InfiniteListItem { |
|
|
/** |
|
|
/** |
|
|
* Creates an instance of InfiniteListItem. |
|
|
* Creates an instance of InfiniteListItem. |
|
|
* @param {any} list Parent list, instance of InfiniteList |
|
|
* @param {*} list Parent list, instance of InfiniteList |
|
|
* @param {any} value Current value |
|
|
* @param {Number} value Current value |
|
|
* @param {any} index Current index |
|
|
* @param {Number} index Current index |
|
|
* @memberof InfiniteListItem |
|
|
* @memberof InfiniteListItem |
|
|
*/ |
|
|
*/ |
|
|
constructor(list, value, index) { |
|
|
constructor(list, value, index) { |
|
@ -31,12 +31,12 @@ class InfiniteListItem { |
|
|
if (typeof Symbol !== 'undefined' && Symbol.iterator) { |
|
|
if (typeof Symbol !== 'undefined' && Symbol.iterator) { |
|
|
/** |
|
|
/** |
|
|
* ES6 Symbol.iterator |
|
|
* ES6 Symbol.iterator |
|
|
* @returns {Iterable.<*>} |
|
|
* @returns {Iterable.<InfiniteListItem>} |
|
|
*/ |
|
|
*/ |
|
|
this[Symbol.iterator] = () => ({ |
|
|
this[Symbol.iterator] = () => ({ |
|
|
next: () => ({ |
|
|
next: () => ({ |
|
|
done: false, |
|
|
value: list.get(index + 1), |
|
|
value: list.get(index + 1) |
|
|
done: false |
|
|
}) |
|
|
}) |
|
|
}); |
|
|
}); |
|
|
} |
|
|
} |
|
@ -45,6 +45,7 @@ class InfiniteListItem { |
|
|
/** |
|
|
/** |
|
|
* toString method for pretty printing InfiniteListItem instance. |
|
|
* toString method for pretty printing InfiniteListItem instance. |
|
|
* @returns {String} Decycled and beautified string |
|
|
* @returns {String} Decycled and beautified string |
|
|
|
|
|
* @memberof InfiniteListItem |
|
|
*/ |
|
|
*/ |
|
|
toString() { |
|
|
toString() { |
|
|
return ('InfiniteListItem [ .. ' + |
|
|
return ('InfiniteListItem [ .. ' + |
|
@ -62,6 +63,7 @@ class InfiniteList { |
|
|
* @param {*} start Starting value |
|
|
* @param {*} start Starting value |
|
|
* @param {Function} next Function to find next item |
|
|
* @param {Function} next Function to find next item |
|
|
* Accepts current value and optionally previous value |
|
|
* Accepts current value and optionally previous value |
|
|
|
|
|
* @memberof InfiniteList |
|
|
* @constructs InfiniteList |
|
|
* @constructs InfiniteList |
|
|
*/ |
|
|
*/ |
|
|
constructor(start, next) { |
|
|
constructor(start, next) { |
|
@ -74,6 +76,7 @@ class InfiniteList { |
|
|
* Get InfiniteListItem at index. |
|
|
* Get InfiniteListItem at index. |
|
|
* @param {Number} index A non-negative integer representing index |
|
|
* @param {Number} index A non-negative integer representing index |
|
|
* @returns {InfiniteListItem} |
|
|
* @returns {InfiniteListItem} |
|
|
|
|
|
* @memberof InfiniteList |
|
|
*/ |
|
|
*/ |
|
|
this.get = function (index) { |
|
|
this.get = function (index) { |
|
|
|
|
|
|
|
@ -99,7 +102,10 @@ class InfiniteList { |
|
|
if (!(index in cache)) { |
|
|
if (!(index in cache)) { |
|
|
if (cache.length <= index && (cache.length - 1) in cache) |
|
|
if (cache.length <= index && (cache.length - 1) in cache) |
|
|
while (cache.length <= index) |
|
|
while (cache.length <= index) |
|
|
cache[cache.length] = next(cache[cache.length - 1], cache[cache.length - 2]); |
|
|
cache[cache.length] = next( |
|
|
|
|
|
cache[cache.length - 1], |
|
|
|
|
|
cache[cache.length - 2] |
|
|
|
|
|
); |
|
|
} |
|
|
} |
|
|
return new InfiniteListItem(this, cache[index], index); |
|
|
return new InfiniteListItem(this, cache[index], index); |
|
|
} |
|
|
} |
|
@ -109,6 +115,7 @@ class InfiniteList { |
|
|
* Forces destroy reference to cache, and creates a new cache. |
|
|
* Forces destroy reference to cache, and creates a new cache. |
|
|
* Old cache will be GC'd. |
|
|
* Old cache will be GC'd. |
|
|
* @returns {undefined} |
|
|
* @returns {undefined} |
|
|
|
|
|
* @memberof InfiniteList |
|
|
*/ |
|
|
*/ |
|
|
this.clearCache = () => (cache = [], undefined); |
|
|
this.clearCache = () => (cache = [], undefined); |
|
|
|
|
|
|
|
@ -116,13 +123,14 @@ class InfiniteList { |
|
|
if (typeof Symbol !== 'undefined' && Symbol.iterator) { |
|
|
if (typeof Symbol !== 'undefined' && Symbol.iterator) { |
|
|
/** |
|
|
/** |
|
|
* ES6 Symbol.iterator |
|
|
* ES6 Symbol.iterator |
|
|
* @returns {Iterable.<*>} |
|
|
* @returns {Iterable.<InfiniteListItem>} |
|
|
|
|
|
* @memberof InfiniteList |
|
|
*/ |
|
|
*/ |
|
|
this[Symbol.iterator] = function () { |
|
|
this[Symbol.iterator] = function () { |
|
|
return { |
|
|
return { |
|
|
next: () => ({ |
|
|
next: () => ({ |
|
|
done: false, |
|
|
value: this.get(j++), |
|
|
value: this.get(j++) |
|
|
done: false |
|
|
}) |
|
|
}) |
|
|
}; |
|
|
}; |
|
|
}; |
|
|
}; |
|
@ -159,6 +167,7 @@ class InfiniteList { |
|
|
* @param {Number} from Number of elements or starting index |
|
|
* @param {Number} from Number of elements or starting index |
|
|
* @param {Number} to Optional ending index |
|
|
* @param {Number} to Optional ending index |
|
|
* @returns {Array<InfiniteListItem>} An array of InfiniteListItems |
|
|
* @returns {Array<InfiniteListItem>} An array of InfiniteListItems |
|
|
|
|
|
* @memberof InfiniteList |
|
|
*/ |
|
|
*/ |
|
|
InfiniteList.prototype.take = function (from, to) { |
|
|
InfiniteList.prototype.take = function (from, to) { |
|
|
const arr = []; |
|
|
const arr = []; |
|
@ -189,6 +198,7 @@ InfiniteList.prototype.take = function (from, to) { |
|
|
/** |
|
|
/** |
|
|
* Returns first element of InfiniteList. |
|
|
* Returns first element of InfiniteList. |
|
|
* @returns {InfiniteListItem} Instance of InfiniteListItem |
|
|
* @returns {InfiniteListItem} Instance of InfiniteListItem |
|
|
|
|
|
* @memberof InfiniteList |
|
|
*/ |
|
|
*/ |
|
|
InfiniteList.prototype.top = function () { |
|
|
InfiniteList.prototype.top = function () { |
|
|
return this.get(0); |
|
|
return this.get(0); |
|
@ -197,6 +207,7 @@ InfiniteList.prototype.top = function () { |
|
|
/** |
|
|
/** |
|
|
* Returns last element of InfiniteList (Infinity). |
|
|
* Returns last element of InfiniteList (Infinity). |
|
|
* @returns {InfiniteListItem} Instance of InfiniteListItem |
|
|
* @returns {InfiniteListItem} Instance of InfiniteListItem |
|
|
|
|
|
* @memberof InfiniteList |
|
|
*/ |
|
|
*/ |
|
|
InfiniteList.prototype.end = function () { |
|
|
InfiniteList.prototype.end = function () { |
|
|
return this.get(Infinity); |
|
|
return this.get(Infinity); |
|
@ -206,6 +217,7 @@ InfiniteList.prototype.end = function () { |
|
|
* toString method for pretty printing InfiniteList instance. |
|
|
* toString method for pretty printing InfiniteList instance. |
|
|
* Snips at 2 elements for arrays and objects, or 5 elements otherwise. |
|
|
* Snips at 2 elements for arrays and objects, or 5 elements otherwise. |
|
|
* @returns {String} Pretty printed InfiniteList |
|
|
* @returns {String} Pretty printed InfiniteList |
|
|
|
|
|
* @memberof InfiniteList |
|
|
*/ |
|
|
*/ |
|
|
InfiniteList.prototype.toString = function () { |
|
|
InfiniteList.prototype.toString = function () { |
|
|
const length = typeof this.first() === 'object' ? 2 : 5; |
|
|
const length = typeof this.first() === 'object' ? 2 : 5; |
|
|