UNPKG

@etsoo/appscript

Version:

Applications shared TypeScript framework

124 lines (123 loc) 3.96 kB
/** * Business utils */ export var BusinessUtils; (function (BusinessUtils) { let resourcesCache = {}; /** * Format avatar title * @param title Title * @param maxChars Max characters * @param defaultTitle Default title * @returns Result */ function formatAvatarTitle(title, maxChars = 3, defaultTitle = "ME") { // Just return for empty cases if (title == null || title === "") return defaultTitle; // split with words const items = title.trim().split(/\s+/g); if (items.length === 1) { // 2-3 Chinese names const titleLen = title.length; if (titleLen <= maxChars) return title.toUpperCase(); // Return default for simplicity return defaultTitle; } // First letter of each item var firstLetters = items .map((item) => item[0]) .join("") .toUpperCase(); const flen = firstLetters.length; if (flen <= maxChars) return firstLetters; return defaultTitle; } BusinessUtils.formatAvatarTitle = formatAvatarTitle; /** * Format query, dealing with paging data * @param rq Query * @returns Result */ function formatQuery(rq) { let { queryPaging, ...rest } = rq; if (typeof queryPaging === "number") { queryPaging = { currentPage: 0, batchSize: queryPaging }; } return { queryPaging, ...rest }; } BusinessUtils.formatQuery = formatQuery; /** * Get 12-month items * @param monthLabels Month labels * @param startMonth Start month, 0 as Jan. * @returns 12 months */ function getMonths(monthLabels, startMonth = 0) { const months = []; for (let i = 0; i < 12; i++) { if (startMonth >= 12) startMonth = 0; months.push({ id: startMonth, label: monthLabels[startMonth] }); startMonth++; } return months; } BusinessUtils.getMonths = getMonths; /** * Check if the item is a custom culture data * @param item Item to check * @param hasOrgId Has orgId or not * @returns Result */ function isCustomCultureData(item, hasOrgId = false) { return (item != null && typeof item === "object" && "key" in item && "title" in item && (!hasOrgId || "orgId" in item)); } BusinessUtils.isCustomCultureData = isCustomCultureData; /** * Merge custom resources to target collection * @param target Target collection merges to * @param resources New resources to merge */ function mergeCustomResources(target, resources) { for (const item of resources) { if (item.orgId) { // Backup const backup = target[item.key]; if (backup != null && !isCustomCultureData(backup, true)) { resourcesCache[item.key] = backup; } } if (item.description || item.jsonData) { const { key, ...rest } = item; target[key] = rest; } else { target[item.key] = item.title; } } } BusinessUtils.mergeCustomResources = mergeCustomResources; /** * Restore resources to target collection * @param target Target collection to restore resources to, null to clear cache */ function restoreResources(target) { // Clear cache if no target if (target == null) { resourcesCache = {}; return; } // Restore resources for (const key in resourcesCache) { target[key] = resourcesCache[key]; } } BusinessUtils.restoreResources = restoreResources; })(BusinessUtils || (BusinessUtils = {}));