1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
let class2type = {}; let toString = class2type.toString; let hasOwn = class2type.hasOwnProperty; let getProto = Object.getPrototypeOf; let fnToString = hasOwn.toString; let ObjectFunctionString = fnToString.call(Object); ("Boolean Number String Function Array Date RegExp Object Error Symbol").split(" ").forEach(function (name) { class2type["[object " + name + "]"] = name.toLowerCase(); }); //获取数据类型 let type = function (obj) { if (obj == null) { return obj + ""; } // Support: Android <=2.3 only (functionish RegExp) return typeof obj === "object" || typeof obj === "function" ? class2type[toString.call(obj)] || "object" : typeof obj; } //判断是否为数组 let isArray = Array.isArray; //判断是否为function function isFunction(obj) { return type(obj) === "function"; } //判断是否为数字类型 function isNumeric(obj) { // As of jQuery 3.0, isNumeric is limited to // strings and numbers (primitives or objects) // that can be coerced to finite numbers (gh-2662) let _type = type(obj); return (_type === "number" || _type === "string") && // parseFloat NaNs numeric-cast false positives ("") // ...but misinterprets leading-number strings, particularly hex literals ("0x...") // subtraction forces infinities to NaN !isNaN(obj - parseFloat(obj)); } //判断是否为空对象 function isEmptyObject(obj) { /* eslint-disable no-unused-vars */ // See https://github.com/eslint/eslint/issues/6125 var name; for (name in obj) { return false; } return true; } //判断是否为原生的对象 function isPlainObject(obj) { var proto, Ctor; // Detect obvious negatives // Use toString instead of jQuery.type to catch host objects if (!obj || toString.call(obj) !== "[object Object]") { return false; } proto = getProto(obj); // Objects with no prototype (e.g., `Object.create( null )`) are plain if (!proto) { return true; } // Objects with prototype are plain iff they were constructed by a global Object function Ctor = hasOwn.call(proto, "constructor") && proto.constructor; return typeof Ctor === "function" && fnToString.call(Ctor) === ObjectFunctionString; } //数组,对象复制,深度拷贝 function extend() { let target = arguments[0] || {}; let i = 1; let length = arguments.length; let deep = false; let options; let name; let src; let copy; if (typeof arguments[0] === 'boolean') { deep = target; target = arguments[1] || {}; i = 2; } if (typeof target !== 'object' && !isFunction(target)) { target = {}; } for (; i < length; i++) { if ((options = arguments[i]) != null) { for (name in options) { src = target[name]; copy = options[name]; if (target === copy) { continue; } if (deep && copy && (isPlainObject(copy) || isArray(copy))) { var clone = src && (isPlainObject(src) || isArray(src)) ? src : isArray(copy) ? [] : {}; target[name] = extend(deep, clone, copy); } else if (copy !== undefined) { target[name] = copy; } } } } return target; } //格式化数字,前面补零 function formatNumber(n) { n = n.toString() return n[1] ? n : '0' + n } //格式化时间. function formatTime(date) { var year = date.getFullYear() var month = date.getMonth() + 1 var day = date.getDate() var hour = date.getHours() var minute = date.getMinutes() var second = date.getSeconds() return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':') } //获取指定文字的字节数 function getBytesLen(str) { if (str) return str.length + str.replace(/[\u0000-\u00ff]/g, '').length; return 0; } //转义字符,例如将将<img>转为为</img> function pregQuote(str, delimiter) { // http://kevin.vanzonneveld.net return (str + '').replace(new RegExp('[.\\\\+*?\\[\\^\\]$(){}=!<>|:\\' + (delimiter || '') + '-]', 'g'), '\\$&'); } //校验手机号码 function checkPhone(phone) { return /^(11|13|14|15|17|18|19)[0-9]{9}$/.test(phone); } //获取地址栏参数 function getParam(key, def, url) { var url = url || location.search, svalue = url.match(new RegExp('[\?\&]' + key + '=([^\&]*)(\&?)', 'i')), val = svalue ? svalue[1] : svalue; val = (val === null) ? '' : val; val = decodeURIComponent(val); return val || def || ""; } module.exports = { formatTime: formatTime, getBytesLen: getBytesLen, pregQuote: pregQuote, checkPhone: checkPhone, replaceTag: function (txt, before, after) { txt = txt || ''; txt = txt.replace("{", before); txt = txt.replace("}", after); return txt; }, type: type, isArray: isArray, isFunction: isFunction, isNumeric: isNumeric, isEmptyObject: isEmptyObject, isPlainObject: isPlainObject, extend: extend, getParam:getParam } |
转载请注明:飞飞的个人网站 » javascript 常用方法整理