Newer
Older
const log = debug('helper:lib:info');
const error = debug('helper:lib:error');
export class Lib {
constructor() {
this.items = [];
this._hooks = {};
/** returns a sorted array, alias for `lib.bubbleSort`*/
sortBy(...args) { return this.sortByBubble(...args); }
/** returns sorted array
*@param
* {function} smallerThen - callback called with 2 args `(val1,val2) => {return
*(val1.nr < val2.nr);}`
for (let i = 1; i < items.length; i++) {
if (smallerThen(items[i], items[i - 1])) {
let tmp = items[i];
items[i] = items[i - 1];
items[i - 1] = tmp;
return items;
}
/** LSD sort an array*
* @param {key} key - value.key - must be numeric
* @param {array} items - option, unsorted array
* @param {integer} maxDigitSymbols - max length of int
* @return {array} items - sorted array
*/
sortByLsd(key, items = this.getArray(), maxDigitSymbols) {
var mod = 10;
var dev = 1;
for (var i = 0; i < maxDigitSymbols; i++, dev *= 10, mod *= 10) {
for (var j = 0; j < items.length; j++) {
var bucket = parseInt((items[j][key] % mod) / dev);
if (this.lsdCountner[bucket] == null) {
this.lsdCountner[bucket] = [];
}
this.lsdCountner[bucket].push(items[j][key]);
}
var pos = 0;
for (var j = 0; j < this.lsdCountner.length; j++) {
var value = null;
if (this.lsdCountner[j] != null) {
while ((value = this.lsdCountner[j].shift()) != null) {
items[pos++][key] = value;
}
}
}
}
return items;
}
/** get by id
*@param {String} id - id of item
*@return {object} item - the selected **item** or `undefined`
*/
getById(id) { return this.getArray().filter((item) => (item.id === id))[0]; }
/** get an array with all items
@return {array} items - all visible **items**
*/
/** add an item to the list
* @param {object} item - item to add to list
* @param {boolean} overwrite - overwrite or not? *will not throw*
*/
log("add item", item);
if ("function" === typeof this._hooks['beforeAdd']) {
item = this._hooks['beforeAdd'](item);
}
let existingItem = this.getById(item.id);
if ("undefined" !== typeof existingItem) {
if (overwrite) {
existingItem = item;
return existingItem;
} else {
return log('add duplicate',
'item with this id already exist overwrite != true');
}
} else {
this.items.push(item);
return item;
}
setHook(type, fn) {
log('setHook', type, fn);
this._hooks[type] = fn;
}
/** mark a item as removed -> will not be in getArray but getArrayById
* @param {String} id - id of item to remove
* @return {boolean} deleted - deleted or not found in undeleted objs
const item = this.getById(id);
if('undefined' !== typeof item){
this.getById(id)._remove = true;
return true;
}else{
return false;
}
/** cb fired on every item from `this.getArray`
*@param {function} cb - `(item)=>{console.log(item)};`
*/