json-storage-formatter 1.0.3 → 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.
@@ -1,5 +1,5 @@
1
1
  export type IValueWithMedaData = {
2
- $t?: 'map' | 'set' | 'date';
2
+ $t?: 'map' | 'set' | 'date' | 'regex' | 'error';
3
3
  $v?: unknown;
4
4
  };
5
5
  /**
@@ -48,6 +48,12 @@ export declare const isString: (value: unknown) => boolean;
48
48
  * false otherwise
49
49
  */
50
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;
51
57
  /**
52
58
  * Check if a value is a primitive
53
59
  * @param value
@@ -1 +1 @@
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"}
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
@@ -116,6 +123,14 @@ const formatFromStore = (value) => {
116
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));
@@ -171,6 +186,21 @@ const formatToStore = (value, { stringify } = { stringify: false }) => {
171
186
  };
172
187
  return value;
173
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;
203
+ }
174
204
  const keys = Object.keys(obj);
175
205
  return keys.reduce((acumulator, key) => {
176
206
  const prop = obj[key];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "json-storage-formatter",
3
- "version": "1.0.3",
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": [