json-storage-formatter 1.0.2 → 1.0.3
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 +19 -14
- package/lib/json-storage-formatter.d.ts +14 -5
- package/lib/json-storage-formatter.d.ts.map +1 -1
- package/lib/json-storage-formatter.js +30 -20
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -19,28 +19,28 @@ const objectWithMetadata = formatToStore(object);
|
|
|
19
19
|
console.log(objectWithMetadata);
|
|
20
20
|
/*
|
|
21
21
|
{
|
|
22
|
-
|
|
23
|
-
|
|
22
|
+
$t: 'map',
|
|
23
|
+
$v: [
|
|
24
24
|
[
|
|
25
25
|
'key1',
|
|
26
26
|
{
|
|
27
|
-
|
|
28
|
-
|
|
27
|
+
$t: 'date',
|
|
28
|
+
$v: '2021-05-08T13:30:00.000Z',
|
|
29
29
|
},
|
|
30
30
|
],
|
|
31
31
|
[
|
|
32
32
|
'key2',
|
|
33
33
|
{
|
|
34
|
-
|
|
35
|
-
|
|
34
|
+
$t: 'set',
|
|
35
|
+
$v: [
|
|
36
36
|
{
|
|
37
|
-
|
|
38
|
-
|
|
37
|
+
$t: 'map',
|
|
38
|
+
$v: [
|
|
39
39
|
[
|
|
40
40
|
'key1',
|
|
41
41
|
{
|
|
42
|
-
|
|
43
|
-
|
|
42
|
+
$t: 'date',
|
|
43
|
+
$v: '2021-05-08T13:30:00.000Z',
|
|
44
44
|
},
|
|
45
45
|
],
|
|
46
46
|
],
|
|
@@ -50,23 +50,28 @@ console.log(objectWithMetadata);
|
|
|
50
50
|
],
|
|
51
51
|
],
|
|
52
52
|
}
|
|
53
|
-
|
|
53
|
+
*/
|
|
54
|
+
|
|
55
|
+
// you can also stringify directly the result by specifying it on the configuration parameter
|
|
56
|
+
const objectString = formatToStore(object, { stringify: true });
|
|
57
|
+
|
|
58
|
+
console.log(objectString); // {"$t":"map","$v":[["key1",{"$t":"date","$v":"2021-05-08T13:30:00.000Z"}],["key2",{"$t":"set","$v":[{"$t":"map","$v":[["key1",{"$t":"date","$v":"2021-05-08T13:30:00.000Z"}]]}]}]]}
|
|
54
59
|
|
|
55
60
|
```
|
|
56
61
|
|
|
57
|
-
## formatFromStore
|
|
62
|
+
## formatFromStore<T>
|
|
58
63
|
|
|
59
64
|
Format a value with possible metadata to his original form, it also supports Map, Set, Arrays
|
|
60
65
|
|
|
61
66
|
```TS
|
|
62
|
-
const object = formatFromStore(objectWithMetadata);
|
|
67
|
+
const object = formatFromStore<Map<string, unknown>>(objectWithMetadata);
|
|
63
68
|
|
|
64
69
|
// Original types of the object with metadata
|
|
65
70
|
console.log(object);
|
|
66
71
|
|
|
67
72
|
/*
|
|
68
73
|
// the result will be the same than executing the following code
|
|
69
|
-
const formatFromStoreResultExample = new Map
|
|
74
|
+
const formatFromStoreResultExample = new Map([
|
|
70
75
|
['key1', new Date('2021-05-08T13:30:00.000Z')],
|
|
71
76
|
[
|
|
72
77
|
'key2',
|
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
export type IValueWithMedaData = {
|
|
2
|
+
$t?: 'map' | 'set' | 'date';
|
|
3
|
+
$v?: unknown;
|
|
4
|
+
};
|
|
1
5
|
/**
|
|
2
6
|
* Deep clone an object, it also suppors Map, Set, Arrays
|
|
3
7
|
* @param obj
|
|
@@ -59,11 +63,16 @@ export declare const isPrimitive: (value: unknown) => boolean;
|
|
|
59
63
|
* @returns
|
|
60
64
|
* Orinal form of the value
|
|
61
65
|
*/
|
|
62
|
-
export declare const formatFromStore: <T>(value:
|
|
66
|
+
export declare const formatFromStore: <T = unknown>(value: unknown) => T;
|
|
63
67
|
/**
|
|
64
|
-
* Add metadata to a value to store it as json, it also supports Map, Set, Arrays
|
|
65
|
-
*
|
|
66
|
-
*
|
|
68
|
+
* Add metadata to a value to store it as json, it also supports Map, Set, Arrays,
|
|
69
|
+
* Returns a new object wich is a clone of the original object with metadata
|
|
70
|
+
* @template {TValue} The type of the value to format
|
|
71
|
+
* @template {TStringify} If the value should be stringified
|
|
72
|
+
* @param {TValue} value The value to format
|
|
73
|
+
* @param {{ stringify: TStringify }} { stringify: boolean } If the value should be stringified
|
|
67
74
|
*/
|
|
68
|
-
export declare const formatToStore: <
|
|
75
|
+
export declare const formatToStore: <TValue, TStringify extends boolean = false>(value: TValue, { stringify }?: {
|
|
76
|
+
stringify: TStringify;
|
|
77
|
+
}) => TStringify extends true ? string : unknown;
|
|
69
78
|
//# sourceMappingURL=json-storage-formatter.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"json-storage-formatter.d.ts","sourceRoot":"","sources":["../src/json-storage-formatter.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"json-storage-formatter.d.ts","sourceRoot":"","sources":["../src/json-storage-formatter.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,kBAAkB,GAAG;IAC/B,EAAE,CAAC,EAAE,KAAK,GAAG,KAAK,GAAG,MAAM,CAAC;IAC5B,EAAE,CAAC,EAAE,OAAO,CAAC;CACd,CAAC;AAEF;;;;;KAKK;AACL,eAAO,MAAM,KAAK,kBAqCjB,CAAC;AAEF;;;;;;KAMK;AACL,eAAO,MAAM,KAAK,UAAW,OAAO,YAA0C,CAAC;AAE/E;;;;;;MAMM;AACN,eAAO,MAAM,QAAQ,UAAW,OAAO,YAA8B,CAAC;AAEtE;;;;;;KAMK;AACL,eAAO,MAAM,SAAS,UAAW,OAAO,YAA+B,CAAC;AAExE;;;;;;KAMK;AACL,eAAO,MAAM,QAAQ,UAAW,OAAO,YAA8B,CAAC;AAEtE;;;;;GAKG;AACH,eAAO,MAAM,MAAM,UAAW,OAAO,YAA0B,CAAC;AAEhE;;;;;;;GAOG;AACH,eAAO,MAAM,WAAW,UAAW,OAAO,YAKf,CAAC;AAE5B;;;;;GAKG;AACH,eAAO,MAAM,eAAe,uBAAwB,OAAO,MAoD1D,CAAC;AAEF;;;;;;;GAOG;AACH,eAAO,MAAM,aAAa;;gDAuEzB,CAAC"}
|
|
@@ -102,18 +102,18 @@ const formatFromStore = (value) => {
|
|
|
102
102
|
if ((0, exports.isPrimitive)(obj)) {
|
|
103
103
|
return obj;
|
|
104
104
|
}
|
|
105
|
-
const isMetaDate = (obj === null || obj === void 0 ? void 0 : obj
|
|
105
|
+
const isMetaDate = (obj === null || obj === void 0 ? void 0 : obj.$t) === 'date';
|
|
106
106
|
if (isMetaDate) {
|
|
107
|
-
return new Date(obj
|
|
107
|
+
return new Date(obj.$v);
|
|
108
108
|
}
|
|
109
|
-
const isMetaMap = (obj === null || obj === void 0 ? void 0 : obj
|
|
109
|
+
const isMetaMap = (obj === null || obj === void 0 ? void 0 : obj.$t) === 'map';
|
|
110
110
|
if (isMetaMap) {
|
|
111
|
-
const mapData = ((_a = obj
|
|
111
|
+
const mapData = ((_a = obj.$v) !== null && _a !== void 0 ? _a : []).map(([key, item]) => [key, (0, exports.formatFromStore)(item)]);
|
|
112
112
|
return new Map(mapData);
|
|
113
113
|
}
|
|
114
|
-
const isMetaSet = (obj === null || obj === void 0 ? void 0 : obj
|
|
114
|
+
const isMetaSet = (obj === null || obj === void 0 ? void 0 : obj.$t) === 'set';
|
|
115
115
|
if (isMetaSet) {
|
|
116
|
-
const setData = (_b = obj
|
|
116
|
+
const setData = (_b = obj.$v) !== null && _b !== void 0 ? _b : [].map((item) => (0, exports.formatFromStore)(item));
|
|
117
117
|
return new Set(setData);
|
|
118
118
|
}
|
|
119
119
|
const isArray = Array.isArray(obj);
|
|
@@ -130,11 +130,14 @@ const formatFromStore = (value) => {
|
|
|
130
130
|
};
|
|
131
131
|
exports.formatFromStore = formatFromStore;
|
|
132
132
|
/**
|
|
133
|
-
* Add metadata to a value to store it as json, it also supports Map, Set, Arrays
|
|
134
|
-
*
|
|
135
|
-
*
|
|
133
|
+
* Add metadata to a value to store it as json, it also supports Map, Set, Arrays,
|
|
134
|
+
* Returns a new object wich is a clone of the original object with metadata
|
|
135
|
+
* @template {TValue} The type of the value to format
|
|
136
|
+
* @template {TStringify} If the value should be stringified
|
|
137
|
+
* @param {TValue} value The value to format
|
|
138
|
+
* @param {{ stringify: TStringify }} { stringify: boolean } If the value should be stringified
|
|
136
139
|
*/
|
|
137
|
-
const formatToStore = (value) => {
|
|
140
|
+
const formatToStore = (value, { stringify } = { stringify: false }) => {
|
|
138
141
|
const format = (obj) => {
|
|
139
142
|
if ((0, exports.isPrimitive)(obj)) {
|
|
140
143
|
return obj;
|
|
@@ -146,24 +149,27 @@ const formatToStore = (value) => {
|
|
|
146
149
|
const isMap = obj instanceof Map;
|
|
147
150
|
if (isMap) {
|
|
148
151
|
const pairs = Array.from(obj.entries());
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
+
const value = {
|
|
153
|
+
$t: 'map',
|
|
154
|
+
$v: pairs.map((pair) => (0, exports.formatToStore)(pair)),
|
|
152
155
|
};
|
|
156
|
+
return value;
|
|
153
157
|
}
|
|
154
158
|
const isSet = obj instanceof Set;
|
|
155
159
|
if (isSet) {
|
|
156
160
|
const values = Array.from(obj.values());
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
161
|
+
const value = {
|
|
162
|
+
$t: 'set',
|
|
163
|
+
$v: values.map((item) => (0, exports.formatToStore)(item)),
|
|
160
164
|
};
|
|
165
|
+
return value;
|
|
161
166
|
}
|
|
162
167
|
if ((0, exports.isDate)(obj)) {
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
168
|
+
const value = {
|
|
169
|
+
$t: 'date',
|
|
170
|
+
$v: obj.toISOString(),
|
|
166
171
|
};
|
|
172
|
+
return value;
|
|
167
173
|
}
|
|
168
174
|
const keys = Object.keys(obj);
|
|
169
175
|
return keys.reduce((acumulator, key) => {
|
|
@@ -171,6 +177,10 @@ const formatToStore = (value) => {
|
|
|
171
177
|
return Object.assign(Object.assign({}, acumulator), { [key]: (0, exports.formatToStore)(prop) });
|
|
172
178
|
}, {});
|
|
173
179
|
};
|
|
174
|
-
|
|
180
|
+
const objectWithMetadata = format((0, exports.clone)(value));
|
|
181
|
+
const result = stringify
|
|
182
|
+
? JSON.stringify(objectWithMetadata)
|
|
183
|
+
: objectWithMetadata;
|
|
184
|
+
return result;
|
|
175
185
|
};
|
|
176
186
|
exports.formatToStore = formatToStore;
|