From 737661c915e10655b68e741c0da0be78a783517a Mon Sep 17 00:00:00 2001 From: Muthu Kumar Date: Wed, 16 May 2018 09:04:43 +0530 Subject: [PATCH] [refactor] Extracted InfiniteListItem to class --- index.js | 66 ++++++++++++++++++++++++++++-------------------------------- spec/spec.js | 21 +++++++++++++------ 2 files changed, 46 insertions(+), 41 deletions(-) diff --git a/index.js b/index.js index 6d8dd4c..cecb04f 100644 --- a/index.js +++ b/index.js @@ -1,12 +1,35 @@ const always = x => _ => x; +class InfiniteListItem { + constructor (list, value, index) { + this.value = value, + this.index = index, + this.next = () => list.get(index + 1), + this.previous = () => list.get(index - 1), + this[Symbol.iterator] = () => ({ + next: () => ({ + done: false, + value: list.get(index + 1) + }) + }), + this.toString = () => { + const val = list.get(i).value; + return ( 'InfiniteListItem [ ..., ' + + ((typeof val === 'object' && val !== null) + ? JSON.stringify(val, null, 2) + : val.toString()) + + ', ... ]' ) + } + } +} + const infiniteList = { /** * InfiniteList Constructor. Iterates infinitely until index value is found. * * @param {any} start * @param {any} next - * @returns InfiniteLinkedListItem object + * @returns InfiniteList instance */ create (start, next) { @@ -32,39 +55,11 @@ const infiniteList = { // Initializing first item if it doesn't exist if(!cache[0]) cache[0] = start; - // Create returnable object - const obj = { - next: () => this.get(i+1), - previous: () => this.get(i - 1), - [Symbol.iterator]: () => ({ - next: () => ({ - done: false, - value: this.get(i+1) - }) - }), - toString: () => { - const val = get(i).value; - return ( 'InfiniteLinkedListItem [ ..., ' - + ((typeof val === 'object' && val !== null) - ? JSON.stringify(val, null, 2) - : val.toString()) - + ', ... ]' ) - } - } - // If index were to be infinity, value and index are infinity - if(i === Infinity) { - obj.value = Infinity; - obj.index = Infinity; - return obj; - } + if(i === Infinity) return new InfiniteListItem(this, Infinity, Infinity) // If index exists in cache, return the value - if(i in cache) { - obj.value = cache[i]; - obj.index = i; - return obj; - } + if(i in cache) return new InfiniteListItem(this, cache[i], i); // If i doesn't exist in cache if(!(i in cache)) { @@ -72,9 +67,7 @@ const infiniteList = { while (cache.length <= i) cache[cache.length] = next(cache[cache.length - 1]); } - obj.value = cache[i]; - obj.index = i; - return obj; + return new InfiniteListItem(this, cache[i], i); } const take = (from, to) => { @@ -109,10 +102,13 @@ const infiniteList = { value: get(j++) }) }), - toString: () => 'InfiniteLinkedList [ ' + take(0, 10).map(x => (' ' + x.value.toString())) + ' ... ]' + toString: () => 'InfiniteList [ ' + + take(0, 10).map(x => (' ' + x.value.toString())) + + ' ... ]' }; return returns; } } module.exports = infiniteList; +module.exports.InfiniteListItem = InfiniteListItem; diff --git a/spec/spec.js b/spec/spec.js index ec68752..65dbad0 100644 --- a/spec/spec.js +++ b/spec/spec.js @@ -1,32 +1,41 @@ 'use strict'; -const infiniteList = require('../index'); +const InfiniteList = require('..'); +const { InfiniteListItem } = require('..'); /* global describe it expect */ describe("InfiniteList", () => { + it("Should be true", () => { + const infinite = InfiniteList.create(0, x => x + 2); + expect(infinite.get(5) instanceof InfiniteListItem).toBe(true); + }) + it("Should be 0", () => { - const infinite = infiniteList.create(0, x => x + 2); + const infinite = InfiniteList.create(0, x => x + 2); expect(infinite.first().value).toBe(0); + expect(infinite.top().value).toBe(0); + expect(infinite.last().value).toBe(Infinity); + expect(infinite.end().value).toBe(Infinity); }) it("Should be 22", () => { - const infinite = infiniteList.create(0, x => x + 2); + const infinite = InfiniteList.create(0, x => x + 2); expect(infinite.get(11).value).toBe(22); }) it("Should be [ 0, 2, 4, 6, 8 ]", () => { - const infinite = infiniteList.create(0, x => x + 2); + const infinite = InfiniteList.create(0, x => x + 2); expect(infinite.take(5).map(x => x.value)).toEqual([ 0, 2, 4, 6, 8 ]); }) it("Should be [ 16, 18, 20 ]", () => { - const infinite = infiniteList.create(0, x => x + 2); + const infinite = InfiniteList.create(0, x => x + 2); expect(infinite.take(8, 10).map(x => x.value)).toEqual([ 16, 18, 20 ]); }) it("Should be [ 0, 2, 4, 6, 8 ]", () => { - const infinite = infiniteList.create(0, x => x + 2); + const infinite = InfiniteList.create(0, x => x + 2); const acc = []; for(let i of infinite) { if(i.index >= 5) break;