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.

iterate.js 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. 'use strict';
  2. var bind = require('../internals/function-bind-context');
  3. var call = require('../internals/function-call');
  4. var anObject = require('../internals/an-object');
  5. var tryToString = require('../internals/try-to-string');
  6. var isArrayIteratorMethod = require('../internals/is-array-iterator-method');
  7. var lengthOfArrayLike = require('../internals/length-of-array-like');
  8. var isPrototypeOf = require('../internals/object-is-prototype-of');
  9. var getIterator = require('../internals/get-iterator');
  10. var getIteratorMethod = require('../internals/get-iterator-method');
  11. var iteratorClose = require('../internals/iterator-close');
  12. var $TypeError = TypeError;
  13. var Result = function (stopped, result) {
  14. this.stopped = stopped;
  15. this.result = result;
  16. };
  17. var ResultPrototype = Result.prototype;
  18. module.exports = function (iterable, unboundFunction, options) {
  19. var that = options && options.that;
  20. var AS_ENTRIES = !!(options && options.AS_ENTRIES);
  21. var IS_RECORD = !!(options && options.IS_RECORD);
  22. var IS_ITERATOR = !!(options && options.IS_ITERATOR);
  23. var INTERRUPTED = !!(options && options.INTERRUPTED);
  24. var fn = bind(unboundFunction, that);
  25. var iterator, iterFn, index, length, result, next, step;
  26. var stop = function (condition) {
  27. if (iterator) iteratorClose(iterator, 'normal', condition);
  28. return new Result(true, condition);
  29. };
  30. var callFn = function (value) {
  31. if (AS_ENTRIES) {
  32. anObject(value);
  33. return INTERRUPTED ? fn(value[0], value[1], stop) : fn(value[0], value[1]);
  34. } return INTERRUPTED ? fn(value, stop) : fn(value);
  35. };
  36. if (IS_RECORD) {
  37. iterator = iterable.iterator;
  38. } else if (IS_ITERATOR) {
  39. iterator = iterable;
  40. } else {
  41. iterFn = getIteratorMethod(iterable);
  42. if (!iterFn) throw new $TypeError(tryToString(iterable) + ' is not iterable');
  43. // optimisation for array iterators
  44. if (isArrayIteratorMethod(iterFn)) {
  45. for (index = 0, length = lengthOfArrayLike(iterable); length > index; index++) {
  46. result = callFn(iterable[index]);
  47. if (result && isPrototypeOf(ResultPrototype, result)) return result;
  48. } return new Result(false);
  49. }
  50. iterator = getIterator(iterable, iterFn);
  51. }
  52. next = IS_RECORD ? iterable.next : iterator.next;
  53. while (!(step = call(next, iterator)).done) {
  54. try {
  55. result = callFn(step.value);
  56. } catch (error) {
  57. iteratorClose(iterator, 'throw', error);
  58. }
  59. if (typeof result == 'object' && result && isPrototypeOf(ResultPrototype, result)) return result;
  60. } return new Result(false);
  61. };