UNPKG

@eclipse-glsp/protocol

Version:

The protocol definition for client-server communication in GLSP

137 lines 6.73 kB
"use strict"; /******************************************************************************** * Copyright (c) 2021-2024 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.hasArrayProp = exports.hasFunctionProp = exports.hasObjectProp = exports.hasNumberProp = exports.hasBooleanProp = exports.hasStringProp = exports.MaybeActions = exports.call = exports.toTypeGuard = exports.typeGuardOr = exports.typeGuard = exports.AnyObject = void 0; const array_util_1 = require("../utils/array-util"); var AnyObject; (function (AnyObject) { /** * Type guard to check wether a given object is of type {@link AnyObject}. * @param object The object to check. * @returns The given object as {@link AnyObject} or `false`. */ function is(object) { // eslint-disable-next-line no-null/no-null return object !== null && typeof object === 'object'; } AnyObject.is = is; })(AnyObject || (exports.AnyObject = AnyObject = {})); /** Utility function to combine two type guards */ function typeGuard(one, other) { return (element) => one(element) && other(element); } exports.typeGuard = typeGuard; /** Utility function to combine two type guards with an OR */ function typeGuardOr(one, other) { return (element) => one(element) || other(element); } exports.typeGuardOr = typeGuardOr; /** * Utility function that create a typeguard function for a given class constructor. * Essentially this wraps an instance of check as typeguard function. * @param constructor The constructor of the class for which the typeguard should be created. * @returns The typeguard for this class. */ function toTypeGuard(constructor) { return (element) => element instanceof constructor; } exports.toTypeGuard = toTypeGuard; function call(maybeFun, ...args) { return typeof maybeFun === 'function' ? maybeFun(...args) : maybeFun; } exports.call = call; var MaybeActions; (function (MaybeActions) { function asArray(actions) { const cleanup = actions ? call(actions) : []; return cleanup ? (0, array_util_1.asArray)(cleanup) : []; } MaybeActions.asArray = asArray; })(MaybeActions || (exports.MaybeActions = MaybeActions = {})); /** * Validates whether the given object has a property of type `string` with the given key. * @param object The object that should be validated * @param propertyKey The key of the property * @param optional Flag to indicate wether the property can be optional i.e. also return true if the given key is undefined * @returns `true` if the object has property with matching key of type `string`. */ function hasStringProp(object, propertyKey, optional = false) { const property = object[propertyKey]; return property !== undefined ? typeof property === 'string' : optional; } exports.hasStringProp = hasStringProp; /** * Validates whether the given object has a property of type `boolean` with the given key. * @param object The object that should be validated * @param propertyKey The key of the property * @param optional Flag to indicate wether the property can be optional i.e. also return true if the given key is undefined * @returns `true` if the object has property with matching key of type `boolean`. */ function hasBooleanProp(object, propertyKey, optional = false) { const property = object[propertyKey]; return property !== undefined ? typeof property === 'boolean' : optional; } exports.hasBooleanProp = hasBooleanProp; /** * Validates whether the given object has a property of type `number` with the given key. * @param object The object that should be validated * @param propertyKey The key of the property * @param optional Flag to indicate wether the property can be optional i.e. also return true if the given key is undefined * @returns `true` if the object has property with matching key of type `number`. */ function hasNumberProp(object, propertyKey, optional = false) { const property = object[propertyKey]; return property !== undefined ? typeof property === 'number' : optional; } exports.hasNumberProp = hasNumberProp; /** * Validates whether the given object has a property of type `object` with the given key. * @param object The object that should be validated * @param propertyKey The key of the property * @param optional Flag to indicate wether the property can be optional i.e. also return true if the given key is undefined * @returns `true` if the object has property with matching key of type `object`. */ function hasObjectProp(object, propertyKey, optional = false) { const property = object[propertyKey]; return property !== undefined ? AnyObject.is(property) : optional; } exports.hasObjectProp = hasObjectProp; /** * Validates whether the given object has a property of type `function` with the given key. * @param object The object that should be validated * @param propertyKey The key of the property * @param optional Flag to indicate wether the property can be optional i.e. also return true if the given key is undefined * @returns `true` if the object has property with matching key of type `function`. */ function hasFunctionProp(object, propertyKey, optional = false) { const property = object[propertyKey]; return property !== undefined ? typeof property === 'function' : optional; } exports.hasFunctionProp = hasFunctionProp; /** * Validates whether the given object has a property of type `Array` with the given key. * @param object The object that should be validated * @param propertyKey The key of the property * @param optional Flag to indicate wether the property can be optional i.e. also return true if the given key is undefined * @returns `true` if the object has property with matching key of type `Array`. */ function hasArrayProp(object, propertyKey, optional = false) { const property = object[propertyKey]; return property !== undefined ? Array.isArray(property) : optional; } exports.hasArrayProp = hasArrayProp; //# sourceMappingURL=type-util.js.map