UNPKG

additional_hof

Version:
70 lines (65 loc) 1.87 kB
const addHOF = () => { // ! 1 Array.prototype.addAllElements = function () { return this.reduce((acc, curr) => acc + curr, 0); }; // ! 2 Array.prototype.multiplyAllElements = function () { return this.reduce((acc, curr) => acc * curr, 1); }; // ! 3 Array.prototype.calculate = function () { return this.map((exp) => eval(String(exp))); }; // ! 4 Array.prototype.logAll = function (el, i, arr) { if (el && i && arr) { this.forEach((el, i, arr) => { console.log(el, i, arr); }); } else if (el && i) { this.forEach((el, i) => { console.log(el, i); }); } else { this.forEach((el) => { console.log(el); }); } }; // ! 5 Array.prototype.joinAndCapitalize = function (seperator) { return this.join(seperator || "").toUpperCase(); }; // ! 6 Array.prototype.joinLowerCase = function (seperator) { return this.join(seperator || "").toLowerCase(); }; // ! 7 Array.prototype.joinSnakeCase = function () { return this.reduce((acc, curr) => { String(prev).toLocaleLowerCase() + "_" + String(curr).toLocaleLowerCase(); }, ""); }; // ! 8 Array.prototype.joinKababCase = function () { return this.reduce((acc, curr) => { String(prev).toLocaleLowerCase() + "-" + String(curr).toLocaleLowerCase(); }, ""); }; // ! 9 Array.prototype.joinCamelCase = function () { return this.reduce((acc, curr, i) => { if (i === 0) { return acc + String(curr).toLocaleLowerCase(); } else { return acc + String(curr)[0].toUpperCase() + String(curr).slice(1); } }, ""); }; // ! 10 Array.prototype.precizeValuesBy = function (num) { return this.map((el) => el.toFixed(num)); }; }; export default addHOF;