mm-utils
Version:
mm-utils,JavaScript工具包,日常开发工作常用的公共函数库
213 lines (197 loc) • 4.84 kB
JavaScript
// 浏览器相关处理库
/**
* 字符串转路由query参数,如:?a=1&b=2 =>{a:1, b:2}
* @method query2Object
* @param {string} str - url字符串
* @returns {object}
*/
function query2Object(str) {
let search = decodeURIComponent(str.split("?")[1]).replace(/\+/g, " ");
if (!search) {
return {};
}
let obj = {};
let searchArr = search.split("&");
searchArr.forEach((v) => {
let index = v.indexOf("=");
if (index !== -1) {
let name = v.substring(0, index);
let val = v.substring(index + 1, v.length);
obj[name] = val;
}
});
return obj;
}
/**
* 路由query参数转字符串
* @method object2Qquery
* @param {object} obj - query对象
* @returns {string}
*/
function object2Qquery(obj) {
if (Object.prototype.isPrototypeOf(obj) && Object.keys(obj).length === 0) {
return "对象不能为空";
} else {
let ary = [];
for (let i in obj) {
ary.push(i);
ary.push(`=${obj[i]}&`);
}
let arrUnshift = ary.unshift("?");
let str = ary.join("");
return str.slice(0, str.length - 1);
}
}
/**
* url地址解析
* @method parseUrl
* @param {string} str - url字符串
* @returns {object}
*/
function parseUrl(str) {
str = str || location.href;
let a = document.createElement("a");
a.href = str;
let protocol = a.protocol.replace(":", "");
let port;
if (a.port == "") {
if (protocol == "https") {//默认端口号:https=443,http=80,其他默认端口号默认为空" "
port = 443;
} else if (protocol == "http") {
port = 80;
} else {
port = a.port;
}
} else {
return (port = a.port);
}
let parseObj = {
source: str,
protocol: protocol,
host: a.hostname,
port: port,
query: a.search,
params: (function () {
let ret = {},
seg = a.search.replace(/^\?/, "").split("&"),
len = seg.length,
i = 0,
s;
for (; i < len; i++) {
if (!seg[i]) {
continue;
}
s = seg[i].split("=");
ret[s[0]] = s[1];
}
return ret;
})(),
file: (a.pathname.match(/\/([^\/?#]+)$/i) || [, ""])[1],
hash: a.hash.replace("#", ""),
path: a.pathname.replace(/^([^\/])/, "/$1"),
relative: (a.href.match(/tps?:\/\/[^\/]+(.+)/) || [, ""])[1],
segments: a.pathname.replace(/^\//, "").split("/"),
};
parseObj.hostFull = `${parseObj.protocol}://${parseObj.host}`;
if (parseObj.port) {
parseObj.hostFull += `:${parseObj.port}`;
}
return parseObj;
}
/**
* 判断是否为iphone
* @method isIphone
* @param {string} agent - 浏览器userAgent
* @returns {boolean}
*/
function isIphone(agent = navigator.userAgent) {
return !!agent.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //true为Iphone
}
/**
* 判断是否Android
* @method isAndroid
* @param {string} agent - 浏览器userAgent
* @returns {boolean}
*/
function isAndroid(agent = navigator.userAgent) {
return agent.indexOf("Android") > -1 || agent.indexOf("Linux") > -1; //true为Android
}
/**
* 判断是否为wechat
* @method isWechat
* @param {string} agent - 浏览器userAgent
* @returns {boolean}
*/
function isWechat(agent = navigator.userAgent) {
return agent.toLowerCase().indexOf("micromessenger") > -1;
}
/**
* 判断是否为ie
* @method isIE
* @param {string} agent - 浏览器userAgent
* @returns {boolean}
*/
function isIE(agent = navigator.userAgent) {
return agent.indexOf("Trident") > -1;
}
/**
* 判断是否为firefox
* @method isFirefox
* @param {string} agent - 浏览器userAgent
* @returns {boolean}
*/
function isFirefox(agent = navigator.userAgent) {
return agent.indexOf("Gecko") > -1 && agent.indexOf("KHTML") > -1; // gecko--火狐内核
}
/**
* 判断是否为opera
* @method isOpera
* @param {string} agent - 浏览器userAgent
* @returns {boolean}
*/
function isOpera(agent = navigator.userAgent) {
return agent.indexOf("AppleWebKit") > -1;
}
/**
* 判断是否为chrome
* @method isChrome
* @param {string} agent - 浏览器userAgent
* @returns {boolean}
*/
function isChrome(agent = navigator.userAgent) {
return agent.indexOf("AppleWebKit") > -1; //AppleWebKit---Chrome内核
}
/**
* 判断是否为safari
* @method isSafari
* @param {string} agent - 浏览器userAgent
* @returns {boolean}
*/
function isSafari(agent = navigator.userAgent) {
return agent.indexOf("AppleWebKit") > -1;
}
/**
* 判断是否userAgent类型
* @method hasUserAgent
* @param {string} str - agentKey
* @param {string} agent - 浏览器userAgent
* @returns {boolean}
*/
function hasUserAgent(str, agent = navigator.userAgent) {
// todo
return;
}
export default {
query2Object,
object2Qquery,
parseUrl,
isIphone,
isAndroid,
isWechat,
isIE,
isFirefox,
isOpera,
isChrome,
isSafari,
hasUserAgent,
};