tlojs
Version:
The Last One - The last npm package you'll need to install
45 lines (44 loc) • 1.16 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var EMPTY_ARRAY = 'The array is empty';
Array.prototype.select = function (callback) {
return this.map(callback);
};
Array.prototype.min = function () {
return Math.min.apply(Math, this);
};
Array.prototype.max = function () {
return Math.max.apply(Math, this);
};
Array.prototype.where = function (callback) {
return this.filter(callback);
};
Array.prototype.distinct = function () {
return this.filter(function (value, index, self) { return self.indexOf(value) === index; });
};
Array.prototype.first = function () {
var first = this.firstOrDefault();
if (!first) {
throw new Error(EMPTY_ARRAY);
}
return first;
};
Array.prototype.firstOrDefault = function () {
if (this.length > 0) {
return this[0];
}
return undefined;
};
Array.prototype.last = function () {
var last = this.lastOrDefault();
if (!last) {
throw new Error(EMPTY_ARRAY);
}
return last;
};
Array.prototype.lastOrDefault = function () {
if (this.length > 0) {
return this[this.length - 1];
}
return undefined;
};