System.register(["cc"], function (_export, _context) { "use strict"; var _cclegacy, Utils, _crd; _export("Utils", void 0); return { setters: [function (_cc) { _cclegacy = _cc.cclegacy; }], execute: function () { _crd = true; _cclegacy._RF.push({}, "7ea66r0AApIFbjuTFjXJIWh", "Utils", undefined); _export("Utils", Utils = class Utils { /** * 随机数 (包含min,不包含max) * @param min * @param max * @param isInt * @return {*} */ static getRandom(min = 0, max = 1, isInt = false) { if (min == null) min = 0; if (max == null) max = 1; if (isInt == null) isInt = false; if (min === max) return min; let value = min + Math.random() * (max - min); if (isInt) { value = Math.floor(value); } return value; } /** * 随机整数-包含最大值,最小值 * @param min * @param max * @return {*} */ static getRandomIntInclusive(min, max) { min = Math.ceil(min); max = Math.floor(max); if (min == max) return min; return Math.floor(Math.random() * (max - min + 1)) + min; //The maximum is inclusive and the minimum is inclusive } /** * 随机整数-不包含最大值,最小值 * @param min * @param max * @return {*} */ static getRandomInt(min, max) { min = Math.ceil(min); max = Math.ceil(max); return Math.floor(Math.random() * (max - min)) + min; } /** */ static getRandomNumberInRange(min, max) { return Math.random() * (max - min) + min; } /**生成随机整数 -1 或 1*/ static getRandomDir() { return Math.floor(Math.random() * 2) === 0 ? -1 : 1; // } /** * 在指定数组中随机取出N个不重复的数据 * @param resArr * @param ranNum * @returns {Array} */ static getRandomDiffValueFromArr(resArr, ranNum) { let arr = new Array(); let result = new Array(); if (!resArr || resArr.length <= 0 || ranNum <= 0) { return result; } for (let i = 0; i < resArr.length; i++) { arr.push(resArr[i]); } if (ranNum >= arr.length) { return arr; } ranNum = Math.min(ranNum, arr.length - 1); for (let i = 0; i < ranNum; i++) { let ran = Utils.getRandomIntInclusive(0, arr.length - 1); // Math.floor(Math.random() * arr.length); result.push(arr.splice(ran, 1)[0]); } return result; } /** * 随机一个字符串 * @param length * @returns */ static randomStr(length) { let result = ''; const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; const charactersLength = characters.length; for (let i = 0; i < length; i++) { result += characters.charAt(Math.floor(Math.random() * charactersLength)); } return result; } /** * 时间转换 毫秒=》2019-03-28 19:55:34 * @param milliseconds * @return {string} */ static time2str(milliseconds) { let time = new Date(); time.setTime(milliseconds); let year = time.getFullYear(); let hours = time.getHours(); let min = time.getMinutes(); let sec = time.getSeconds(); let month = time.getMonth() + 1; let date = time.getDate(); return year + "-" + month + "-" + date + " " + (hours > 9 ? hours : "0" + hours) + ":" + (min > 9 ? min : "0" + min) + ":" + (sec > 9 ? sec : "0" + sec); } /** * 将毫秒转换为时间格式 00:00:00 * @param delay * @param detail * @return */ static second2time(delay, detail = 2) { // if (detail == undefined) detail = 2; //天 let d = Math.floor(delay / 86400000); delay = delay % 86400000; // 小时 let h = Math.floor(delay / 3600000); delay = delay % 3600000; // 分钟 let m = Math.floor(delay / 60000); delay = delay % 60000; // 秒 let s = Math.floor(delay / 1000); delay = delay % 1000; // 毫秒 let ml = delay; let result = ""; if (d > 0 && detail > 0) { result += (d > 9 ? d : "0" + d) + ":"; // "天"; detail--; } if (h > 0 && detail > 0) { result += (h > 9 ? h : "0" + h) + ":"; // "小时"; detail--; } if (m > 0 && detail > 0) { result += (m > 9 ? m : "0" + m) + ":"; // "分钟"; detail--; } if (detail > 0) { result += s > 9 ? s : "0" + s; // "秒"; detail--; } if (ml > 0 && detail > 0) { detail--; } return result; } static formatMilliseconds(milliseconds) { const seconds = Math.floor(milliseconds / 1000 % 60); const minutes = Math.floor(milliseconds / (1000 * 60) % 60); const hours = Math.floor(milliseconds / (1000 * 60 * 60) % 24); //const formattedSeconds = seconds < 10 ? "0" + seconds : seconds; //const formattedMinutes = minutes < 10 ? "0" + minutes : minutes; //const formattedHours = hours < 10 ? "0" + hours : hours; //`${formattedHours}:${formattedMinutes}:${formattedSeconds}`; return `${hours.toString().padStart(2, "0")}:${minutes.toString().padStart(2, "0")}:${seconds.toString().padStart(2, "0")}`; } /** * 取小数位 * @param decimal 小数 * @param places 位数 * @return {number} */ static getDecimal(decimal, places) { let round = Math.pow(10, places); return Math.round(decimal * round) / round; } /** * 本年第几周 * @param timestamp 毫秒 * @returns */ static getYearWeek(timestamp) { // let nowDate: Date = new Date(date); // let firstDay: Date = new Date(date); // firstDay.setMonth(0);//设置1月 // firstDay.setDate(1);//设置1号 // let diffDays = Math.ceil((nowDate.valueOf() - firstDay.valueOf())/(24*60*60*1000)); // return Math.ceil((diffDays + (firstDay.getDay() + 1 - 1)) / 7); let currentDate = new Date(timestamp); let year = new Date(currentDate.getFullYear(), 0, 1); let days = Math.floor((currentDate.valueOf() - year.valueOf()) / (24 * 60 * 60 * 1000)); let week = Math.ceil((currentDate.getDay() + 1 + days) / 7); return week; } /** * 是否是同一天 * @param timeStampA * @param timeStampB * @return {boolean} */ static isSameDay(timeStampA, timeStampB) { let dateA = new Date(timeStampA); dateA.setHours(0, 0, 0, 0); let dateB = new Date(timeStampB); dateB.setHours(0, 0, 0, 0); return dateA.getTime() == dateB.getTime() ? true : false; } // static checkAndAdd(map, k, v) { if (map.has(k)) { map.set(k, map.get(k) + v); } else { map.set(k, v); } } // /** * 随机权重下标 * @param weights 权重数组 */ static randomWeights(weights) { // 总权重值 const totalWeight = weights.reduce((a, b) => a + b, 0); // 随机的权重值 const randomWeight = this.getRandom(0, totalWeight); for (let index = 0, weight = 0; index < weights.length; index++) { weight += weights[index]; if (weight >= randomWeight) { return index; } } return -1; } /** * 随机数组下标 * @param array 数组 */ /** * 根据权重随机数组下标 * @param array 数组 * @param weights 权重数组 */ static randomArray(array, weights) { if (!weights) { const index = this.getRandom(0, array.length, true); return index; } return this.randomWeights(weights.slice(0, array.length)); } }); _cclegacy._RF.pop(); _crd = false; } }; }); //# sourceMappingURL=d8ea009be5644c7164c4b5ae51cd349e5525e465.js.map