UNPKG

4.76 kBJavaScriptView Raw
1"use strict";
2// *****************************************************************************
3// Copyright (C) 2023 Ericsson and others.
4//
5// This program and the accompanying materials are made available under the
6// terms of the Eclipse Public License v. 2.0 which is available at
7// http://www.eclipse.org/legal/epl-2.0.
8//
9// This Source Code may also be made available under the following Secondary
10// Licenses when the conditions for such availability set forth in the Eclipse
11// Public License v. 2.0 are satisfied: GNU General Public License, version 2
12// with the GNU Classpath Exception which is available at
13// https://www.gnu.org/software/classpath/license.html.
14//
15// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
16// *****************************************************************************
17Object.defineProperty(exports, "__esModule", { value: true });
18exports.ArrayUtils = void 0;
19var ArrayUtils;
20(function (ArrayUtils) {
21 ArrayUtils.TailImpl = {
22 tail() {
23 return this[this.length - 1];
24 },
25 };
26 ArrayUtils.HeadAndChildrenImpl = {
27 head() {
28 return this[0];
29 },
30 children() {
31 return Object.assign(this.slice(1), ArrayUtils.TailImpl);
32 }
33 };
34 function asTail(array) {
35 return Object.assign(array, ArrayUtils.TailImpl);
36 }
37 ArrayUtils.asTail = asTail;
38 function asHeadAndTail(array) {
39 return Object.assign(array, ArrayUtils.HeadAndChildrenImpl, ArrayUtils.TailImpl);
40 }
41 ArrayUtils.asHeadAndTail = asHeadAndTail;
42 let Sort;
43 (function (Sort) {
44 Sort[Sort["LeftBeforeRight"] = -1] = "LeftBeforeRight";
45 Sort[Sort["RightBeforeLeft"] = 1] = "RightBeforeLeft";
46 Sort[Sort["Equal"] = 0] = "Equal";
47 })(Sort = ArrayUtils.Sort || (ArrayUtils.Sort = {}));
48 // Copied from https://github.com/microsoft/vscode/blob/9c29becfad5f68270b9b23efeafb147722c5feba/src/vs/base/common/arrays.ts
49 /**
50 * Performs a binary search algorithm over a sorted collection. Useful for cases
51 * when we need to perform a binary search over something that isn't actually an
52 * array, and converting data to an array would defeat the use of binary search
53 * in the first place.
54 *
55 * @param length The collection length.
56 * @param compareToKey A function that takes an index of an element in the
57 * collection and returns zero if the value at this index is equal to the
58 * search key, a negative number if the value precedes the search key in the
59 * sorting order, or a positive number if the search key precedes the value.
60 * @return A non-negative index of an element, if found. If not found, the
61 * result is -(n+1) (or ~n, using bitwise notation), where n is the index
62 * where the key should be inserted to maintain the sorting order.
63 */
64 function binarySearch2(length, compareToKey) {
65 let low = 0;
66 let high = length - 1;
67 while (low <= high) {
68 const mid = ((low + high) / 2) | 0;
69 const comp = compareToKey(mid);
70 if (comp < 0) {
71 low = mid + 1;
72 }
73 else if (comp > 0) {
74 high = mid - 1;
75 }
76 else {
77 return mid;
78 }
79 }
80 return -(low + 1);
81 }
82 ArrayUtils.binarySearch2 = binarySearch2;
83 function partition(array, filter) {
84 const pass = [];
85 const fail = [];
86 array.forEach((e, idx, arr) => (filter(e, idx, arr) ? pass : fail).push(e));
87 return [pass, fail];
88 }
89 ArrayUtils.partition = partition;
90 /**
91 * @returns New array with all falsy values removed. The original array IS NOT modified.
92 */
93 function coalesce(array) {
94 return array.filter(e => !!e);
95 }
96 ArrayUtils.coalesce = coalesce;
97 /**
98 * groups array elements through a comparator function
99 * @param data array of elements to group
100 * @param compare comparator function: return of 0 means should group, anything above means not group
101 * @returns array of arrays with grouped elements
102 */
103 function groupBy(data, compare) {
104 const result = [];
105 let currentGroup = undefined;
106 for (const element of data.slice(0).sort(compare)) {
107 if (!currentGroup || compare(currentGroup[0], element) !== 0) {
108 currentGroup = [element];
109 result.push(currentGroup);
110 }
111 else {
112 currentGroup.push(element);
113 }
114 }
115 return result;
116 }
117 ArrayUtils.groupBy = groupBy;
118})(ArrayUtils = exports.ArrayUtils || (exports.ArrayUtils = {}));
119//# sourceMappingURL=array-utils.js.map
\No newline at end of file