English | 简体中文
Enhanced browser localStorage and sessionStorage, support for setting expiration time, key name prefix, sync/async, functions to convert JS values to JSON strings, functions to convert JSON strings to JS values, encrypt functions, and decrypt functions.
Provides stringify/parse functions, compared to the JSON.stringify/JSON.parse method, additional support for function, regexp, date, undefined, NaN, Infinity, -Infinity, bigint.
Provide encode/decode functions to convert strings to base64 encoding or base64 encoding to strings, which can be used for encryption when security requirements are not high, and when security requirements are high, it is recommended to use your own encryption and decryption functions.
$ npm install web-storage-plus
$ yarn add web-storage-plus
$ pnpm add web-storage-plus
import { setStorage, getStorage, setStorageAsync, getStorageAsync } from 'web-storage-plus'
const data = { name: 'test', data: 'this is a test.' }
setStorage('storage', data)
getStorage<{ name: string; data: string; }>('storage') // { name: 'test', data: 'this is a test.' }
setStorageAsync('test', data).then(() => {
console.log('setStorage success.');
});
getStorageAsync('test').then((data) => {
console.log('getStorage success.', data);
});
import { setStorage, getStorage, setGlobalPrefix, stringify, parse, encode, decode } from 'web-storage-plus'
setGlobalPrefix('test_')
const data = { name: 'test', data: 'this is a test.' }
setStorage('storage', data, {
maxAge: 60 * 60 * 24,
stringifyFn: stringify,
encryptFn: encode
})
getStorage('storage', {
isDeleteExpired: true,
parseFn: parse,
decryptFn: decode
})
Set the given key-value pair, in the form of a JSON string, in localStorage or sessionStorage.
If the options object is provided:
-
options.maxAge
- a number representing the seconds from for expiry(it does not expire by default). -
options.prefix
- a string representing the prefix of the key name(default globalPrefix). -
options.isLocalStorage
- a boolean representing the type of Web Storage(default true). -
options.stringifyFn
- a function representing the function to convert JS values to JSON strings(default globalStringifyFn). -
options.encryptFn
- a function representing the function to encrypt the JSON string(default null).
Get the value of the given key in localStorage or sessionStorage.
If the options object is provided:
-
options.prefix
- a string representing the prefix of the key name(default globalPrefix). -
options.isLocalStorage
- a boolean representing the type of Web Storage(default true). -
options.isDeleteExpired
- a boolean representing whether to delete the expired key-value(default false). -
options.parseFn
- a function representing the function to convert JSON strings to JS values(default globalParseFn). -
options.decryptFn
- a function representing the function to decrypt the encrypted string(default null).
Remove the given key in localStorage or sessionStorage.
If the options object is provided:
-
options.prefix
- a string representing the prefix of the key name(default globalPrefix). -
options.isLocalStorage
- a boolean representing the type of Web Storage(default true).
Set the global prefix of the key name. globalPrefix
default ''
.
enhanced JSON.stringify
, support function, regexp, date, undefined, NaN, Infinity, -Infinity, bigint.
optionally replacing values if a replacer function is specified.
import { stringify } from 'web-storage-plus'
const test = { a: 1,b: 2 }
stringify(test, (key, value) => {
if (key === 'a') {
return value + 1
}
return value
}) // {"a":2,"b":2}
enhanced JSON.parse
, support function, regexp, date, undefined, NaN, Infinity, -Infinity, bigint.
An optional reviver function can be provided to perform a transformation on the resulting object before it is returned.
import { parse } from 'web-storage-plus'
const json = '{"a":1,"b":2}'
parse(json, (key, value) => {
if (key === 'a') {
return value + 1
}
return value
}) // {a: 2, b: 2}
encode string to base64.
decode base64 to string.