storejs
Version:
Local storage localstorage package provides a simple API
274 lines (205 loc) ⢠14.1 kB
Markdown
<div markdown="1">
<sup>Using <a href="https://wangchujiang.com/#/app" target="_blank">my app</a> is also a way to <a href="https://wangchujiang.com/#/sponsor" target="_blank">support</a> me:</sup>
<br>
<a target="_blank" href="https://apps.apple.com/app/6758053530" title="Scap: Screenshot & Markup Edit for macOS"><img alt="Scap: Screenshot & Markup Edit" height="52" width="52" src="https://wangchujiang.com/appicon/scap.png"></a>
<a target="_blank" href="https://apps.apple.com/app/6757317079" title="Screen Test for macOS"><img alt="Screen Test" height="52" width="52" src="https://wangchujiang.com/appicon/screen-test.png"></a>
<a target="_blank" href="https://apps.apple.com/app/Deskmark/6755948110" title="Deskmark for macOS"><img alt="Deskmark" height="52" width="52" src="https://wangchujiang.com/appicon/deskmark.png"></a>
<a target="_blank" href="https://apps.apple.com/app/Keyzer/6500434773" title="Keyzer for macOS"><img alt="Keyzer" height="52" width="52" src="https://wangchujiang.com/appicon/keyzer.png"></a>
<a target="_blank" href="https://github.com/jaywcjlove/vidwall-hub" title="Vidwall Hub for macOS"><img alt="Vidwall Hub" height="52" width="52" src="https://wangchujiang.com/appicon/vidwall-hub.png"></a>
<a target="_blank" href="https://apps.apple.com/app/VidCrop/6752624705" title="VidCrop for macOS"><img alt="VidCrop" height="52" width="52" src="https://wangchujiang.com/appicon/vidcrop.png"></a>
<a target="_blank" href="https://apps.apple.com/app/Vidwall/6747587746" title="Vidwall for macOS"><img alt="Vidwall" height="52" width="52" src="https://wangchujiang.com/appicon/vidwall.png"></a>
<a target="_blank" href="https://wangchujiang.com/mousio-hint/" title="Mousio Hint for macOS"><img alt="Mousio Hint" height="52" width="52" src="https://wangchujiang.com/appicon/mousio-hint.png"></a>
<a target="_blank" href="https://apps.apple.com/app/6746747327" title="Mousio for macOS"><img alt="Mousio" height="52" width="52" src="https://wangchujiang.com/appicon/mousio.png"></a>
<a target="_blank" href="https://apps.apple.com/app/6745227444" title="Musicer for macOS"><img alt="Musicer" height="52" width="52" src="https://wangchujiang.com/appicon/musicer.png"></a>
<a target="_blank" href="https://apps.apple.com/app/6743841447" title="Audioer for macOS"><img alt="Audioer" height="52" width="52" src="https://wangchujiang.com/appicon/audioer.png"></a>
<a target="_blank" href="https://apps.apple.com/app/6744690194" title="FileSentinel for macOS"><img alt="FileSentinel" height="52" width="52" src="https://wangchujiang.com/appicon/file-sentinel.png"></a>
<a target="_blank" href="https://apps.apple.com/app/6743495172" title="FocusCursor for macOS"><img alt="FocusCursor" height="52" width="52" src="https://wangchujiang.com/appicon/focus-cursor.png"></a>
<a target="_blank" href="https://apps.apple.com/app/6742680573" title="Videoer for macOS"><img alt="Videoer" height="52" width="52" src="https://wangchujiang.com/appicon/videoer.png"></a>
<a target="_blank" href="https://apps.apple.com/app/6740425504" title="KeyClicker for macOS"><img alt="KeyClicker" height="52" width="52" src="https://wangchujiang.com/appicon/key-clicker.png"></a>
<a target="_blank" href="https://apps.apple.com/app/6739052447" title="DayBar for macOS"><img alt="DayBar" height="52" width="52" src="https://wangchujiang.com/appicon/daybar.png"></a>
<a target="_blank" href="https://apps.apple.com/app/6739444407" title="Iconed for macOS"><img alt="Iconed" height="52" width="52" src="https://wangchujiang.com/appicon/iconed.png"></a>
<a target="_blank" href="https://apps.apple.com/app/6737160756" title="Menuist for macOS"><img alt="Menuist" height="52" width="52" src="https://wangchujiang.com/appicon/rightmenu-master.png"></a>
<a target="_blank" href="https://apps.apple.com/app/6723903021" title="Paste Quick for macOS"><img alt="Quick RSS" height="52" width="52" src="https://wangchujiang.com/appicon/paste-quick.png"></a>
<a target="_blank" href="https://apps.apple.com/app/6670696072" title="Quick RSS for macOS/iOS"><img alt="Quick RSS" height="52" width="52" src="https://wangchujiang.com/appicon/quick-rss.png"></a>
<a target="_blank" href="https://apps.apple.com/app/6670167443" title="Web Serve for macOS"><img alt="Web Serve" height="52" width="52" src="https://wangchujiang.com/appicon/web-serve.png"></a>
<a target="_blank" href="https://apps.apple.com/app/6503953628" title="Copybook Generator for macOS/iOS"><img alt="Copybook Generator" height="52" width="52" src="https://wangchujiang.com/appicon/copybook-generator.png"></a>
<a target="_blank" href="https://apps.apple.com/app/6471227008" title="DevTutor for macOS/iOS"><img alt="DevTutor for SwiftUI" height="52" width="52" src="https://wangchujiang.com/appicon/devtutor.png"></a>
<a target="_blank" href="https://apps.apple.com/app/6479819388" title="RegexMate for macOS/iOS"><img alt="RegexMate" height="52" width="52" src="https://wangchujiang.com/appicon/regex-mate.png"></a>
<a target="_blank" href="https://apps.apple.com/app/6479194014" title="Time Passage for macOS/iOS"><img alt="Time Passage" height="52" width="52" src="https://wangchujiang.com/appicon/time-passage.png"></a>
<a target="_blank" href="https://apps.apple.com/app/6478772538" title="IconizeFolder for macOS"><img alt="Iconize Folder" height="52" width="52" src="https://wangchujiang.com/appicon/iconize-folder.png"></a>
<a target="_blank" href="https://apps.apple.com/app/6478511402" title="Textsound Saver for macOS/iOS"><img alt="Textsound Saver" height="52" width="52" src="https://wangchujiang.com/appicon/textsound-saver.png"></a>
<a target="_blank" href="https://apps.apple.com/app/6476924627" title="Create Custom Symbols for macOS"><img alt="Create Custom Symbols" height="52" width="52" src="https://wangchujiang.com/appicon/create-custom-symbols.png"></a>
<a target="_blank" href="https://apps.apple.com/app/6476452351" title="DevHub for macOS"><img alt="DevHub" height="52" width="52" src="https://wangchujiang.com/appicon/devhub.png"></a>
<a target="_blank" href="https://apps.apple.com/app/6476400184" title="Resume Revise for macOS"><img alt="Resume Revise" height="52" width="52" src="https://wangchujiang.com/appicon/resume-revise.png"></a>
<a target="_blank" href="https://apps.apple.com/app/6472593276" title="Palette Genius for macOS"><img alt="Palette Genius" height="52" width="52" src="https://wangchujiang.com/appicon/palette-genius.png"></a>
<a target="_blank" href="https://apps.apple.com/app/6470879005" title="Symbol Scribe for macOS"><img alt="Symbol Scribe" height="52" width="52" src="https://wangchujiang.com/appicon/symbol-scribe.png"></a>
</div>
<hr>
JavaScript localStorage
===
[](https://jaywcjlove.github.io/#/sponsor)
[](https://www.npmjs.com/package/storejs)
[](https://github.com/jaywcjlove/store.js/actions/workflows/ci.yml)
[](https://jaywcjlove.github.io/store.js/lcov-report/)
[](./README-zh.md)
A simple, lightweight JavaScript API for handling browser [localStorage](https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage), it is easy to pick up and use, has a reasonable footprint 2.08kb(gzipped: 0.97kb), and has no dependencies. It should not interfere with any JavaScript libraries or frameworks.
> Old [v1](https://raw.githack.com/jaywcjlove/store.js/doc-v1.1/index.html) version [document preview](https://raw.githack.com/jaywcjlove/store.js/doc-v1.1/index.html).
**Features:**
š Has no dependencies
š± Works in all browsers
š„ Heavily tested
š¦ Supports AMD/CommonJS
š„ [store.min.js](dist/store.min.js) 2.08kb(gzipped: 0.97kb)
Installed via [npm](https://www.npmjs.com/package/storejs). You will need `Node.js` installed on your system.
```bash
$ npm install storejs --save
```
```js
import store from 'storejs';
store('test', 'tank', 1)
```
Or manually download and link `storejs` in your HTML, It can also be downloaded via [UNPKG](https://unpkg.com/storejs/dist/) or [jsDelivr CDN](https://www.jsdelivr.com/package/npm/storejs):
CDN: [UNPKG](https://unpkg.com/storejs/dist/) | [jsDelivr](https://cdn.jsdelivr.net/npm/storejs) | [Githack](https://raw.githack.com/jaywcjlove/store.js/master/dist/store.min.js) | [Statically](https://cdn.statically.io/gh/jaywcjlove/store.js/master/dist/store.min.js) | [bundle.run](https://bundle.run/storejs)
```html
<script src="https://unpkg.com/storejs/dist/store.js"></script>
<script type="text/javascript">
store('test', 'tank');
</script>
```
```js
store(key, data); // Single storage string data
store({key: data, key2: data2}); // Bulk storage of multiple string data
store(key); // Get `key` string data
store('?key'); // Determine if the `key` exists
store(); // Get all key/data
//store(false);š« // (Deprecated) because it is easy to empty the storage because of a null value or an error
//store(key, false); š« // (Deprecated)
store.set(key, data[, overwrite]); // === store(key, data);
store.set({key: data, key2: data2}) // === store({key: data, key2: data});
store.get(key[, alt]); // === store(key);
store.get('?key'); // Determine if the `key` exists
store.get('key1', 'key2', 'key3'); // Get `key1`,`key2`,`key3` data
store.remove(key); // ===store(key,false)
store.clear(); // Clean all key/data
store.keys(); // Returns an array of all the keys
store.forEach(callback); // Loop traversal, return false to end traversal
store.search(string); // Search method
store.len(); // Returns store length
store.has(key); //ā Determine if there is a return true/false
//ā Provide callback method to process data
store('test', (key,val) => {
console.log(val) // Processing the data obtained through the test here
return [3,4,5] // Return data and set store
})
store(['key', 'key2'], (key) => {
// Get data processing of multiple keys, return and save;
console.log('key:', key)
return 'éäøŖę“ę¹ę°ę®'
})
```
Store or delete string data individually `store.set(key, data[, overwrite]);`. Same effect `store(key, data);`.
```js
store.set('wcj', '1') //ā 1
store.set('wcj') //ā Delete `wcj` and string data
```
Get the string data of the `key` `store.get(key[, alt])`. Same effect `store(key)`.
```js
store.get('wcj1') // Get the string data of `wcj1`
store('wcj1') // Same function as above
```
Bulk storage of multiple string data `store.setAll(data[, overwrite])`. Same effect `store({key: data, key2: data});`.
```js
store.setAll({
"wcj1": 123,
"wcj2": 345
}) // Store two string data
store.setAll(["w1", "w2", "w3"])
// Store three strings of data
// 0ā "w1"
// 1ā "w2"
// 2ā "w3"
```
Get all key/data ~~`store.getAll()`~~. Same effect `store()`.
```js
store.getAll() // ā JSON
store() // Same function as above
```
Clear all key/data. `store.clear()`
ā ļø Deprecate ~~`store(false)`~~ because it is easy to empty the library because of passing in a null value or reporting an error
```js
store.clear()
```
Return an array of all `keys`. `store.keys()`.
```js
store.keys() //ā ["w1", "w2", "w3"]
```
Judge whether it exists, return true/false `store.has(key)`.
```js
store.has('w1'); //ā true
```
Delete key string data including key `store.remove(key)`
```js
store.remove('w1'); // Delete w1 and return the value of w1
store('w1', false) // So also delete w1
```
Returns the length of the store `store.len()`
```js
store.len(); //ā 10
```
Loop traversal, return `false` to end the traversal
```js
store.forEach((k, d) => {
console.log(k, d);
if (k== 3) return false
});
```
Responding to storage changes with the [StorageEvent](https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API/Using_the_Web_Storage_API#Responding_to_storage_changes_with_the_StorageEvent)
```js
if (window.addEventListener) {
window.addEventListener('storage', handleStorage,false);
} else if (window.attachEvent){
window.attachEvent('onstorage', handleStorage);
}
function handleStorage(e) {
if(!e) { e=window.event; }
//showStorage();
}
```
| Property | Type | Description |
| ----- | ----- | ----- |
| key | String | The named key that was added, removed, or moddified |
| oldValue | Any | The previous value(now overwritten), or null if a new item was added |
| newValue | Any | The new value, or null if an item was added |
| url/uri | String | The page that called the method that triggered this change |
## Chained Call
```js
store.set('ad', 234).get('ad')
```
## TODO
- [ ] `store.get([key,key2])` Get method, return json
- [ ] `store([key,key2])` Get method, return json
- [ ] `onStorage` Method test cases, and implementation
## Related
- [cookiejs](https://github.com/jaywcjlove/cookie.js) šŖ A simple, lightweight JavaScript API for handling browser cookies , it is easy to pick up and use, has a reasonable footprint(~2kb, gzipped: 0.95kb), and has no dependencies. It should not interfere with any JavaScript libraries or frameworks.
## Contributors
As always, thanks to our amazing contributors!
<a href="https://github.com/jaywcjlove/store.js/graphs/contributors">
<img src="https://jaywcjlove.github.io/store.js/CONTRIBUTORS.svg" />
</a>
Made with [action-contributors](https://github.com/jaywcjlove/github-action-contributors).
Licensed under the MIT License.