import { defaultColumns, defaultGaps, defaultProps } from "../config"; import type { IGrid } from "../config/types"; /** * Получает адаптированное значение для `align` или `justify` * @param from{*} * @returns {string[]} */ const getPropAdapter = (from: unknown): string[] => { const [ defaultDesktop, defaultTablet, defaultMobile ] = defaultProps; switch (true) { case (Array.isArray(from) && from.every((item) => typeof item === "string" && item.length > 0)): { if (from.length === 3) { return from; } else { const [ desktop = defaultDesktop, tablet = defaultTablet, mobile = defaultMobile, ] = from; return [ desktop, tablet, mobile ] .map((item) => item.toString()); } } case typeof from === "string": return [ from.toString(), defaultTablet, defaultMobile ]; default: console.error(`[Grid] Value "${from}" as align or justify isn't supported`); return [ defaultDesktop, defaultTablet, defaultMobile ]; } }; /** * Получает массив колонок для сетки * @param from{number|number[]} * @returns {IGrid["columns"]} */ const getColumnsAdapter = (from: IGrid["columns"]): number[] | "auto"[] => { const isNumber = (item: unknown) => Number.isFinite(item) && Number(item) > 0; const [ defaultDesktop, defaultTablet, defaultMobile ] = defaultColumns; switch (true) { case from === "auto" || Array.isArray(from) && from.every((item) => item === "auto"): return [ "auto", "auto", "auto" ]; case Array.isArray(from) && from.every((item: number | string) => isNumber(item)): { if (from.length === 3) { return from; } else { const [ desktop = defaultDesktop, tablet = defaultTablet, mobile = defaultMobile, ] = from; return [ desktop, tablet, mobile ] .map((item) => Number(item)); } } case isNumber(Number(from)): return [ Number(from), defaultTablet, defaultMobile ]; default: console.error(`[Grid] Value "${from}" as columns count isn't supported`); return [ defaultDesktop, defaultTablet, defaultMobile ]; } }; /** * Получает адаптированный массив полей * @param from{string|string[]} * @returns {string[]} */ const getGapAdapter = (from: IGrid["gaps"]): string[] => { const [ defaultDesktop, defaultTablet, defaultMobile ] = defaultGaps; switch (true) { case Array.isArray(from) && from.every((item) => typeof item === "string" && item.length > 0): if (from.length === 3) { return from; } else { const [ desktop = defaultDesktop, tablet = defaultTablet, mobile = defaultMobile, ] = from; return [ desktop, tablet, mobile ] .map((item) => item.toString()); } case typeof from === "string": return [ from.toString(), defaultTablet, defaultMobile ]; default: console.error(`[Grid] Value "${from}" as gap isn't supported`); return [ defaultDesktop, defaultTablet, defaultMobile ]; } }; /** * Проверяет, являются ли два объекта одинаковыми * @returns {boolean} */ const isSameObjects = (one: unknown, two: unknown): boolean => { return !!(one && two) ? JSON.stringify(one) === JSON.stringify(two) : false; }; export { getColumnsAdapter, getGapAdapter, getPropAdapter, isSameObjects, };