阅读量:0
记录一下老项目自定义js工具组件(ES5),持续补充方法
目前涉及如下
字符处理:trim,补0,hashcode
日期处理:字符日期时间转Date,format等
js处理:动态js脚本导入
数组处理:去重等
函数处理:函数重载续写等
var _commonUtil = { str: { trim: function (s) { return s ? s.toString().trim() : ""; }, adjust: function (s, max) { var sub = max - s.length; if (sub === 0) { return s; } else if (sub < 0) { return s.substring(0, max); } else { var _s = ""; for (var i = 0; i < sub; i++) { _s += "0"; } _s += s; return _s; } }, hashCode: function (s) { var hash = 0; if (!s) { return hash; } for (var i = 0; i < s.length; i++) { var char = s.charCodeAt(i); hash = ((hash << 5) - hash) + char; hash = hash & hash; } return hash; } }, date: { parseStr: function () { if (arguments.length) { return new Date( arguments[0] || 1999, arguments[1] - 1 || 0, arguments[2] || 1, arguments[3] || 0, arguments[4] || 0, arguments[5] || 0, arguments[6] || 0 ); } return null; }, parse: function (dateStr) { dateStr = _commonUtil.str.trim(dateStr); var numbers = dateStr.match(/\d+/g); if (numbers) { // yyyy MM dd HH mm ss SSS var values = []; var patternLens = [4, 2, 2, 2, 2, 2, 3]; if (numbers.length === 1) { var s = numbers[0]; var index = 0; for (var i = 0; i < patternLens.length; i++) { var len = patternLens[i]; var end = index + len; if (end <= s.length) { values.push(+s.substring(index, end)); index += len; } else { break; } } } else { for (var j = 0; j < patternLens.length; j++) { if (j < numbers.length) { var n = numbers[j]; var l = patternLens[j]; if (n.length > l) { numbers.splice(j + 1, 0, n.substring(l)); } if (j === 0) { // yyyy默认两位年份都拼接20 n = n.length === 2 ? "20" + n : n; } values.push(+_commonUtil.str.adjust(n, l)) } else { break; } } } return this.parseStr.apply(this, values); } return null; }, getCurrentDate: function () { var today = new Date(); var yyyy = today.getFullYear(); var mm = ('0' + (today.getMonth() + 1)).slice(-2); // 月份从0开始,需要+1 var dd = ('0' + today.getDate()).slice(-2); return yyyy + '-' + mm + '-' + dd; }, getCurrentTime: function () { var today = new Date(); var hh = ('0' + today.getHours()).slice(-2); var mm = ('0' + today.getMinutes()).slice(-2); var ss = ('0' + today.getSeconds()).slice(-2); return hh + ':' + mm + ':' + ss; }, formatDate: function (date, format) { if (!this.isDate(date)) { return null; } var map = { 'yyyy': date.getFullYear(), 'MM': ('0' + (date.getMonth() + 1)).slice(-2), 'dd': ('0' + date.getDate()).slice(-2), 'HH': ('0' + date.getHours()).slice(-2), 'mm': ('0' + date.getMinutes()).slice(-2), 'ss': ('0' + date.getSeconds()).slice(-2), 'SSS': ('00' + date.getMilliseconds()).slice(-3) }; return format.replace(/yyyy|MM|dd|HH|mm|ss|SSS/g, function (matched) { return map[matched]; }); }, isDate: function (d) { return Object.prototype.toString.call(d) === '[object Date]'; }, compare: function (d1, d2) { d1 = this.isDate(d1) ? d1 : this.parse(d1); d2 = this.isDate(d2) ? d2 : this.parse(d2); if (d1 && d2) { var sub = d1.getTime() - d2.getTime(); if (sub === 0) { return 0; } else { return sub > 0 ? 1 : -1; } } return NaN; } }, js: { impJs: function (src, callback) { var script = document.createElement('script'); script.src = src; script.type = "text/javascript"; script.onload = callback; script.onerror = function () { console.error('Failed to load script: ' + src); }; document.head.appendChild(script); } }, list: { sort: function (list, options) { if (list && list.length) { var func; var desc = options && options.desc; var map = options && options.map; if (typeof list[0] === 'number') { if (typeof map === 'function') { func = function (a, b) { return desc ? map(b) - map(a) : map(a) - map(b); }; } else { func = function (a, b) { return desc ? b - a : a - b; }; } } else { if (typeof map === 'function') { func = function (a, b) { return desc ? (map(b) + "").localeCompare(map(a) + "") : (map(a) + "").localeCompare(map(b) + ""); }; } else { func = function (a, b) { return desc ? (b + "").localeCompare(a + "") : (a + "").localeCompare(b + ""); }; } } return list.sort(func); } return []; }, set: function (list, options) { if (list && list.length) { var map = typeof (options && options.map) === 'function' ? options.map : null; var uniqueList = []; var source; if (map) { var uniIds = []; for (var i = 0; i < list.length; i++) { source = list[i]; var d = map(source); if (uniIds.indexOf(d, 0) === -1) { uniIds.push(d); uniqueList.push(source); } } } else { for (var j = 0; j < list.length; j++) { source = list[j]; if (uniqueList.indexOf(source, 0) === -1) { uniqueList.push(source); } } } return uniqueList; } return []; }, setThenSort: function (list, options) { return this.sort(this.set(list, options), options); }, join: function (list, s) { var str = ""; if (list && list.length) { s = s ? s : ","; for (var i = 0; i < list.length; i++) { str += list[i] + s; } return str.substring(0, str.length - 1); } return str; }, hashCode: function (list) { return _commonUtil.str.hashCode(this.join(list)); } }, func: { overwrite: function (target, name, newFunc) { var f = target[name]; if (typeof f === 'function') { target[name] = function () { return newFunc.apply( { inherit: function () { return f.apply(target, arguments); } } , arguments); }; } else { target[name] = function () { return newFunc.apply( { inherit: function () {} } , arguments); }; } } } };