UNPKG

@adaptabletools/adaptable

Version:

Powerful AG Grid extension which provides advanced, cutting-edge functionality to meet all DataGrid requirements

182 lines (181 loc) 5.58 kB
import { StringExtensions } from '../Extensions/StringExtensions'; import { AdaptableLogger } from '../../agGrid/AdaptableLogger'; export function objectExists(item) { return item != null && item != undefined; } export function objectNotExists(item) { return !objectExists(item); } export function objectHasKeys(item) { return Object.keys(item).length > 0; } export function getStringRepresentionFromKey(event) { if (event.key == null) { return event.char; } return event.key; } export function cloneObject(obj) { return JSON.parse(JSON.stringify(obj)); } export const arrayToKeyMap = (arr) => { const defaultAccumulator = {}; if (!arr || !Array.isArray(arr)) { return defaultAccumulator; } return arr.reduce((acc, key) => { acc[key] = true; return acc; }, defaultAccumulator); }; export function convertArrayToCsv(array, separator = " ' ") { let csvContent = ''; array.forEach((infoArray, index) => { let line = []; let item; let i; for (i = 0; i < infoArray.length; ++i) { item = infoArray[i]; if (separator == ',') { if (item != null && item != undefined) { if (typeof item === 'string' || item instanceof String) { if (item.indexOf(',') !== -1 || item.indexOf('"') !== -1) { item = `"${item.replace(/"/g, '""')}"`; } if (item.indexOf('+') == 0) { item = `'${item}'`; } } } } line.push(item); } csvContent += line.join(separator) + (index != array.length - 1 ? '\n' : ''); }); return csvContent; } export function createDownloadedFile(content, fileName, mimeType) { const win = document.defaultView || window; if (!win) { console.error('No window found, cannot download file'); return; } const a = document.createElement('a'); mimeType = mimeType || 'application/octet-stream'; if (URL && 'download' in a) { a.href = URL.createObjectURL(new Blob([content], { type: mimeType, })); a.setAttribute('download', fileName); document.body.appendChild(a); a.dispatchEvent(new MouseEvent('click', { bubbles: false, cancelable: true, view: win, })); document.body.removeChild(a); } else { location.href = `data:application/octet-stream,${encodeURIComponent(content)}`; } } export async function copyToClipboard(text) { try { await navigator.clipboard.writeText(text); return true; } catch (ex) { return copyToClipboardForOldBrowsers(text); } } function copyToClipboardForOldBrowsers(text) { if (document.queryCommandSupported && document.queryCommandSupported('copy')) { let textarea = document.createElement('textarea'); textarea.textContent = text; textarea.style.width = '1px'; textarea.style.height = '1px'; textarea.style.top = '0px'; textarea.style.left = '0px'; textarea.style.position = 'absolute'; textarea.style.opacity = '0.0'; document.body.appendChild(textarea); textarea.select(); textarea.focus(); try { return document.execCommand('copy'); } catch (ex) { AdaptableLogger.consoleErrorBase('Copy to clipboard failed.', ex); return false; } finally { document.body.removeChild(textarea); } } } export function returnItemCount(items, itemName) { if (items.length == 0) { return `No ${itemName}s`; } return items.length == 1 ? `1 ${itemName}` : `${items.length} ${itemName}s`; } export function isInputNullOrEmpty(itemToCheck) { if (itemToCheck == 'Invalid Date ') { return true; } if (typeof itemToCheck === 'string') { return StringExtensions.IsNullOrEmpty(itemToCheck); } if (typeof itemToCheck === 'number') { return StringExtensions.IsNullOrEmpty(itemToCheck.toString()); } if (itemToCheck instanceof Date) { return StringExtensions.IsNullOrEmpty(itemToCheck.toString()); } return itemToCheck == null; } export function isInputNotNullOrEmpty(itemToCheck) { return !isInputNullOrEmpty(itemToCheck); } export function extractColsFromText(text) { const regex = /\[rowData\.(.*?)\]/g; let m; const cols = []; while ((m = regex.exec(text)) !== null) { cols.push(m[1]); } return cols; } export function replaceAll(text, toReplace, replaceWith) { if (!text) { return text; } toReplace = toReplace.replace('[', '\\[').replace(']', '\\]'); return text.replace(new RegExp(toReplace, 'g'), replaceWith); } export function extractContextKeysFromText(text) { const regex = /\[context\.(.*?)\]/g; let m; const contextKeys = []; while ((m = regex.exec(text)) !== null) { contextKeys.push(m[1]); } return contextKeys; } export const Helper = { objectExists, objectNotExists, objectHasKeys, getStringRepresentionFromKey, cloneObject, convertArrayToCsv, createDownloadedFile, copyToClipboard, returnItemCount, isInputNullOrEmpty, isInputNotNullOrEmpty, extractColsFromText, replaceAll, extractContextKeysFromText, }; export default Helper;