UNPKG

@brandup/ui-helpers

Version:
184 lines (171 loc) 5.07 kB
function getProperty(obj, path) { if (!obj) return null; const props = path.split('.'); for (let i = 0; i < props.length; i++) { const name = props[i]; if (!(name in obj)) return undefined; obj = obj[name]; } return obj; } function hasProperty(obj, path) { if (!obj) return false; const props = path.split('.'); for (let i = 0; i < props.length; i++) { const name = props[i]; if (!(name in obj)) return false; obj = obj[name]; } return true; } var object = /*#__PURE__*/Object.freeze({ __proto__: null, getProperty: getProperty, hasProperty: hasProperty }); function isFunction(value) { return (typeof value === "function"); } function isString(value) { return (typeof value === "string" || value instanceof String); } var type = /*#__PURE__*/Object.freeze({ __proto__: null, isFunction: isFunction, isString: isString }); const minWait = (func, minTime) => { if (!minTime) return func; const beginTime = Date.now(); const ret = (...args) => { const rightTime = getRightTime(beginTime, minTime); if (rightTime) window.setTimeout(() => func(...args), rightTime); else func(...args); }; return ret; }; async function minWaitAsync(func, minTime, abort) { if (!minTime) return func(); const beginTime = Date.now(); const result = await func(); const rightTime = getRightTime(beginTime, minTime); if (rightTime) await delay(rightTime, abort); return result; } const getRightTime = (start, minTime) => { const finishTime = Date.now(); const w = minTime - (finishTime - start); return w > minTime * 0.1 ? w : 0; }; function delay(time, abort) { return new Promise((resolve, reject) => { abort?.throwIfAborted(); const onAbort = () => { window.clearTimeout(timer); reject(abort?.reason); }; const timer = window.setTimeout(() => { abort?.removeEventListener("abort", onAbort); resolve(); }, time); abort?.addEventListener("abort", onAbort, { once: true }); }); } function timeout(promise, timeout, abort) { return new Promise((resolve, reject) => { if (timeout <= 0) throw new Error("Invalid timeout value."); abort?.throwIfAborted(); const onAbort = () => { window.clearTimeout(timer); reject(abort?.reason); }; const timer = window.setTimeout(() => { abort?.removeEventListener("abort", onAbort); reject(TIMEOUT_REASON); }, timeout); abort?.addEventListener("abort", onAbort, { once: true }); promise .then(result => resolve(result)) .catch(reason => reject(reason)) .finally(() => { window.clearTimeout(timer); abort?.removeEventListener("abort", onAbort); }); }); } const TIMEOUT_REASON = "Timeout"; var func = /*#__PURE__*/Object.freeze({ __proto__: null, TIMEOUT_REASON: TIMEOUT_REASON, delay: delay, minWait: minWait, minWaitAsync: minWaitAsync, timeout: timeout }); function getWordEnd(count, word, one, two, five) { const tt = count % 100; if (tt >= 5 && tt <= 20) return word + (five || ""); const t = count % 10; return (t === 1 ? (word + (one || "")) : ((t >= 2 && t <= 4) ? (word + (two || "")) : (word + (five || "")))); } var word = /*#__PURE__*/Object.freeze({ __proto__: null, getWordEnd: getWordEnd }); const createGuid = () => { var result; var i; var j; result = ""; for (j = 0; j < 32; j++) { if (j == 8 || j == 12 || j == 16 || j == 20) result = result + '-'; i = Math.floor(Math.random() * 16).toString(16).toUpperCase(); result = result + i; } return result; }; const empty = "00000000-0000-0000-0000-000000000000"; var guid = /*#__PURE__*/Object.freeze({ __proto__: null, createGuid: createGuid, empty: empty }); function formatText(template, ...args) { if (!args.length) return template; const obj = typeof args[0] === "object" ? args[0] : null; return template.replace(/\{([^}]+)\}/g, (_match, key) => { if (obj) return getProperty(obj, key) ?? ""; else { const paramIndex = parseInt(key); if (!isNaN(paramIndex) && paramIndex < args.length) return args[paramIndex] ?? ""; } return ""; }); } Object.prop = function (obj, path) { return getProperty(obj, path); }; Object.hasProp = function (obj, path) { return hasProperty(obj, path); }; String.prototype.format = function (...args) { return formatText(this.toString(), ...args); }; export { func as FuncHelper, guid as Guid, object as ObjectHelper, type as TypeHelper, word as WordHelper, formatText }; //# sourceMappingURL=index.js.map