/** *随机数 * @param {number} max * @param {number} min * @returns {number} */ export const randomNumber = (min: number, max: number) => { return Math.floor(min + Math.random() * (max - min + 1)) } /** *车牌 * @param {string} str * @returns {boolean} */ export const isLicensePlate = (str: string) => { return /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领]{1}[A-HJ-NP-Z]{1}(?:(([0-9]{5}[DF])|([DF][A-HJ-NP-Z0-9][0-9]{4}))|[A-HJ-NP-Z0-9]{4}[A-HJ-NP-Z0-9挂学警港澳]{1})$/.test( str ) } /** *数组内元素为对象时 去重 * @param {Array} array * @param {string} key * @returns {Array>} */ export const uniqBy = (array: any[], key: string): Array> => { const obj: Record = {} const arraySet = array.reduce((item: Record, next: Record) => { obj[next[key]] ? '' : (obj[next[key]] = true && item.push(next)) return item }, []) return arraySet } /** *未来几天的时间 默认一周 * @param {number} [len=7] 时间长度 * @returns {Array>} */ export const futureDate = (len = 7): Array> => { const threeDay = ['今天', '明天', '后天'] //获取系统当前时间 const now = new Date() const nowTime = now.getTime() const oneDayTime = 24 * 60 * 60 * 1000 const timeArr = [] for (let i = 0; i < len; i++) { //显示星期 const showTime = nowTime + i * oneDayTime //初始化日期时间 const myDate = new Date(showTime) const month = myDate.getMonth() + 1 const date = myDate.getDate() const str = '周' + '日一二三四五六'.charAt(myDate.getDay()) const _date = `${month.toString().padStart(2, '0')}-${date.toString().padStart(2, '0')}` timeArr.push({ week: i < 3 ? threeDay[i] : str, customDate: _date }) } return timeArr } /** *利用require.context引入文件 * @param {context} * @returns {Record} */ export const importAll = (context: any) => { const map: Record = {} for (const key of context.keys()) { const keyArr = key.split('/') keyArr.shift() // 移除. map[keyArr.join('.').replace(/\.js$/g, '')] = context(key) } return map } /** *格式化日期 * @param {Date} date * @param {string} format * @returns {string} */ export type Format = 'y.m.d' | 'y/m/d' | 'y-m-d' export const formatDay = (format: Format = 'y-m-d') => { const date = new Date() const year = date.getFullYear() const month = String(date.getMonth() + 1).padStart(2, '0') const day = String(date.getDate()).padStart(2, '0') if (format === 'y.m.d') { return `${year}.${month}.${day}` } if (format === 'y/m/d') { return `${year}/${month}/${day}` } return year + '-' + month + '-' + day }