hyperstorage-js 5.0.5 → 5.0.7
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 -255
- package/dist/index.cjs +7 -4
- package/dist/index.d.ts +1 -1
- package/dist/index.mjs +7 -4
- package/dist/index.umd.js +1 -1
- package/package.json +69 -69
- package/src/index.ts +16 -8
package/README.md
CHANGED
|
@@ -1,255 +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
|
|
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`,
|
|
17
|
-
-
|
|
18
|
-
-
|
|
19
|
-
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
npm
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
pnpm
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
yarn
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
const
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
//
|
|
76
|
-
console.log(userStore.
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
//
|
|
86
|
-
userStore.value = {
|
|
87
|
-
|
|
88
|
-
//
|
|
89
|
-
userStore.
|
|
90
|
-
|
|
91
|
-
//
|
|
92
|
-
userStore.set((v) => (v.theme = '
|
|
93
|
-
|
|
94
|
-
//
|
|
95
|
-
|
|
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: '
|
|
156
|
-
language: string
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
const defaultValue: Settings = { theme: '
|
|
160
|
-
const userStore = new HyperStorage<Settings>('userSettings',
|
|
161
|
-
|
|
162
|
-
// Property 'language' is missing in type '{ theme: "
|
|
163
|
-
userStore.value = { theme: '
|
|
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
|
-
- Returns
|
|
205
|
-
|
|
206
|
-
### `
|
|
207
|
-
|
|
208
|
-
-
|
|
209
|
-
-
|
|
210
|
-
- Returns
|
|
211
|
-
|
|
212
|
-
### `
|
|
213
|
-
|
|
214
|
-
-
|
|
215
|
-
-
|
|
216
|
-
- Returns
|
|
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
|
-
|
|
255
|
-
|
|
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 this all by providing a safe, automatic and efficient wrapper that handles everything for you. You can read/store numbers and objects without any extra steps, lose no performance and improve code readability.
|
|
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 (numbers, `undefined`, `NaN`, etc.) which the Storage API does not support by default.
|
|
17
|
+
- 🛠️ **Utility helpers**: built-in helper methods (like `.set()` and `.isDefault()`) to simplify storage operations.
|
|
18
|
+
- ⚡ **Fast caching**: memory cache avoids repeated JSON convertions.
|
|
19
|
+
- 🔒 **Optional encoding/decoding** hooks to obfuscate data.
|
|
20
|
+
- 🌐 **Custom storage**: works with any object implementing the standard Storage API. (`localStorage`, `sessionStorage`, ...)
|
|
21
|
+
|
|
22
|
+
<br>
|
|
23
|
+
|
|
24
|
+
## Installation
|
|
25
|
+
|
|
26
|
+
```bash
|
|
27
|
+
# npm
|
|
28
|
+
npm install hyperstorage-js
|
|
29
|
+
|
|
30
|
+
# pnpm
|
|
31
|
+
pnpm add hyperstorage-js
|
|
32
|
+
|
|
33
|
+
# yarn
|
|
34
|
+
yarn add hyperstorage-js
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
<br>
|
|
38
|
+
|
|
39
|
+
## Constructor Syntax
|
|
40
|
+
|
|
41
|
+
```ts
|
|
42
|
+
class StorageManager<T> {
|
|
43
|
+
constructor(
|
|
44
|
+
itemName: string,
|
|
45
|
+
defaultValue: T,
|
|
46
|
+
options: {
|
|
47
|
+
encodeFn?: (value: string) => string
|
|
48
|
+
decodeFn?: (value: string) => string
|
|
49
|
+
storage?: Storage
|
|
50
|
+
} = {}
|
|
51
|
+
)
|
|
52
|
+
}
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
<br>
|
|
56
|
+
|
|
57
|
+
## Usage
|
|
58
|
+
|
|
59
|
+
```js
|
|
60
|
+
import HyperStorage from 'hyperstorage-js'
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
```js
|
|
64
|
+
const defaultValue = { theme: 'light', language: 'en' }
|
|
65
|
+
const userStore = new HyperStorage('userSettings', defaultValue)
|
|
66
|
+
|
|
67
|
+
// If 'userSettings' is not present in the Storage, the defaultValue is set:
|
|
68
|
+
console.log(userStore.value) // { theme: 'light', language: 'en' }
|
|
69
|
+
|
|
70
|
+
// Change theme to dark:
|
|
71
|
+
userStore.value = { theme: 'dark', language: 'en' }
|
|
72
|
+
// or
|
|
73
|
+
userStore.set((v) => (v.theme = 'dark'))
|
|
74
|
+
|
|
75
|
+
console.log(userStore.value) // { theme: 'dark', language: 'en' }
|
|
76
|
+
console.log(userStore.value.theme) // 'dark'
|
|
77
|
+
|
|
78
|
+
// Present in localStorage:
|
|
79
|
+
console.log(userStore.storage) // Storage {userSettings: '\x00{"theme":"dark","language":"en"}', length: 1}
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
### Different Ways to Assign a New Value
|
|
83
|
+
|
|
84
|
+
```js
|
|
85
|
+
// Using setter
|
|
86
|
+
userStore.value = { theme: 'dark', language: 'en' }
|
|
87
|
+
|
|
88
|
+
// Change single property using the setter
|
|
89
|
+
userStore.value = { ...userStore.value, theme: 'dark' }
|
|
90
|
+
|
|
91
|
+
// Change single property using a callback
|
|
92
|
+
userStore.set((v) => (v.theme = 'dark'))
|
|
93
|
+
|
|
94
|
+
// Change single property using a property setter
|
|
95
|
+
userStore.set('theme', 'dark')
|
|
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: 'system' | 'light' | 'dark'
|
|
156
|
+
language: string
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
const defaultValue: Settings = { theme: 'system', language: 'en' }
|
|
160
|
+
const userStore = new HyperStorage<Settings>('userSettings', defaultValue)
|
|
161
|
+
|
|
162
|
+
// Property 'language' is missing in type '{ theme: "dark"; }' but required in type 'Settings'. ts(2741)
|
|
163
|
+
userStore.value = { theme: 'dark' }
|
|
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: 'dark' }
|
|
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":"dark"}')
|
|
249
|
+
|
|
250
|
+
// Resynchronize the cache, optionally with a custom decoder
|
|
251
|
+
userStore.sync((value) => JSON.parse(value))
|
|
252
|
+
|
|
253
|
+
console.log(userStore.value) // { theme: 'dark' }
|
|
254
|
+
console.log(userStore.storage) // Storage {userSettings: '\x00{"theme":"dark"}', 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.7";
|
|
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. */
|
|
@@ -26,7 +26,7 @@ class HyperStorage {
|
|
|
26
26
|
* Creates a new HyperStorage instance.
|
|
27
27
|
*
|
|
28
28
|
* @param {string} itemName - The key name under which the data will be stored.
|
|
29
|
-
* @param {T} [defaultValue] - Default value
|
|
29
|
+
* @param {T} [defaultValue] - Default value assigned to the key if it does not exist yet.
|
|
30
30
|
* @param {Object} [options={}] - Optional configuration parameters.
|
|
31
31
|
* @param {(value: string) => string} [options.encodeFn] - Optional function to encode stored values.
|
|
32
32
|
* @param {(value: string) => string} [options.decodeFn] - Optional function to decode stored values.
|
|
@@ -69,11 +69,14 @@ class HyperStorage {
|
|
|
69
69
|
stringValue = value;
|
|
70
70
|
}
|
|
71
71
|
else if (value === undefined ||
|
|
72
|
-
(typeof value === 'number' &&
|
|
72
|
+
(typeof value === 'number' &&
|
|
73
|
+
(isNaN(value) || value === Infinity || value === -Infinity))) {
|
|
73
74
|
// Manually stringify non-JSON values
|
|
74
75
|
stringValue = String(value);
|
|
75
|
-
|
|
76
|
+
}
|
|
77
|
+
else {
|
|
76
78
|
stringValue = '\0' + JSON.stringify(value);
|
|
79
|
+
}
|
|
77
80
|
this.storage.setItem(this.itemName, this.encodeFn(stringValue));
|
|
78
81
|
}
|
|
79
82
|
/**
|
package/dist/index.d.ts
CHANGED
|
@@ -23,7 +23,7 @@ declare class HyperStorage<T> {
|
|
|
23
23
|
* Creates a new HyperStorage instance.
|
|
24
24
|
*
|
|
25
25
|
* @param {string} itemName - The key name under which the data will be stored.
|
|
26
|
-
* @param {T} [defaultValue] - Default value
|
|
26
|
+
* @param {T} [defaultValue] - Default value assigned to the key if it does not exist yet.
|
|
27
27
|
* @param {Object} [options={}] - Optional configuration parameters.
|
|
28
28
|
* @param {(value: string) => string} [options.encodeFn] - Optional function to encode stored values.
|
|
29
29
|
* @param {(value: string) => string} [options.decodeFn] - Optional function to decode stored values.
|
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.7";
|
|
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. */
|
|
@@ -24,7 +24,7 @@ class HyperStorage {
|
|
|
24
24
|
* Creates a new HyperStorage instance.
|
|
25
25
|
*
|
|
26
26
|
* @param {string} itemName - The key name under which the data will be stored.
|
|
27
|
-
* @param {T} [defaultValue] - Default value
|
|
27
|
+
* @param {T} [defaultValue] - Default value assigned to the key if it does not exist yet.
|
|
28
28
|
* @param {Object} [options={}] - Optional configuration parameters.
|
|
29
29
|
* @param {(value: string) => string} [options.encodeFn] - Optional function to encode stored values.
|
|
30
30
|
* @param {(value: string) => string} [options.decodeFn] - Optional function to decode stored values.
|
|
@@ -67,11 +67,14 @@ class HyperStorage {
|
|
|
67
67
|
stringValue = value;
|
|
68
68
|
}
|
|
69
69
|
else if (value === undefined ||
|
|
70
|
-
(typeof value === 'number' &&
|
|
70
|
+
(typeof value === 'number' &&
|
|
71
|
+
(isNaN(value) || value === Infinity || value === -Infinity))) {
|
|
71
72
|
// Manually stringify non-JSON values
|
|
72
73
|
stringValue = String(value);
|
|
73
|
-
|
|
74
|
+
}
|
|
75
|
+
else {
|
|
74
76
|
stringValue = '\0' + JSON.stringify(value);
|
|
77
|
+
}
|
|
75
78
|
this.storage.setItem(this.itemName, this.encodeFn(stringValue));
|
|
76
79
|
}
|
|
77
80
|
/**
|
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.7";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.
|
|
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.7",
|
|
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.1",
|
|
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
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -34,7 +34,7 @@ class HyperStorage<T> {
|
|
|
34
34
|
* Creates a new HyperStorage instance.
|
|
35
35
|
*
|
|
36
36
|
* @param {string} itemName - The key name under which the data will be stored.
|
|
37
|
-
* @param {T} [defaultValue] - Default value
|
|
37
|
+
* @param {T} [defaultValue] - Default value assigned to the key if it does not exist yet.
|
|
38
38
|
* @param {Object} [options={}] - Optional configuration parameters.
|
|
39
39
|
* @param {(value: string) => string} [options.encodeFn] - Optional function to encode stored values.
|
|
40
40
|
* @param {(value: string) => string} [options.decodeFn] - Optional function to decode stored values.
|
|
@@ -55,17 +55,21 @@ class HyperStorage<T> {
|
|
|
55
55
|
) {
|
|
56
56
|
const { encodeFn, decodeFn, storage = window.localStorage } = options
|
|
57
57
|
|
|
58
|
-
if (typeof itemName !== 'string')
|
|
58
|
+
if (typeof itemName !== 'string')
|
|
59
|
+
throw new TypeError('itemName is not a string')
|
|
59
60
|
this.itemName = itemName
|
|
60
61
|
this.defaultValue = defaultValue
|
|
61
62
|
|
|
62
|
-
if (encodeFn && typeof encodeFn !== 'function')
|
|
63
|
+
if (encodeFn && typeof encodeFn !== 'function')
|
|
64
|
+
throw new TypeError('encodeFn is defined but is not a function')
|
|
63
65
|
this.encodeFn = encodeFn || ((v) => v)
|
|
64
66
|
|
|
65
|
-
if (decodeFn && typeof decodeFn !== 'function')
|
|
67
|
+
if (decodeFn && typeof decodeFn !== 'function')
|
|
68
|
+
throw new TypeError('decodeFn is defined but is not a function')
|
|
66
69
|
this.decodeFn = decodeFn || ((v) => v)
|
|
67
70
|
|
|
68
|
-
if (!(storage instanceof Storage))
|
|
71
|
+
if (!(storage instanceof Storage))
|
|
72
|
+
throw new TypeError('storage must be an instance of Storage')
|
|
69
73
|
this.storage = storage
|
|
70
74
|
|
|
71
75
|
this.sync()
|
|
@@ -87,11 +91,15 @@ class HyperStorage<T> {
|
|
|
87
91
|
else stringValue = value
|
|
88
92
|
} else if (
|
|
89
93
|
value === undefined ||
|
|
90
|
-
(typeof value === 'number' &&
|
|
91
|
-
|
|
94
|
+
(typeof value === 'number' &&
|
|
95
|
+
(isNaN(value) || value === Infinity || value === -Infinity))
|
|
96
|
+
) {
|
|
92
97
|
// Manually stringify non-JSON values
|
|
93
98
|
stringValue = String(value)
|
|
94
|
-
else
|
|
99
|
+
} else {
|
|
100
|
+
stringValue = '\0' + JSON.stringify(value)
|
|
101
|
+
}
|
|
102
|
+
|
|
95
103
|
this.storage.setItem(this.itemName, this.encodeFn(stringValue))
|
|
96
104
|
}
|
|
97
105
|
|