UNPKG

@applitools/utils

Version:
135 lines (133 loc) 4.74 kB
"use strict"; /* eslint {"@typescript-eslint/ban-types": ["error", {"types": {"Function": false}}]} */ Object.defineProperty(exports, "__esModule", { value: true }); exports.instanceOf = exports.has = exports.isEnumValue = exports.isFunction = exports.isEmpty = exports.isPlainObject = exports.isObject = exports.isArray = exports.isUint8Array = exports.isAnyArrayBuffer = exports.isInteger = exports.isNumber = exports.isHttpUrl = exports.isBase64 = exports.isString = exports.isBoolean = exports.isNull = exports.isDefined = exports.isNotDefined = void 0; function isNotDefined(value) { return (isNull(value) || (isString(value) ? value === '' || value.toLowerCase() === 'null' || value.toLowerCase() === 'undefined' : false)); } exports.isNotDefined = isNotDefined; function isDefined(value) { return !isNotDefined(value); } exports.isDefined = isDefined; function isNull(value) { return value == null; } exports.isNull = isNull; function isBoolean(value) { return typeof value === 'boolean' || value instanceof Boolean; } exports.isBoolean = isBoolean; function isString(value) { return Object.prototype.toString.call(value) === '[object String]'; } exports.isString = isString; function isBase64(value) { return isString(value) && /^[A-Za-z0-9+/]*={0,2}$/.test(value); } exports.isBase64 = isBase64; function isHttpUrl(value) { try { const url = new URL(value); return url.protocol === 'http:' || url.protocol === 'https:'; } catch { return false; } } exports.isHttpUrl = isHttpUrl; function isNumber(value) { return typeof value === 'number' || value instanceof Number; } exports.isNumber = isNumber; function isInteger(value) { return isNumber(value) && Number.isInteger(value); } exports.isInteger = isInteger; function isAnyArrayBuffer(value) { return !!value && (value[Symbol.toStringTag] === 'ArrayBuffer' || value[Symbol.toStringTag] === 'SharedArrayBuffer'); } exports.isAnyArrayBuffer = isAnyArrayBuffer; function isUint8Array(value) { return !!value && value[Symbol.toStringTag] === 'Uint8Array'; } exports.isUint8Array = isUint8Array; function isArray(value) { return Array.isArray(value); } exports.isArray = isArray; function isObject(value) { return typeof value === 'object' && value !== null; } exports.isObject = isObject; function isPlainObject(value) { return isObject(value) && (!value.constructor || value.constructor.name === 'Object'); } exports.isPlainObject = isPlainObject; function isEmpty(value) { if (!value) return true; if (isObject(value)) return Object.keys(value).length === 0; return value.length === 0; } exports.isEmpty = isEmpty; function isFunction(value, key) { if (key && has(value, key)) return typeof value[key] === 'function'; return typeof value === 'function'; } exports.isFunction = isFunction; function isEnumValue(value, enumeration) { const values = new Set(Object.values(enumeration)); return values.has(value); } exports.isEnumValue = isEnumValue; function has(value, keys) { if (!isObject(value)) return false; if (!isArray(keys)) keys = [keys]; for (const key of keys) { if (!(key in value)) return false; } return true; } exports.has = has; function instanceOf(value, ctorOrName) { if (!isObject(value) && !isFunction(value)) return false; if (!isString(ctorOrName)) return value instanceof ctorOrName; let proto1 = Object.getPrototypeOf(value); while (proto1) { if (proto1.constructor.name === ctorOrName) return true; proto1 = Object.getPrototypeOf(proto1); } /* in webdriverIO, the constructor is not always the same as the prototype's constructor. if you want to make this research, you can import {browser} from '@wdio/global', and see that: * isObject(browser) === false * isFunction(browser) === true * typeof browser === 'function' * String(browser) === '[object Object]' * browser.constructor.name === 'bound Browser' * JSON.stringify(browser) === undefined * browser.__proto__.constructor.name === 'Browser' * Object.getPrototypeOf(browser) is a function - "function () { [native code] }" It's too weird for me right now. If you see this comment and you understand what's going on, I'm eager to learn from you - please contact me. */ if ('__proto__' in value) { let proto2 = value.__proto__; while (proto2) { if (proto2.constructor.name === ctorOrName) return true; proto2 = proto2.__proto__; } } return false; } exports.instanceOf = instanceOf;