json-storage-formatter 1.0.2 → 1.0.4

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
@@ -19,28 +19,28 @@ const objectWithMetadata = formatToStore(object);
19
19
  console.log(objectWithMetadata);
20
20
  /*
21
21
  {
22
- _type_: 'map',
23
- value: [
22
+ $t: 'map',
23
+ $v: [
24
24
  [
25
25
  'key1',
26
26
  {
27
- _type_: 'date',
28
- value: '2021-05-08T13:30:00.000Z',
27
+ $t: 'date',
28
+ $v: '2021-05-08T13:30:00.000Z',
29
29
  },
30
30
  ],
31
31
  [
32
32
  'key2',
33
33
  {
34
- _type_: 'set',
35
- value: [
34
+ $t: 'set',
35
+ $v: [
36
36
  {
37
- _type_: 'map',
38
- value: [
37
+ $t: 'map',
38
+ $v: [
39
39
  [
40
40
  'key1',
41
41
  {
42
- _type_: 'date',
43
- value: '2021-05-08T13:30:00.000Z',
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<string, unknown>([
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' | 'regex' | 'error';
3
+ $v?: unknown;
4
+ };
1
5
  /**
2
6
  * Deep clone an object, it also suppors Map, Set, Arrays
3
7
  * @param obj
@@ -44,6 +48,12 @@ export declare const isString: (value: unknown) => boolean;
44
48
  * false otherwise
45
49
  */
46
50
  export declare const isDate: (value: unknown) => boolean;
51
+ /**
52
+ * Check if a value is a RegExp
53
+ * @param value The value to check
54
+ * @returns true if the value is a RegExp, false otherwise
55
+ * */
56
+ export declare const isRegex: (value: unknown) => boolean;
47
57
  /**
48
58
  * Check if a value is a primitive
49
59
  * @param value
@@ -59,11 +69,16 @@ export declare const isPrimitive: (value: unknown) => boolean;
59
69
  * @returns
60
70
  * Orinal form of the value
61
71
  */
62
- export declare const formatFromStore: <T>(value: T) => unknown;
72
+ export declare const formatFromStore: <T = unknown>(value: unknown) => T;
63
73
  /**
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
74
+ * Add metadata to a value to store it as json, it also supports Map, Set, Arrays,
75
+ * Returns a new object wich is a clone of the original object with metadata
76
+ * @template {TValue} The type of the value to format
77
+ * @template {TStringify} If the value should be stringified
78
+ * @param {TValue} value The value to format
79
+ * @param {{ stringify: TStringify }} { stringify: boolean } If the value should be stringified
67
80
  */
68
- export declare const formatToStore: <T>(value: T) => unknown;
81
+ export declare const formatToStore: <TValue, TStringify extends boolean = false>(value: TValue, { stringify }?: {
82
+ stringify: TStringify;
83
+ }) => TStringify extends true ? string : unknown;
69
84
  //# 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,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,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,GAAG,OAAO,GAAG,OAAO,CAAC;IAChD,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;;;;KAIK;AACL,eAAO,MAAM,OAAO,UAAW,OAAO,YAA4B,CAAC;AAEnE;;;;;;;GAOG;AACH,eAAO,MAAM,WAAW,UAAW,OAAO,YAKf,CAAC;AAE5B;;;;;GAKG;AACH,eAAO,MAAM,eAAe,uBAAwB,OAAO,MAgE1D,CAAC;AAEF;;;;;;;GAOG;AACH,eAAO,MAAM,aAAa;;gDA2FzB,CAAC"}
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.formatToStore = exports.formatFromStore = exports.isPrimitive = exports.isDate = exports.isString = exports.isBoolean = exports.isNumber = exports.isNil = exports.clone = void 0;
3
+ exports.formatToStore = exports.formatFromStore = exports.isPrimitive = exports.isRegex = exports.isDate = exports.isString = exports.isBoolean = exports.isNumber = exports.isNil = exports.clone = void 0;
4
4
  /**
5
5
  * Deep clone an object, it also suppors Map, Set, Arrays
6
6
  * @param obj
@@ -76,6 +76,13 @@ exports.isString = isString;
76
76
  */
77
77
  const isDate = (value) => value instanceof Date;
78
78
  exports.isDate = isDate;
