hyperstorage-js 5.1.0 β 6.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +47 -20
- package/dist/index.cjs +872 -46
- package/dist/index.d.ts +8 -5
- package/dist/index.mjs +872 -46
- package/dist/index.umd.js +1 -1
- package/package.json +7 -2
- package/src/index.ts +27 -54
package/README.md
CHANGED
|
@@ -13,12 +13,32 @@ The biggest burdens of working with the **Storage API** is verifying values on e
|
|
|
13
13
|
## Features
|
|
14
14
|
|
|
15
15
|
- π **Default values**: are automatically set when the key is not in Storage.
|
|
16
|
-
- π§© **JSON support**: automatically serializes and parses
|
|
17
|
-
-
|
|
16
|
+
- π§© **(Super)JSON support**: automatically serializes and parses everything using [superjson](https://github.com/flightcontrolhq/superjson).
|
|
17
|
+
- π§ **TypeScript support**: full type safety with strongly typed keys, values, and callbacks.
|
|
18
|
+
- π **Optional encoding/decoding**: hooks to obfuscate data or change the serializer.
|
|
18
19
|
- β‘ **Fast caching**: memory cache avoids repeated JSON convertions.
|
|
19
|
-
-
|
|
20
|
+
- π οΈ **Utility helpers**: use `.set()` and `.isDefault()` to simplify storage operations.
|
|
20
21
|
- π **Custom storage**: works with any object implementing the standard Storage API. (`localStorage`, `sessionStorage`, ...)
|
|
21
22
|
|
|
23
|
+
### Supported Types
|
|
24
|
+
|
|
25
|
+
| Type | Supported by Storage API | Supported by HyperStorage (trough superjson) |
|
|
26
|
+
| --------- | ------------------------ | -------------------------------------------- |
|
|
27
|
+
| string | β
| β
|
|
|
28
|
+
| number | β | β
|
|
|
29
|
+
| boolean | β | β
|
|
|
30
|
+
| null | β | β
|
|
|
31
|
+
| Array | β | β
|
|
|
32
|
+
| Object | β | β
|
|
|
33
|
+
| undefined | β | β
|
|
|
34
|
+
| bigint | β | β
|
|
|
35
|
+
| Date | β | β
|
|
|
36
|
+
| RegExp | β | β
|
|
|
37
|
+
| Set | β | β
|
|
|
38
|
+
| Map | β | β
|
|
|
39
|
+
| Error | β | β
|
|
|
40
|
+
| URL | β | β
|
|
|
41
|
+
|
|
22
42
|
<br>
|
|
23
43
|
|
|
24
44
|
## Installation
|
|
@@ -44,8 +64,8 @@ class HyperStorage<T> {
|
|
|
44
64
|
itemName: string,
|
|
45
65
|
defaultValue: T,
|
|
46
66
|
options: {
|
|
47
|
-
encodeFn?: (value:
|
|
48
|
-
decodeFn?: (value: string) =>
|
|
67
|
+
encodeFn?: (value: T) => string
|
|
68
|
+
decodeFn?: (value: string) => T
|
|
49
69
|
storage?: Storage
|
|
50
70
|
} = {}
|
|
51
71
|
)
|
|
@@ -70,16 +90,16 @@ console.log(userStore.value) // { theme: 'light', language: 'en' }
|
|
|
70
90
|
// Change theme to dark
|
|
71
91
|
userStore.value = { theme: 'dark', language: 'en' }
|
|
72
92
|
// or
|
|
73
|
-
userStore.set(
|
|
93
|
+
userStore.set('theme', 'dark')
|
|
74
94
|
|
|
75
95
|
console.log(userStore.value) // { theme: 'dark', language: 'en' }
|
|
76
96
|
console.log(userStore.value.theme) // 'dark'
|
|
77
97
|
|
|
78
98
|
// Present in localStorage
|
|
79
|
-
console.log(userStore.storage) // StorageΒ {userSettings: '
|
|
99
|
+
console.log(userStore.storage) // StorageΒ {userSettings: '{"json":{"theme":"dark","language":"en"}}', length: 1}
|
|
80
100
|
```
|
|
81
101
|
|
|
82
|
-
###
|
|
102
|
+
### Advanced Ways to Assign a New Value
|
|
83
103
|
|
|
84
104
|
```js
|
|
85
105
|
// Using setter
|
|
@@ -106,12 +126,12 @@ const sessionStore = new HyperStorage('sessionData', 'none', {
|
|
|
106
126
|
|
|
107
127
|
sessionStore.value = 'temporary'
|
|
108
128
|
console.log(sessionStore.value) // 'temporary'
|
|
109
|
-
console.log(sessionStore.storage) // Storage
|
|
129
|
+
console.log(sessionStore.storage) // Storage {sessionData: '{"json":"temporary"}', length: 1}
|
|
110
130
|
```
|
|
111
131
|
|
|
112
132
|
### Using Encoding and Decoding Functions
|
|
113
133
|
|
|
114
|
-
If you want to make stored data significantly harder to reverse-engineer,
|
|
134
|
+
If you want to make stored data significantly harder to reverse-engineer, use the `encodeFn` and `decodeFn` options.
|
|
115
135
|
|
|
116
136
|
Apply Base64 encoding using JavaScript's `btoa` (String to Base64) and `atob` (Base64 to String).
|
|
117
137
|
|
|
@@ -123,7 +143,14 @@ const sessionStore = new HyperStorage('sessionData', 'none', {
|
|
|
123
143
|
|
|
124
144
|
sessionStore.value = 'temporary'
|
|
125
145
|
console.log(sessionStore.value) // 'temporary'
|
|
126
|
-
console.log(sessionStore.storage) // Storage
|
|
146
|
+
console.log(sessionStore.storage) // StorageΒ Β {sessionData: 'dGVtcG9yYXJ5', length: 1}
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
The default values for `encodeFn` and `decodeFn` are:
|
|
150
|
+
|
|
151
|
+
```ts
|
|
152
|
+
encodeFn = (value) => HyperStorage.superjson.stringify(value)
|
|
153
|
+
decodeFn = (value) => HyperStorage.superjson.parse<T>(value)
|
|
127
154
|
```
|
|
128
155
|
|
|
129
156
|
### Resetting Values
|
|
@@ -140,7 +167,7 @@ Internally uses `Storage.removeItem()` to remove the item from storage and sets
|
|
|
140
167
|
|
|
141
168
|
```js
|
|
142
169
|
sessionStore.remove()
|
|
143
|
-
console.log(sessionStore.value) //
|
|
170
|
+
console.log(sessionStore.value) // 'none'
|
|
144
171
|
console.log(sessionStore.storage) // StorageΒ {length: 0}
|
|
145
172
|
```
|
|
146
173
|
|
|
@@ -168,6 +195,8 @@ userStore.value = { theme: 'dark' }
|
|
|
168
195
|
Safe usage of `sync()` requires explicit runtime validation before accessing any properties. It quickly becomes clear how type-unsafe `sync()` is and why it should be avoided.
|
|
169
196
|
|
|
170
197
|
```ts
|
|
198
|
+
// ... continues from the above example
|
|
199
|
+
|
|
171
200
|
const result = userStore.sync() // (method): unknown
|
|
172
201
|
// Right now, 'result' equals 'userStore.value' exactly
|
|
173
202
|
|
|
@@ -202,11 +231,11 @@ if (result && typeof result === 'object' && 'theme' in result) {
|
|
|
202
231
|
- **Getter** β returns the cached value (very fast, does not use `JSON.parse`).
|
|
203
232
|
- **Setter** β sets and caches the value, serializing and encoding it into `Storage`.
|
|
204
233
|
|
|
205
|
-
### `set(
|
|
234
|
+
### `set(keyOrCallback: (keyof T) | ((value: T) => T), value?: T[keyof T]): T`
|
|
206
235
|
|
|
207
|
-
-
|
|
208
|
-
-
|
|
209
|
-
- Returns the
|
|
236
|
+
- If a **callback** is provided, it receives the current value and must return the new value.
|
|
237
|
+
- If a **key and value** are provided, it updates that single property on the stored object.
|
|
238
|
+
- Returns the updated stored value.
|
|
210
239
|
|
|
211
240
|
### `reset(): T`
|
|
212
241
|
|
|
@@ -256,7 +285,7 @@ localStorage.setItem('userSettings', '{"theme":"dark"}')
|
|
|
256
285
|
userStore.sync((value) => JSON.parse(value))
|
|
257
286
|
|
|
258
287
|
console.log(userStore.value) // { theme: 'dark' }
|
|
259
|
-
console.log(userStore.storage) // Storage {userSettings: '
|
|
288
|
+
console.log(userStore.storage) // Storage {userSettings: '{"json":{"theme":"dark"}}', length: 1}
|
|
260
289
|
```
|
|
261
290
|
|
|
262
291
|
#### Why `sync()` is unsafe
|
|
@@ -297,9 +326,7 @@ if (
|
|
|
297
326
|
typeof result === 'object' &&
|
|
298
327
|
'theme' in result &&
|
|
299
328
|
'language' in result &&
|
|
300
|
-
(result.theme === 'system' ||
|
|
301
|
-
result.theme === 'light' ||
|
|
302
|
-
result.theme === 'dark') &&
|
|
329
|
+
(result.theme === 'system' || result.theme === 'light' || result.theme === 'dark') &&
|
|
303
330
|
typeof result.language === 'string'
|
|
304
331
|
) {
|
|
305
332
|
// 'result' is of type 'T'
|