qiao.ls.js
Advanced tools
Comparing version 1.1.6 to 1.1.7
158
index.js
@@ -1,2 +0,2 @@ | ||
'use strict'; | ||
"use strict"; | ||
@@ -22,15 +22,15 @@ /** | ||
const ls = (name, value, expires) => { | ||
// remove | ||
if (value === null) { | ||
_removeItem(name); | ||
return; | ||
} | ||
// remove | ||
if (value === null) { | ||
_removeItem(name); | ||
return; | ||
} | ||
// get | ||
if (typeof value == 'undefined') { | ||
return _getItem(name); | ||
} | ||
// get | ||
if (typeof value == "undefined") { | ||
return _getItem(name); | ||
} | ||
// set | ||
_setItem(name, value, expires); | ||
// set | ||
_setItem(name, value, expires); | ||
}; | ||
@@ -40,12 +40,12 @@ | ||
function _setItem(name, value, expires) { | ||
if (!localStorage) { | ||
console.log('unsupport localStorage'); | ||
return; | ||
} | ||
if (!localStorage) { | ||
console.log("unsupport localStorage"); | ||
return; | ||
} | ||
let obj = {}; | ||
obj.value = value; | ||
if (expires) obj.expires = Date.now() + expires; | ||
let obj = {}; | ||
obj.value = value; | ||
if (expires) obj.expires = Date.now() + expires; | ||
localStorage.setItem(name, JSON.stringify(obj)); | ||
localStorage.setItem(name, JSON.stringify(obj)); | ||
} | ||
@@ -55,23 +55,23 @@ | ||
function _getItem(name) { | ||
if (!localStorage) { | ||
console.log('unsupport localStorage'); | ||
return; | ||
} | ||
if (!localStorage) { | ||
console.log("unsupport localStorage"); | ||
return; | ||
} | ||
const objStr = localStorage.getItem(name); | ||
let obj; | ||
try { | ||
obj = JSON.parse(objStr); | ||
} catch (e) { | ||
console.log('json parse error:'); | ||
console.log(e); | ||
} | ||
if (!obj) return; | ||
const objStr = localStorage.getItem(name); | ||
let obj; | ||
try { | ||
obj = JSON.parse(objStr); | ||
} catch (e) { | ||
console.log("json parse error:"); | ||
console.log(e); | ||
} | ||
if (!obj) return; | ||
if (obj.expires && obj.expires < Date.now()) { | ||
localStorage.removeItem(name); | ||
return; | ||
} | ||
if (obj.expires && obj.expires < Date.now()) { | ||
localStorage.removeItem(name); | ||
return; | ||
} | ||
return obj.value; | ||
return obj.value; | ||
} | ||
@@ -81,8 +81,8 @@ | ||
function _removeItem(name) { | ||
if (!localStorage) { | ||
console.log('unsupport localStorage'); | ||
return; | ||
} | ||
if (!localStorage) { | ||
console.log("unsupport localStorage"); | ||
return; | ||
} | ||
localStorage.removeItem(name); | ||
localStorage.removeItem(name); | ||
} | ||
@@ -94,23 +94,23 @@ | ||
const cache = (name, key, value, expires) => { | ||
if (!name) return; | ||
if (!name) return; | ||
// clear | ||
if (key === null) { | ||
_clearCache(name); | ||
return; | ||
} | ||
// clear | ||
if (key === null) { | ||
_clearCache(name); | ||
return; | ||
} | ||
// remove | ||
if (value === null) { | ||
_removeCache(name, key); | ||
return; | ||
} | ||
// remove | ||
if (value === null) { | ||
_removeCache(name, key); | ||
return; | ||
} | ||
// get | ||
if (typeof value == 'undefined') { | ||
return _getCache(name, key); | ||
} | ||
// get | ||
if (typeof value == "undefined") { | ||
return _getCache(name, key); | ||
} | ||
// set | ||
_setCache(name, key, value, expires); | ||
// set | ||
_setCache(name, key, value, expires); | ||
}; | ||
@@ -120,13 +120,13 @@ | ||
function _setCache(name, key, value, exp) { | ||
if (!localStorage) { | ||
console.log('unsupport localStorage'); | ||
return; | ||
} | ||
if (!localStorage) { | ||
console.log("unsupport localStorage"); | ||
return; | ||
} | ||
if (!name || !key) return; | ||
if (!name || !key) return; | ||
let data = ls(name) || {}; | ||
data[key] = value; | ||
let data = ls(name) || {}; | ||
data[key] = value; | ||
ls(name, data, exp || 7 * 24 * 60 * 60 * 1000); | ||
ls(name, data, exp || 7 * 24 * 60 * 60 * 1000); | ||
} | ||
@@ -136,8 +136,8 @@ | ||
function _getCache(name, key) { | ||
if (!name || !key) return; | ||
if (!name || !key) return; | ||
const data = ls(name); | ||
if (!data) return; | ||
const data = ls(name); | ||
if (!data) return; | ||
return data[key]; | ||
return data[key]; | ||
} | ||
@@ -147,9 +147,9 @@ | ||
function _removeCache(name, key) { | ||
if (!name || !key) return; | ||
if (!name || !key) return; | ||
let data = ls(name); | ||
if (!data) return; | ||
let data = ls(name); | ||
if (!data) return; | ||
delete data[key]; | ||
ls(name, data); | ||
delete data[key]; | ||
ls(name, data); | ||
} | ||
@@ -159,5 +159,5 @@ | ||
function _clearCache(name) { | ||
if (!name) return; | ||
if (!name) return; | ||
ls(name, null); | ||
ls(name, null); | ||
} | ||
@@ -164,0 +164,0 @@ |
{ | ||
"name": "qiao.ls.js", | ||
"version": "1.1.6", | ||
"description": "local storage expires", | ||
"keywords": [ | ||
"local", | ||
"storage", | ||
"localStorage", | ||
"expires" | ||
], | ||
"author": "uikoo9 <uikoo9@qq.com>", | ||
"homepage": "https://code.insistime.com/qiao.ls.js", | ||
"license": "MIT", | ||
"main": "index.js", | ||
"module": "src/index.js", | ||
"sideEffets": false, | ||
"files": [ | ||
"src" | ||
], | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/uikoo9/qiao-monorepo.git" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/uikoo9/qiao-monorepo/issues" | ||
}, | ||
"scripts": { | ||
"build": "rollup --config _config.rollup.js", | ||
"eslint": "eslint . --ext .js -c _config.eslint.js", | ||
"eslintfix": "eslint . --ext .js --fix -c _config.eslint.js", | ||
"prepublish": "npm run build && npm run eslintfix", | ||
"test": "jest --config _config.jest.js" | ||
}, | ||
"gitHead": "a90554b9aa8312bd0f5dac1b1c2eb297ea126ae5" | ||
"name": "qiao.ls.js", | ||
"version": "1.1.7", | ||
"description": "local storage expires", | ||
"keywords": [ | ||
"local", | ||
"storage", | ||
"localStorage", | ||
"expires" | ||
], | ||
"author": "uikoo9 <uikoo9@qq.com>", | ||
"homepage": "https://code.insistime.com/qiao.ls.js", | ||
"license": "MIT", | ||
"main": "index.js", | ||
"module": "src/index.js", | ||
"sideEffets": false, | ||
"files": [ | ||
"src" | ||
], | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/uikoo9/qiao-monorepo.git" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/uikoo9/qiao-monorepo/issues" | ||
}, | ||
"scripts": { | ||
"build": "rollup --config _config.rollup.js", | ||
"test": "jest --config _config.jest.js" | ||
}, | ||
"gitHead": "10e87c4bb8a4dd7bde6ea4aac9ee50aa2ecb9fc2" | ||
} |
## qiao.ls.js | ||
[![npm version](https://img.shields.io/npm/v/qiao.ls.js.svg?style=flat-square)](https://www.npmjs.org/package/qiao.ls.js) | ||
@@ -6,5 +7,6 @@ [![npm downloads](https://img.shields.io/npm/dm/qiao.ls.js.svg?style=flat-square)](https://npm-stat.com/charts.html?package=qiao.ls.js) | ||
浏览器localStorage本地存储常见api封装和增强,详情:[一篇文章学会LocalStorage](https://blog.insistime.com/localstorage) | ||
浏览器 localStorage 本地存储常见 api 封装和增强,详情:[一篇文章学会 LocalStorage](https://blog.insistime.com/localstorage) | ||
## install | ||
```bash | ||
@@ -15,39 +17,47 @@ npm i qiao.ls.js | ||
## use | ||
```javascript | ||
// cjs | ||
const q = require('qiao.ls.js'); | ||
q.ls | ||
const q = require("qiao.ls.js"); | ||
q.ls; | ||
// mjs | ||
import { ls } from 'qiao.ls.js'; | ||
import { ls } from "qiao.ls.js"; | ||
``` | ||
## api | ||
### set | ||
```javascript | ||
// set | ||
q.ls('name', 'value'); | ||
q.ls("name", "value"); | ||
``` | ||
### set with expires | ||
```javascript | ||
// set 10s expires | ||
q.ls('name', 'value', 10 * 1000); | ||
q.ls("name", "value", 10 * 1000); | ||
``` | ||
### get | ||
```javascript | ||
// get | ||
console.log(q.ls('name')); // value | ||
console.log(q.ls("name")); // value | ||
``` | ||
### delete | ||
```javascript | ||
// delete | ||
q.ls('name', null); | ||
console.log(q.ls('name')); // undefined | ||
q.ls("name", null); | ||
console.log(q.ls("name")); // undefined | ||
``` | ||
## version | ||
### 0.1.2.20220926 | ||
1. add synk | ||
@@ -58,17 +68,23 @@ 2. modify md | ||
### 0.1.1.20220711 | ||
1. tree shaking | ||
### 0.1.0.20220709 | ||
1. add eslint | ||
1. add eslint | ||
### 0.0.9.20220512 | ||
1. lerna | ||
### 0.0.8.20210215 | ||
1. qls --> q | ||
### 0.0.7.20210214 | ||
1. add jsdoc | ||
### 0.0.6.20210209 | ||
1. add jest | ||
@@ -79,8 +95,11 @@ 2. expires to ms | ||
### 0.0.5.20201022 | ||
1. export ls and cache | ||
### 0.0.4.20200803 | ||
1. ncu | ||
### 0.0.3.20200414 | ||
1. set cache | ||
@@ -93,5 +112,7 @@ 2. get cache | ||
### 0.0.2.20191206 | ||
1. add funding | ||
### 0.0.1.20190624 | ||
1. init project | ||
@@ -98,0 +119,0 @@ 2. set item |
// ls | ||
import { ls } from './_ls.js'; | ||
import { ls } from "./_ls.js"; | ||
// cache | ||
export const cache = (name, key, value, expires) => { | ||
if (!name) return; | ||
if (!name) return; | ||
// clear | ||
if (key === null) { | ||
_clearCache(name); | ||
return; | ||
} | ||
// clear | ||
if (key === null) { | ||
_clearCache(name); | ||
return; | ||
} | ||
// remove | ||
if (value === null) { | ||
_removeCache(name, key); | ||
return; | ||
} | ||
// remove | ||
if (value === null) { | ||
_removeCache(name, key); | ||
return; | ||
} | ||
// get | ||
if (typeof value == 'undefined') { | ||
return _getCache(name, key); | ||
} | ||
// get | ||
if (typeof value == "undefined") { | ||
return _getCache(name, key); | ||
} | ||
// set | ||
_setCache(name, key, value, expires); | ||
// set | ||
_setCache(name, key, value, expires); | ||
}; | ||
@@ -31,13 +31,13 @@ | ||
function _setCache(name, key, value, exp) { | ||
if (!localStorage) { | ||
console.log('unsupport localStorage'); | ||
return; | ||
} | ||
if (!localStorage) { | ||
console.log("unsupport localStorage"); | ||
return; | ||
} | ||
if (!name || !key) return; | ||
if (!name || !key) return; | ||
let data = ls(name) || {}; | ||
data[key] = value; | ||
let data = ls(name) || {}; | ||
data[key] = value; | ||
ls(name, data, exp || 7 * 24 * 60 * 60 * 1000); | ||
ls(name, data, exp || 7 * 24 * 60 * 60 * 1000); | ||
} | ||
@@ -47,8 +47,8 @@ | ||
function _getCache(name, key) { | ||
if (!name || !key) return; | ||
if (!name || !key) return; | ||
const data = ls(name); | ||
if (!data) return; | ||
const data = ls(name); | ||
if (!data) return; | ||
return data[key]; | ||
return data[key]; | ||
} | ||
@@ -58,9 +58,9 @@ | ||
function _removeCache(name, key) { | ||
if (!name || !key) return; | ||
if (!name || !key) return; | ||
let data = ls(name); | ||
if (!data) return; | ||
let data = ls(name); | ||
if (!data) return; | ||
delete data[key]; | ||
ls(name, data); | ||
delete data[key]; | ||
ls(name, data); | ||
} | ||
@@ -70,5 +70,5 @@ | ||
function _clearCache(name) { | ||
if (!name) return; | ||
if (!name) return; | ||
ls(name, null); | ||
ls(name, null); | ||
} |
@@ -20,15 +20,15 @@ /** | ||
export const ls = (name, value, expires) => { | ||
// remove | ||
if (value === null) { | ||
_removeItem(name); | ||
return; | ||
} | ||
// remove | ||
if (value === null) { | ||
_removeItem(name); | ||
return; | ||
} | ||
// get | ||
if (typeof value == 'undefined') { | ||
return _getItem(name); | ||
} | ||
// get | ||
if (typeof value == "undefined") { | ||
return _getItem(name); | ||
} | ||
// set | ||
_setItem(name, value, expires); | ||
// set | ||
_setItem(name, value, expires); | ||
}; | ||
@@ -38,12 +38,12 @@ | ||
function _setItem(name, value, expires) { | ||
if (!localStorage) { | ||
console.log('unsupport localStorage'); | ||
return; | ||
} | ||
if (!localStorage) { | ||
console.log("unsupport localStorage"); | ||
return; | ||
} | ||
let obj = {}; | ||
obj.value = value; | ||
if (expires) obj.expires = Date.now() + expires; | ||
let obj = {}; | ||
obj.value = value; | ||
if (expires) obj.expires = Date.now() + expires; | ||
localStorage.setItem(name, JSON.stringify(obj)); | ||
localStorage.setItem(name, JSON.stringify(obj)); | ||
} | ||
@@ -53,23 +53,23 @@ | ||
function _getItem(name) { | ||
if (!localStorage) { | ||
console.log('unsupport localStorage'); | ||
return; | ||
} | ||
if (!localStorage) { | ||
console.log("unsupport localStorage"); | ||
return; | ||
} | ||
const objStr = localStorage.getItem(name); | ||
let obj; | ||
try { | ||
obj = JSON.parse(objStr); | ||
} catch (e) { | ||
console.log('json parse error:'); | ||
console.log(e); | ||
} | ||
if (!obj) return; | ||
const objStr = localStorage.getItem(name); | ||
let obj; | ||
try { | ||
obj = JSON.parse(objStr); | ||
} catch (e) { | ||
console.log("json parse error:"); | ||
console.log(e); | ||
} | ||
if (!obj) return; | ||
if (obj.expires && obj.expires < Date.now()) { | ||
localStorage.removeItem(name); | ||
return; | ||
} | ||
if (obj.expires && obj.expires < Date.now()) { | ||
localStorage.removeItem(name); | ||
return; | ||
} | ||
return obj.value; | ||
return obj.value; | ||
} | ||
@@ -79,8 +79,8 @@ | ||
function _removeItem(name) { | ||
if (!localStorage) { | ||
console.log('unsupport localStorage'); | ||
return; | ||
} | ||
if (!localStorage) { | ||
console.log("unsupport localStorage"); | ||
return; | ||
} | ||
localStorage.removeItem(name); | ||
localStorage.removeItem(name); | ||
} |
@@ -1,2 +0,2 @@ | ||
export * from './_ls.js'; | ||
export * from './_cache.js'; | ||
export * from "./_ls.js"; | ||
export * from "./_cache.js"; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
118
8770