SerovMihail
8/14/2017 - 8:26 AM

array.js extension

array.js extension

(function () {
    if (!Array.prototype.first) {
        Object.defineProperty(Array.prototype, "first", {
            enumerable: false,
            configurable: true,
            writable: true,
            value: function (func) {
                if (!func) return this[0];
                var elem = null;

                for (var i = 0; i < this.length; i++) {
                    var bool = func(this[i]);
                    if (!bool) continue;
                    elem = this[i];
                    break;
                }
                return elem;
            }
        });
    }
    if (!Array.prototype.firstIndex) {
        Object.defineProperty(Array.prototype, "firstIndex", {
            enumerable: false,
            configurable: true,
            writable: true,
            value: function (func) {
                var index = -1;
                if (typeof func != "function") return index;
                for (var i = 0; i < this.length; i++) {
                    var bool = func(this[i]);
                    if (bool) {
                        index = i;
                        break;
                    }
                }
                return index;
            }
        });
    }
    if (!Array.prototype.remove) {
        Object.defineProperty(Array.prototype, "remove", {
            enumerable: false,
            configurable: true,
            writable: true,
            value: function (elem) {
                var index = typeof elem == "function" ? this.firstIndex(elem) : this.indexOf(elem);
                if (index == -1) return;
                this.splice(index, 1);
            }
        });
    }
    if (!Array.prototype.any) {
        Object.defineProperty(Array.prototype, "any", {
            enumerable: false,
            configurable: true,
            writable: true,
            value: function (func) {
                if (!func)
                    return this.length > 0;

                return this.first(func) != null;
            }
        });
    }


    if (!Array.prototype.sum) {
        Object.defineProperty(Array.prototype, "sum", {
            enumerable: false,
            configurable: true,
            writable: true,
            value: function (func) {
                if (!func || typeof func != "function")
                    return 0;

                var sum = 0;

                this.map(func).forEach(function (el) {
                    sum += el;
                });

                return sum;
            }
        });
    }

    if (!Array.prototype.selectMany) {
        Object.defineProperty(Array.prototype, 'selectMany', {
            enumerable: false,
            configurable: true,
            writable: true,
            value: function (columnName) {
                if (!columnName || typeof columnName != "string")
                    return this;

                var newArray = [];

                this.forEach(function (el) {
                    var arr = el[columnName];

                    if (!arr || !Array.isArray(arr))
                        return;

                    arr.forEach(function (a) {
                        newArray.push(a);
                    });
                });

                return newArray;
            }
        });
    }

    if (!Array.prototype.clear) {
        Object.defineProperty(Array.prototype, 'clear', {
            enumerable: false,
            configurable: true,
            writable: true,
            value: function () {
                for (var i = this.length; i > -1; i++) {
                    this.remove(this[i]);
                }
            }
        });
    }

    if (!Array.prototype.pushRange) {
        Object.defineProperty(Array.prototype, 'pushRange', {
            enumerable: false,
            configurable: true,
            writable: true,
            value: function (range) {
                if (!Array.isArray(range))
                    return;

                for (var i = 0; i < range.length; i++) {
                    this.push(range[i]);
                }
            }
        });
    }

    if (!Array.prototype.max) {
        Object.defineProperty(Array.prototype, 'max', {
            enumerable: false,
            configurable: true,
            writable: true,
            value: function (func) {
                if (this.length == 0)
                    return 0;

                var sortFunc = function (a, b) {
                    if (a < b) return 1;
                    if (a > b) return -1;
                    return 0;
                }

                if (!func)
                    return this.sort(sortFunc)[0];

                return this.map(func).sort(sortFunc)[0];
            }
        });
    }

})(Array);