在平常開發中,咱們常常會用一些工具類方法來實現業務邏輯 下面列舉幾種最經常使用的json
//直接調用輸入想要截取的參數名稱幾個 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; } //示例 let city = getParamFromUrl('city');
//輸入想要檢測的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 Object.prototype.toString.call(item).slice(8, -1); } //返回String Function Boolean Object Number
//獲取document下cookie的具體某個參數值 export function getCookie(key) { if (key === undefined) { return undefined; } let cookies = document.cookie; 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 = ret.replace(/^\"|\'/i, '').replace(/\"|\'$/i, ''); } return ret; }
通常在作APP端開發的時候須要用到一些版本控制,那麼就須要針對版本號來進行對比,高版本或者低版本作一些特殊的邏輯處理,下面就是提供版本對比的方法數組
//傳入要對比的版本號,通常前面一個傳入當前的版本號,後面一個寫上要對比的版本號 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 < lower:-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; }
export function isIphoneX() { // iPhone X、iPhone XS var isIPhoneX = /iphone/gi.test(window.navigator.userAgent) && window.devicePixelRatio && window.devicePixelRatio === 3 && window.screen.width === 375 && window.screen.height === 812; // iPhone XS Max var isIPhoneXSMax = /iphone/gi.test(window.navigator.userAgent) && window.devicePixelRatio && window.devicePixelRatio === 3 && window.screen.width === 414 && window.screen.height === 896; // iPhone XR var isIPhoneXR = /iphone/gi.test(window.navigator.userAgent) && window.devicePixelRatio && window.devicePixelRatio === 2 && window.screen.width === 414 && window.screen.height === 896; if (isIPhoneX || isIPhoneXSMax || isIPhoneXR) { return true; } return false; }