UNPKG

burdy

Version:

Open Source Headless Platform

83 lines (82 loc) 2.96 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.testPaths = exports.findSettingsValue = exports.isTrue = exports.isEmptyString = exports.getMetaValue = exports.getMeta = exports.copyToClipboard = exports.humanFileSize = void 0; const path_to_regexp_1 = require("path-to-regexp"); const humanFileSize = (bytes, dp = 1) => { const thresh = 1024; if (Math.abs(bytes) < thresh) { return `${bytes} B`; } const units = ['kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']; let u = -1; const r = Math.pow(10, dp); do { bytes /= thresh; ++u; } while (Math.round(Math.abs(bytes) * r) / r >= thresh && u < units.length - 1); return `${bytes.toFixed(dp)} ${units[u]}`; }; exports.humanFileSize = humanFileSize; const copyToClipboard = (str) => { const el = document.createElement('textarea'); el.value = str; el.setAttribute('readonly', ''); el.style.display = 'hidden'; document.body.appendChild(el); const selected = document.getSelection().rangeCount > 0 ? document.getSelection().getRangeAt(0) : false; el.select(); document.execCommand('copy'); document.body.removeChild(el); if (selected) { document.getSelection().removeAllRanges(); document.getSelection().addRange(selected); } }; exports.copyToClipboard = copyToClipboard; const getMeta = (obj, key) => { var _a; return ((_a = obj === null || obj === void 0 ? void 0 : obj.meta) !== null && _a !== void 0 ? _a : []).find((meta) => (meta === null || meta === void 0 ? void 0 : meta.key) === key); }; exports.getMeta = getMeta; const getMetaValue = (obj, key) => { var _a; return (_a = (0, exports.getMeta)(obj, key)) === null || _a === void 0 ? void 0 : _a.value; }; exports.getMetaValue = getMetaValue; /** * The function returns true if the string passed to it has no content. */ const isEmptyString = (str) => { if (str === undefined || str === null || str.length === 0 || str.trim().length === 0) { return true; } return false; }; exports.isEmptyString = isEmptyString; const isTrue = (str) => { return str === true || str === 1 || (str || '').toLowerCase() === 'true'; }; exports.isTrue = isTrue; const findSettingsValue = (settings = [], key) => { var _a; return (_a = (settings || []).find((settings) => (settings === null || settings === void 0 ? void 0 : settings.key) === key)) === null || _a === void 0 ? void 0 : _a.value; }; exports.findSettingsValue = findSettingsValue; const testPaths = (paths = [], path) => { return paths.find((rule) => { try { const result = (0, path_to_regexp_1.match)((rule === null || rule === void 0 ? void 0 : rule[0]) === '/' ? rule : `/${rule}`); return result(`/${path}`); } catch (_a) { return false; } }); }; exports.testPaths = testPaths;