UNPKG

5.93 kBMarkdownView Raw
1storage-sync-lite<br>
2[![NPM Version](https://img.shields.io/npm/v/storage-sync-lite.svg?branch=main)](https://www.npmjs.com/package/storage-sync-lite)
3[![Install Size](https://badgen.net/packagephobia/install/storage-sync-lite)](https://packagephobia.now.sh/result?p=storage-sync-lite)
4[![Downloads](https://img.shields.io/npm/dt/storage-sync-lite)](https://www.npmjs.com/package/storage-sync-lite)
5[![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://github.com/SheikhAminul/storage-sync-lite/blob/main/LICENSE)
6================
7
8Easily store objects or any type of data to localStorage or sessionStorage.<br>
9Why this? When you store a JSON/object to localStorage or sessionStorage you need to stringify the data. After storing a number in localStorage when you will read it, you will get the number as a string. But storage-sync-lite allows storing and retrieving data without changing the type. If you store an object when you will read, it will return the object.
10
11
12## Table of Contents
13
14- [Features](#features)
15- [Install](#install)
16- [Usage](#usage)
17- [API Reference](#API-Reference)
18- [Contributing](#contributing)
19- [License](#license)
20- [Author](#author)
21
22
23## Features
24
25- Set, get and delete localStorage or sessionStorage without stringifying or changing the data type.
26- Change/update objects in localStorage or sessionStorage.
27- Clear localStorage or sessionStorage.
28
29
30## Install
31
32```sh
33npm i storage-sync-lite
34```
35
36## Usage
37
38Set and get local:
39```javascript
40import { setLocal, getLocal } from 'storage-sync-lite'
41
42// Set/Store
43setLocal('user', {
44 email: 'user@example.com',
45 age: 25
46})
47
48// Get
49let user = getLocal('user')
50console.log(user) // Returns: { email: 'user@example.com', age: 25 }
51```
52
53Change/update properties from local:
54```javascript
55import { changeLocal, getLocal } from 'storage-sync-lite'
56
57// Change/update properties
58changeLocal('user', {
59 name: 'Mr. User'
60})
61
62console.log(getLocal('user')) // Returns: { email: 'user@example.com', age: 25, name: 'Mr. User' }
63```
64
65Delete data from local:
66```javascript
67import { deleteLocal, clearLocal } from 'storage-sync-lite'
68
69// Delete single data from local
70deleteLocal('user')
71
72// Clear / delete all data from local
73clearLocal()
74```
75
76Set and get session:
77```javascript
78import { setSession, getSession } from 'storage-sync-lite'
79
80// Set/Store
81setSession('user', {
82 email: 'user@example.com',
83 age: 25
84})
85
86// Get
87let user = getSession('user')
88console.log(user) // Returns: { email: 'user@example.com', age: 25 }
89```
90
91Change/update properties from session:
92```javascript
93import { changeSession, getSession } from 'storage-sync-lite'
94
95// Change/update properties
96changeSession('user', {
97 name: 'Mr. User'
98})
99
100console.log(getSession('user')) // Returns: { email: 'user@example.com', age: 25, name: 'Mr. User' }
101```
102
103Delete data from session:
104```javascript
105import { deleteSession, clearSession } from 'storage-sync-lite'
106
107// Delete single data from session
108deleteSession('user')
109
110// Clear / delete all data from session
111clearSession()
112```
113
114## API Reference
115
116### `setLocal(name, value, options)`
117
118Stores a value in local storage.
119
120- `name` (string): The name of the item to store.
121- `value` (*): The value to store.
122- `options` (object, optional): Optional configuration object.
123 - `options.expiration` (string | number | Date): The expiration date/time for the item.
124 - `options.timeToLive` (number): The time to live in milliseconds.
125
126Returns the stored value.
127
128### `getLocal(name)`
129
130Retrieves a value from local storage.
131
132- `name` (string): The name of the item to retrieve.
133
134Returns the value stored in local storage, or undefined if the item does not exist or has expired.
135
136### `changeLocal(name, changes)`
137
138Updates a value in local storage with the provided changes.
139
140- `name` (string): The name of the item to update.
141- `changes` (object): An object containing the changes to apply to the stored value.
142
143Returns the updated value.
144
145### `deleteLocal(name)`
146
147Deletes an item from local storage.
148
149- `name` (string): The name of the item to delete.
150
151### `clearLocal()`
152
153Clears all items from local storage.
154
155### `setSession(name, value, options)`
156
157Stores a value in session storage.
158
159- `name` (string): The name of the item to store.
160- `value` (*): The value to store.
161- `options` (object, optional): Optional configuration object.
162 - `options.expiration` (string | number | Date): The expiration date/time for the item.
163 - `options.timeToLive` (number): The time to live in milliseconds.
164
165Returns the stored value.
166
167### `getSession(name)`
168
169Retrieves a value from session storage.
170
171- `name` (string): The name of the item to retrieve.
172
173Returns the value stored in session storage, or undefined if the item does not exist or has expired.
174
175### `changeSession(name, changes)`
176
177Updates a value in session storage with the provided changes.
178
179- `name` (string): The name of the item to update.
180- `changes` (object): An object containing the changes to apply to the stored value.
181
182Returns the updated value.
183
184### `deleteSession(name)`
185
186Deletes an item from session storage.
187
188- `name` (string): The name of the item to delete.
189
190### `clearSession()`
191
192Clears all items from session storage.
193
194
195## Contributing
196
197You are welcome to contribute! If you are adding a feature or fixing a bug, please contribute to the [GitHub repository](https://github.com/SheikhAminul/storage-sync-lite/).
198
199
200## License
201
202storage-sync-lite is licensed under the [MIT license](https://github.com/SheikhAminul/storage-sync-lite/blob/main/LICENSE).
203
204
205## Author
206
207|[![@SheikhAminul](https://avatars.githubusercontent.com/u/25372039?v=4&s=96)](https://github.com/SheikhAminul)|
208|:---:|
209|[@SheikhAminul](https://github.com/SheikhAminul)|