驼峰转css属性 aaaBbb=>‘aaa-bbb’
function humpParse(s) {
const reg = /([a-z]+)|([A-Z]{1}[a-z]+)/g;
let r = s.match(reg)
let attr = ''
r.forEach((e,index)=>{
if(index===0){
attr = e;
}else{
e = e.toLowerCase()
attr = attr + '-' + e;
}
})
return attr
}
检查某个数据的数据类型
输入值 | 输出 |
---|
123 | Number |
‘abcdef’ | String |
true | Bollean |
[1, 2, 3, 4] | Array |
{name:‘wenzi’, age:25} | Object |
console.log(‘this is function’) | Function |
undefined | Undefined |
null | Null |
new Date() | Date |
/[a-z]{5,20}$/ | RegExp |
new Error() | Error |
const typeOf = function(value,type){
let r = typeof value
if (r !== 'object') {
if(type){
return r.charAt(0).toUpperCase() + r.slice(1,r.length) == type
}else{
return r.charAt(0).toUpperCase() + r.slice(1,r.length)
}
}else{
if(type){
return Object.prototype.toString.call(value).replace(/^\[object (\S+)\]$/, '$1') == type
}else{
return Object.prototype.toString.call(value).replace(/^\[object (\S+)\]$/, '$1')
}
}
}
export default typeOf
浅拷贝
const copy = function (input) {
let inputType = typeOf(input)
if(inputType!='Array'||inputType!='Object'){
throw new Error('expected Array or Object')
}
let s = JSON.stringify(input)
return JSON.parse(s)
}
export default copy
判断当前浏览器环境是苹果还是安卓 主要用于移动端hack
const IosOrAndroid = function(){
if(typeof navigator == 'undefined'){
throw new Error('Is not currently a browser environment')
}
const u = navigator.userAgent
const isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1
const isIos = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/)
if(isIos){
return 'ios'
}
if(isAndroid){
return 'android'
}
return undefined
}
export default IosOrAndroid
解决ios keyboard 导致页面不弹起问题
const solveIosKeybordBug = function(){
setTimeout(() => {
let height = document.documentElement.scrollTop || document.body.scrollTop
window.scrollTo(0, height + 1)
window.scrollTo(0, height - 1)
}, 17)
}
export default solveIosKeybordBug