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
Copy file name to clipboardExpand all lines: CHANGELOG.md
+11-10Lines changed: 11 additions & 10 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,17 +5,18 @@
5
5
### BREAKING CHANGES
6
6
7
7
- The `WebStorage` module is now exclusively available as an ES module (ESM), aligning with the modern JavaScript module standard. Additionally, it is no longer the default export — you must import it using a named import.
Gets the saved item for the specified key from the storage for a specific datastore.
88
+
Saves an item to storage with the specified key. You can store items of any of the following data types as long as data can be serialized to JSON.
89
+
90
+
- String
91
+
- Number
92
+
- Array
93
+
- Object
89
94
90
95
**Throws:**`TypeError` - Throws if `key` is not a string.
91
-
**Returns:**`[any, Error | null]` - 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.
96
+
**Returns:**`[boolean, Error | null]` - 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.
92
97
93
98
| Param | Type | Default | Description |
94
99
| ----- | ---- | ------- | ----------- |
95
-
| key |`string`| - | The key of the item to retrieve. |
100
+
| key |`string`| - | The key under which to store the item. |
101
+
| value |`any`| - | The item to save to the selected storage. |
Saves an item to storage with the specified key. You can store items of any of the following data types as long as data can be serialized to JSON.
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:
106
112
107
-
- String
108
-
- Number
109
-
- Array
110
-
- Object
113
+
-`NaN`, `Infinity`, `-Infinity`, and `undefined` → become null
114
+
- Functions and symbols → are omitted or stored as `null/undefined`
115
+
- Circular references → will throw a `TypeError`
116
+
117
+
For example:
118
+
119
+
```js
120
+
storage.setItem('foo', NaN);
121
+
// Will be stored as: "null"
122
+
123
+
storage.getItem('foo');
124
+
// => [null, null]
125
+
```
126
+
127
+
**Why this matters:**
128
+
129
+
If you store special or non-JSON-safe values, they may not round-trip exactly as expected. This is a deliberate design decision to keep the API simple and compatible with `Storage` constraints. If needed, consider manually encoding such values before storing them.
130
+
131
+
### getItem(key)
132
+
133
+
Gets the saved item for the specified key from the storage for a specific datastore.
111
134
112
135
**Throws:**`TypeError` - Throws if `key` is not a string.
113
-
**Returns:**`[boolean, Error | null]` - 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.
136
+
**Returns:**`[any, Error | null]` - 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.
114
137
115
138
| Param | Type | Default | Description |
116
139
| ----- | ---- | ------- | ----------- |
117
-
| key |`string`| - | The key under which to store the item. |
118
-
| value |`any`| - | The item to save to the selected storage. |
140
+
| key |`string`| - | The key of the item to retrieve. |
Copy file name to clipboardExpand all lines: src/web-storage.js
+19-19Lines changed: 19 additions & 19 deletions
Original file line number
Diff line number
Diff line change
@@ -98,45 +98,45 @@ class WebStorage {
98
98
}
99
99
100
100
/**
101
-
* Gets the saved item for the specified key from the storage for a specific datastore.
101
+
* Saves an item to storage with the specified key.
102
102
*
103
-
* @param {string} key - The key of the item to retrieve.
103
+
* @param {string} key - The key under which to store the item.
104
+
* @param {any} value - The item to save to the selected storage.
104
105
* @throws {TypeError} - Throws if `key` is not a string.
105
-
* @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.
106
+
* @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.
106
107
*/
107
-
getItem(key){
108
+
setItem(key,value){
108
109
if(typeofkey!=='string'){
109
-
thrownewTypeError("Failed to execute 'getItem' on 'Storage': The first argument must be a string.");
110
+
thrownewTypeError("Failed to execute 'setItem' on 'Storage': The first argument must be a string.");
* Saves an item to storage with the specified key.
124
+
* Gets the saved item for the specified key from the storage for a specific datastore.
122
125
*
123
-
* @param {string} key - The key under which to store the item.
124
-
* @param {any} value - The item to save to the selected storage.
126
+
* @param {string} key - The key of the item to retrieve.
125
127
* @throws {TypeError} - Throws if `key` is not a string.
126
-
* @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.
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.
127
129
*/
128
-
setItem(key,value){
130
+
getItem(key){
129
131
if(typeofkey!=='string'){
130
-
thrownewTypeError("Failed to execute 'setItem' on 'Storage': The first argument must be a string.");
132
+
thrownewTypeError("Failed to execute 'getItem' on 'Storage': The first argument must be a string.");
0 commit comments