UNPKG

ws-rmi

Version:

A Remote Method Invocation implementation written in JavaScript utilising the WebSocket protocol

72 lines (71 loc) 2.15 kB
import { RMIMessageType } from "./types/RMIMessageType"; /** * Represents a function that narrows down the given data to a particular type. This is mainly used during JSON * validation. */ export declare type Validator<T> = (data: unknown) => data is T; /** * Determines if the given data is null. * * @param data The data to narrow. */ export declare const isNull: Validator<null>; /** * Determines if the given data is an object. * * @param data The data to narrow. */ export declare const isObject: Validator<object>; /** * Determines if the given data is a string. * * @param data The data to narrow. */ export declare const isString: Validator<string>; /** * Determines if the given string matches a valid RMIMessageType. * * @param data The data to narrow. */ export declare const isRMIMessageType: Validator<RMIMessageType>; /** * Determines if the given data is a number. * * @param data The data to narrow. */ export declare const isNumber: Validator<number>; /** * Determines if the given data is an array. * * @param data The data to narrow. */ export declare const isArray: Validator<unknown[]>; /** * Determines if the given object has a particular key. * * @example * declare var obj: object; * * if (hasProperty(obj, "foo")) { * obj.foo // foo exists on obj, and is of type unknown * } * * @param object The object to examine. * @param key The key to use in the given object. */ export declare const hasProperty: <T extends object, K extends string>(object: T, key: K) => object is T & { [key in K]: unknown; }; /** * Determines if the given object has a particular key that matches a given validator. * * @example * declare var obj: object; * * if (hasProperty(obj, "foo", isString)) { * obj.foo // foo exists on obj, and is of type string * } * * @param object The object to examine. * @param key The key to use in the given object. * @param validator The validator that will narrow the type of object[key]. */ export declare const hasPropertyOfType: <T extends object, K extends string, V>(object: T, key: K, validator: Validator<V>) => object is T & { [key in K]: V; };