UNPKG

for-ease

Version:

a simple library for itrating over anything

81 lines (80 loc) 3.24 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const doFactory_class_1 = require("../doFactory/doFactory.class"); const returnsOfCounterClass_1 = require("../returnsOfCounterClass"); class TillValueClass { constructor(mixed) { this.mixed = mixed; } getTills() { return { tillValue: (condition) => { let condtionedArrayOrObj = this.mixed; let counterForThisType = (new doFactory_class_1.DoFactory(this.mixed)).getCounter(); // console.log(counterForThisType); counterForThisType.do((key, val) => { const conditionResult = this.parseConditionOnThisVal(condition, val); if (!Boolean(conditionResult)) delete condtionedArrayOrObj[key]; }); if (Array.isArray(condtionedArrayOrObj)) condtionedArrayOrObj = removeUndefinedsFromArray(condtionedArrayOrObj); let returns = (new returnsOfCounterClass_1.ReturnsOfCounter(condtionedArrayOrObj)).getReturns(); let counter = (new doFactory_class_1.DoFactory(condtionedArrayOrObj)).getCounter(); return Object.assign({}, returns, counter); } }; } parseConditionOnThisVal(condition, onWhat) { let cond, value, keyInObj = ''; [cond, value] = condition.trim().split(' '); if (typeof this.mixed[0] === 'object') [keyInObj, cond, value] = condition.trim().split(' '); let val = parseFloat(value); if (isNaN(val)) { val = value; } switch (cond) { case "<": if ((onWhat[keyInObj] || onWhat) < val) return true; else return false; case ">": if ((onWhat[keyInObj] || onWhat) > val) return true; else return false; case "<=": if ((onWhat[keyInObj] || onWhat) <= val) return true; else return false; case ">=": if ((onWhat[keyInObj] || onWhat) >= val) return true; else return false; case "=": // if (typeof (onWhat[keyInObj] || onWhat) === 'string') val = val + ''; if ((onWhat[keyInObj] || onWhat) === val) return true; else return false; default: throw new Error(`the condtion you passed is incorrect \n * if this is an object the first thing in condition string must be the desured field in object *`); } } } exports.TillValueClass = TillValueClass; function removeUndefinedsFromArray(array) { let length = array.length; let resultArr = []; for (let i = 0; i < length; i++) { let currentElem = array[i]; if (currentElem !== undefined) resultArr.push(currentElem); } return resultArr; }