Browse Source

[merge] remote master

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

10
README.md

@ -2,7 +2,9 @@
## Infinity ## 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 ## Installation
@ -15,7 +17,7 @@ npm install --save @codefeathers/infinity
In the browser: In the browser:
```HTML ```HTML
<script src="https://unpkg.com/@codefeathers/iseq"> <script src="https://unpkg.com/@codefeathers/infinity">
``` ```
## Usage ## Usage
@ -26,7 +28,7 @@ const infinity = new InfiniteList(<start>, <next>);
// Gets item at index // Gets item at index
infinity.get(<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>); infinity.take(<number>);
// Returns array of InfiniteListItems from index startIndex to endIndex // 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 log(fibonacci.get(50).next(5).value); // -> 139583862445
// To Infinity and beyond! // 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 { 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;
@ -226,4 +238,4 @@ InfiniteList.prototype.last = InfiniteList.prototype.end;
// Exports // Exports
module.exports = InfiniteList; module.exports = InfiniteList;
module.exports.InfiniteListItem = InfiniteListItem; module.exports.InfiniteListItem = InfiniteListItem;

Loading…
Cancel
Save