You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Update to Node.js 22, docs, and add type improvements
- Bump Node.js version to 22 in .nvmrc and docs
- Improve README with clearer usage, dev, and API docs
- Refine JSDoc types and generics in WebStorage class
- Improve type safety and documentation throughout
Copy file name to clipboardExpand all lines: CHANGELOG.md
+6Lines changed: 6 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -35,12 +35,18 @@
35
35
- Removed support for error callback functions in all methods (`getItem`, `setItem`, `removeItem`, `clear`, `keys`, `length`, `iterate`). Errors must now be handled via the returned tuple.
36
36
- Internal `_keyPrefix` and `_driver` fields are now private class fields (`#keyPrefix`, `#driver`). They are no longer accessible outside the class.
37
37
38
+
## NEW FEATURES
39
+
40
+
- Export type declaration files (`.d.ts`) for TypeScript users, ensuring better type safety and autocompletion support in TypeScript projects.
41
+
38
42
### INTERNAL CHANGES
39
43
40
44
- Rewrite to use native class private fields.
41
45
- Internal noopStorage fallback now fully conforms to the Storage interface.
42
46
- Drop Jest in favor of @web/test-runner and Playwright for testing.
Copy file name to clipboardExpand all lines: README.md
+43-11Lines changed: 43 additions & 11 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,11 +7,11 @@
7
7
8
8
# WebStorage
9
9
10
-
WebStorage is a lightweight JavaScript library that improves how you work with `localStorage` or `sessionStorage` by providing a clean, consistent API. It supports storing and retrieving any serializable value (not just strings) by automatically handling JSON encoding and decoding internally.
10
+
`WebStorage` is a lightweight JavaScript library that improves how you work with `localStorage` or `sessionStorage` by providing a clean, consistent API. It supports storing and retrieving any serializable value (not just strings) by automatically handling JSON encoding and decoding internally.
11
11
12
-
A key feature of WebStorage is its use of namespacing via a configurable key prefix (default: `'web-storage/'`). This ensures that all stored items are scoped to your application, preventing collisions with other data in storage. For example, using a prefix like `'my-app/'` means calling clear() will only remove items with that prefix—leaving unrelated data untouched.
12
+
A key feature of `WebStorage` is its use of namespacing via a configurable key prefix (default: `'web-storage/'`). This ensures that all stored items are scoped to your application, preventing collisions with other data in storage. For example, using a prefix like `'my-app/'` means calling clear() will only remove items with that prefix—leaving unrelated data untouched.
13
13
14
-
WebStorage is also designed with error handling in mind. Instead of throwing exceptions, all methods return a `[result, error]` tuple-style value allowing you to handle errors gracefully—or ignore them entirely—without needing `try...catch`.
14
+
`WebStorage` is also designed with error handling in mind. Instead of throwing exceptions, all methods return a `[result, error]` tuple-style value allowing you to handle errors gracefully—or ignore them entirely—without needing `try...catch`.
15
15
16
16
## Install
17
17
@@ -33,7 +33,7 @@ import { WebStorage } from '@georapbox/web-storage';
33
33
34
34
### new WebStorage(options = {})
35
35
36
-
Creates a new instance of the WebStorage with the specified options. The following options can be set:
36
+
Creates a new instance of the `WebStorage` with the specified options. The following options can be set:
37
37
38
38
| Option | Type | Default | Description |
39
39
| ------ | ---- | ------- | ----------- |
@@ -53,7 +53,7 @@ const myStore = new WebStorage({
53
53
54
54
### WebStorage.createInstance(options = {})
55
55
56
-
Same as the constructor, but returns a new instance of `WebStorage`. This is a convenience method to create an instance without using the `new` keyword.
56
+
Creates and returns a new `WebStorage` instance, just like the constructor. This convenience method allows you to instantiate without using the `new` keyword.
WebStorage uses `JSON.stringify()` internally to serialize values before saving them. While this supports most common JavaScript types, some special values are silently converted:
111
+
`WebStorage` uses `JSON.stringify()` internally to serialize values before saving them. While this supports most common JavaScript types, some special values are silently converted:
112
112
113
113
-`NaN`, `Infinity`, `-Infinity`, and `undefined` → become null
114
114
- Functions and symbols → are omitted or stored as `null/undefined`
Iterates over all saved items in storage for a specific datastore and execute a callback function for each key-value pair.
204
204
205
+
> [!IMPORTANT]
206
+
> `iterate` does not guarantee the order of iteration. The order may vary depending on the browser implementation and storage driver used.
207
+
205
208
**Throws:**`TypeError` - Throws if `callback` is not a function.
206
209
**Returns:**`[boolean, Error | null]` - Returns an array with two elements: the first is `true` if the iteration was successful, or `false` if it was not, and the second is `null` if no error occurred, or an `Error` object if an error occurred.
* Creates a new instance of WebStorage with the provided options.
123
+
*
124
+
* @param {WebStorageOptions} [options] - The options to configure the WebStorage instance.
125
+
* @returns {WebStorage} - Returns a new instance of WebStorage.
126
+
*/
127
+
staticcreateInstance(options){
128
+
returnnewWebStorage(options);
129
+
}
130
+
100
131
/**
101
132
* Saves an item to storage with the specified key.
102
133
*
134
+
* @template T
103
135
* @param {string} key - The key under which to store the item.
104
-
* @param {any} value - The item to save to the selected storage.
136
+
* @param {T} value - The item to save to the selected storage.
105
137
* @throws {TypeError} - Throws if `key` is not a string.
106
138
* @returns {Result<boolean>} - Returns an array with two elements: the first is `true` if the item was saved successfully, or `false` if it was not, and the second is `null` if no error occurred, or an `Error` object if an error occurred.
107
139
*/
@@ -123,9 +155,10 @@ class WebStorage {
123
155
/**
124
156
* Gets the saved item for the specified key from the storage for a specific datastore.
125
157
*
158
+
* @template T
126
159
* @param {string} key - The key of the item to retrieve.
127
160
* @throws {TypeError} - Throws if `key` is not a string.
128
-
* @returns {Result<unknown>} - Returns an array with two elements: the first is the value of the saved item, and the second is `null` if no error occurred, or an `Error` object if an error occurred.
161
+
* @returns {Result<T>} - Returns an array with two elements: the first is the value of the saved item, and the second is `null` if no error occurred, or an `Error` object if an error occurred.
* Iterates over all saved items in storage for a specific datastore and execute a callback function for each key-value pair.
209
248
*
210
-
* @param {(value: any, key: string) => void} iteratorCallback - The callback function to execute for each key-value pair.
249
+
* @template T
250
+
* @param {(value: T, key: string) => void} iteratorCallback - The callback function to execute for each key-value pair.
211
251
* @throws {TypeError} - Throws if `iteratorCallback` is not a function.
212
252
* @returns {Result<boolean>} - Returns an array with two elements: the first is `true` if the iteration was successful, or `false` if it was not, and the second is `null` if no error occurred, or an `Error` object if an error occurred.
0 commit comments