Browse Source

[merge] remote master

master
Muthu Kumar 6 years ago
parent
commit
4b7652dc9d
  1. 10
      README.md
  2. 34
      index.js

10
README.md

@ -2,7 +2,9 @@
## Infinity
Create infinite lists in JavaScript.
Create infinite linked lists in JavaScript. Each item is linked to the next and previous item.
This can also be used as an n<sup>th</sup> generator with a pure interface (see [example](#example)).
## Installation
@ -15,7 +17,7 @@ npm install --save @codefeathers/infinity
In the browser:
```HTML
<script src="https://unpkg.com/@codefeathers/iseq">
<script src="https://unpkg.com/@codefeathers/infinity">
```
## Usage
@ -26,7 +28,7 @@ const infinity = new InfiniteList(<start>, <next>);
// Gets item at index
infinity.get(<index>);
// Returns array of InfiniteListItems from index 0 to given number
// Returns array of given number of InfiniteListItems from index 0
infinity.take(<number>);
// Returns array of InfiniteListItems from index startIndex to endIndex
@ -72,5 +74,5 @@ log(fibonacci.get(50).next().value); // -> 20365011074
log(fibonacci.get(50).next(5).value); // -> 139583862445
// To Infinity and beyond!
log(fibonacci.last(50).value); // -> Infinity
log(fibonacci.last().value); // -> Infinity
```

34
index.js

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

Loading…
Cancel
Save