@bitrix24/b24ui-nuxt
Version:
Bitrix24 UI-Kit for developing web applications REST API for NUXT & VUE
197 lines (194 loc) • 5.77 kB
JavaScript
import { isEqual } from "ohash/utils";
import { withTrailingSlash, withLeadingSlash, joinURL } from "ufo";
import icons from "../dictionary/icons.js";
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, by } = options;
const foundItem = items.find((item) => {
const itemValue = typeof item === "object" && item !== null && valueKey ? get(item, valueKey) : item;
return compare(itemValue, value, by);
});
if (isEmpty(value) && foundItem) {
return labelKey ? get(foundItem, labelKey) : void 0;
}
if (isEmpty(value)) {
return void 0;
}
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);
}
export function getSlotChildrenText(children) {
return children.map((node) => {
if (!node.children || typeof node.children === "string") return node.children || "";
else if (Array.isArray(node.children)) return getSlotChildrenText(node.children);
else if (node.children.default) return getSlotChildrenText(node.children.default());
}).join("");
}
const PROMPT_BLOCK_TAGS = /* @__PURE__ */ new Set(["p", "h1", "h2", "h3", "h4", "h5", "h6", "blockquote"]);
function walkPromptElement(node) {
if (node.nodeType === 3) {
const text = node.textContent || "";
if (text.includes("\n") && !text.trim()) return "";
return text;
}
if (node.nodeType !== 1) return "";
const element = node;
const tag = element.tagName.toLowerCase();
let inner = "";
node.childNodes.forEach((child) => {
inner += walkPromptElement(child);
});
if (PROMPT_BLOCK_TAGS.has(tag)) return `${inner}
`;
if (tag === "pre") return `
\`\`\`
${inner.replace(/^`+|`+$/g, "")}
\`\`\`
`;
if (tag === "ul" || tag === "ol") return `${inner}
`;
if (tag === "li") return `- ${inner}
`;
if (tag === "br") return "\n";
if (tag === "hr") return "\n---\n\n";
if (tag === "code") return `\`${inner}\``;
if (tag === "strong" || tag === "b") return `**${inner}**`;
if (tag === "em" || tag === "i") return `*${inner}*`;
if (tag === "a") {
const href = element.getAttribute("href");
return href ? `[${inner}](${href})` : inner;
}
return inner;
}
export function extractPromptText(el) {
if (!el) return "";
return walkPromptElement(el).replace(/[ \t]+\n/g, "\n").replace(/\n{3,}/g, "\n\n").trim();
}
export function resolveIcon(name) {
if (!name) return void 0;
return icons[name];
}
export function transformUI(ui, uiProp) {
return Object.entries(ui).reduce((acc, [key, value]) => {
acc[key] = typeof value === "function" ? value({ class: uiProp?.[key] }) : value;
return acc;
}, { ...uiProp || {} });
}
export function resolveBaseURL(path, baseURL) {
if (path?.startsWith("/") && !path.startsWith("//")) {
const _base = withLeadingSlash(withTrailingSlash(baseURL || "/"));
if (_base !== "/" && !path.startsWith(_base)) {
return joinURL(_base, path);
}
}
return path;
}
export * from "./content.js";