1/** 2 * 时间解析工具 3 * @param {(Object|string|number)} time 4 * @param {string} cFormat 5 * @returns {string | null} 6 */ 7export function parseTime(time, cFormat) { 8 if (arguments.length === 0 || !time) { 9 return null 10 } 11 const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}' 12 let date 13 if (typeof time === 'object') { 14 date = time 15 } else { 16 if ((typeof time === 'string')) { 17 if ((/^[0-9]+$/.test(time))) { 18 // support "1548221490638" 19 time = parseInt(time) 20 } else { 21 // support safari 22 // https://stackoverflow.com/questions/4310953/invalid-date-in-safari 23 time = time.replace(new RegExp(/-/gm), '/') 24 } 25 } 26 27 if ((typeof time === 'number') && (time.toString().length === 10)) { 28 time = time * 1000 29 } 30 date = new Date(time) 31 } 32 const formatObj = { 33 y: date.getFullYear(), 34 m: date.getMonth() + 1, 35 d: date.getDate(), 36 h: date.getHours(), 37 i: date.getMinutes(), 38 s: date.getSeconds(), 39 a: date.getDay() 40 } 41 const time_str = format.replace(/{([ymdhisa])+}/g, (result, key) => { 42 const value = formatObj[key] 43 // Note: getDay() returns 0 on Sunday 44 if (key === 'a') { return ['日', '一', '二', '三', '四', '五', '六'][value ] } 45 return value.toString().padStart(2, '0') 46 }) 47 return time_str 48} 49 50 51 52/** 53 * @param {number} time 54 * @param {string} option 55 * @returns {string} 56 */ 57export function formatTime(time, option) { 58 if (('' + time).length === 10) { 59 time = parseInt(time) * 1000 60 } else { 61 time = +time 62 } 63 const d = new Date(time) 64 const now = Date.now() 65 66 const diff = (now - d) / 1000 67 68 if (diff < 30) { 69 return '刚刚' 70 } else if (diff < 3600) { 71 // less 1 hour 72 return Math.ceil(diff / 60) + '分钟前' 73 } else if (diff < 3600 * 24) { 74 return Math.ceil(diff / 3600) + '小时前' 75 } else if (diff < 3600 * 24 * 2) { 76 return '1天前' 77 } 78 if (option) { 79 return parseTime(time, option) 80 } else { 81 return ( 82 d.getMonth() + 83 1 + 84 '月' + 85 d.getDate() + 86 '日' + 87 d.getHours() + 88 '时' + 89 d.getMinutes() + 90 '分' 91 ) 92 } 93} 94 95 96 97/** 98 * 查被选元素是否包含指定的 class 99 * @param {HTMLElement} elm 100 * @param {string} cls 101 * @returns {boolean} 102 */ 103export function hasClass(ele, cls) { 104 return !!ele.className.match(new RegExp('(\\s|^)' + cls + '(\\s|$)')) 105} 106 107/** 108 * 添加class到dom 109 * @param {HTMLElement} elm 110 * @param {string} cls 111 */ 112export function addClass(ele, cls) { 113 if (!hasClass(ele, cls)) ele.className += ' ' + cls 114} 115 116/** 117 * 删除class到dom 118 * @param {HTMLElement} elm 119 * @param {string} cls 120 */ 121export function removeClass(ele, cls) { 122 if (hasClass(ele, cls)) { 123 const reg = new RegExp('(\\s|^)' + cls + '(\\s|$)') 124 ele.className = ele.className.replace(reg, ' ') 125 } 126} 127 128 129 130/** 131 * 对象深拷贝 132 * @param {Object} source 133 * @returns {Object} 134 */ 135export function deepClone(source) { 136 if (!source && typeof source !== 'object') { 137 throw new Error('error arguments', 'deepClone') 138 } 139 const targetObj = source.constructor === Array ? [] : {} 140 Object.keys(source).forEach(keys => { 141 if (source[keys] && typeof source[keys] === 'object') { 142 targetObj[keys] = deepClone(source[keys]) 143 } else { 144 targetObj[keys] = source[keys] 145 } 146 }) 147 return targetObj 148} 149 150 151 152/** 153* 防抖函数 154 * @param {Function} func 155 * @param {number} wait 156 * @param {boolean} immediate 157 * @return {*} 158 */ 159export function debounce(func, wait, immediate) { 160 let timeout, args, context, timestamp, result 161 162 const later = function() { 163 // 据上一次触发时间间隔 164 const last = +new Date() - timestamp 165 166 // 上次被包装函数被调用时间间隔 last 小于设定时间间隔 wait 167 if (last < wait && last > 0) { 168 timeout = setTimeout(later, wait - last) 169 } else { 170 timeout = null 171 // 如果设定为immediate===true,因为开始边界已经调用过了此处无需调用 172 if (!immediate) { 173 result = func.apply(context, args) 174 if (!timeout) context = args = null 175 } 176 } 177 } 178 179 return function(...args) { 180 context = this 181 timestamp = +new Date() 182 const callNow = immediate && !timeout 183 // 如果延时不存在,重新设定延时 184 if (!timeout) timeout = setTimeout(later, wait) 185 if (callNow) { 186 result = func.apply(context, args) 187 context = args = null 188 } 189 190 return result 191 } 192} 193 194 195 196 197/** 198* url参数解析 199 * @param {string} url 200 * @returns {Object} 201 */ 202export function getQueryObject(url) { 203 url = url == null ? window.location.href : url 204 const search = url.substring(url.lastIndexOf('?') + 1) 205 const obj = {} 206 const reg = /([^?&=]+)=([^?&=]*)/g 207 search.replace(reg, (rs, $1, $2) => { 208 const name = decodeURIComponent($1) 209 let val = decodeURIComponent($2) 210 val = String(val) 211 obj[name] = val 212 return rs 213 }) 214 return obj 215} 216 217 218
utils.js 文件 工具类 方法分享
陈发良
2021-02-04
2137 1 0
点赞
收藏
评论区
加载中...