UNPKG

playwright-fluent

Version:
147 lines (146 loc) 4.67 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.areSameTypeOfObjects = exports.areSameValueType = exports.areBothFunctions = exports.isId = exports.isDictionaryType = exports.isFunctionType = exports.isValueType = exports.toArray = exports.areEquivalentDictionary = exports.areEquivalentArray = exports.areSameType = void 0; function areSameType(obj1, obj2) { if (isValueType(obj1)) { return areSameValueType(obj1, obj2); } if (isFunctionType(obj1)) { return areBothFunctions(obj1, obj2); } if (Array.isArray(obj1)) { return areEquivalentArray(obj1, obj2); } if (isDictionaryType(obj1)) { return areEquivalentDictionary(obj1, obj2); } if (areSameTypeOfObjects(obj1, obj2)) { return true; } return false; } exports.areSameType = areSameType; function areEquivalentArray(obj1, obj2) { if (!Array.isArray(obj2)) { return false; } const length1 = obj1.length; const length2 = obj2.length; const length = Math.min(length1, length2); for (let i = 0; i < length; i++) { const isItemTypeFoundInSecondArray = obj2.some((item) => areSameType(obj1[i], item)); if (!isItemTypeFoundInSecondArray) { return false; } } return true; } exports.areEquivalentArray = areEquivalentArray; function areEquivalentDictionary(obj1, obj2) { if (!isDictionaryType(obj2)) { return false; } const obj1AsArray = toArray(obj1); const obj2AsArray = toArray(obj2); return areEquivalentArray(obj1AsArray, obj2AsArray); } exports.areEquivalentDictionary = areEquivalentDictionary; function toArray(obj) { const keys = Object.getOwnPropertyNames(obj); // eslint-disable-next-line @typescript-eslint/no-explicit-any return keys.map((key) => obj[key]); } exports.toArray = toArray; function isValueType(obj) { return (typeof obj === 'number' || typeof obj === 'string' || typeof obj === 'boolean' || obj === null || obj === undefined); } exports.isValueType = isValueType; function isFunctionType(obj) { return typeof obj === 'function'; } exports.isFunctionType = isFunctionType; function isDictionaryType(obj) { if (obj === null || obj === undefined) { return false; } const objProperties = Object.getOwnPropertyNames(obj); if (objProperties.length === 0) { return false; } for (let index = 0; index < objProperties.length; index++) { const key = objProperties[index]; if (isId(`${key}`)) { return true; } } return false; } exports.isDictionaryType = isDictionaryType; const idsRegex = [ /[0-9]{4}[:][A-Z0-9]{5}/, // etag /[0-9a-zA-Z]{30}/, // base64 /[0-9a-zA-Z]{20,}==$/, // guid /[a-zA-Z0-9]{8}[-][a-zA-Z0-9]{4}[-][a-zA-Z0-9]{4}/, ]; function isId(key) { return idsRegex.some((regex) => regex.test(key)); } exports.isId = isId; function areBothFunctions(obj1, obj2) { return isFunctionType(obj1) && isFunctionType(obj2); } exports.areBothFunctions = areBothFunctions; function areSameValueType(value1, value2) { if (value1 === null && value2 === null) { return true; } if (value1 === undefined && value2 === undefined) { return true; } if (typeof value1 === 'string' && typeof value2 === 'string') { return true; } if (typeof value1 === 'number' && typeof value2 === 'number') { return true; } if (typeof value1 === 'boolean' && typeof value2 === 'boolean') { return true; } return false; } exports.areSameValueType = areSameValueType; function areSameTypeOfObjects(obj1, obj2) { if (typeof obj1 === 'object' && typeof obj2 !== 'object') { return false; } if (typeof obj1 !== 'object' && typeof obj2 === 'object') { return false; } if (obj2 === null || obj2 === undefined) { return false; } const obj1Properties = Object.getOwnPropertyNames(obj1); const obj2Properties = Object.getOwnPropertyNames(obj2); if (obj1Properties.length !== obj2Properties.length) { return false; } for (let index = 0; index < obj1Properties.length; index++) { const key = obj1Properties[index]; // eslint-disable-next-line @typescript-eslint/no-explicit-any const child1 = obj1[key]; // eslint-disable-next-line @typescript-eslint/no-explicit-any const child2 = obj2[key]; if (!areSameType(child1, child2)) { return false; } } return true; } exports.areSameTypeOfObjects = areSameTypeOfObjects;