码迷,mamicode.com
首页 > 其他好文 > 详细

数据类型判断和数据类型转换

时间:2019-01-17 23:54:41      阅读:236      评论:0      收藏:0      [点我收藏+]

标签:component   trim   split   div   oca   value   tran   copy   ram   

var utils ={};
var
typeMap = { ‘[object Null]‘: ‘null‘, ‘[object Undefined]‘: ‘undefined‘, ‘[object String]‘: ‘string‘, ‘[object Number]‘: ‘number‘, ‘[object Boolean]‘: ‘boolean‘, ‘[object Function]‘: ‘function‘, ‘[object Date]‘: ‘date‘, ‘[object Array]‘: ‘array‘, ‘[object Object]‘: ‘object‘, }; utils.dataType = function (data) { return typeMap[Object.prototype.toString.call(data)]; }; //是否为数组 utils.isArray = function (arr) { return this.isFunction(Array.isArray) ? Array.isArray(arr) : this.dataType(arr) == ‘array‘; }; //是否为undefined utils.isUndefined = function (arr) { return typeof arr == ‘undefined‘; }; //是否为function utils.isFunction = function (func) { return typeof func == ‘function‘; }; /** * 数组转obj * arr 转换的数组 * deepTranslate 是否深转换 * */ utils.arrayToObj = function (arr, deepTranslate) { var obj = {}; deepTranslate = deepTranslate || false; if (this.isArray(arr) && arr.length > 0) { for (var i = 0, len = arr.length; i < len; i++) { var _arr = arr[i]; if (typeof _arr == ‘object‘) { if (this.isArray(_arr)) { var _obj = {}; var len = _arr.length; if (deepTranslate) { for (var j = 0; j < len; j++) { _obj[j] = (this.arrayToObj(_arr[j])); } } else { _obj = _arr; } obj[i] = _obj; } else if (_arr == null) { obj[i] = _arr; } else { if (this.isUndefined(_arr.k)) { obj[k] = _arr.v; } else if (this.isUndefined(_arr.key)) { obj[key] = _arr.value; } else { obj[i] = _arr; } } } else { obj[i] = arr[i]; } } } return obj; }; /** * 数据合并 * arg_0 合并后被修改数据 * arg_1 合并原数据 * deepClone 是否深克隆 * */ utils.dataCombine = function (arg_0, arg_1, deepClone) { deepClone = deepClone || false; arg_0 = arg_0 || {}; for (var item in arg_1) { if (arg_1.hasOwnProperty(item)) { var copy = arg_1[item]; if (deepClone && this.isArray(copy)) { arg_0[item] = this.dataCombine([], arg_1[item], deepClone); } else if (deepClone && this.dataType(copy) == ‘object‘) { arg_0[item] = this.dataCombine({}, arg_1[item], deepClone); } else if (copy !== undefined) { arg_0[item] = copy; } } } return arg_0; }; /** * 数据克隆 * arg_1 克隆复制原数据 * deepClone 是否深克隆 * */ utils.clone = function (arg_1, deepClone) { deepClone = deepClone || false; var arg_0 = this.isArray(arg_1) ? [] : {}; for (var item in arg_1) { if (arg_1.hasOwnProperty(item)) { var copy = arg_1[item]; if (deepClone && this.isArray(copy)) { arg_0[item] = this.clone(arg_1[item], deepClone); } else if (deepClone && this.dataType(copy) == ‘object‘) { arg_0[item] = this.clone(arg_1[item], deepClone); } else if (copy !== undefined) { arg_0[item] = copy; } } } return arg_0; }; /** * URL解析 * url 地址 * */ utils.getUrlParams = function (url) { url = url || window.location.search; //获取url中"?"符后的字串 var params = {}; if (url.indexOf("?") != -1) { var str = url.split("?")[1]; var strArr = str.split("&"); for (var i = 0; i < strArr.length; i++) { var tmpArr = strArr[i].split("="); params[tmpArr[0]] = decodeURIComponent(tmpArr[1]); } } return params; }; utils.obj2query = function (obj) { var __str = ""; if (typeof obj == "object") { var __arr = []; for (var item in obj) { //modify xuelong .这里不考虑 hasOwnProperty,因为在重庆的盒子上不支持, 所以这里修改成只要是 str 与number 都拼接的模式 if (typeof obj[item] === "string" || typeof obj[item] === "number") { __arr.push(item + "=" + encodeURIComponent(obj[item])); } } __str = __arr.join("&"); } else if (typeof obj === "string" || typeof obj === "number") { __str = obj; } return __str; }; utils.query2obj = function (url) { if (url) { if (url.indexOf("?") != -1) { return utils.getUrlParams(url); } else { return utils.getUrlParams("?" + url); } } }; /** * 占位符补位方法 * str 初始字符串 * num 补位后字符串位数 * zw 占位符 默认为0 * */ utils.addZwToStr = function (str, num, zw) { var len = str.length; num = num || len; zw = zw || 0; for (var i = 0, _len = Math.max(0, num - len); i < _len; i++) { str = zw + ‘‘ + str; } return str; }; /** * 去字符串前后空格方法 * str 初始字符串 * */ utils.trim = function (str) { return str.replace(/(^\s*)|(\s*$)/g, ""); }; function getFinalStyle($dom, property) { var _this = $dom; var s; if (window.getComputedStyle) { s = window.getComputedStyle(_this, null)[property]; } else { s = _this.currentStyle[property]; } return s.replace(/[^0-9.]/g, ‘‘); } function getText($dom) { var s; if ($dom.innerText) { s = $dom.innerText } else { s = $dom.textContent; } return s; } function setText($dom, text) { if ($dom.innerText) { $dom.innerText = text; } else { $dom.textContent = text; } } /** * 字符串截取区分中英文的截取。 * str 初始字符串 * */ utils.ellipsis = function ($dom, num) { var _d = $dom; var _lineH = getFinalStyle(_d, "lineHeight"); _lineH == 0 && (_lineH = getFinalStyle(_d, "fontSize") * 1.5);//默认按1.5倍设置行高 var _h = _lineH * num; var str = getText(_d); while (_h * 3 < getFinalStyle(_d, "height")) { setText(_d, str.substring(0, str.length / 2)); str = getText(_d); } while (_h < _d.clientHeight) { str = getText(_d); setText(_d, str.replace(/(\s)*([a-zA-Z0-9]+|\W)(\.\.\.)?$/, "...")); } };

 

数据类型判断和数据类型转换

标签:component   trim   split   div   oca   value   tran   copy   ram   

原文地址:https://www.cnblogs.com/yaoyao-sun/p/10284879.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!