Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 

42 строки
1.1 KiB

  1. /**
  2. * Copyright (c) Facebook, Inc. and its affiliates.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. *
  7. * @format
  8. * @flow strict-local
  9. * @emails oncall+draft_js
  10. */
  11. 'use strict';
  12. import type { List } from "immutable";
  13. /**
  14. * Search through an array to find contiguous stretches of elements that
  15. * match a specified filter function.
  16. *
  17. * When ranges are found, execute a specified `found` function to supply
  18. * the values to the caller.
  19. */
  20. function findRangesImmutable<T>(haystack: List<T>, areEqualFn: (a: T, b: T) => boolean, filterFn: (value: T) => boolean, foundFn: (start: number, end: number) => void): void {
  21. if (!haystack.size) {
  22. return;
  23. }
  24. let cursor: number = 0;
  25. haystack.reduce((value: T, nextValue, nextIndex) => {
  26. if (!areEqualFn(value, nextValue)) {
  27. if (filterFn(value)) {
  28. foundFn(cursor, nextIndex);
  29. }
  30. cursor = nextIndex;
  31. }
  32. return nextValue;
  33. });
  34. filterFn(haystack.last()) && foundFn(cursor, haystack.count());
  35. }
  36. module.exports = findRangesImmutable;