UNPKG

@craftercms/studio-ui

Version:

Services, components, models & utils to build CrafterCMS authoring extensions.

186 lines (184 loc) 6.76 kB
/* * Copyright (C) 2007-2022 Crafter Software Corporation. All Rights Reserved. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License version 3 as published by * the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ /* * Copyright (C) 2007-2022 Crafter Software Corporation. All Rights Reserved. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License version 3 as published by * the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ /** * Converts a string separated by dashes into a * camelCase equivalent. For instance, 'foo-bar' * would be converted to 'fooBar'. **/ export function camelize(str) { return str.replace(/-+(.)?/g, function (match, chr) { return chr ? chr.toUpperCase() : ''; }); } /** * Capitalizes the first letter of a string and down-cases all the others. **/ export function capitalize(str) { return `${str.charAt(0).toUpperCase()}${str.substr(1)}`; } /** * Converts a camelized string into a series of words separated by an underscore (_). **/ export function underscore(str) { return str .replace(/::/g, '/') .replace(/([A-Z]+)([A-Z][a-z])/g, '$1_$2') .replace(/([a-z\d])([A-Z])/g, '$1_$2') .replace(/-/g, '_') .toLowerCase(); } /** * Replaces every instance of the underscore character "_" by a dash "-". **/ export function dasherize(str) { return str.replace(/_/g, '-'); } export function removeSpaces(str) { return str.replace(/\s+/g, ''); } export function removeLastPiece(str, splitChar = '.') { return (str = `${str}`).substr(0, str.lastIndexOf(splitChar)); } export function popPiece(str, splitChar = '.') { return String(str).split(splitChar).pop(); } export function isJSON(str) { throw new Error('[isJSON] Not implemented.'); } export function hasUppercaseChars(str) { return /[A-Z]/.test(str); } export function getInitials(str) { if (!str) { return ''; } else if (typeof str === 'string') { const pieces = (str !== null && str !== void 0 ? str : '').split(' '); return `${pieces[0].substr(0, 1)}${pieces[1] ? pieces[1].substr(0, 1) : ''}`.toUpperCase(); } else { return `${str.firstName.substr(0, 1)}${str.lastName.substr(0, 1)}`.toUpperCase(); } } export function formatBytes(bytes, decimals = 2) { if (bytes === 0) return '0 Bytes'; const k = 1024; const dm = decimals < 0 ? 0 : decimals; const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']; const i = Math.floor(Math.log(bytes) / Math.log(k)); return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i]; } export function isBlank(str) { return (str !== null && str !== void 0 ? str : '').trim() === ''; } export function dataUriToBlob(dataURI) { // convert base64 to raw binary data held in a string // doesn't handle URLEncoded DataURIs - see SO answer #6850276 for code that does this const byteString = atob(dataURI.split(',')[1]); // separate out the mime component const mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0]; // write the bytes of the string to an ArrayBuffer const ab = new ArrayBuffer(byteString.length); // create a view into the buffer const ia = new Uint8Array(ab); // set the bytes of the buffer to the correct values for (let i = 0; i < byteString.length; i++) { ia[i] = byteString.charCodeAt(i); } // write the ArrayBuffer to a blob, and you're done const blob = new Blob([ab], { type: mimeString }); return blob; } export function fileNameFromPath(path) { return path.substr(path.lastIndexOf('/') + 1).replace(/\.xml/, ''); } export function escapeHTML(str) { const element = document.createElement('textarea'); element.textContent = str; return element.innerHTML; } export function unescapeHTML(html) { const element = document.createElement('textarea'); element.innerHTML = html; return element.textContent; } export function legacyEscapeXml(value) { if (typeof value === 'string') { return value.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;'); } return value; } export function legacyUnescapeXml(value) { if (typeof value === 'string') { return value .replace(/&lt;/g, '<') .replace(/&gt;/g, '>') .replace(/&quot;/g, '"') .replace(/&amp;/g, '&'); } return value; } export function bytesToSize(bytes, separator = '') { const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB']; if (bytes === 0) return 'n/a'; const i = parseInt(`${Math.floor(Math.log(bytes) / Math.log(1024))}`, 10); if (i === 0) return `${bytes}${separator}${sizes[i]}`; return `${(bytes / Math.pow(1024, i)).toFixed(1)}${separator}${sizes[i]}`; } /** * Removes double slashes from urls * @param url {string} The URL to clean up */ export function ensureSingleSlash(url) { return /^(http|https):\/\//g.test(url) ? url.replace(/([^:]\/)\/+/g, '$1') : url.replace(/\/+/g, '/'); } export function getSimplifiedVersion(version, options = {}) { if (!version) { return version; } const { minor = true, patch = false } = options; const pieces = version.split('.'); !patch && pieces.pop(); !minor && pieces.pop(); return pieces.join('.'); } export function preFill(str, minLength = 2, char = '0') { str = `${str}`; return str.length >= minLength ? str : `${new Array(minLength - str.length).fill(char).join('')}${str}`; } export function postFill(str, minLength = 2, char = '0') { str = `${str}`; return str.length >= minLength ? str : `${str}${new Array(minLength - str.length).fill(char).join('')}`; } export const isSimple = (str, separator = '.') => String(str).split(separator).length === 1; export const isSymmetricCombination = (string1, string2, separator = '.') => String(string1).split(separator).length === String(string2).split(separator).length; export function stripCData(str) { return str.replace(/<!\[CDATA\[|\]\]>/gi, ''); }