@eclipse-glsp/protocol
Version:
The protocol definition for client-server communication in GLSP
154 lines • 7.47 kB
JavaScript
/********************************************************************************
* Copyright (c) 2021-2025 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.MaybeActions = exports.AnyObject = void 0;
exports.typeGuard = typeGuard;
exports.typeGuardOr = typeGuardOr;
exports.toTypeGuard = toTypeGuard;
exports.call = call;
exports.hasStringProp = hasStringProp;
exports.hasBooleanProp = hasBooleanProp;
exports.hasNumberProp = hasNumberProp;
exports.hasObjectProp = hasObjectProp;
exports.hasFunctionProp = hasFunctionProp;
exports.hasArrayProp = hasArrayProp;
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);
}
/** Utility function to combine two type guards with an OR */
function typeGuardOr(one, other) {
return (element) => one(element) || other(element);
}
/**
* 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;
}
/**
* Utility function that calls a given function if it is a function, otherwise returns the value.
* This is useful to allow functions to be passed as parameters but also allow
* simple values to be passed.
* @template T The type of the value that might be returned by the function.
* @param maybeFun The value that might be a function or a value of type `T`.
* @param args The arguments to pass to the function if it is called.
* @returns The result of the function call or the value itself if it is not a function.
*/
function call(maybeFun, ...args) {
return typeof maybeFun === 'function' ? maybeFun(...args) : maybeFun;
}
var MaybeActions;
(function (MaybeActions) {
/**
* Utility function that converts a given `MaybeActions` value into an array of actions.
* If the value is a function, it will be called to get the actions.
* If the value is an array, it will be returned as is.
* If the value is undefined, an empty array will be returned.
* @param actions The value that might be a function or an array of actions.
* @returns An array of actions.
*/
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;
}
/**
* 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;
}
/**
* 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;
}
/**
* 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;
}
/**
* 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;
}
/**
* 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;
}
//# sourceMappingURL=type-util.js.map
;