79
+ /**
80
+ * Check if a value is a RegExp
81
+ * @param value The value to check
82
+ * @returns true if the value is a RegExp, false otherwise
83
+ * */
84
+ const isRegex = (value) => value instanceof RegExp;
85
+ exports.isRegex = isRegex;
79
86
  /**
80
87
  * Check if a value is a primitive
81
88
  * @param value
@@ -102,20 +109,28 @@ const formatFromStore = (value) => {
102
109
  if ((0, exports.isPrimitive)(obj)) {
103
110
  return obj;
104
111
  }
105
- const isMetaDate = (obj === null || obj === void 0 ? void 0 : obj._type_) === 'date';
112
+ const isMetaDate = (obj === null || obj === void 0 ? void 0 : obj.$t) === 'date';
106
113
  if (isMetaDate) {
107
- return new Date(obj.value);
114
+ return new Date(obj.$v);
108
115
  }
109
- const isMetaMap = (obj === null || obj === void 0 ? void 0 : obj._type_) === 'map';
116
+ const isMetaMap = (obj === null || obj === void 0 ? void 0 : obj.$t) === 'map';
110
117
  if (isMetaMap) {
111
- const mapData = ((_a = obj.value) !== null && _a !== void 0 ? _a : []).map(([key, item]) => [key, (0, exports.formatFromStore)(item)]);
118
+ const mapData = ((_a = obj.$v) !== null && _a !== void 0 ? _a : []).map(([key, item]) => [key, (0, exports.formatFromStore)(item)]);
112
119
  return new Map(mapData);
113
120
  }
114
- const isMetaSet = (obj === null || obj === void 0 ? void 0 : obj._type_) === 'set';
121
+ const isMetaSet = (obj === null || obj === void 0 ? void 0 : obj.$t) === 'set';
115
122
  if (isMetaSet) {
116
- const setData = (_b = obj.value) !== null && _b !== void 0 ? _b : [].map((item) => (0, exports.formatFromStore)(item));
123
+ const setData = (_b = obj.$v) !== null && _b !== void 0 ? _b : [].map((item) => (0, exports.formatFromStore)(item));
117
124
  return new Set(setData);
118
125
  }
126
+ const isMetaReg = (obj === null || obj === void 0 ? void 0 : obj.$t) === 'regex';
127
+ if (isMetaReg) {
128
+ return new RegExp(obj.$v);
129
+ }
130
+ const isMetaError = (obj === null || obj === void 0 ? void 0 : obj.$t) === 'error';
131
+ if (isMetaError) {
132
+ return new Error(obj.$v);
133
+ }
119
134
  const isArray = Array.isArray(obj);
120
135
  if (isArray) {
121
136
  return obj.map((item) => (0, exports.formatFromStore)(item));
@@ -130,11 +145,14 @@ const formatFromStore = (value) => {
130
145
  };
131
146
  exports.formatFromStore = formatFromStore;
132
147
  /**
133
- * Add metadata to a value to store it as json, it also supports Map, Set, Arrays
134
- * @returns
135
- * A value with metadata to store it as json
148
+ * Add metadata to a value to store it as json, it also supports Map, Set, Arrays,
149
+ * Returns a new object wich is a clone of the original object with metadata
150
+ * @template {TValue} The type of the value to format
151
+ * @template {TStringify} If the value should be stringified
152
+ * @param {TValue} value The value to format
153
+ * @param {{ stringify: TStringify }} { stringify: boolean } If the value should be stringified
136
154
  */
137
- const formatToStore = (value) => {
155
+ const formatToStore = (value, { stringify } = { stringify: false }) => {
138
156
  const format = (obj) => {
139
157
  if ((0, exports.isPrimitive)(obj)) {
140
158
  return obj;
@@ -146,24 +164,42 @@ const formatToStore = (value) => {
146
164
  const isMap = obj instanceof Map;
147
165
  if (isMap) {
148
166
  const pairs = Array.from(obj.entries());
149
- return {
150
- _type_: 'map',
151
- value: pairs.map((pair) => (0, exports.formatToStore)(pair)),
167
+ const value = {
168
+ $t: 'map',
169
+ $v: pairs.map((pair) => (0, exports.formatToStore)(pair)),
152
170
  };
171
+ return value;
153
172
  }
154
173
  const isSet = obj instanceof Set;
155
174
  if (isSet) {
156
175
  const values = Array.from(obj.values());
157
- return {
158
- _type_: 'set',
159
- value: values.map((item) => (0, exports.formatToStore)(item)),
176
+ const value = {
177
+ $t: 'set',
178
+ $v: values.map((item) => (0, exports.formatToStore)(item)),
160
179
  };
180
+ return value;
161
181
  }
162
182
  if ((0, exports.isDate)(obj)) {
163
- return {
164
- _type_: 'date',
165
- value: obj.toISOString(),
183
+ const value = {
184
+ $t: 'date',
185
+ $v: obj.toISOString(),
166
186
  };
187
+ return value;
188
+ }
189
+ if ((0, exports.isRegex)(obj)) {
190
+ const value = {
191
+ $t: 'regex',
192
+ $v: obj.toString(),
193
+ };
194
+ return value;
195
+ }
196
+ const isError = obj instanceof Error;
197
+ if (isError) {
198
+ const value = {
199
+ $t: 'error',
200
+ $v: obj.message,
201
+ };
202
+ return value;
167
203
  }
168
204
  const keys = Object.keys(obj);
169
205
  return keys.reduce((acumulator, key) => {
@@ -171,6 +207,10 @@ const formatToStore = (value) => {
171
207
  return Object.assign(Object.assign({}, acumulator), { [key]: (0, exports.formatToStore)(prop) });
172
208
  }, {});
173
209
  };
174
- return format((0, exports.clone)(value));
210
+ const objectWithMetadata = format((0, exports.clone)(value));
211
+ const result = stringify
212
+ ? JSON.stringify(objectWithMetadata)
213
+ : objectWithMetadata;
214
+ return result;
175
215
  };
176
216
  exports.formatToStore = formatToStore;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "json-storage-formatter",
3
- "version": "1.0.2",
3
+ "version": "1.0.4",
4
4
  "description": "Package for json stringify objects without losing data types",
5
5
  "main": "lib/json-storage-formatter.js",
6
6
  "files": [