json-storage-formatter 1.0.1 → 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 CHANGED
@@ -13,67 +13,72 @@ The main methods of the library are **formatToStore** and **formatFromStore**
13
13
  Format an object to be stored as JSON
14
14
 
15
15
  ```TS
16
- const objectWithMetadata = formatToStore(object);
17
-
18
- // The result can be JSON.stringify
19
- console.log(objectWithMetadata);
20
- /*
21
- {
22
- _type_: 'map',
23
- value: [
24
- [
25
- 'key1',
26
- {
27
- _type_: 'date',
28
- value: '2021-05-08T13:30:00.000Z',
29
- },
30
- ],
31
- [
32
- 'key2',
16
+ const objectWithMetadata = formatToStore(object);
17
+
18
+ // The result can be JSON.stringify
19
+ console.log(objectWithMetadata);
20
+ /*
21
+ {
22
+ $t: 'map',
23
+ $v: [
24
+ [
25
+ 'key1',
26
+ {
27
+ $t: 'date',
28
+ $v: '2021-05-08T13:30:00.000Z',
29
+ },
30
+ ],
31
+ [
32
+ 'key2',
33
+ {
34
+ $t: 'set',
35
+ $v: [
33
36
  {
34
- _type_: 'set',
35
- value: [
36
- {
37
- _type_: 'map',
38
- value: [
39
- [
40
- 'key1',
41
- {
42
- _type_: 'date',
43
- value: '2021-05-08T13:30:00.000Z',
44
- },
45
- ],
46
- ],
47
- },
37
+ $t: 'map',
38
+ $v: [
39
+ [
40
+ 'key1',
41
+ {
42
+ $t: 'date',
43
+ $v: '2021-05-08T13:30:00.000Z',
44
+ },
45
+ ],
48
46
  ],
49
47
  },
50
48
  ],
49
+ },
51
50
  ],
52
- }
53
- */
51
+ ],
52
+ }
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);
63
-
64
- // Original types of the object with metadata
65
- console.log(object);
66
-
67
- /*
68
- // the result will be the same than executing the following code
69
- const formatFromStoreResultExample = new Map<string, unknown>([
70
- ['key1', new Date('2021-05-08T13:30:00.000Z')],
71
- [
72
- 'key2',
73
- new Set([new Map([['key1', new Date('2021-05-08T13:30:00.000Z')]])]),
74
- ],
75
- ]);
76
- */
67
+ const object = formatFromStore<Map<string, unknown>>(objectWithMetadata);
68
+
69
+ // Original types of the object with metadata
70
+ console.log(object);
71
+
72
+ /*
73
+ // the result will be the same than executing the following code
74
+ const formatFromStoreResultExample = new Map([
75
+ ['key1', new Date('2021-05-08T13:30:00.000Z')],
76
+ [
77
+ 'key2',
78
+ new Set([new Map([['key1', new Date('2021-05-08T13:30:00.000Z')]])]),
79
+ ],
80
+ ]);
81
+ */
77
82
  ```
78
83
 
79
84
  ## clone
@@ -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: T) => unknown;
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
- * @returns
66
- * A value with metadata to store it as json
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: <T>(value: T) => unknown;
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":"AAKA;;;;;KAKK;AACL,eAAO,MAAM,KAAK,kBAsCjB,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,mBAAkB,OAoD7C,CAAC;AAEF;;;;GAIG;AACH,eAAO,MAAM,aAAa,mBAAkB,OAwD3C,CAAC"}
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"}
@@ -8,7 +8,6 @@ exports.formatToStore = exports.formatFromStore = exports.isPrimitive = exports.
8
8
  * A deep clone of the object
9
9
  * */
