array-iteration-from-last.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334
  1. var bind = require('../internals/function-bind-context');
  2. var IndexedObject = require('../internals/indexed-object');
  3. var toObject = require('../internals/to-object');
  4. var toLength = require('../internals/to-length');
  5. // `Array.prototype.{ findLast, findLastIndex }` methods implementation
  6. var createMethod = function (TYPE) {
  7. var IS_FIND_INDEX = TYPE == 6;
  8. return function ($this, callbackfn, that) {
  9. var O = toObject($this);
  10. var self = IndexedObject(O);
  11. var boundFunction = bind(callbackfn, that, 3);
  12. var index = toLength(self.length);
  13. var value, result;
  14. while (index-- > 0) {
  15. value = self[index];
  16. result = boundFunction(value, index, O);
  17. if (result) switch (TYPE) {
  18. case 5: return value; // find
  19. case 6: return index; // findIndex
  20. }
  21. }
  22. return IS_FIND_INDEX ? -1 : undefined;
  23. };
  24. };
  25. module.exports = {
  26. // `Array.prototype.findLast` method
  27. // https://github.com/tc39/proposal-array-find-from-last
  28. findLast: createMethod(5),
  29. // `Array.prototype.findLastIndex` method
  30. // https://github.com/tc39/proposal-array-find-from-last
  31. findLastIndex: createMethod(6)
  32. };