ws-rmi
Version:
A Remote Method Invocation implementation written in JavaScript utilising the WebSocket protocol
78 lines (77 loc) • 2.45 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.hasPropertyOfType = exports.hasProperty = exports.isArray = exports.isNumber = exports.isRMIMessageType = exports.isString = exports.isObject = exports.isNull = void 0;
const Utils_1 = require("./Utils");
/**
* Determines if the given data is null.
*
* @param data The data to narrow.
*/
const isNull = (data) => data === null;
exports.isNull = isNull;
/**
* Determines if the given data is an object.
*
* @param data The data to narrow.
*/
const isObject = (data) => data !== null && typeof data === "object";
exports.isObject = isObject;
/**
* Determines if the given data is a string.
*
* @param data The data to narrow.
*/
const isString = (data) => typeof data === "string" || data instanceof String;
exports.isString = isString;
/**
* Determines if the given string matches a valid RMIMessageType.
*
* @param data The data to narrow.
*/
const isRMIMessageType = (data) => (0, exports.isString)(data) && (0, Utils_1.isEqualToAny)(data, "REQUEST", "RESPONSE_RESULT", "RESPONSE_ERROR");
exports.isRMIMessageType = isRMIMessageType;
/**
* Determines if the given data is a number.
*
* @param data The data to narrow.
*/
const isNumber = (data) => typeof data === "number";
exports.isNumber = isNumber;
/**
* Determines if the given data is an array.
*
* @param data The data to narrow.
*/
const isArray = (data) => Array.isArray(data);
exports.isArray = isArray;
/**
* 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.
*/
const hasProperty = (object, key) => key in object;
exports.hasProperty = hasProperty;
/**
* 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].
*/
const hasPropertyOfType = (object, key, validator) => (0, exports.hasProperty)(object, key) && validator(object[key]);
exports.hasPropertyOfType = hasPropertyOfType;