Skip to content
Snippets Groups Projects
lib.js 3.86 KiB
Newer Older
clemo's avatar
clemo committed
import debug from 'debug';
import Math from './math.js';
clemo's avatar
clemo committed
const log = debug('helper:lib:info');
const error = debug('helper:lib:error');
clemo's avatar
clemo committed
/** Lib*/
clemo's avatar
clemo committed
export class Lib {
  constructor() {
    this.items = [];
    this._hooks = {};
clemo's avatar
clemo committed
    this.lsdCountner = [ [] ]
clemo's avatar
clemo committed
  }
clemo's avatar
clemo committed
  /** returns a sorted array, alias for `lib.bubbleSort`*/
  sortBy(...args) { return this.sortByBubble(...args); }
clemo's avatar
clemo committed
  /** returns sorted array
  *@param
  * {function} smallerThen - callback called with 2 args `(val1,val2) => {return
  *(val1.nr < val2.nr);}`
clemo's avatar
clemo committed
  * @param {array} items - unsorted array
clemo's avatar
clemo committed
  * @return {array} - sorted array
  */
clemo's avatar
clemo committed
  sortByBubble(smallerThen, items = this.getArray()) {
clemo's avatar
clemo committed
    let sorted = false;
    do {
      sorted = false;
clemo's avatar
clemo committed
      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;
clemo's avatar
clemo committed
          sorted = true;
        }
      }
    } while (sorted);
clemo's avatar
clemo committed
    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;
clemo's avatar
clemo committed
  }
  /** get by id
  *@param {String} id - id of item
  *@return {object} item - the selected **item** or `undefined`
  */

clemo's avatar
clemo committed
  getById(id) { return this.getArray().filter((item) => (item.id === id))[0]; }
clemo's avatar
clemo committed
  /** get an array with all items
  @return {array} items - all visible **items**
  */
clemo's avatar
clemo committed
  getArray() { return this.items.filter((item) => (!item._remove)); }
clemo's avatar
clemo committed
  /** add an item to the list
  * @param {object} item - item to add to list
  * @param {boolean} overwrite - overwrite or not? *will not throw*
  */
clemo's avatar
clemo committed
  add(item, overwrite = false) {
clemo's avatar
clemo committed
    log("add item", item);
    if ("function" === typeof this._hooks['beforeAdd']) {
      item = this._hooks['beforeAdd'](item);
    }
clemo's avatar
clemo committed
    if ('undefined' === typeof item.id) {
clemo's avatar
clemo committed
      item.id = new Math().uuid();
    }
clemo's avatar
clemo committed
    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;
    }
clemo's avatar
clemo committed
  }
clemo's avatar
clemo committed
  /** set a hook
  *@event beforeAdd
  */
clemo's avatar
clemo committed
  setHook(type, fn) {
    log('setHook', type, fn);
    this._hooks[type] = fn;
  }
clemo's avatar
clemo committed
  /** mark a item as removed  -> will not be in getArray but getArrayById
  * @param {String} id - id of item to remove
clemo's avatar
clemo committed
  * @return {boolean} deleted - deleted or not found in undeleted objs
clemo's avatar
clemo committed
  */
clemo's avatar
clemo committed
  remove(id) {
    log('remove item', id);
clemo's avatar
clemo committed
    const item = this.getById(id);
    if('undefined' !== typeof item){
        this.getById(id)._remove = true;
        return true;
    }else{
      return false;
    }

clemo's avatar
clemo committed
  }
clemo's avatar
clemo committed
  /** cb fired on every item from `this.getArray`
  *@param {function} cb - `(item)=>{console.log(item)};`
  */
clemo's avatar
clemo committed
  each(cb) {
    let items = this.getArray();
    for (let i = items.length; i >= 0; i--) {
      if (items[i] && items[i].id !== null) {
        cb(items[i]);
      }
    }
  }
}
export default Lib;