hyperstorage-js 5.0.4 → 5.0.6
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 +267 -254
- package/dist/index.cjs +1 -1
- package/dist/index.mjs +1 -1
- package/dist/index.umd.js +1 -1
- package/package.json +69 -69
package/README.md
CHANGED
|
@@ -1,254 +1,267 @@
|
|
|
1
|
-
# HyperStorage: Storage Manager for JavaScript/TypeScript
|
|
2
|
-
|
|
3
|
-
A lightweight wrapper for Storage interfaces (e.g., `localStorage` or `sessionStorage`) with **efficient caching** and **type-preserving serialization**.
|
|
4
|
-
|
|
5
|
-
The biggest burdens of working with the **Storage API** is verifying values on every read, providing proper default values and only being able to store strings, having to `JSON.stringify()` and `JSON.parse()` manually everytime. This package eliminates all of this by providing a safe and automatic wrapper that handles everything at once. You can read/store numbers and objects without any extra steps and lose no performance.
|
|
6
|
-
|
|
7
|
-
[](https://www.npmjs.com/package/hyperstorage-js)
|
|
8
|
-
[](https://www.npmjs.com/package/hyperstorage-js)
|
|
9
|
-
[](https://www.jsdelivr.com/package/npm/hyperstorage-js)
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
## Features
|
|
14
|
-
|
|
15
|
-
- 📝 **Default values**: are automatically set when the key is not in Storage.
|
|
16
|
-
- 🧩 **JSON support**: automatically serializes and parses objects or non-string primitives (`undefined`, `NaN`, `Infinity`) which the Storage API does not support by default.
|
|
17
|
-
- ⚡ **Fast caching**: memory cache avoids repeated JSON convertions.
|
|
18
|
-
- 🔒 **Optional encoding/decoding** hooks to obfuscate data.
|
|
19
|
-
- 🌐 **Custom storage**: works with any object implementing the standard Storage API. (`localStorage`, `sessionStorage`, ...)
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
## Installation
|
|
24
|
-
|
|
25
|
-
```bash
|
|
26
|
-
# npm
|
|
27
|
-
npm install hyperstorage-js
|
|
28
|
-
|
|
29
|
-
# pnpm
|
|
30
|
-
pnpm add hyperstorage-js
|
|
31
|
-
|
|
32
|
-
# yarn
|
|
33
|
-
yarn add hyperstorage-js
|
|
34
|
-
```
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
## Constructor Syntax
|
|
39
|
-
|
|
40
|
-
```ts
|
|
41
|
-
class StorageManager<T> {
|
|
42
|
-
constructor(
|
|
43
|
-
itemName: string,
|
|
44
|
-
defaultValue: T,
|
|
45
|
-
options: {
|
|
46
|
-
encodeFn?: (value: string) => string
|
|
47
|
-
decodeFn?: (value: string) => string
|
|
48
|
-
storage?: Storage
|
|
49
|
-
} = {}
|
|
50
|
-
)
|
|
51
|
-
}
|
|
52
|
-
```
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
## Usage
|
|
57
|
-
|
|
58
|
-
```js
|
|
59
|
-
import HyperStorage from 'hyperstorage-js'
|
|
60
|
-
```
|
|
61
|
-
|
|
62
|
-
```js
|
|
63
|
-
const defaultValue = { theme: 'dark', language: 'en' }
|
|
64
|
-
const userStore = new HyperStorage('userSettings', defaultValue)
|
|
65
|
-
|
|
66
|
-
// If 'userSettings' is not present in the Storage, the defaultValue is set:
|
|
67
|
-
console.log(userStore.value) // { theme: 'dark', language: 'en' }
|
|
68
|
-
|
|
69
|
-
// Change theme to light:
|
|
70
|
-
userStore.value = { theme: 'light', language: 'en' }
|
|
71
|
-
|
|
72
|
-
console.log(userStore.value) // { theme: 'light' }
|
|
73
|
-
console.log(userStore.value.theme) // 'light'
|
|
74
|
-
|
|
75
|
-
// Present in localStorage:
|
|
76
|
-
console.log(userStore.storage) // Storage {userSettings: '\x00{"theme":"light"}', length: 1}
|
|
77
|
-
```
|
|
78
|
-
|
|
79
|
-
### Different Ways to Assign a New Value
|
|
80
|
-
|
|
81
|
-
```js
|
|
82
|
-
// Overwrite all
|
|
83
|
-
userStore.value = { theme: 'light', language: 'en' }
|
|
84
|
-
|
|
85
|
-
// Overwrite specific
|
|
86
|
-
userStore.value = { ...userStore.value, theme: 'light' }
|
|
87
|
-
|
|
88
|
-
// Overwrite all using callback
|
|
89
|
-
userStore.set((v) => (v = { theme: 'light', language: 'en' }))
|
|
90
|
-
|
|
91
|
-
// Overwrite specific using callback
|
|
92
|
-
userStore.set((v) => (v.theme = 'light'))
|
|
93
|
-
|
|
94
|
-
// Overwrite and store result
|
|
95
|
-
const result = userStore.set((v) => (v.theme = 'light'))
|
|
96
|
-
```
|
|
97
|
-
|
|
98
|
-
### Using Another Storage API
|
|
99
|
-
|
|
100
|
-
Use `sessionStorage` to only remember data for the duration of a session.
|
|
101
|
-
|
|
102
|
-
```js
|
|
103
|
-
const sessionStore = new HyperStorage('sessionData', 'none', {
|
|
104
|
-
storage: window.sessionStorage,
|
|
105
|
-
})
|
|
106
|
-
|
|
107
|
-
sessionStore.value = 'temporary'
|
|
108
|
-
console.log(sessionStore.value) // 'temporary'
|
|
109
|
-
console.log(sessionStore.storage) // Storage {sessionData: 'temporary', length: 1}
|
|
110
|
-
```
|
|
111
|
-
|
|
112
|
-
### Using Encoding and Decoding Functions
|
|
113
|
-
|
|
114
|
-
If you want to make stored data significantly harder to reverse-engineer, you should use the `encodeFn` and `decodeFn` options.
|
|
115
|
-
|
|
116
|
-
Apply Base64 encoding using JavaScript's `btoa` (String to Base64) and `atob` (Base64 to String).
|
|
117
|
-
|
|
118
|
-
```js
|
|
119
|
-
const sessionStore = new HyperStorage('sessionData', 'none', {
|
|
120
|
-
encodeFn: (value) => btoa(value),
|
|
121
|
-
decodeFn: (value) => atob(value),
|
|
122
|
-
})
|
|
123
|
-
|
|
124
|
-
sessionStore.value = 'temporary'
|
|
125
|
-
console.log(sessionStore.value) // 'temporary'
|
|
126
|
-
console.log(sessionStore.storage) // Storage {sessionData: 'hN0IEUdoqmJ/', length: 1}
|
|
127
|
-
```
|
|
128
|
-
|
|
129
|
-
### Resetting Values
|
|
130
|
-
|
|
131
|
-
```js
|
|
132
|
-
sessionStore.reset()
|
|
133
|
-
console.log(sessionStore.
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
console.log(sessionStore.
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
const
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
-
|
|
190
|
-
-
|
|
191
|
-
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
-
|
|
203
|
-
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
-
|
|
209
|
-
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
-
|
|
215
|
-
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
-
|
|
228
|
-
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
1
|
+
# HyperStorage: Storage Manager for JavaScript/TypeScript
|
|
2
|
+
|
|
3
|
+
A lightweight wrapper for Storage interfaces (e.g., `localStorage` or `sessionStorage`) with **efficient caching** and **type-preserving serialization**.
|
|
4
|
+
|
|
5
|
+
The biggest burdens of working with the **Storage API** is verifying values on every read, providing proper default values and only being able to store strings, having to `JSON.stringify()` and `JSON.parse()` manually everytime. This package eliminates all of this by providing a safe and automatic wrapper that handles everything at once. You can read/store numbers and objects without any extra steps and lose no performance.
|
|
6
|
+
|
|
7
|
+
[](https://www.npmjs.com/package/hyperstorage-js)
|
|
8
|
+
[](https://www.npmjs.com/package/hyperstorage-js)
|
|
9
|
+
[](https://www.jsdelivr.com/package/npm/hyperstorage-js)
|
|
10
|
+
|
|
11
|
+
<br>
|
|
12
|
+
|
|
13
|
+
## Features
|
|
14
|
+
|
|
15
|
+
- 📝 **Default values**: are automatically set when the key is not in Storage.
|
|
16
|
+
- 🧩 **JSON support**: automatically serializes and parses objects or non-string primitives (`undefined`, `NaN`, `Infinity`) which the Storage API does not support by default.
|
|
17
|
+
- ⚡ **Fast caching**: memory cache avoids repeated JSON convertions.
|
|
18
|
+
- 🔒 **Optional encoding/decoding** hooks to obfuscate data.
|
|
19
|
+
- 🌐 **Custom storage**: works with any object implementing the standard Storage API. (`localStorage`, `sessionStorage`, ...)
|
|
20
|
+
|
|
21
|
+
<br>
|
|
22
|
+
|
|
23
|
+
## Installation
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
# npm
|
|
27
|
+
npm install hyperstorage-js
|
|
28
|
+
|
|
29
|
+
# pnpm
|
|
30
|
+
pnpm add hyperstorage-js
|
|
31
|
+
|
|
32
|
+
# yarn
|
|
33
|
+
yarn add hyperstorage-js
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
<br>
|
|
37
|
+
|
|
38
|
+
## Constructor Syntax
|
|
39
|
+
|
|
40
|
+
```ts
|
|
41
|
+
class StorageManager<T> {
|
|
42
|
+
constructor(
|
|
43
|
+
itemName: string,
|
|
44
|
+
defaultValue: T,
|
|
45
|
+
options: {
|
|
46
|
+
encodeFn?: (value: string) => string
|
|
47
|
+
decodeFn?: (value: string) => string
|
|
48
|
+
storage?: Storage
|
|
49
|
+
} = {}
|
|
50
|
+
)
|
|
51
|
+
}
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
<br>
|
|
55
|
+
|
|
56
|
+
## Usage
|
|
57
|
+
|
|
58
|
+
```js
|
|
59
|
+
import HyperStorage from 'hyperstorage-js'
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
```js
|
|
63
|
+
const defaultValue = { theme: 'dark', language: 'en' }
|
|
64
|
+
const userStore = new HyperStorage('userSettings', defaultValue)
|
|
65
|
+
|
|
66
|
+
// If 'userSettings' is not present in the Storage, the defaultValue is set:
|
|
67
|
+
console.log(userStore.value) // { theme: 'dark', language: 'en' }
|
|
68
|
+
|
|
69
|
+
// Change theme to light:
|
|
70
|
+
userStore.value = { theme: 'light', language: 'en' }
|
|
71
|
+
|
|
72
|
+
console.log(userStore.value) // { theme: 'light' }
|
|
73
|
+
console.log(userStore.value.theme) // 'light'
|
|
74
|
+
|
|
75
|
+
// Present in localStorage:
|
|
76
|
+
console.log(userStore.storage) // Storage {userSettings: '\x00{"theme":"light"}', length: 1}
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
### Different Ways to Assign a New Value
|
|
80
|
+
|
|
81
|
+
```js
|
|
82
|
+
// Overwrite all
|
|
83
|
+
userStore.value = { theme: 'light', language: 'en' }
|
|
84
|
+
|
|
85
|
+
// Overwrite specific
|
|
86
|
+
userStore.value = { ...userStore.value, theme: 'light' }
|
|
87
|
+
|
|
88
|
+
// Overwrite all using callback
|
|
89
|
+
userStore.set((v) => (v = { theme: 'light', language: 'en' }))
|
|
90
|
+
|
|
91
|
+
// Overwrite specific using callback
|
|
92
|
+
userStore.set((v) => (v.theme = 'light'))
|
|
93
|
+
|
|
94
|
+
// Overwrite and store result
|
|
95
|
+
const result = userStore.set((v) => (v.theme = 'light'))
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
### Using Another Storage API
|
|
99
|
+
|
|
100
|
+
Use `sessionStorage` to only remember data for the duration of a session.
|
|
101
|
+
|
|
102
|
+
```js
|
|
103
|
+
const sessionStore = new HyperStorage('sessionData', 'none', {
|
|
104
|
+
storage: window.sessionStorage,
|
|
105
|
+
})
|
|
106
|
+
|
|
107
|
+
sessionStore.value = 'temporary'
|
|
108
|
+
console.log(sessionStore.value) // 'temporary'
|
|
109
|
+
console.log(sessionStore.storage) // Storage {sessionData: 'temporary', length: 1}
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
### Using Encoding and Decoding Functions
|
|
113
|
+
|
|
114
|
+
If you want to make stored data significantly harder to reverse-engineer, you should use the `encodeFn` and `decodeFn` options.
|
|
115
|
+
|
|
116
|
+
Apply Base64 encoding using JavaScript's `btoa` (String to Base64) and `atob` (Base64 to String).
|
|
117
|
+
|
|
118
|
+
```js
|
|
119
|
+
const sessionStore = new HyperStorage('sessionData', 'none', {
|
|
120
|
+
encodeFn: (value) => btoa(value),
|
|
121
|
+
decodeFn: (value) => atob(value),
|
|
122
|
+
})
|
|
123
|
+
|
|
124
|
+
sessionStore.value = 'temporary'
|
|
125
|
+
console.log(sessionStore.value) // 'temporary'
|
|
126
|
+
console.log(sessionStore.storage) // Storage {sessionData: 'hN0IEUdoqmJ/', length: 1}
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
### Resetting Values
|
|
130
|
+
|
|
131
|
+
```js
|
|
132
|
+
sessionStore.reset()
|
|
133
|
+
console.log(sessionStore.defaultValue) // 'none'
|
|
134
|
+
console.log(sessionStore.value) // 'none'
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
### Removing Values
|
|
138
|
+
|
|
139
|
+
Internally uses `Storage.removeItem()` to remove the item from storage and sets the cached value to `undefined`.
|
|
140
|
+
|
|
141
|
+
```js
|
|
142
|
+
sessionStore.remove()
|
|
143
|
+
console.log(sessionStore.value) // undefined
|
|
144
|
+
console.log(sessionStore.storage) // Storage {length: 0}
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
<br>
|
|
148
|
+
|
|
149
|
+
## TypeScript Usage
|
|
150
|
+
|
|
151
|
+
### Using Type Parameter `T`
|
|
152
|
+
|
|
153
|
+
```ts
|
|
154
|
+
interface Settings {
|
|
155
|
+
theme: 'dark' | 'light'
|
|
156
|
+
language: string
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
const defaultValue: Settings = { theme: 'dark', language: 'en' }
|
|
160
|
+
const userStore = new HyperStorage<Settings>('userSettings', defaultValue)
|
|
161
|
+
|
|
162
|
+
// Property 'language' is missing in type '{ theme: "light"; }' but required in type 'Settings'. ts(2741)
|
|
163
|
+
userStore.value = { theme: 'light' }
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
### Using `sync()`
|
|
167
|
+
|
|
168
|
+
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
|
+
|
|
170
|
+
```ts
|
|
171
|
+
const current = userStore.sync() // (method): unknown
|
|
172
|
+
|
|
173
|
+
// 'current' is of type 'unknown'. ts(18046)
|
|
174
|
+
console.log(current.theme) // { theme: 'light' }
|
|
175
|
+
|
|
176
|
+
// Must narrow down
|
|
177
|
+
if (current && typeof current === 'object' && 'theme' in current) {
|
|
178
|
+
console.log(current.theme)
|
|
179
|
+
}
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
<br>
|
|
183
|
+
|
|
184
|
+
## API
|
|
185
|
+
|
|
186
|
+
### `constructor<T>(itemName: string, defaultValue: T, options = {})`
|
|
187
|
+
|
|
188
|
+
- **itemName**: `string` — key under which the data is stored.
|
|
189
|
+
- **defaultValue**: default value to be stored if none exists.
|
|
190
|
+
- **options** _(optional)_:
|
|
191
|
+
- `encodeFn` — function to encode values before writing to the `Storage`.
|
|
192
|
+
- `decodeFn` — function to decode values when reading from the `Storage`.
|
|
193
|
+
- `storage` — a `Storage` instance (e.g., `localStorage` or `sessionStorage`).
|
|
194
|
+
|
|
195
|
+
### `value`
|
|
196
|
+
|
|
197
|
+
- **Getter** — returns the cached value (very fast, does not use `JSON.parse`).
|
|
198
|
+
- **Setter** — sets and caches the value, serializing and encoding it into `Storage`.
|
|
199
|
+
|
|
200
|
+
### `set(callback: (value: T) => T): T`
|
|
201
|
+
|
|
202
|
+
- Updates the stored value using a callback function.
|
|
203
|
+
- The callback receives the current value and must return the new value.
|
|
204
|
+
- Returns the newly stored value.
|
|
205
|
+
|
|
206
|
+
### `reset(): T`
|
|
207
|
+
|
|
208
|
+
- Resets the stored value to `defaultValue`.
|
|
209
|
+
- Updates both `Storage` and internal cache.
|
|
210
|
+
- Returns the restored default value.
|
|
211
|
+
|
|
212
|
+
### `remove(): void`
|
|
213
|
+
|
|
214
|
+
- Removes the key and its value from `Storage`.
|
|
215
|
+
- Sets the internal cache to `undefined`.
|
|
216
|
+
- Returns nothing.
|
|
217
|
+
|
|
218
|
+
### `clear(): void`
|
|
219
|
+
|
|
220
|
+
- Clears **all keys** in `Storage`.
|
|
221
|
+
- Affects all stored data, not just this key.
|
|
222
|
+
- Returns nothing.
|
|
223
|
+
|
|
224
|
+
### `isDefault(): boolean`
|
|
225
|
+
|
|
226
|
+
- Checks whether the cached value equals the configured default.
|
|
227
|
+
- Uses reference comparison for objects and strict equality for primitives.
|
|
228
|
+
- Returns `true` if the current value matches the default, otherwise `false`.
|
|
229
|
+
|
|
230
|
+
```js
|
|
231
|
+
if (userStore.isDefault()) {
|
|
232
|
+
console.log('value equals the default value.')
|
|
233
|
+
}
|
|
234
|
+
```
|
|
235
|
+
|
|
236
|
+
### `sync(decodeFn = this.decodeFn): unknown`
|
|
237
|
+
|
|
238
|
+
If the underlying `Storage` is not modified through the value setter, the internal cache will **not automatically update**. Use `sync()` to synchronize the internal cache with the actual value stored in `Storage`.
|
|
239
|
+
|
|
240
|
+
- **decodeFn** _(optional)_ — a function to decode values when reading (defaults to `this.decodeFn`).
|
|
241
|
+
- Reads the value from storage.
|
|
242
|
+
- Decodes it using `decodeFn`.
|
|
243
|
+
- Updates the internal cache.
|
|
244
|
+
- Returns the synchronized value. The return type is `unknown` because data read from `Storage` cannot be type-checked or trusted at compile time, especially when it may have been modified externally.
|
|
245
|
+
|
|
246
|
+
```js
|
|
247
|
+
// External change to storage (to be avoided)
|
|
248
|
+
localStorage.setItem('userSettings', '{"theme":"blue"}')
|
|
249
|
+
|
|
250
|
+
// Resynchronize the cache, optionally with a custom decoder
|
|
251
|
+
userStore.sync((value) => JSON.parse(value))
|
|
252
|
+
|
|
253
|
+
console.log(userStore.value) // { theme: 'blue' }
|
|
254
|
+
console.log(userStore.storage) // Storage {userSettings: '\x00{"theme":"blue"}', length: 1}
|
|
255
|
+
```
|
|
256
|
+
|
|
257
|
+
<br>
|
|
258
|
+
|
|
259
|
+
## Source
|
|
260
|
+
|
|
261
|
+
[GitHub Repository](https://github.com/Khoeckman/HyperStorage)
|
|
262
|
+
|
|
263
|
+
<br>
|
|
264
|
+
|
|
265
|
+
## License
|
|
266
|
+
|
|
267
|
+
MIT
|
package/dist/index.cjs
CHANGED
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
*/
|
|
10
10
|
class HyperStorage {
|
|
11
11
|
/** Version of the library, injected via Rollup replace plugin. */
|
|
12
|
-
static version = "5.0.
|
|
12
|
+
static version = "5.0.6";
|
|
13
13
|
/** Key name under which the data is stored. */
|
|
14
14
|
itemName;
|
|
15
15
|
/** Default value used when the key does not exist in storage. */
|
package/dist/index.mjs
CHANGED
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
*/
|
|
8
8
|
class HyperStorage {
|
|
9
9
|
/** Version of the library, injected via Rollup replace plugin. */
|
|
10
|
-
static version = "5.0.
|
|
10
|
+
static version = "5.0.6";
|
|
11
11
|
/** Key name under which the data is stored. */
|
|
12
12
|
itemName;
|
|
13
13
|
/** Default value used when the key does not exist in storage. */
|
package/dist/index.umd.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).StorageManager=t()}(this,function(){"use strict";return class{static version="5.0.
|
|
1
|
+
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).StorageManager=t()}(this,function(){"use strict";return class{static version="5.0.6";itemName;defaultValue;encodeFn;decodeFn;storage;#e;constructor(e,t,i={}){const{encodeFn:n,decodeFn:s,storage:o=window.localStorage}=i;if("string"!=typeof e)throw new TypeError("itemName is not a string");if(this.itemName=e,this.defaultValue=t,n&&"function"!=typeof n)throw new TypeError("encodeFn is defined but is not a function");if(this.encodeFn=n||(e=>e),s&&"function"!=typeof s)throw new TypeError("decodeFn is defined but is not a function");if(this.decodeFn=s||(e=>e),!(o instanceof Storage))throw new TypeError("storage must be an instance of Storage");this.storage=o,this.sync()}set value(e){let t;this.#e=e,t="string"==typeof e?"\0"===e[0]?"\0"+e:e:void 0===e||"number"==typeof e&&(isNaN(e)||e===1/0||e===-1/0)?String(e):"\0"+JSON.stringify(e),this.storage.setItem(this.itemName,this.encodeFn(t))}get value(){return this.#e??this.defaultValue}set(e){return this.value=e(this.value)}sync(e=this.decodeFn){let t=this.storage.getItem(this.itemName);if("string"!=typeof t)return this.reset();try{t=e(t)}catch(e){return console.error(e),this.reset()}return"\0"!==t[0]?this.value=t:(t=t.slice(1),"\0"===t[0]?this.value=t:this.value="undefined"===t?void 0:"NaN"===t?NaN:"Infinity"===t?1/0:"-Infinity"===t?-1/0:JSON.parse(t))}reset(){return this.value=this.defaultValue}remove(){this.#e=void 0,this.storage.removeItem(this.itemName)}clear(){this.#e=void 0,this.storage.clear()}isDefault(){return this.#e===this.defaultValue}}});
|
package/package.json
CHANGED
|
@@ -1,69 +1,69 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "hyperstorage-js",
|
|
3
|
-
"version": "5.0.
|
|
4
|
-
"description": "A lightweight wrapper for localStorage/sessionStorage with efficient caching and type-preserving serialization.",
|
|
5
|
-
"license": "MIT",
|
|
6
|
-
"author": "Khoeckman",
|
|
7
|
-
"type": "module",
|
|
8
|
-
"main": "dist/index.umd.js",
|
|
9
|
-
"module": "dist/index.mjs",
|
|
10
|
-
"types": "dist/index.d.ts",
|
|
11
|
-
"exports": {
|
|
12
|
-
".": {
|
|
13
|
-
"types": "./dist/index.d.ts",
|
|
14
|
-
"import": "./dist/index.mjs",
|
|
15
|
-
"require": "./dist/index.cjs",
|
|
16
|
-
"default": "./dist/index.umd.js"
|
|
17
|
-
}
|
|
18
|
-
},
|
|
19
|
-
"files": [
|
|
20
|
-
"src/",
|
|
21
|
-
"dist/"
|
|
22
|
-
],
|
|
23
|
-
"scripts": {
|
|
24
|
-
"test": "exit 0",
|
|
25
|
-
"build": "rollup -c rollup.config.js",
|
|
26
|
-
"prepack": "npm run build"
|
|
27
|
-
},
|
|
28
|
-
"devDependencies": {
|
|
29
|
-
"@rollup/plugin-replace": "^6.0.3",
|
|
30
|
-
"@rollup/plugin-terser": "^0.4.4",
|
|
31
|
-
"@rollup/plugin-typescript": "^12.3.0",
|
|
32
|
-
"@types/node": "^24.10.7",
|
|
33
|
-
"pnpm": "^10.28.0",
|
|
34
|
-
"prettier": "^3.7.4",
|
|
35
|
-
"rollup": "^4.55.1",
|
|
36
|
-
"rollup-plugin-delete": "^3.0.2",
|
|
37
|
-
"rollup-plugin-prettier": "^4.1.2",
|
|
38
|
-
"tslib": "^2.8.1",
|
|
39
|
-
"typescript": "^5.9.3"
|
|
40
|
-
},
|
|
41
|
-
"publishConfig": {
|
|
42
|
-
"access": "public"
|
|
43
|
-
},
|
|
44
|
-
"homepage": "https://github.com/Khoeckman/HyperStorage#readme",
|
|
45
|
-
"bugs": {
|
|
46
|
-
"url": "https://github.com/Khoeckman/HyperStorage/issues"
|
|
47
|
-
},
|
|
48
|
-
"repository": {
|
|
49
|
-
"type": "git",
|
|
50
|
-
"url": "git+https://github.com/Khoeckman/HyperStorage.git"
|
|
51
|
-
},
|
|
52
|
-
"keywords": [
|
|
53
|
-
"localStorage",
|
|
54
|
-
"sessionStorage",
|
|
55
|
-
"storage",
|
|
56
|
-
"utility",
|
|
57
|
-
"javascript",
|
|
58
|
-
"js",
|
|
59
|
-
"typescript",
|
|
60
|
-
"ts",
|
|
61
|
-
"ecmascript",
|
|
62
|
-
"es",
|
|
63
|
-
"umd",
|
|
64
|
-
"browser",
|
|
65
|
-
"client",
|
|
66
|
-
"module",
|
|
67
|
-
"commonjs"
|
|
68
|
-
]
|
|
69
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "hyperstorage-js",
|
|
3
|
+
"version": "5.0.6",
|
|
4
|
+
"description": "A lightweight wrapper for localStorage/sessionStorage with efficient caching and type-preserving serialization.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"author": "Khoeckman",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"main": "dist/index.umd.js",
|
|
9
|
+
"module": "dist/index.mjs",
|
|
10
|
+
"types": "dist/index.d.ts",
|
|
11
|
+
"exports": {
|
|
12
|
+
".": {
|
|
13
|
+
"types": "./dist/index.d.ts",
|
|
14
|
+
"import": "./dist/index.mjs",
|
|
15
|
+
"require": "./dist/index.cjs",
|
|
16
|
+
"default": "./dist/index.umd.js"
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
"files": [
|
|
20
|
+
"src/",
|
|
21
|
+
"dist/"
|
|
22
|
+
],
|
|
23
|
+
"scripts": {
|
|
24
|
+
"test": "exit 0",
|
|
25
|
+
"build": "rollup -c rollup.config.js",
|
|
26
|
+
"prepack": "npm run build"
|
|
27
|
+
},
|
|
28
|
+
"devDependencies": {
|
|
29
|
+
"@rollup/plugin-replace": "^6.0.3",
|
|
30
|
+
"@rollup/plugin-terser": "^0.4.4",
|
|
31
|
+
"@rollup/plugin-typescript": "^12.3.0",
|
|
32
|
+
"@types/node": "^24.10.7",
|
|
33
|
+
"pnpm": "^10.28.0",
|
|
34
|
+
"prettier": "^3.7.4",
|
|
35
|
+
"rollup": "^4.55.1",
|
|
36
|
+
"rollup-plugin-delete": "^3.0.2",
|
|
37
|
+
"rollup-plugin-prettier": "^4.1.2",
|
|
38
|
+
"tslib": "^2.8.1",
|
|
39
|
+
"typescript": "^5.9.3"
|
|
40
|
+
},
|
|
41
|
+
"publishConfig": {
|
|
42
|
+
"access": "public"
|
|
43
|
+
},
|
|
44
|
+
"homepage": "https://github.com/Khoeckman/HyperStorage#readme",
|
|
45
|
+
"bugs": {
|
|
46
|
+
"url": "https://github.com/Khoeckman/HyperStorage/issues"
|
|
47
|
+
},
|
|
48
|
+
"repository": {
|
|
49
|
+
"type": "git",
|
|
50
|
+
"url": "git+https://github.com/Khoeckman/HyperStorage.git"
|
|
51
|
+
},
|
|
52
|
+
"keywords": [
|
|
53
|
+
"localStorage",
|
|
54
|
+
"sessionStorage",
|
|
55
|
+
"storage",
|
|
56
|
+
"utility",
|
|
57
|
+
"javascript",
|
|
58
|
+
"js",
|
|
59
|
+
"typescript",
|
|
60
|
+
"ts",
|
|
61
|
+
"ecmascript",
|
|
62
|
+
"es",
|
|
63
|
+
"umd",
|
|
64
|
+
"browser",
|
|
65
|
+
"client",
|
|
66
|
+
"module",
|
|
67
|
+
"commonjs"
|
|
68
|
+
]
|
|
69
|
+
}
|