1 | storage-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 |
|
8 | Easily store objects or any type of data to localStorage or sessionStorage.<br>
|
9 | Why 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
|
33 | npm i storage-sync-lite
|
34 | ```
|
35 |
|
36 | ## Usage
|
37 |
|
38 | Set and get local:
|
39 | ```javascript
|
40 | import { setLocal, getLocal } from 'storage-sync-lite'
|
41 |
|
42 | // Set/Store
|
43 | setLocal('user', {
|
44 | email: 'user@example.com',
|
45 | age: 25
|
46 | })
|
47 |
|
48 | // Get
|
49 | let user = getLocal('user')
|
50 | console.log(user) // Returns: { email: 'user@example.com', age: 25 }
|
51 | ```
|
52 |
|
53 | Change/update properties from local:
|
54 | ```javascript
|
55 | import { changeLocal, getLocal } from 'storage-sync-lite'
|
56 |
|
57 | // Change/update properties
|
58 | changeLocal('user', {
|
59 | name: 'Mr. User'
|
60 | })
|
61 |
|
62 | console.log(getLocal('user')) // Returns: { email: 'user@example.com', age: 25, name: 'Mr. User' }
|
63 | ```
|
64 |
|
65 | Delete data from local:
|
66 | ```javascript
|
67 | import { deleteLocal, clearLocal } from 'storage-sync-lite'
|
68 |
|
69 | // Delete single data from local
|
70 | deleteLocal('user')
|
71 |
|
72 | // Clear / delete all data from local
|
73 | clearLocal()
|
74 | ```
|
75 |
|
76 | Set and get session:
|
77 | ```javascript
|
78 | import { setSession, getSession } from 'storage-sync-lite'
|
79 |
|
80 | // Set/Store
|
81 | setSession('user', {
|
82 | email: 'user@example.com',
|
83 | age: 25
|
84 | })
|
85 |
|
86 | // Get
|
87 | let user = getSession('user')
|
88 | console.log(user) // Returns: { email: 'user@example.com', age: 25 }
|
89 | ```
|
90 |
|
91 | Change/update properties from session:
|
92 | ```javascript
|
93 | import { changeSession, getSession } from 'storage-sync-lite'
|
94 |
|
95 | // Change/update properties
|
96 | changeSession('user', {
|
97 | name: 'Mr. User'
|
98 | })
|
99 |
|
100 | console.log(getSession('user')) // Returns: { email: 'user@example.com', age: 25, name: 'Mr. User' }
|
101 | ```
|
102 |
|
103 | Delete data from session:
|
104 | ```javascript
|
105 | import { deleteSession, clearSession } from 'storage-sync-lite'
|
106 |
|
107 | // Delete single data from session
|
108 | deleteSession('user')
|
109 |
|
110 | // Clear / delete all data from session
|
111 | clearSession()
|
112 | ```
|
113 |
|
114 | ## API Reference
|
115 |
|
116 | ### `setLocal(name, value, options)`
|
117 |
|
118 | Stores 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 |
|
126 | Returns the stored value.
|
127 |
|
128 | ### `getLocal(name)`
|
129 |
|
130 | Retrieves a value from local storage.
|
131 |
|
132 | - `name` (string): The name of the item to retrieve.
|
133 |
|
134 | Returns the value stored in local storage, or undefined if the item does not exist or has expired.
|
135 |
|
136 | ### `changeLocal(name, changes)`
|
137 |
|
138 | Updates 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 |
|
143 | Returns the updated value.
|
144 |
|
145 | ### `deleteLocal(name)`
|
146 |
|
147 | Deletes an item from local storage.
|
148 |
|
149 | - `name` (string): The name of the item to delete.
|
150 |
|
151 | ### `clearLocal()`
|
152 |
|
153 | Clears all items from local storage.
|
154 |
|
155 | ### `setSession(name, value, options)`
|
156 |
|
157 | Stores 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 |
|
165 | Returns the stored value.
|
166 |
|
167 | ### `getSession(name)`
|
168 |
|
169 | Retrieves a value from session storage.
|
170 |
|
171 | - `name` (string): The name of the item to retrieve.
|
172 |
|
173 | Returns the value stored in session storage, or undefined if the item does not exist or has expired.
|
174 |
|
175 | ### `changeSession(name, changes)`
|
176 |
|
177 | Updates 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 |
|
182 | Returns the updated value.
|
183 |
|
184 | ### `deleteSession(name)`
|
185 |
|
186 | Deletes an item from session storage.
|
187 |
|
188 | - `name` (string): The name of the item to delete.
|
189 |
|
190 | ### `clearSession()`
|
191 |
|
192 | Clears all items from session storage.
|
193 |
|
194 |
|
195 | ## Contributing
|
196 |
|
197 | You 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 |
|
202 | storage-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)|
|