@bitrix24/b24jssdk
Version:
Bitrix24 REST API JavaScript SDK
338 lines (336 loc) • 8.83 kB
JavaScript
/**
* @package @bitrix24/b24jssdk
* @version 1.0.3
* @copyright (c) 2026 Bitrix24
* @license MIT
* @see https://github.com/bitrix24/b24jssdk
* @see https://bitrix24.github.io/b24jssdk/
*/
var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
const OBJECT_CONSTRUCTOR_STRING = Function.prototype.toString.call(Object);
class TypeManager {
static {
__name(this, "TypeManager");
}
getTag(value) {
return Object.prototype.toString.call(value);
}
/**
* Checks that value is string
* @param value
* @return {boolean}
*
* @memo get from pull.client.Utils
*/
isString(value) {
return typeof value === "string" || value instanceof String;
}
/**
* Returns true if a value is not an empty string
* @param value
* @returns {boolean} Returns true if a value is not an empty string
*/
isStringFilled(value) {
return this.isString(value) && value !== "";
}
/**
* Checks that value is function
* @param value
* @return {boolean}
*
* @memo get from pull.client.Utils
*/
// eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
isFunction(value) {
return value === null ? false : typeof value === "function" || value instanceof Function;
}
/**
* Checks that value is an object
* @param value
* @return {boolean}
*/
// eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
isObject(value) {
return !!value && (typeof value === "object" || typeof value === "function");
}
/**
* Checks that value is object like
* @param value
* @return {boolean}
*/
isObjectLike(value) {
return !!value && typeof value === "object";
}
/**
* Checks that value is plain object
* @param value
* @return {boolean}
*/
isPlainObject(value) {
if (!this.isObjectLike(value) || this.getTag(value) !== "[object Object]") {
return false;
}
const proto = Object.getPrototypeOf(value);
if (proto === null) {
return true;
}
const ctor = Object.prototype.hasOwnProperty.call(proto, "constructor") && proto.constructor;
return typeof ctor === "function" && Function.prototype.toString.call(ctor) === OBJECT_CONSTRUCTOR_STRING;
}
isJsonRpcRequest(value) {
return typeof value === "object" && value && "jsonrpc" in value && this.isStringFilled(value.jsonrpc) && "method" in value && this.isStringFilled(value.method);
}
isJsonRpcResponse(value) {
return typeof value === "object" && value && "jsonrpc" in value && this.isStringFilled(value.jsonrpc) && "id" in value && ("result" in value || "error" in value);
}
/**
* Checks that value is boolean
* @param value
* @return {boolean}
*/
isBoolean(value) {
return value === true || value === false;
}
/**
* Checks that value is number
* @param value
* @return {boolean}
*/
isNumber(value) {
return typeof value === "number" && !Number.isNaN(value);
}
/**
* Checks that value is integer
* @param value
* @return {boolean}
*/
isInteger(value) {
return Number.isInteger(value);
}
/**
* Checks that value is float
* @param value
* @return {boolean}
*/
isFloat(value) {
return this.isNumber(value) && !this.isInteger(value);
}
/**
* Checks that value is nil
* @param value
* @return {boolean}
*/
isNil(value) {
return value === null || value === void 0;
}
/**
* Checks that value is an array
* @param value
* @return {boolean}
*/
isArray(value) {
return !this.isNil(value) && Array.isArray(value);
}
/**
* Returns true if a value is an array, and it has at least one element
* @param value
* @returns {boolean} Returns true if a value is an array, and it has at least one element
*/
isArrayFilled(value) {
return this.isArray(value) && value.length > 0;
}
/**
* Checks that value is array like
* @param value
* @return {boolean}
*/
isArrayLike(value) {
return !this.isNil(value) && !this.isFunction(value) && value.length > -1 && value.length <= Number.MAX_SAFE_INTEGER;
}
/**
* Checks that value is Date
* @param value
* @return {boolean}
*/
isDate(value) {
return value instanceof Date;
}
/**
* Checks that is a DOM node
* @param value
* @return {boolean}
*/
isDomNode(value) {
return this.isObjectLike(value) && !this.isPlainObject(value) && "nodeType" in value;
}
/**
* Checks that value is element node
* @param value
* @return {boolean}
*/
isElementNode(value) {
return this.isDomNode(value) && value.nodeType === Node.ELEMENT_NODE;
}
/**
* Checks that value is a text node
* @param value
* @return {boolean}
*/
isTextNode(value) {
return this.isDomNode(value) && value.nodeType === Node.TEXT_NODE;
}
/**
* Checks that value is Map
* @param value
* @return {boolean}
*/
isMap(value) {
return this.isObjectLike(value) && this.getTag(value) === "[object Map]";
}
/**
* Checks that value is Set
* @param value
* @return {boolean}
*/
isSet(value) {
return this.isObjectLike(value) && this.getTag(value) === "[object Set]";
}
/**
* Checks that value is WeakMap
* @param value
* @return {boolean}
*/
isWeakMap(value) {
return this.isObjectLike(value) && this.getTag(value) === "[object WeakMap]";
}
/**
* Checks that value is WeakSet
* @param value
* @return {boolean}
*/
isWeakSet(value) {
return this.isObjectLike(value) && this.getTag(value) === "[object WeakSet]";
}
/**
* Checks that value is prototype
* @param value
* @return {boolean}
*/
isPrototype(value) {
return (typeof (value && value.constructor) === "function" && value.constructor.prototype || Object.prototype) === value;
}
/**
* Checks that value is regexp
* @param value
* @return {boolean}
*/
isRegExp(value) {
return this.isObjectLike(value) && this.getTag(value) === "[object RegExp]";
}
/**
* Checks that value is null
* @param value
* @return {boolean}
*/
isNull(value) {
return value === null;
}
/**
* Checks that value is undefined
* @param value
* @return {boolean}
*/
isUndefined(value) {
return typeof value === "undefined";
}
/**
* Checks that value is ArrayBuffer
* @param value
* @return {boolean}
*/
isArrayBuffer(value) {
return this.isObjectLike(value) && this.getTag(value) === "[object ArrayBuffer]";
}
/**
* Checks that value is typed array
* @param value
* @return {boolean}
*/
isTypedArray(value) {
const regExpTypedTag = /^\[object (?:Float(?:32|64)|(?:Int|Uint)(?:8|16|32)|Uint8Clamped)\]$/;
return this.isObjectLike(value) && regExpTypedTag.test(this.getTag(value));
}
/**
* Checks that value is Blob
* @param value
* @return {boolean}
*/
isBlob(value) {
return this.isObjectLike(value) && this.isNumber(value.size) && this.isString(value.type) && this.isFunction(value.slice);
}
/**
* Checks that value is File
* @param value
* @return {boolean}
*/
isFile(value) {
return this.isBlob(value) && this.isString(value.name) && (this.isNumber(value.lastModified) || this.isObjectLike(value.lastModifiedDate));
}
/**
* Checks that value is FormData
* @param value
* @return {boolean}
*/
isFormData(value) {
if (typeof FormData !== "undefined" && value instanceof FormData) {
return true;
}
return this.isObjectLike(value) && this.getTag(value) === "[object FormData]";
}
clone(obj, bCopyObj = true) {
let _obj, i, l;
if (this.isNil(obj) || typeof obj !== "object") {
return obj;
}
if (this.isDomNode(obj)) {
_obj = obj.cloneNode(bCopyObj);
} else if (typeof obj == "object") {
if (this.isArray(obj)) {
_obj = [];
for (i = 0, l = obj.length; i < l; i++) {
if (typeof obj[i] == "object" && bCopyObj) {
_obj[i] = this.clone(obj[i], bCopyObj);
} else {
_obj[i] = obj[i];
}
}
} else {
_obj = {};
if (obj.constructor) {
if (this.isDate(obj)) {
_obj = new Date(obj);
} else {
_obj = new obj.constructor();
}
}
for (i in obj) {
if (!Object.prototype.hasOwnProperty.call(obj, i)) {
continue;
}
if (typeof obj[i] === "object" && bCopyObj) {
_obj[i] = this.clone(obj[i], bCopyObj);
} else {
_obj[i] = obj[i];
}
}
}
} else {
_obj = obj;
}
return _obj;
}
}
const Type = new TypeManager();
export { Type };
//# sourceMappingURL=type.mjs.map