json-storage-formatter 3.0.1 β 3.0.2
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 +38 -53
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,26 +1,10 @@
|
|
|
1
|
-
const README = `
|
|
2
|
-
|
|
3
1
|
# json-storage-formatter π
|
|
4
2
|
|
|
5
3
|

|
|
6
4
|
|
|
7
|
-
A **lightweight solution** to
|
|
8
|
-
|
|
9
|
-
---
|
|
10
|
-
|
|
11
|
-
## π€ The Problem
|
|
12
|
-
|
|
13
|
-
When working with tools like **localStorage**, **sessionStorage**, **AsyncStorage**, or even databases like **Redis** or **PostgreSQL (JSON columns)**,
|
|
14
|
-
we often need to store application state or configuration objects as strings using `JSON.stringify`.
|
|
15
|
-
|
|
16
|
-
But thereβs a catch:
|
|
17
|
-
`JSON.stringify` has no idea what to do with values like `Date`, `Map`, or `Set`... It just flattens them into empty objects or strings, and when you parse them back with `JSON.parse`, itβs too late.
|
|
5
|
+
A **lightweight solution** to format complex JavaScript objects for string-based storage systems **without losing their types**. π
|
|
18
6
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
---
|
|
22
|
-
|
|
23
|
-
### π Example: Losing Real Data
|
|
7
|
+
## π€ The Problem?
|
|
24
8
|
|
|
25
9
|
```ts
|
|
26
10
|
const userProfile = {
|
|
@@ -45,24 +29,12 @@ console.log(JSON.stringify(userProfile, null, 2));
|
|
|
45
29
|
}
|
|
46
30
|
```
|
|
47
31
|
|
|
48
|
-
|
|
32
|
+
When working with tools like **localStorage**, **sessionStorage**, **AsyncStorage**, or even databases like **Redis** or **PostgreSQL (JSON columns)**, we often need to store application state or configuration objects as strings using `JSON.stringify`.
|
|
49
33
|
|
|
50
|
-
|
|
51
|
-
- The **Map** disappeared.
|
|
52
|
-
- The **Set** vanished inside the Map.
|
|
34
|
+
_But thereβs a catch!:_
|
|
53
35
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
```ts
|
|
57
|
-
const parsed = JSON.parse(JSON.stringify(userProfile));
|
|
58
|
-
console.log(parsed.createdAt instanceof Date); // β false
|
|
59
|
-
console.log(parsed.preferences instanceof Map); // β false
|
|
60
|
-
```
|
|
61
|
-
|
|
62
|
-
When serializing JavaScript objects, we lose important value types β
|
|
63
|
-
Dates, Sets, Maps, RegExps, and even nested structures.
|
|
64
|
-
This forces us to write endless **custom parsers and validators** to recover data
|
|
65
|
-
that could have been automatically restored.
|
|
36
|
+
`JSON.stringify` has no idea what to do with values like `Date`, `Map`, or `Set`...
|
|
37
|
+
It just flattens them into empty objects or strings, making you lose the original data types or the entire structure.
|
|
66
38
|
|
|
67
39
|
---
|
|
68
40
|
|
|
@@ -70,18 +42,18 @@ that could have been automatically restored.
|
|
|
70
42
|
|
|
71
43
|
Exposes **two simple functions**:
|
|
72
44
|
|
|
73
|
-
1. `formatToStore(value)` β safely prepares your data for storage returning
|
|
45
|
+
1. `formatToStore(value)` β safely prepares your data for storage, returning a JSON string representation.
|
|
74
46
|
2. `formatFromStore(value)` β restores it to the **exact original shape and types**.
|
|
75
47
|
|
|
76
48
|
As simple as that.
|
|
77
49
|
|
|
78
|
-
# Letβs see
|
|
50
|
+
# Letβs see a couple of examples
|
|
79
51
|
|
|
80
52
|
---
|
|
81
53
|
|
|
82
|
-
## βοΈ Example: useDashboardConfig
|
|
54
|
+
## βοΈ Example: useDashboardConfig Hook
|
|
83
55
|
|
|
84
|
-
Letβs create a hook that
|
|
56
|
+
Letβs create a hook that syncs `localStorage` with React state:
|
|
85
57
|
|
|
86
58
|
```ts
|
|
87
59
|
import { formatToStore, formatFromStore } from 'json-storage-formatter';
|
|
@@ -104,7 +76,6 @@ const useDashboardConfig = () => {
|
|
|
104
76
|
|
|
105
77
|
const saveConfig = (newConfig: DashboardConfig) => {
|
|
106
78
|
localStorage.setItem('dashboard-config', formatToStore(newConfig));
|
|
107
|
-
|
|
108
79
|
setConfig(newConfig);
|
|
109
80
|
};
|
|
110
81
|
|
|
@@ -125,7 +96,7 @@ const example: DashboardConfig = {
|
|
|
125
96
|
|
|
126
97
|
---
|
|
127
98
|
|
|
128
|
-
## π Example: Sync
|
|
99
|
+
## π Example: Sync Dashboard Queries Through URL (Dashboards / Reports)
|
|
129
100
|
|
|
130
101
|
This pattern is common in dashboards, where filters are shared via URL.
|
|
131
102
|
You can safely serialize complex filters (with Dates, Sets, Maps, etc.) and encode them for sharing.
|
|
@@ -155,8 +126,9 @@ const useUrlQuery = () => {
|
|
|
155
126
|
const updateQuery = (newQuery: DashboardQuery) => {
|
|
156
127
|
const encoded = btoa(formatToStore(newQuery));
|
|
157
128
|
|
|
158
|
-
// encode the
|
|
159
|
-
|
|
129
|
+
// encode the JSON as base64 to make it URL-safe
|
|
130
|
+
// avoids breaking query strings with +, /, or = characters
|
|
131
|
+
window.history.replaceState(null, '', `\${location.pathname}?query=\${encoded}`);
|
|
160
132
|
|
|
161
133
|
setQuery(newQuery);
|
|
162
134
|
};
|
|
@@ -165,12 +137,15 @@ const useUrlQuery = () => {
|
|
|
165
137
|
};
|
|
166
138
|
```
|
|
167
139
|
|
|
168
|
-
The examples above
|
|
140
|
+
The examples above use React, but the library itself is **framework-agnostic**
|
|
141
|
+
and can be used anywhere in JavaScript or TypeScript projects.
|
|
142
|
+
|
|
143
|
+
---
|
|
169
144
|
|
|
170
145
|
## π§© Why Itβs Useful
|
|
171
146
|
|
|
172
147
|
This becomes incredibly powerful when your app needs to **sync complex state**
|
|
173
|
-
to a string-based storage layer β like when syncing to **localStorage**, **Redis**, or
|
|
148
|
+
to a string-based storage layer β like when syncing to **localStorage**, **Redis**, or a **shared dashboard URL**.
|
|
174
149
|
|
|
175
150
|
Instead of being limited by what JSON can handle,
|
|
176
151
|
you can now serialize **any structure** β Maps, Sets, nested objects, or Dates β
|
|
@@ -194,7 +169,7 @@ The `$t` field stores the **type**, and `$v` holds the actual value.
|
|
|
194
169
|
When using `formatFromStore`, it reads that metadata and recreates your data structure
|
|
195
170
|
exactly as it was before β even if itβs deeply nested.
|
|
196
171
|
|
|
197
|
-
Resulting
|
|
172
|
+
Resulting in:
|
|
198
173
|
|
|
199
174
|
```ts
|
|
200
175
|
new Map([['key', new Date('2024-10-01T10:30:00.000Z')]]);
|
|
@@ -220,6 +195,9 @@ Restores the serialized object back to its original types.
|
|
|
220
195
|
const result = formatFromStore<Map<string, unknown>>(objectWithMetadata);
|
|
221
196
|
```
|
|
222
197
|
|
|
198
|
+
Both functions work directly with strings,
|
|
199
|
+
so you can safely use them with localStorage, AsyncStorage, or URLs.
|
|
200
|
+
|
|
223
201
|
---
|
|
224
202
|
|
|
225
203
|
## π§° Utility Functions
|
|
@@ -236,13 +214,14 @@ const result = formatFromStore<Map<string, unknown>>(objectWithMetadata);
|
|
|
236
214
|
|
|
237
215
|
## βοΈ Comparison
|
|
238
216
|
|
|
239
|
-
| Behavior
|
|
240
|
-
|
|
|
241
|
-
| **Date**
|
|
242
|
-
| **Set**
|
|
243
|
-
| **Map**
|
|
244
|
-
| **
|
|
245
|
-
| **
|
|
217
|
+
| Behavior | JSON.stringify | json-storage-formatter |
|
|
218
|
+
| ------------- | --------------- | ------------------------ |
|
|
219
|
+
| **Date** | Becomes string | β
Restored as Date |
|
|
220
|
+
| **Set** | Lost completely | β
Restored as Set |
|
|
221
|
+
| **Map** | Lost completely | β
Restored as Map |
|
|
222
|
+
| **Undefined** | Omitted | β
Restored as undefined |
|
|
223
|
+
| **Regexp** | Lost completely | β
Restored as RegExp |
|
|
224
|
+
| **Error** | Lost completely | β
Restored as Error |
|
|
246
225
|
|
|
247
226
|
---
|
|
248
227
|
|
|
@@ -252,9 +231,15 @@ const result = formatFromStore<Map<string, unknown>>(objectWithMetadata);
|
|
|
252
231
|
npm install json-storage-formatter
|
|
253
232
|
```
|
|
254
233
|
|
|
234
|
+
or
|
|
235
|
+
|
|
236
|
+
```bash
|
|
237
|
+
yarn add json-storage-formatter
|
|
238
|
+
```
|
|
239
|
+
|
|
255
240
|
---
|
|
256
241
|
|
|
257
242
|
## π― Ready to Try It?
|
|
258
243
|
|
|
259
244
|
π **NPM:** [json-storage-formatter](https://www.npmjs.com/package/json-storage-formatter)
|
|
260
|
-
Serialize, store, and restore **any JavaScript data type** without losing its identity β lightweight, fast
|
|
245
|
+
Serialize, store, and restore **any JavaScript data type** without losing its identity β lightweight, fast. β¨
|