10
10
  const clone = (obj) => {
11
- debugger;
12
11
  if ((0, exports.isPrimitive)(obj) || (0, exports.isDate)(obj)) {
13
12
  return obj;
14
13
  }
@@ -103,18 +102,18 @@ const formatFromStore = (value) => {
103
102
  if ((0, exports.isPrimitive)(obj)) {
104
103
  return obj;
105
104
  }
106
- const isMetaDate = (obj === null || obj === void 0 ? void 0 : obj._type_) === 'date';
105
+ const isMetaDate = (obj === null || obj === void 0 ? void 0 : obj.$t) === 'date';
107
106
  if (isMetaDate) {
108
- return new Date(obj.value);
107
+ return new Date(obj.$v);
109
108
  }
110
- const isMetaMap = (obj === null || obj === void 0 ? void 0 : obj._type_) === 'map';
109
+ const isMetaMap = (obj === null || obj === void 0 ? void 0 : obj.$t) === 'map';
111
110
  if (isMetaMap) {
112
- const mapData = ((_a = obj.value) !== null && _a !== void 0 ? _a : []).map(([key, item]) => [key, (0, exports.formatFromStore)(item)]);
111
+ const mapData = ((_a = obj.$v) !== null && _a !== void 0 ? _a : []).map(([key, item]) => [key, (0, exports.formatFromStore)(item)]);
113
112
  return new Map(mapData);
114
113
  }
115
- const isMetaSet = (obj === null || obj === void 0 ? void 0 : obj._type_) === 'set';
114
+ const isMetaSet = (obj === null || obj === void 0 ? void 0 : obj.$t) === 'set';
116
115
  if (isMetaSet) {
117
- const setData = (_b = obj.value) !== null && _b !== void 0 ? _b : [].map((item) => (0, exports.formatFromStore)(item));
116
+ const setData = (_b = obj.$v) !== null && _b !== void 0 ? _b : [].map((item) => (0, exports.formatFromStore)(item));
118
117
  return new Set(setData);
119
118
  }
120
119
  const isArray = Array.isArray(obj);
@@ -131,11 +130,14 @@ const formatFromStore = (value) => {
131
130
  };
132
131
  exports.formatFromStore = formatFromStore;
133
132
  /**
134
- * Add metadata to a value to store it as json, it also supports Map, Set, Arrays
135
- * @returns
136
- * A value with metadata to store it as json
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
137
139
  */
138
- const formatToStore = (value) => {
140
+ const formatToStore = (value, { stringify } = { stringify: false }) => {
139
141
  const format = (obj) => {
140
142
  if ((0, exports.isPrimitive)(obj)) {
141
143
  return obj;
@@ -147,24 +149,27 @@ const formatToStore = (value) => {
147
149
  const isMap = obj instanceof Map;
148
150
  if (isMap) {
149
151
  const pairs = Array.from(obj.entries());
150
- return {
151
- _type_: 'map',
152
- value: pairs.map((pair) => (0, exports.formatToStore)(pair)),
152
+ const value = {
153
+ $t: 'map',
154
+ $v: pairs.map((pair) => (0, exports.formatToStore)(pair)),
153
155
  };
156
+ return value;
154
157
  }
155
158
  const isSet = obj instanceof Set;
156
159
  if (isSet) {
157
160
  const values = Array.from(obj.values());
158
- return {
159
- _type_: 'set',
160
- value: values.map((item) => (0, exports.formatToStore)(item)),
161
+ const value = {
162
+ $t: 'set',
163
+ $v: values.map((item) => (0, exports.formatToStore)(item)),
161
164
  };
165
+ return value;
162
166
  }
163
167
  if ((0, exports.isDate)(obj)) {
164
- return {
165
- _type_: 'date',
166
- value: obj.toISOString(),
168
+ const value = {
169
+ $t: 'date',
170
+ $v: obj.toISOString(),
167
171
  };
172
+ return value;
168
173
  }
169
174
  const keys = Object.keys(obj);
170
175
  return keys.reduce((acumulator, key) => {
@@ -172,6 +177,10 @@ const formatToStore = (value) => {
172
177
  return Object.assign(Object.assign({}, acumulator), { [key]: (0, exports.formatToStore)(prop) });
173
178
  }, {});
174
179
  };
175
- return format((0, exports.clone)(value));
180
+ const objectWithMetadata = format((0, exports.clone)(value));
181
+ const result = stringify
182
+ ? JSON.stringify(objectWithMetadata)
183
+ : objectWithMetadata;
184
+ return result;
176
185
  };
177
186
  exports.formatToStore = formatToStore;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "json-storage-formatter",
3
- "version": "1.0.1",
3
+ "version": "1.0.3",
4
4
  "description": "Package for json stringify objects without losing data types",
5
5
  "main": "lib/json-storage-formatter.js",
6
6
  "files": [