UNPKG

@eclipse-glsp/protocol

Version:

The protocol definition for client-server communication in GLSP

192 lines 8.1 kB
"use strict"; /******************************************************************************** * Copyright (c) 2019-2026 EclipseSource and others. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v. 2.0 which is available at * http://www.eclipse.org/legal/epl-2.0. * * This Source Code may also be made available under the following Secondary * Licenses when the conditions for such availability set forth in the Eclipse * Public License v. 2.0 are satisfied: GNU General Public License, version 2 * with the GNU Classpath Exception which is available at * https://www.gnu.org/software/classpath/license.html. * * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 ********************************************************************************/ Object.defineProperty(exports, "__esModule", { value: true }); exports.first = first; exports.last = last; exports.pluck = pluck; exports.remove = remove; exports.flatPush = flatPush; exports.asArray = asArray; exports.distinctAdd = distinctAdd; exports.isArrayOfType = isArrayOfType; exports.isArrayOfClass = isArrayOfClass; exports.isArrayOfPrimitive = isArrayOfPrimitive; exports.isStringArray = isStringArray; exports.isArrayMatching = isArrayMatching; exports.partition = partition; exports.arrayOf = arrayOf; exports.groupBy = groupBy; function first(array, n) { if (n) { return array.filter((_, index) => index < n); } return array[0]; } function last(array, n) { if (n) { return array.filter((_, index) => array.length - index <= n); } return array[array.length - 1]; } /** * Plucks (i.e. extracts) the property value that corresponds to the given key from all objects of the array. * @param array The array which should be plucked. * @param key The key of the property that should be extracted. * @returns A new array containing the plugged property for each element of the array. */ function pluck(array, key) { return array.map(element => element[key]); } /** * Removes the given values from the given array (if present). * @param array The array to execute the remove operation on. * @param values The values that should be removed from the array. */ function remove(array, ...values) { values.forEach(value => { const index = array.indexOf(value); if (index >= 0) { array.splice(index, 1); } }); } /** * Push an array of values to the given array. The values can either be single objects of a concrete type `T` * or can also be nested arrays of T. If nested arrays are passed the they will be destructured (i.e. flattened) * so that they can be pushed to the given array. * @param array The array to push to. * @param toPush The values of {@link MaybeArray}s that should be pushed. */ function flatPush(array, toPush) { toPush.forEach(value => (Array.isArray(value) ? array.push(...value) : array.push(value))); } /** * Helper function to convert a {@link MaybeArray} into an array. * @param maybe The MaybeArray to convert * @returns The corresponding array */ function asArray(maybe) { if (Array.isArray(maybe)) { return maybe; } return [maybe]; } /** * Adds the given values to the given array. The add operation is executed distinct meaning * a value will not be pushed to the array if its already present in the array. * @param array The array to push to. * @param values The values that should be added distinctively. */ function distinctAdd(array, ...values) { values.forEach(value => { if (!array.includes(value)) { array.push(value); } }); } /** * A typeguard function to check wether a given object is an array of a specific type `T`. As it checks the type of each individual * array element this guard check is expensive and should only be used in cases where complete type-safety is required. * @param object The object to check. * @param typeGuard A typeguard to check the type of the individual elements. * @param supportEmpty A flag to determine wether empty arrays should pass the typeguard check. * @returns A type predicate indicating wether the given object has passed the type guard check. */ function isArrayOfType(object, typeGuard, supportEmpty = false) { return isArrayMatching(object, element => typeGuard(element), supportEmpty); } /** * A typeguard function to check wether a given object is an array of a class`T`. As it checks the wether each individual element * is an instance of the given class this guard check is expensive and should only be used in cases where complete type-safety is required. * @param object The object to check. * @param constructor The constructor for the class under test. * @param supportEmpty A flag to determine wether empty arrays should pass the typeguard check. * @returns A type predicate indicating wether the given object has passed the type guard check. */ function isArrayOfClass(object, constructor, supportEmpty = false) { return isArrayMatching(object, element => element instanceof constructor, supportEmpty); } /** * A typeguard function to check wether a given object is an array of a {@link PrimitiveType} `T. As it checks the type of each individual * array element this guard check is expensive and should only be used in cases where complete type-safety is required. * @param object The object to check. * @param primitiveType The expected primitive type of the elements. * @param supportEmpty A flag to determine wether empty arrays should pass the typeguard check. * @returns A type predicate indicating wether the given object has passed the type guard check. */ function isArrayOfPrimitive(object, primitiveType, supportEmpty = false) { return isArrayMatching(object, element => typeof element === primitiveType, supportEmpty); } /** * A typeguard function to check wether a given object is an array of a strings. As it checks the type of each individual * array element this guard check is expensive and should only be used in cases where complete type-safety is required. * @param object The object to check. * @param supportEmpty A flag to determine wether empty arrays should pass the typeguard check. * @returns A type predicate indicating wether the given object has passed the type guard check. */ function isStringArray(object, supportEmpty = false) { return isArrayOfPrimitive(object, 'string', supportEmpty); } /** * A typeguard function to check wether a given object is an array where each element matches the given predicate. * @param object The object to check. * @param predicate The predicate to test with. * @param supportEmpty A flag to determine wether empty arrays be matched by the predicate.. * @returns `true` if the given object is an array and all elements match the given predicate. `false` otherwise. */ function isArrayMatching(object, predicate, supportEmpty = false) { return Array.isArray(object) && object.every(predicate) && (supportEmpty || object.length > 0); } function partition(source, matchGuard) { const match = []; const rest = []; source.forEach(element => { if (matchGuard(element)) { match.push(element); } else { rest.push(element); } }); return { match, rest }; } /** * Helper function to create an array of values without any undefined values. * @param values The values to create the array from. * @returns The array of values without any undefined values. */ function arrayOf(...values) { return values.filter(element => element !== undefined); } function groupBy(array, keyFn, sorted) { const grouped = new Map(); for (const item of array) { const key = keyFn(item); const values = grouped.get(key); if (values) { values.push(item); } else { grouped.set(key, [item]); } } if (sorted) { return new Map([...grouped.entries()].sort()); } return grouped; } //# sourceMappingURL=array-util.js.map