UNPKG

@brandup/ui-helpers

Version:
156 lines (144 loc) 3.96 kB
'use strict'; function hasProperty(obj, property) { if (!obj) return false; const props = property.split("."); let t = obj; for (let i = 0; i < props.length; i++) { if (!t) return false; const pName = props[i]; if (!(pName in t)) return false; t = t[pName]; } return true; } function getProperty(obj, property) { if (!obj) return null; const props = property.split("."); let t = obj; for (let i = 0; i < props.length; i++) { const pName = props[i]; if (!(pName in t)) return undefined; t = t[pName]; } return t; } 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) => { const timer = window.setTimeout(() => { if (abort && abort.aborted) reject(abort.reason); else resolve(); }, time); abort?.addEventListener("abort", () => { window.clearTimeout(timer); reject(abort.reason); }); }); } function timeout(promise, timeout) { return new Promise((resolve, reject) => { if (timeout <= 0) throw new Error("Invalid timeout value."); const timer = window.setTimeout(() => { reject(new Error(TIMEOUT_ERROR)); }, timeout); promise .then(result => resolve(result)) .catch(reason => reject(reason)) .finally(() => window.clearTimeout(timer)); }); } const TIMEOUT_ERROR = "Timeout"; var func = /*#__PURE__*/Object.freeze({ __proto__: null, TIMEOUT_ERROR: TIMEOUT_ERROR, 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; }; var guid = /*#__PURE__*/Object.freeze({ __proto__: null, createGuid: createGuid }); exports.FuncHelper = func; exports.Guid = guid; exports.ObjectHelper = object; exports.TypeHelper = type; exports.WordHelper = word; //# sourceMappingURL=index.js.map