for-ease
Version:
a simple library for itrating over anything
77 lines (76 loc) • 2.83 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const doFactory_class_1 = require("../doFactory/doFactory.class");
const returnsOfCounterClass_1 = require("../returnsOfCounterClass");
class TillKeyClass {
constructor(mixed) {
this.mixed = mixed;
}
getTills() {
return {
tillKey: (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, key);
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] = condition.trim().split(' ');
let val = parseFloat(value);
if (isNaN(val)) {
val = value;
}
switch (cond) {
case "<":
if (onWhat < val)
return true;
else
return false;
case ">":
if (onWhat > val)
return true;
else
return false;
case "<=":
if (onWhat <= val)
return true;
else
return false;
case ">=":
if (onWhat >= val)
return true;
else
return false;
case "=":
// if (typeof onWhat === 'string') val = val + '';
if (onWhat === val)
return true;
else
return false;
default:
throw new Error('the condtion you passed is incorrect');
}
}
}
exports.TillKeyClass = TillKeyClass;
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;
}