UNPKG

@mi-gpt/utils

Version:
160 lines (156 loc) 3.93 kB
'use strict'; var chunkYVY2UWBT_cjs = require('./chunk-YVY2UWBT.cjs'); // src/index.ts function timestamp() { return (/* @__PURE__ */ new Date()).getTime(); } async function sleep(time) { return new Promise((resolve) => setTimeout(resolve, time)); } function println(...v) { console.log(...v); } function printJson(obj) { console.log(JSON.stringify(obj, void 0, 4)); } function firstOf(items) { return items ? items.length < 1 ? void 0 : items[0] : void 0; } function lastOf(items) { return (items == null ? void 0 : items.length) ? items[items.length - 1] : void 0; } function randomInt(_min, _max) { let min = _min; let max = _max; if (!max) { max = min; min = 0; } return Math.floor(Math.random() * (max - min + 1) + min); } function pickOne(items) { return items.length < 1 ? void 0 : items[randomInt(items.length - 1)]; } function range(_start, _end) { let start = _start; let end = _end; if (!end) { end = start; start = 0; } return Array.from({ length: end - start }, (_, index) => start + index); } function clamp(num, min, max) { return num < max ? num > min ? num : min : max; } function toInt(str) { return Number.parseInt(str, 10); } function toDouble(str) { return Number.parseFloat(str); } function toFixed(n, fractionDigits = 2) { let s = n.toFixed(fractionDigits); while (s[s.length - 1] === "0") { s = s.substring(0, s.length - 1); } if (s[s.length - 1] === ".") { s = s.substring(0, s.length - 1); } return s; } function toSet(items, byKey) { if (byKey) { const keys = {}; const items2 = []; for (const e of items2) { const key = byKey(e); if (!keys[key]) { items2.push(e); keys[key] = true; } } return items2; } return Array.from(new Set(items)); } function withDefault(e, defaultValue) { return chunkYVY2UWBT_cjs.isEmpty(e) ? defaultValue : e; } function removeEmpty(data) { if (!data) { return data; } if (Array.isArray(data)) { return data.filter((e) => e != null); } const res = {}; for (const key in data) { if (data[key] != null) { res[key] = data[key]; } } return res; } function deepClone(obj) { if (obj === null || typeof obj !== "object") { return obj; } if (Array.isArray(obj)) { const copy2 = []; obj.forEach((item, index) => { copy2[index] = deepClone(item); }); return copy2; } const copy = {}; for (const key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { copy[key] = deepClone(obj[key]); } } return copy; } function repeat(text, count) { return Array(count).fill(text).join(""); } function deepMerge(target, source = {}) { const result = { ...target }; for (const key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { const sourceValue = source[key]; const targetValue = target[key]; if (sourceValue && targetValue && typeof sourceValue === "object" && typeof targetValue === "object") { if (Array.isArray(sourceValue)) { result[key] = [...sourceValue]; } else if (Array.isArray(targetValue)) { result[key] = [...targetValue]; } else { result[key] = deepMerge(targetValue, sourceValue); } } else if (sourceValue !== void 0) { result[key] = sourceValue; } } } return result; } exports.clamp = clamp; exports.deepClone = deepClone; exports.deepMerge = deepMerge; exports.firstOf = firstOf; exports.lastOf = lastOf; exports.pickOne = pickOne; exports.printJson = printJson; exports.println = println; exports.randomInt = randomInt; exports.range = range; exports.removeEmpty = removeEmpty; exports.repeat = repeat; exports.sleep = sleep; exports.timestamp = timestamp; exports.toDouble = toDouble; exports.toFixed = toFixed; exports.toInt = toInt; exports.toSet = toSet; exports.withDefault = withDefault;