html中let函数,前端开发中经常使⽤的⼯具函数在⽇常开发中经常会⽤到的⼯具函数
URL截取参数//直接调⽤输⼊想要截取的参数名称⼏个
export function getParamFromUrl(key) {
if (key === undefined) return null;
let search = location.search.substr(1);
let mReg = new RegExp('(^|&)' + key + '=([^&]*)(&|$)');
let mValue = search.match(mReg);
if (mValue != null) return unescape(mValue[2]);
return null;
}
onpaste不能用input//⽰例
let city = getParamFromUrl('city');
JSON是否为空判断//输⼊想要检测的json数据 如果为空返回返回false
export function isNullObject(model) {
if (typeof model === "object") {
let hasProp = false;
for (const prop in model) {
hasProp = true;
break;
}
if (hasProp) {
return false;
}
return true;
} else {
throw "model is not object";
}
}
数据类型检测//检测变量的数据类型
export function getParamType(item) {
if (item === null) return null;
if (item === undefined) return undefined;
return String.call(item).slice(8, -1);
/
/返回String, Function, Boolean, Object, Number
获取cookie//获取document下cookie的具体某个参数值
export function getCookie(key) {
if (key === undefined) {
return undefined;
}
let cookies = kie;
let mReg = new RegExp('(^|;)\\s*' + key + '=([^;]*)(;|$)');
let mValue = cookies.match(mReg);
let ret = undefined;
if (mValue != null) {
ret = unescape(mValue[2]);
}
if (ret !== undefined) {
ret = place(/^\"|\'/i, '').replace(/\"|\'$/i, '');
}
return ret;
}
版本号对⽐//传⼊要对⽐的版本号,⼀般前⾯⼀个传⼊当前的版本号,后⾯⼀个写上要对⽐的版本号export function versionCompare(higher, lower) {
let sep = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : '.';
let higherAry = higher.split(sep),
lowerAry = lower.split(sep);
let l = Math.max(higherAry.length, lowerAry.length);
for (let i = 0; i < l; i++) {
let high = parseInt(higherAry[i] || 0);
let low = parseInt(lowerAry[i] || 0);
if (high > low) {
return 1;
}
if (high < low) {
return -1;
}
return 0;
}
//返回值 higher>lower: 1; higher=lower: 0; higher<-1>数组去重export function arrayUniq(array){
let temp = [];
for(var i = 0; i < array.length; i++){
if(temp.indexOf(array[i]) == -1){
temp.push(array[i]);
}
}
return temp;
}
iPhone X系列机型判断export function isIphoneX() {
/
/ iPhone X、iPhone XS
var isIPhoneX =
/st(window.navigator.userAgent) && window.devicePixelRatio &&
window.devicePixelRatio === 3 &&
window.screen.width === 375 &&
window.screen.height === 812;
// iPhone XS Max
var isIPhoneXSMax =
/st(window.navigator.userAgent) && window.devicePixelRatio &&
window.devicePixelRatio === 3 &&
window.screen.width === 414 &&
window.screen.height === 896;
// iPhone XR
var isIPhoneXR =
/st(window.navigator.userAgent) && window.devicePixelRatio &&
window.devicePixelRatio === 2 &&
window.screen.width === 414 &&
window.screen.height === 896;
if (isIPhoneX || isIPhoneXSMax || isIPhoneXR) {
return true;
}
return false;
}
input框限制只能输⼊中⽂const input = document.querySelector('input[type="text"]')
const clearText = target => {
const {
value
} = target
target.value = place(/[^u4e00-u9fa5]/g, '')
}
clearText(target)
}
clearText(target)
}
clearText(target)
}
clearText(target)
}
去除字符串中的html代码const removehtml = (str = '') => place(/]*>/ig, '')
console.log(removehtml('
哈哈哈哈')) // 哈哈哈哈
禁⽌⽹页复制粘贴const html = document.querySelector('html')
判断是否是移动端const isMobile = () => 'ontouchstart' in window
字符串前⾯空格去除与替换const trimStart = str => place(new RegExp('^([s]*)(.*)$'), '$2') console.log(trimStart(' abc ')) // abc
console.log(trimStart('123 ')) // 123
字符串后⾯空格去除与替换const trimEnd = str => place(new RegExp('^(.*?)([s]*)$'), '$1') console.log(trimEnd(' abc ')) // abc
console.log(trimEnd('123 ')) // 123
函数柯⾥化const curring = fn => {
const { length } = fn
const curried = (...args) => {
return (args.length >= length
fn(...args)
: (...args2) => curried(...at(args2)))
}
return curried
}
const listMerge = (a, b, c) => [a, b, c]
const curried = curring(listMerge)
console.log(curried(1)(2)(3)) // [1, 2, 3]
console.log(curried(1, 2)(3)) // [1, 2, 3]
console.log(curried(1, 2, 3)) // [1, 2, 3]
-1>
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论