import { isObjHasOwnProp, getObjLength } from "@web3r/flowerkit/obj"; import type { TIsStateIncludesOtherState } from "../config/types"; /** * Проверяет, содержит ли одно состояние элементы другого * @returns {boolean} */ export const isStateIncludesOtherState = ({ firstState, secondState, anyValueMask = "*", }: TIsStateIncludesOtherState): boolean => { const isPlainRecord = (value: unknown): value is Record => { return typeof value === "object" && value !== null && !Array.isArray(value); }; let isSame = false; if (isPlainRecord(firstState) && isPlainRecord(secondState)) { Object.entries(firstState).forEach(([ key, value ]) => { if (isObjHasOwnProp(secondState, key)) { const comparedState = secondState[key]; if (Array.isArray(value)) { isSame = value.includes(comparedState); } else if (isPlainRecord(value) && isPlainRecord(comparedState)) { isSame = getObjLength(value) === getObjLength(comparedState) && Object.entries(value).every(([ key, val ]) => { if (isObjHasOwnProp(comparedState, key)) { return val === anyValueMask ? true : val === comparedState[key]; } return false; }); } else { isSame = value === comparedState; } } }); } return isSame; };