UNPKG

phx-react

Version:

PHX REACT

114 lines 4.44 kB
import { MENU_CODE } from './constant'; export function normalizeString(rawValue) { if (!rawValue) return ''; return rawValue .normalize('NFD') // Tách các ký tự có dấu thành ký tự cơ bản và dấu .replace(/[\u0300-\u036f]/g, '') // Loại bỏ các dấu .replace(/Đ/g, 'D') // Thay Đ thành D .replace(/đ/g, 'd') // Thay đ thành d .replace(/y/g, 'i') // Thay y thành i .trim() .toLowerCase(); } export function flattenMenuTree(tree) { const result = []; function insertIfNotExist(newData, currResult) { const isExist = currResult.some((item) => item.name === newData.name); if (!isExist) result.push(newData); } tree.forEach((root) => { var _a; const rootCode = root.code; if (rootCode === MENU_CODE.DASHBOARD) return; const rootPath = root.path; insertIfNotExist({ id: root.code, name: root.name, code: rootCode, path: root.path, pathCode: rootCode, }, result); if (((_a = root === null || root === void 0 ? void 0 : root.child) === null || _a === void 0 ? void 0 : _a.length) > 0 && rootCode !== MENU_CODE.DASHBOARD) { root.child.forEach((child1) => { var _a; let finalPath1 = ''; if (rootPath.includes(child1.path)) { finalPath1 = rootPath; } else { finalPath1 = `/${rootPath.split('/')[1]}${child1.path}`; } insertIfNotExist({ id: `${root.code}_${child1.code}_${child1.name}`, name: `${root.name} / ${child1.name}`, code: rootCode, path: finalPath1, pathCode: child1.code, }, result); if (((_a = child1 === null || child1 === void 0 ? void 0 : child1.child) === null || _a === void 0 ? void 0 : _a.length) > 0) { child1.child.forEach((child2) => { let finalPath2 = ''; if (rootPath.includes(child2.path)) { finalPath2 = finalPath1; } else { finalPath2 = `/${finalPath1.split('/')[1]}${child2.path}`; } insertIfNotExist({ id: `${root.code}_${child1.code}_${child2.code}_${child2.name}`, name: `${root.name} / ${child2.name}`, code: rootCode, path: finalPath2, pathCode: child2.code, }, result); }); } }); } }); return result; } export const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms)); export function enablePer(code, arr) { if (!code) return true; return arr === null || arr === void 0 ? void 0 : arr.some((item) => item === code); } export const checkChildBefore = (item, currentItem) => { const currentIndexActive = item.find((s) => s.current); return currentIndexActive ? currentIndexActive.id > currentItem.id : false; }; export const normalizeCode = (raw) => { if (!raw) return ''; return raw.replace(/-/g, '_'); }; export const htmlToText = (html) => { if (!html) return ''; return html .replace(/<style[\s\S]*?>[\s\S]*?<\/style>/gi, ' ') .replace(/<script[\s\S]*?>[\s\S]*?<\/script>/gi, ' ') .replace(/<br\s*\/?>/gi, ' ') .replace(/<\/p>/gi, ' ') .replace(/<[^>]+>/g, ' ') .replace(/&nbsp;/g, ' ') .replace(/\s+/g, ' ') .trim(); }; export const truncateByChars = (text, limit) => { const t = (text !== null && text !== void 0 ? text : '').trim(); if (!t) return { short: '', isLong: false }; // nếu <= limit => không cắt if (t.length <= limit) return { short: t, isLong: false }; // cắt và trim tránh dính khoảng trắng const short = t.slice(0, limit).trimEnd(); return { short, isLong: true }; }; export const getBoldIconColor = (isActive) => (isActive ? '#111827' : '#4B5563'); //# sourceMappingURL=helper.js.map