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.
Files changed (2) hide show
  1. package/README.md +38 -53
  2. 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
  ![Image John Avatar](https://raw.githubusercontent.com/johnny-quesada-developer/global-hooks-example/main/public/avatar2.jpeg)
6
4
 
7
- A **lightweight solution** to store complex JavaScript values as JSON β€” **without losing their types**. πŸš€
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
- Let’s see an example.
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
- What happened?
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
- - The **Date** turned into a string.
51
- - The **Map** disappeared.
52
- - The **Set** vanished inside the Map.
34
+ _But there’s a catch!:_
53
35
 
54
- If we try to restore it:
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 the JSON string representation.
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 it a couple of examples
50
+ # Let’s see a couple of examples
79
51
 
80
52
  ---
81
53
 
82
- ## βš™οΈ Example: useDashboardConfig hook
54
+ ## βš™οΈ Example: useDashboardConfig Hook
83
55
 
84
- Let’s create a hook that sync localStorage with react state:
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 dashboard queries through URL (Dashboards / Reports)
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 json as base64 to make it URL-safe
159
- window.history.replaceState(null, '', `${location.pathname}?query=${encoded}`);
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 are in react but the library is framework-agnostic and can be used anywhere in JavaScript or TypeScript projects.
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 **a shared dashboard URL**.
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 on:
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 | JSON.stringify | json-storage-formatter |
240
- | ---------------- | --------------- | ---------------------- |
241
- | **Date** | Becomes string | βœ… Restored as Date |
242
- | **Set** | Lost completely | βœ… Restored as Set |
243
- | **Map** | Lost completely | βœ… Restored as Map |
244
- | **Nested types** | Broken | βœ… Preserved |
245
- | **Performance** | Fast | ⚑ Nearly identical |
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, and fully type-safe. ✨
245
+ Serialize, store, and restore **any JavaScript data type** without losing its identity β€” lightweight, fast. ✨
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "json-storage-formatter",
3
- "version": "3.0.1",
3
+ "version": "3.0.2",
4
4
  "description": "Package for json stringify objects without losing data types",
5
5
  "main": "./bundle.js",
6
6
  "types": "./index.d.ts",