Node-Red configuration
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

array-iteration.js 2.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. 'use strict';
  2. var bind = require('../internals/function-bind-context');
  3. var uncurryThis = require('../internals/function-uncurry-this');
  4. var IndexedObject = require('../internals/indexed-object');
  5. var toObject = require('../internals/to-object');
  6. var lengthOfArrayLike = require('../internals/length-of-array-like');
  7. var arraySpeciesCreate = require('../internals/array-species-create');
  8. var push = uncurryThis([].push);
  9. // `Array.prototype.{ forEach, map, filter, some, every, find, findIndex, filterReject }` methods implementation
  10. var createMethod = function (TYPE) {
  11. var IS_MAP = TYPE === 1;
  12. var IS_FILTER = TYPE === 2;
  13. var IS_SOME = TYPE === 3;
  14. var IS_EVERY = TYPE === 4;
  15. var IS_FIND_INDEX = TYPE === 6;
  16. var IS_FILTER_REJECT = TYPE === 7;
  17. var NO_HOLES = TYPE === 5 || IS_FIND_INDEX;
  18. return function ($this, callbackfn, that, specificCreate) {
  19. var O = toObject($this);
  20. var self = IndexedObject(O);
  21. var length = lengthOfArrayLike(self);
  22. var boundFunction = bind(callbackfn, that);
  23. var index = 0;
  24. var create = specificCreate || arraySpeciesCreate;
  25. var target = IS_MAP ? create($this, length) : IS_FILTER || IS_FILTER_REJECT ? create($this, 0) : undefined;
  26. var value, result;
  27. for (;length > index; index++) if (NO_HOLES || index in self) {
  28. value = self[index];
  29. result = boundFunction(value, index, O);
  30. if (TYPE) {
  31. if (IS_MAP) target[index] = result; // map
  32. else if (result) switch (TYPE) {
  33. case 3: return true; // some
  34. case 5: return value; // find
  35. case 6: return index; // findIndex
  36. case 2: push(target, value); // filter
  37. } else switch (TYPE) {
  38. case 4: return false; // every
  39. case 7: push(target, value); // filterReject
  40. }
  41. }
  42. }
  43. return IS_FIND_INDEX ? -1 : IS_SOME || IS_EVERY ? IS_EVERY : target;
  44. };
  45. };
  46. module.exports = {
  47. // `Array.prototype.forEach` method
  48. // https://tc39.es/ecma262/#sec-array.prototype.foreach
  49. forEach: createMethod(0),
  50. // `Array.prototype.map` method
  51. // https://tc39.es/ecma262/#sec-array.prototype.map
  52. map: createMethod(1),
  53. // `Array.prototype.filter` method
  54. // https://tc39.es/ecma262/#sec-array.prototype.filter
  55. filter: createMethod(2),
  56. // `Array.prototype.some` method
  57. // https://tc39.es/ecma262/#sec-array.prototype.some
  58. some: createMethod(3),
  59. // `Array.prototype.every` method
  60. // https://tc39.es/ecma262/#sec-array.prototype.every
  61. every: createMethod(4),
  62. // `Array.prototype.find` method
  63. // https://tc39.es/ecma262/#sec-array.prototype.find
  64. find: createMethod(5),
  65. // `Array.prototype.findIndex` method
  66. // https://tc39.es/ecma262/#sec-array.prototype.findIndex
  67. findIndex: createMethod(6),
  68. // `Array.prototype.filterReject` method
  69. // https://github.com/tc39/proposal-array-filtering
  70. filterReject: createMethod(7)
  71. };