Browse Source

[refactor] Split into class per module

master
Muthu Kumar 6 years ago
parent
commit
02cf05fe0e
  1. 3
      es6/index.js
  2. 52
      src/InfiniteList.js
  3. 50
      src/InfiniteListItem.js

3
es6/index.js

@ -1 +1,2 @@
module.exports = require('../index'); module.exports = require('../src/InfiniteList');
module.exports.InfiniteListItem = require('../src/InfiniteListItem');

52
index.js → src/InfiniteList.js

@ -6,54 +6,8 @@
*/ */
// Utils // Utils
const { always, isNonZeroFalsy, stringify, areNumbers } = require('./utils'); const { always, isNonZeroFalsy, stringify, areNumbers } = require('../utils');
const InfiniteListItem = require('./InfiniteListItem');
/**
* An item of the InfiniteList class. Created when calling .get(n) on an InfiniteList.
* Exposed for instanceof utility sake. Not to be called directly.
* @class InfiniteListItem
*/
class InfiniteListItem {
/**
* Creates an instance of InfiniteListItem.
* @param {*} list Parent list, instance of InfiniteList
* @param {Number} value Current value
* @param {Number} index Current index
* @memberof InfiniteListItem
*/
constructor(list, value, index) {
this.__list__ = list;
this.value = value;
this.index = index;
this.next = z => (!z ? this.__list__.get(index + 1) : this.__list__.get(index + z));
this.previous = z => (!z ? this.__list__.get(index - 1) : this.__list__.get(index - z));
}
}
// Check if environment supports Symbol
if (typeof Symbol !== 'undefined' && Symbol.iterator) {
/**
* ES6 Symbol.iterator
* @returns {Iterable.<InfiniteListItem>}
*/
InfiniteListItem.prototype[Symbol.iterator] = () => ({
next: () => ({
value: this.__list__.get(index + 1),
done: false
})
});
}
/**
* toString method for pretty printing InfiniteListItem instance.
* @returns {String} Decycled and beautified string
* @memberof InfiniteListItem
*/
InfiniteListItem.prototype.toString = function () {
return ('InfiniteListItem [ .. ' +
stringify(this.value) +
' .. ]');
};
class InfiniteList { class InfiniteList {
/** /**
@ -69,6 +23,7 @@ class InfiniteList {
*/ */
constructor(start, next) { constructor(start, next) {
/* Private properties */
this.__start__ = start; this.__start__ = start;
this.__next__ = next; this.__next__ = next;
this.__cache__ = []; this.__cache__ = [];
@ -246,4 +201,3 @@ InfiniteList.prototype.last = InfiniteList.prototype.end;
// Exports // Exports
module.exports = InfiniteList; module.exports = InfiniteList;
module.exports.InfiniteListItem = InfiniteListItem;

50
src/InfiniteListItem.js

@ -0,0 +1,50 @@
const { stringify } = require('../utils');
/**
* An item of the InfiniteList class. Created when calling .get(n) on an InfiniteList.
* Exposed for instanceof utility sake. Not to be called directly.
* @class InfiniteListItem
*/
class InfiniteListItem {
/**
* Creates an instance of InfiniteListItem.
* @param {*} list Parent list, instance of InfiniteList
* @param {Number} value Current value
* @param {Number} index Current index
* @memberof InfiniteListItem
*/
constructor(list, value, index) {
this.__list__ = list;
this.value = value;
this.index = index;
this.next = z => (!z ? this.__list__.get(index + 1) : this.__list__.get(index + z));
this.previous = z => (!z ? this.__list__.get(index - 1) : this.__list__.get(index - z));
}
}
// Check if environment supports Symbol
if (typeof Symbol !== 'undefined' && Symbol.iterator) {
/**
* ES6 Symbol.iterator
* @returns {Iterable.<InfiniteListItem>}
*/
InfiniteListItem.prototype[Symbol.iterator] = () => ({
next: () => ({
value: this.__list__.get(index + 1),
done: false
})
});
}
/**
* toString method for pretty printing InfiniteListItem instance.
* @returns {String} Decycled and beautified string
* @memberof InfiniteListItem
*/
InfiniteListItem.prototype.toString = function () {
return ('InfiniteListItem [ .. ' +
stringify(this.value) +
' .. ]');
};
module.exports = InfiniteListItem;
Loading…
Cancel
Save