UNPKG

sun-king

Version:

sunjay个人使用的js工具。更新日期2016/11/21

149 lines (147 loc) 3.56 kB
//-- 基础,类似underscore const _ = { op(){ var op = Object.prototype return { ostring : op.toString, hasOwn : op.hasOwnProperty, } }, isFunction(it){ return this.op().ostring.call(it) === '[object Function]' }, isArray(it) { return this.op().ostring.call(it) === '[object Array]' }, isObject(it) { return this.op().ostring.call(it) === '[object Object]' }, isString(it) { return this.op().ostring.call(it) === '[object String]' }, isUndefine(it) { return this.op().ostring.call(it) === '[object Undefined]' }, isBool(it) { return this.op().ostring.call(it) === '[object Boolean]' }, isNumber(it) { return it%1 >= 0 }, hasArr(val,array){ var res = false array.map((value) => { if(value === val){ res = true } }) return res }, removeArr(val,array){ array.map((value,i) => { value === val && array.splice(i,1) }) return array }, hasProp(obj, prop){ return this.op().hasOwn.call(obj, prop) }, eachProp(obj, func){ for(let prop in obj){ if(this.hasProp(obj, prop)){ if(func(obj[prop], prop)){ break; } } } }, mixin(target, source) { if(source){ this.eachProp(source, function(value, prop) { if (!this.hasProp(target, prop)) { if (this.isObject(value) && value && !this.isArray(value) && !this.isFunction(value) && !(value instanceof RegExp)){ if(!target[prop]){ target[prop] = {}; } _.mixin(target[prop], value); }else{ target[prop] = value; } }else{ target[prop] = value; } }.bind(this)) } return target; }, now(){ return parseInt( new Date() / 1000 ) }, filterSpace(txt){ return txt.replace(/\s/g,'') }, cut(txt){ return this.filterSpace(txt).split('|') }, random(min,max){ var under = max ? min : 0 var top = max || min return parseInt(Math.random()*(top-under+1) + under) }, browser(){ var u = navigator.userAgent var ua = u.toLowerCase() return { ios : !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), android : u.indexOf('Android') > -1 || u.indexOf('Linux') > -1, wechat : ua.match(/MicroMessenger/i) == "micromessenger" } }, width(){ return window.innerWidth || document.body.clientWidth }, height(){ return window.innerHeight || document.body.clientHeight }, //-- 时间戳转日期 Time(input,fmt){ //-- 判断input格式,为空或非数字则取当前时间 var inputTime = parseInt(input+'') inputTime = !!inputTime ? (inputTime+'').length == 10 ? inputTime * 1000 : inputTime : inputTime = new Date() * 1 if(!!fmt){ var date = new Date(inputTime) var o = { "M+" : date.getMonth()+1, //月份 "d+" : date.getDate(), //日 "h+" : date.getHours(), //小时 "m+" : date.getMinutes(), //分 "s+" : date.getSeconds(), //秒 "S" : date.getMilliseconds() //毫秒 } if(/(y+)/.test(fmt)){ fmt = fmt.replace(RegExp.$1, (date.getFullYear()+"").substr(4 - RegExp.$1.length)) } for(var k in o){ if(new RegExp("("+ k +")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length))); } }else{ var differ = new Date() * 1 - input //-- 刚刚 //-- N分钟前 //-- N小时前 //-- 昨天 //-- N天前 //-- yyyy年MM月dd日 console.log(differ) } return fmt }, } module.exports = _