jslib-tools
Version:
js工具库 封装常用的工具函数 如深拷贝 时间转换日期格式化、浏览器判断等,提高开发效率
282 lines (224 loc) • 10.6 kB
HTML
<html lang="en">
<head>
<meta charset="utf-8">
<title>JSDoc: Source: store/index.js</title>
<script src="scripts/prettify/prettify.js"> </script>
<script src="scripts/prettify/lang-css.js"> </script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>
<body>
<div id="main">
<h1 class="page-title">Source: store/index.js</h1>
<section>
<article>
<pre class="prettyprint source linenums"><code>/*
* @Author: zhangyu
* @Email: zhangdulin@outlook.com
* @Date: 2021-06-23 20:21:44
* @LastEditors: zhangyu
* @LastEditTime: 2021-06-25 17:09:57
* @Description:
*/
import { decode_url_param } from '../transfer/jsonString'
//------------------------------- cookie.js -------------------------------
//解析cookie值
function parseCookieValue(s) {
if (s.indexOf('"') === 0) s = s.slice(1, -1).replace(/\\"/g, '"').replace(/\\\\/g, '\\');
return decode_url_param(s.replace(/\+/g, ' '));
}
//读取cookie值或返回整个对象
/**
* @description:
* @param {*} key
* @return {*}
*/
function getCookie(key) {
var result = key ? undefined : {},
cookies = document.cookie ? document.cookie.split('; ') : [];
for (var i = 0, len = cookies.length; i < len; i++) {
var parts = cookies[i].split('='),
name = decode_url_param(parts[0]),
cookie = parts.slice(1).join('=');
if (key && key === name) {
result = parseCookieValue(cookie);
break;
}
if (!key && (cookie = parseCookieValue(cookie)) !== undefined) {
result[name] = cookie;
}
}
return result;
}
//设置cookie
function setCookie(key, value, ops) {
ops = ops || {};
var expires = ops.expires;
if (typeof expires === "number") expires = new Date().add("d", expires);
document.cookie = [
encode_url_param(key), '=', encode_url_param(value),
expires ? '; expires=' + expires.toUTCString() : '',
ops.path ? '; path=' + ops.path : '',
ops.domain ? '; domain=' + ops.domain : '',
ops.secure ? '; secure' : ''
].join('');
}
//移除cookie
function removeCookie(key) {
if (getCookie(key) != undefined) setCookie(key, '', { expires: -1 });
}
//清空cookie
function clearCookie() {
var cookies = document.cookie ? document.cookie.split('; ') : [];
for (var i = 0, len = cookies.length; i < len; i++) {
var parts = cookies[i].split('='),
key = decode_url_param(parts[0]);
removeCookie(key);
}
}
/**
* @description: 获取设置清除cookie
* @param {*}
* @return {object}
*/
export var cookie = {
get: getCookie,
set: setCookie,
remove: removeCookie,
clear: clearCookie
};
//------------------------------- Storage.js -------------------------------
//type:localStorage | sessionStorage
//useCookie:在其它特性不支持的情况下是否启用cookie模拟
/**
* @description: 获取设置清除Storage
* @param {*} type type:localStorage | sessionStorage
* @param {*} useCookie 是否用cookie 模拟store
* @return {object}
*/
export function storage(type, useCookie) {
var isLocal = type != "sessionStorage";
if (!isLocal && !location.host) return;
var STORE_NAME = type,
storage = window[STORE_NAME],
adapter = storage && "getItem" in storage ? "storage" : null;
if (!adapter) {
var userData = document.documentElement, TEST_KEY = "_TEST_";
try {
//ie userdata
userData.addBehavior('#default#userdata');
//7天后过期
if (isLocal) userData.expires = new Date().add("d", 7).toUTCString();
STORE_NAME = location.hostname || "local";
userData.save(STORE_NAME);
storage = {
getItem: function (key) {
userData.load(STORE_NAME);
return userData.getAttribute(key);
},
setItem: function (key, value) {
userData.setAttribute(key, value);
userData.save(STORE_NAME);
},
removeItem: function (key) {
userData.removeAttribute(key);
userData.save(STORE_NAME);
},
clear: function () {
userData.load(STORE_NAME);
var now = new Date().add("d", -1);
userData.expires = now.toUTCString();
userData.save(STORE_NAME);
}
};
if (storage.getItem(TEST_KEY) === undefined) {
storage.setItem(TEST_KEY, 1);
storage.removeItem(TEST_KEY);
}
adapter = "userdata";
} catch (e) { }
//cookie 模拟
if (!adapter && useCookie) {
storage = {
getItem: getCookie,
//setItem: setCookie,
setItem: isLocal ? function (key, value) {
setCookie(key, value, { expires: 7 });
} : setCookie,
removeItem: removeCookie,
clear: clearCookie
};
adapter = "cookie";
}
}
var support = !!adapter;
var store = {
//是否支持本地缓存
support: support,
//适配器:storage|userdata|cookie|null
adapter: adapter,
//获取本地缓存
get: function (key, isJSON) {
if (support) {
try {
var value = storage.getItem(key);
return isJSON ? (value ? JSON.parse(value) : null) : value;
} catch (e) { }
}
return undefined;
},
//设置本地缓存
set: function (key, value) {
if (support) {
try {
storage.setItem(key, typeof value == "string" ? value : JSON.stringify(value));
return true;
} catch (e) { }
}
return false;
},
//删除本地缓存
remove: function (key) {
if (support) {
try {
storage.removeItem(key);
return true;
} catch (e) { }
}
return false;
},
//清空本地缓存
clear: function () {
if (support) {
try {
storage.clear();
return true;
} catch (e) { }
}
return false;
}
};
return store;
}
export default {
cookie,
storage
}</code></pre>
</article>
</section>
</div>
<nav>
<h2><a href="index.html">Home</a></h2><h3>Global</h3><ul><li><a href="global.html#alias">alias</a></li><li><a href="global.html#arrayMove">arrayMove</a></li><li><a href="global.html#arrayMoveMutate">arrayMoveMutate</a></li><li><a href="global.html#arrayToObject">arrayToObject</a></li><li><a href="global.html#arrayToTree">arrayToTree</a></li><li><a href="global.html#arrFibonacci">arrFibonacci</a></li><li><a href="global.html#arrVals">arrVals</a></li><li><a href="global.html#callHandler">callHandler</a></li><li><a href="global.html#camelize">camelize</a></li><li><a href="global.html#checkInt">checkInt</a></li><li><a href="global.html#checkNum">checkNum</a></li><li><a href="global.html#clipboardObj">clipboardObj</a></li><li><a href="global.html#compareSize">compareSize</a></li><li><a href="global.html#cookie">cookie</a></li><li><a href="global.html#cursortPosition">cursortPosition</a></li><li><a href="global.html#dasherize">dasherize</a></li><li><a href="global.html#dateFormat1">dateFormat1</a></li><li><a href="global.html#dateFormat2">dateFormat2</a></li><li><a href="global.html#debounce">debounce</a></li><li><a href="global.html#decode_url_param">decode_url_param</a></li><li><a href="global.html#def">def</a></li><li><a href="global.html#elDateFormat">elDateFormat</a></li><li><a href="global.html#escapeHTML">escapeHTML</a></li><li><a href="global.html#exportXls">exportXls</a></li><li><a href="global.html#extend">extend</a></li><li><a href="global.html#getBrowserModel">getBrowserModel</a></li><li><a href="global.html#getChangedData">getChangedData</a></li><li><a href="global.html#getChildren">getChildren</a></li><li><a href="global.html#getCookie">getCookie</a></li><li><a href="global.html#getDefaultAvatar">getDefaultAvatar</a></li><li><a href="global.html#getDeviceModel">getDeviceModel</a></li><li><a href="global.html#getHttpBase64">getHttpBase64</a></li><li><a href="global.html#getImgBase64">getImgBase64</a></li><li><a href="global.html#getThumbnails">getThumbnails</a></li><li><a href="global.html#getType">getType</a></li><li><a href="global.html#handleEmoji">handleEmoji</a></li><li><a href="global.html#handleParam">handleParam</a></li><li><a href="global.html#handleText">handleText</a></li><li><a href="global.html#isArray">isArray</a></li><li><a href="global.html#isArrayLike">isArrayLike</a></li><li><a href="global.html#isDiff">isDiff</a></li><li><a href="global.html#isFunc">isFunc</a></li><li><a href="global.html#isInt">isInt</a></li><li><a href="global.html#isNum">isNum</a></li><li><a href="global.html#isObject">isObject</a></li><li><a href="global.html#isObjEmpty">isObjEmpty</a></li><li><a href="global.html#isUInt">isUInt</a></li><li><a href="global.html#isUNum">isUNum</a></li><li><a href="global.html#makeArray">makeArray</a></li><li><a href="global.html#objectConversionToList">objectConversionToList</a></li><li><a href="global.html#objectToArray">objectToArray</a></li><li><a href="global.html#objTools">objTools</a></li><li><a href="global.html#parseTime">parseTime</a></li><li><a href="global.html#setVideoPlay">setVideoPlay</a></li><li><a href="global.html#sleepAction">sleepAction</a></li><li><a href="global.html#sleepSync">sleepSync</a></li><li><a href="global.html#sortMapByKey">sortMapByKey</a></li><li><a href="global.html#storage">storage</a></li><li><a href="global.html#throttle">throttle</a></li><li><a href="global.html#titleize">titleize</a></li><li><a href="global.html#toArray">toArray</a></li><li><a href="global.html#toLower">toLower</a></li><li><a href="global.html#toMap">toMap</a></li><li><a href="global.html#toObjectMap">toObjectMap</a></li><li><a href="global.html#toSimplifiedChinese">toSimplifiedChinese</a></li><li><a href="global.html#toUpper">toUpper</a></li><li><a href="global.html#unescapeHTML">unescapeHTML</a></li></ul>
</nav>
<br class="clear">
<footer>
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.7</a> on Wed Aug 25 2021 17:57:45 GMT+0800 (GMT+08:00)
</footer>
<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>