前端实用小工具(URL参数截取、JSON判断、数据类型检测、版本号对比等)

开发
在日常开发中,我们经常会用一些工具类方法来实现业务逻辑 下面列举几种最常用的!

              [[345208]]

 URL截取参数

  1. //直接调用输入想要截取的参数名称几个 
  2. export function getParamFromUrl(key) { 
  3.     if (key === undefined) return null
  4.     let search = location.search.substr(1); 
  5.     let mReg = new RegExp('(^|&)' + key + '=([^&]*)(&|$)'); 
  6.     let mValue = search.match(mReg); 
  7.     if (mValue != nullreturn unescape(mValue[2]); 
  8.     return null
  9. //示例 
  10. let city = getParamFromUrl('city'); 

JSON是否为空判断

  1. //输入想要检测的json数据 如果为空返回返回false 
  2. export function isNullObject(model) { 
  3.   if (typeof model === "object") { 
  4.     let hasProp = false
  5.     for (const prop in model) { 
  6.         hasProp = true
  7.         break; 
  8.     } 
  9.     if (hasProp) { 
  10.         return false
  11.     } 
  12.     return true
  13.   } else { 
  14.       throw "model is not object"
  15.   } 

数据类型检测

  1. //检测变量的数据类型 
  2. export function getParamType(item) { 
  3.     if (item === nullreturn null
  4.     if (item === undefined) return undefined; 
  5.     return Object.prototype.toString.call(item).slice(8, -1); 
  6. //返回String Function Boolean Object Number 

获取cookie

  1. //获取document下cookie的具体某个参数值 
  2. export function getCookie(key) { 
  3.     if (key === undefined) { 
  4.         return undefined; 
  5.     } 
  6.     let cookies = document.cookie; 
  7.     let mReg = new RegExp('(^|;)\\s*' + key + '=([^;]*)(;|$)'); 
  8.     let mValue = cookies.match(mReg); 
  9.     let ret = undefined; 
  10.     if (mValue != null) { 
  11.         ret = unescape(mValue[2]); 
  12.     } 
  13.     if (ret !== undefined) { 
  14.         ret = ret.replace(/^\"|\'/i, '').replace(/\"|\'$/i, ''); 
  15.     } 
  16.     return ret; 

版本号对比
一般在做APP端开发的时候需要用到一些版本控制,那么就需要针对版本号来进行对比,高版本或者低版本做一些特殊的逻辑处理,下面就是提供版本对比的方法

  1. //传入要对比的版本号,一般前面一个传入当前的版本号,后面一个写上要对比的版本号 
  2. export function versionCompare(higher, lower) { 
  3.     let sep = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : '.'
  4.  
  5.     let higherAry = higher.split(sep), 
  6.         lowerAry = lower.split(sep); 
  7.     let l = Math.max(higherAry.length, lowerAry.length); 
  8.     for (let i = 0; i < l; i++) { 
  9.         let high = parseInt(higherAry[i] || 0); 
  10.         let low = parseInt(lowerAry[i] || 0); 
  11.         if (high > low) { 
  12.             return 1; 
  13.         } 
  14.         if (high < low) { 
  15.             return -1; 
  16.         } 
  17.     } 
  18.     return 0; 
  19. //返回值  higher > lower: 1;higher = lower: 0;higher < lower:-1 

数组去重

  1. export function arrayUniq(array){ 
  2.     let temp = [];  
  3.     for(var i = 0; i < array.length; i++){ 
  4.         if(temp.indexOf(array[i]) == -1){ 
  5.             temp.push(array[i]); 
  6.         } 
  7.     } 
  8.     return temp

iPhone X系列机型判断

  1. export function isIphoneX() { 
  2.     // iPhone X、iPhone XS 
  3.     var isIPhoneX = 
  4.         /iphone/gi.test(window.navigator.userAgent) && 
  5.         window.devicePixelRatio && 
  6.         window.devicePixelRatio === 3 && 
  7.         window.screen.width === 375 && 
  8.         window.screen.height === 812; 
  9.     // iPhone XS Max 
  10.     var isIPhoneXSMax = 
  11.         /iphone/gi.test(window.navigator.userAgent) && 
  12.         window.devicePixelRatio && 
  13.         window.devicePixelRatio === 3 && 
  14.         window.screen.width === 414 && 
  15.         window.screen.height === 896; 
  16.     // iPhone XR 
  17.     var isIPhoneXR = 
  18.         /iphone/gi.test(window.navigator.userAgent) && 
  19.         window.devicePixelRatio && 
  20.         window.devicePixelRatio === 2 && 
  21.         window.screen.width === 414 && 
  22.         window.screen.height === 896; 
  23.     if (isIPhoneX || isIPhoneXSMax || isIPhoneXR) { 
  24.         return true
  25.     } 
  26.     return false

 

 

责任编辑:姜华 来源: 晨曦大前端
相关推荐

2022-05-17 16:56:33

开发工具前端

2021-11-05 06:57:50

架构工具代码

2009-11-19 08:48:10

Windows 7桌面工具

2020-06-03 10:10:15

阿里巴巴互联网工具

2018-02-02 11:34:04

硬盘数据存放分区

2010-07-01 10:24:30

UML小工具

2009-12-08 14:02:25

Windows 7小工

2021-09-21 09:03:48

微信工具技巧

2011-03-09 09:25:12

CSS3

2021-04-25 05:32:52

Windows10操作系统微软

2014-12-15 14:02:48

iOS版本号苹果

2011-05-26 13:54:04

Json

2011-03-10 09:03:35

Python

2023-04-26 01:21:34

工具URL地址

2011-09-09 15:16:55

Windows7任务栏

2010-11-08 10:07:45

Chrome

2015-07-22 10:09:59

Android M版本号

2023-02-27 14:51:40

MySQL数据库

2017-03-30 16:56:43

Windows 10Windows版本号

2017-11-12 21:00:06

戴尔
点赞
收藏

51CTO技术栈公众号