123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261 |
- export class Utils {
-
- /**
- * 随机数 (包含min,不包含max)
- * @param min
- * @param max
- * @param isInt
- * @return {*}
- */
- public static getRandom(min: number = 0, max: number = 1, isInt: boolean = false): number {
- 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 {*}
- */
- public static getRandomIntInclusive(min: number, max: number) {
- 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 {*}
- */
- public static getRandomInt(min: number, max: number):number {
- min = Math.ceil(min); max = Math.ceil(max);
- return Math.floor(Math.random() * (max - min)) + min;
- }
- /** */
- public static getRandomNumberInRange(min:number, max:number):number {
- return Math.random() * (max - min) + min;
- }
- /**生成随机整数 -1 或 1*/
- public static getRandomDir():number {
- return Math.floor(Math.random() * 2) === 0 ? -1 : 1;//
- }
- /**
- * 在指定数组中随机取出N个不重复的数据
- * @param resArr
- * @param ranNum
- * @returns {Array}
- */
- public static getRandomDiffValueFromArr<T>(resArr: Array<T>, ranNum: number): Array<T> {
- let arr = new Array<T>();
- let result = new Array<T>();
- 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
- */
- public static randomStr(length: number): string {
- let result: string = '';
- 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}
- */
- public static time2str (milliseconds: number): string {
- 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
- */
- public static second2time (delay: number, detail: number = 2): string {
- // 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: string = "";
- 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;
- }
- public static formatMilliseconds(milliseconds: number): string {
- 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}
- */
- public static getDecimal (decimal: number, places: number): number {
- let round: number = Math.pow(10, places);
- return Math.round(decimal * round) / round;
- }
- /**
- * 本年第几周
- * @param timestamp 毫秒
- * @returns
- */
- public static getYearWeek(timestamp: number): number {
- // 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: Date = new Date(timestamp);
- let year: Date = new Date(currentDate.getFullYear(), 0, 1);
- let days: number = Math.floor((currentDate.valueOf() - year.valueOf()) / (24 * 60 * 60 * 1000));
- let week: number = Math.ceil(( currentDate.getDay() + 1 + days) / 7);
- return week;
- }
- /**
- * 是否是同一天
- * @param timeStampA
- * @param timeStampB
- * @return {boolean}
- */
- public static isSameDay(timeStampA: number, timeStampB: number): boolean {
- 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;
- }
- //
- public static checkAndAdd<t1>(map:Map<t1,number>,k:t1,v:number){
- if(map.has(k)){
- map.set(k,map.get(k)+v);
- }else{
- map.set(k,v);
- }
- }
- //
- /**
- * 随机权重下标
- * @param weights 权重数组
- */
- static randomWeights(weights: number[]) {
- // 总权重值
- 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 数组
- */
- static randomArray<T>(array: T[]): number;
- /**
- * 根据权重随机数组下标
- * @param array 数组
- * @param weights 权重数组
- */
- static randomArray<T>(array: T[], weights: number[]): number;
- static randomArray(array: any[], weights?: number[]) {
- if (!weights) {
- const index = this.getRandom(0, array.length,true);
- return index;
- }
- return this.randomWeights(weights.slice(0, array.length));
- }
-
- }
|