UNPKG

945 BJavaScriptView Raw
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 *
9 * @emails oncall+draft_js
10 */
11'use strict';
12
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 */
20function findRangesImmutable(haystack, areEqualFn, filterFn, foundFn) {
21 if (!haystack.size) {
22 return;
23 }
24
25 var cursor = 0;
26 haystack.reduce(function (value, nextValue, nextIndex) {
27 if (!areEqualFn(value, nextValue)) {
28 if (filterFn(value)) {
29 foundFn(cursor, nextIndex);
30 }
31
32 cursor = nextIndex;
33 }
34
35 return nextValue;
36 });
37 filterFn(haystack.last()) && foundFn(cursor, haystack.count());
38}
39
40module.exports = findRangesImmutable;
\No newline at end of file