summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/extensions/array.js.es6
blob: 717566a47153a9797399497332154f04c71cb1cb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/* eslint-disable no-extend-native, func-names, space-before-function-paren, semi, space-infix-ops, max-len */
Array.prototype.first = function() {
  return this[0];
}

Array.prototype.last = function() {
  return this[this.length-1];
}

Array.prototype.find = Array.prototype.find || function(predicate, ...args) {
  if (!this) throw new TypeError('Array.prototype.find called on null or undefined');
  if (typeof predicate !== 'function') throw new TypeError('predicate must be a function');

  const list = Object(this);
  const thisArg = args[1];
  let value = {};

  for (let i = 0; i < list.length; i += 1) {
    value = list[i];
    if (predicate.call(thisArg, value, i, list)) return value;
  }

  return undefined;
};