UNPKG

@bitrix24/b24ui-nuxt

Version:

Bitrix24 UI-Kit for developing web applications REST API for NUXT & VUE

123 lines (122 loc) 3.28 kB
import { isEqual } from "ohash/utils"; export function pick(data, keys) { const result = {}; for (const key of keys) { result[key] = data[key]; } return result; } export function omit(data, keys) { const result = { ...data }; for (const key of keys) { delete result[key]; } return result; } export function get(object, path, defaultValue) { if (typeof path === "string") { path = path.split(".").map((key) => { const numKey = Number(key); return Number.isNaN(numKey) ? key : numKey; }); } let result = object; for (const key of path) { if (result === void 0 || result === null) { return defaultValue; } result = result[key]; } return result !== void 0 ? result : defaultValue; } export function set(object, path, value) { if (typeof path === "string") { path = path.split(".").map((key) => { const numKey = Number(key); return Number.isNaN(numKey) ? key : numKey; }); } path.reduce((acc, key, i) => { if (acc[key] === void 0) acc[key] = {}; if (i === path.length - 1) acc[key] = value; return acc[key]; }, object); } export function looseToNumber(val) { const n = Number.parseFloat(val); return Number.isNaN(n) ? val : n; } export function compare(value, currentValue, comparator) { if (value === void 0 || currentValue === void 0) { return false; } if (typeof value === "string") { return value === currentValue; } if (typeof comparator === "function") { return comparator(value, currentValue); } if (typeof comparator === "string") { return get(value, comparator) === get(currentValue, comparator); } return isEqual(value, currentValue); } export function isEmpty(value) { if (value == null) { return true; } if (typeof value === "boolean" || typeof value === "number") { return false; } if (typeof value === "string") { return value.trim().length === 0; } if (Array.isArray(value)) { return value.length === 0; } if (value instanceof Map || value instanceof Set) { return value.size === 0; } if (value instanceof Date || value instanceof RegExp || typeof value === "function") { return false; } if (typeof value === "object") { for (const _ in value) { if (Object.prototype.hasOwnProperty.call(value, _)) { return false; } } return true; } return false; } export function getDisplayValue(items, value, options = {}) { const { valueKey, labelKey } = options; if (isEmpty(value)) { return void 0; } const foundItem = items.find((item) => { const itemValue = typeof item === "object" && item !== null && valueKey ? get(item, valueKey) : item; return compare(itemValue, value); }); const source = foundItem ?? value; if (source === null || source === void 0) { return void 0; } if (typeof source === "object") { return labelKey ? get(source, labelKey) : void 0; } return String(source); } export function isArrayOfArray(item) { return Array.isArray(item[0]); } export function mergeClasses(appConfigClass, propClass) { if (!appConfigClass && !propClass) { return ""; } return [ ...Array.isArray(appConfigClass) ? appConfigClass : [appConfigClass], propClass ].filter(Boolean); }