json-storage-formatter 1.0.0

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022 johnny quesada
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,126 @@
1
+ # json-storage-formater
2
+
3
+ Package for json stringify objects without losing data types. The transformation of the data includes extra metadata into the resulted string which later on is used to restored all the data structures to the their origninal forms.
4
+
5
+ The library suppors Sets, Maps, Objects, Arrays, and Primitive data like Dates
6
+
7
+ # API
8
+
9
+ The main methods of the library are **formatToStore** and **formatFromStore**
10
+
11
+ ## formatToStore
12
+
13
+ Format an object to be stored as JSON
14
+
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',
33
+ {
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
+ },
48
+ ],
49
+ },
50
+ ],
51
+ ],
52
+ }
53
+ */
54
+
55
+ ```
56
+
57
+ ## formatFromStore
58
+
59
+ Format an value with possible metadata to his original form, it also supports Map, Set, Arrays
60
+
61
+ ```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
+ */
77
+ ```
78
+
79
+ ## clone
80
+
81
+ Deep clone an object, it also suppors Map, Set, Arrays.
82
+ The formatters clones the objects to avoid modify the parameter reference
83
+
84
+ ```ts
85
+ const clone: <T>(obj: T) => T;
86
+ ```
87
+
88
+ ## isNil
89
+
90
+ Check if a value is null or undefined
91
+
92
+ ```ts
93
+ const isNil: (value: unknown) => boolean;
94
+ ```
95
+
96
+ ## isNumber
97
+
98
+ Check if a value is a number
99
+
100
+ ```ts
101
+ const isNumber: (value: unknown) => boolean;
102
+ ```
103
+
104
+ ## isBoolean
105
+
106
+ Check if a value is a boolean
107
+
108
+ ```ts
109
+ const isBoolean: (value: unknown) => boolean;
110
+ ```
111
+
112
+ ## isString
113
+
114
+ Check if a value is a string
115
+
116
+ ```ts
117
+ const isString: (value: unknown) => boolean;
118
+ ```
119
+
120
+ ## isDate
121
+
122
+ Check if a value is a Date
123
+
124
+ ```ts
125
+ const isDate: (value: unknown) => boolean;
126
+ ```
@@ -0,0 +1,69 @@
1
+ /**
2
+ * Deep clone an object, it also suppors Map, Set, Arrays
3
+ * @param obj
4
+ * @returns
5
+ * A deep clone of the object
6
+ * */
7
+ export declare const clone: <T>(obj: T) => T;
8
+ /**
9
+ * Check if a value is null or undefined
10
+ * @param value
11
+ * @returns
12
+ * true if the value is null or undefined
13
+ * false otherwise
14
+ * */
15
+ export declare const isNil: (value: unknown) => boolean;
16
+ /**
17
+ * Check if a value is a number
18
+ * @param value
19
+ * @returns
20
+ * true if the value is a number
21
+ * false otherwise
22
+ * */
23
+ export declare const isNumber: (value: unknown) => boolean;
24
+ /**
25
+ * Check if a value is a boolean
26
+ * @param value
27
+ * @returns
28
+ * true if the value is a boolean
29
+ * false otherwise
30
+ * */
31
+ export declare const isBoolean: (value: unknown) => boolean;
32
+ /**
33
+ * Check if a value is a string
34
+ * @param value
35
+ * @returns
36
+ * true if the value is a string
37
+ * false otherwise
38
+ * */
39
+ export declare const isString: (value: unknown) => boolean;
40
+ /** Check if a value is a Date
41
+ * @param value
42
+ * @returns
43
+ * true if the value is a Date
44
+ * false otherwise
45
+ */
46
+ export declare const isDate: (value: unknown) => boolean;
47
+ /**
48
+ * Check if a value is a primitive
49
+ * @param value
50
+ * @returns
51
+ * true if the value is a primitive
52
+ * false otherwise
53
+ * null, number, boolean, string, symbol
54
+ */
55
+ export declare const isPrimitive: (value: unknown) => boolean;
56
+ /**
57
+ * Format an value with possible metadata to his original form, it also supports Map, Set, Arrays
58
+ * @param value
59
+ * @returns
60
+ * Orinal form of the value
61
+ */
62
+ export declare const formatFromStore: <T>(value: T) => unknown;
63
+ /**
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
67
+ */
68
+ export declare const formatToStore: <T>(value: T) => unknown;
69
+ //# sourceMappingURL=json-storage-formatter.d.ts.map
@@ -0,0 +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"}
@@ -0,0 +1,177 @@
1
+ "use strict";
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;
4
+ /**
5
+ * Deep clone an object, it also suppors Map, Set, Arrays
6
+ * @param obj
7
+ * @returns
8
+ * A deep clone of the object
9
+ * */
10
+ const clone = (obj) => {
11
+ debugger;
12
+ if ((0, exports.isPrimitive)(obj) || (0, exports.isDate)(obj)) {
13
+ return obj;
14
+ }
15
+ const isArray = Array.isArray(obj);
16
+ if (isArray) {
17
+ return obj.map((item) => (0, exports.clone)(item));
18
+ }
19
+ const isMap = obj instanceof Map;
20
+ if (isMap) {
21
+ const pairs = Array.from(obj.entries());
22
+ return new Map(pairs.map((pair) => (0, exports.clone)(pair)));
23
+ }
24
+ const isSet = obj instanceof Set;
25
+ if (isSet) {
26
+ const values = Array.from(obj.values());
27
+ return new Set(values.map((value) => (0, exports.clone)(value)));
28
+ }
29
+ const keys = Object.keys(obj);
30
+ return keys.reduce((acumulator, key) => {
31
+ const value = obj[key];
32
+ return Object.assign(Object.assign({}, acumulator), { [key]: (0, exports.clone)(value) });
33
+ }, {});
34
+ };
35
+ exports.clone = clone;
36
+ /**
37
+ * Check if a value is null or undefined
38
+ * @param value
39
+ * @returns
40
+ * true if the value is null or undefined
41
+ * false otherwise
42
+ * */
43
+ const isNil = (value) => value === null || value === undefined;
44
+ exports.isNil = isNil;
45
+ /**
46
+ * Check if a value is a number
47
+ * @param value
48
+ * @returns
49
+ * true if the value is a number
50
+ * false otherwise
51
+ * */
52
+ const isNumber = (value) => typeof value === 'number';
53
+ exports.isNumber = isNumber;
54
+ /**
55
+ * Check if a value is a boolean
56
+ * @param value
57
+ * @returns
58
+ * true if the value is a boolean
59
+ * false otherwise
60
+ * */
61
+ const isBoolean = (value) => typeof value === 'boolean';
62
+ exports.isBoolean = isBoolean;
63
+ /**
64
+ * Check if a value is a string
65
+ * @param value
66
+ * @returns
67
+ * true if the value is a string
68
+ * false otherwise
69
+ * */
70
+ const isString = (value) => typeof value === 'string';
71
+ exports.isString = isString;
72
+ /** Check if a value is a Date
73
+ * @param value
74
+ * @returns
75
+ * true if the value is a Date
76
+ * false otherwise
77
+ */
78
+ const isDate = (value) => value instanceof Date;
79
+ exports.isDate = isDate;
80
+ /**
81
+ * Check if a value is a primitive
82
+ * @param value
83
+ * @returns
84
+ * true if the value is a primitive
85
+ * false otherwise
86
+ * null, number, boolean, string, symbol
87
+ */
88
+ const isPrimitive = (value) => (0, exports.isNil)(value) ||
89
+ (0, exports.isNumber)(value) ||
90
+ (0, exports.isBoolean)(value) ||
91
+ (0, exports.isString)(value) ||
92
+ typeof value === 'symbol';
93
+ exports.isPrimitive = isPrimitive;
94
+ /**
95
+ * Format an value with possible metadata to his original form, it also supports Map, Set, Arrays
96
+ * @param value
97
+ * @returns
98
+ * Orinal form of the value
99
+ */
100
+ const formatFromStore = (value) => {
101
+ const format = (obj) => {
102
+ var _a, _b;
103
+ if ((0, exports.isPrimitive)(obj)) {
104
+ return obj;
105
+ }
106
+ const isMetaDate = (obj === null || obj === void 0 ? void 0 : obj._type_) === 'date';
107
+ if (isMetaDate) {
108
+ return new Date(obj.value);
109
+ }
110
+ const isMetaMap = (obj === null || obj === void 0 ? void 0 : obj._type_) === 'map';
111
+ if (isMetaMap) {
112
+ const mapData = ((_a = obj.value) !== null && _a !== void 0 ? _a : []).map(([key, item]) => [key, (0, exports.formatFromStore)(item)]);
113
+ return new Map(mapData);
114
+ }
115
+ const isMetaSet = (obj === null || obj === void 0 ? void 0 : obj._type_) === 'set';
116
+ if (isMetaSet) {
117
+ const setData = (_b = obj.value) !== null && _b !== void 0 ? _b : [].map((item) => (0, exports.formatFromStore)(item));
118
+ return new Set(setData);
119
+ }
120
+ const isArray = Array.isArray(obj);
121
+ if (isArray) {
122
+ return obj.map((item) => (0, exports.formatFromStore)(item));
123
+ }
124
+ const keys = Object.keys(obj);
125
+ return keys.reduce((acumulator, key) => {
126
+ const unformatedValue = obj[key];
127
+ return Object.assign(Object.assign({}, acumulator), { [key]: (0, exports.formatFromStore)(unformatedValue) });
128
+ }, {});
129
+ };
130
+ return format((0, exports.clone)(value));
131
+ };
132
+ exports.formatFromStore = formatFromStore;
133
+ /**
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
137
+ */
138
+ const formatToStore = (value) => {
139
+ const format = (obj) => {
140
+ if ((0, exports.isPrimitive)(obj)) {
141
+ return obj;
142
+ }
143
+ const isArray = Array.isArray(obj);
144
+ if (isArray) {
145
+ return obj.map((item) => (0, exports.formatToStore)(item));
146
+ }
147
+ const isMap = obj instanceof Map;
148
+ if (isMap) {
149
+ const pairs = Array.from(obj.entries());
150
+ return {
151
+ _type_: 'map',
152
+ value: pairs.map((pair) => (0, exports.formatToStore)(pair)),
153
+ };
154
+ }
155
+ const isSet = obj instanceof Set;
156
+ if (isSet) {
157
+ const values = Array.from(obj.values());
158
+ return {
159
+ _type_: 'set',
160
+ value: values.map((item) => (0, exports.formatToStore)(item)),
161
+ };
162
+ }
163
+ if ((0, exports.isDate)(obj)) {
164
+ return {
165
+ _type_: 'date',
166
+ value: obj.toISOString(),
167
+ };
168
+ }
169
+ const keys = Object.keys(obj);
170
+ return keys.reduce((acumulator, key) => {
171
+ const prop = obj[key];
172
+ return Object.assign(Object.assign({}, acumulator), { [key]: (0, exports.formatToStore)(prop) });
173
+ }, {});
174
+ };
175
+ return format((0, exports.clone)(value));
176
+ };
177
+ exports.formatToStore = formatToStore;
package/package.json ADDED
@@ -0,0 +1,51 @@
1
+ {
2
+ "name": "json-storage-formatter",
3
+ "version": "1.0.0",
4
+ "description": "Package for json stringify objects without losing data types",
5
+ "main": "lib/json-storage-formatter.js",
6
+ "files": [
7
+ "lib"
8
+ ],
9
+ "scripts": {
10
+ "test:debug": "node --inspect-brk node_modules/.bin/jest --watch --runInBand",
11
+ "test:quick": "jest --maxWorkers=4 -c --no-watchman -u",
12
+ "test:coverage": "jest --maxWorkers=4 -c --colors --no-watchman --verbose --coverage",
13
+ "build": "tsc",
14
+ "prepare": "npm run build",
15
+ "version": "npm run format && git add -A src",
16
+ "postversion": "git push && git push --tags"
17
+ },
18
+ "repository": {
19
+ "type": "git",
20
+ "url": "git+https://github.com/johnny-quesada-developer/json-storage-formatter.git"
21
+ },
22
+ "keywords": [
23
+ "json",
24
+ "state",
25
+ "typescript",
26
+ "storage",
27
+ "asynstorage",
28
+ "localstorage"
29
+ ],
30
+ "author": "johnny quesada",
31
+ "license": "MIT",
32
+ "bugs": {
33
+ "url": "https://github.com/johnny-quesada-developer/json-storage-formatter/issues"
34
+ },
35
+ "homepage": "https://github.com/johnny-quesada-developer/json-storage-formatter#readme",
36
+ "devDependencies": {
37
+ "@types/jest": "^26.0.17",
38
+ "@types/lodash": "^4.14.165",
39
+ "@typescript-eslint/eslint-plugin": "^4.9.0",
40
+ "@typescript-eslint/parser": "^4.9.0",
41
+ "eslint": "^7.15.0",
42
+ "eslint-config-airbnb": "^18.2.1",
43
+ "eslint-plugin-import": "^2.22.1",
44
+ "eslint-plugin-jsx-a11y": "^6.4.1",
45
+ "eslint-plugin-react": "^7.21.5",
46
+ "eslint-plugin-react-hooks": "^4.2.0",
47
+ "jest": "^29.3.1",
48
+ "ts-jest": "^29.0.3",
49
+ "typescript": "^4.1.2"
50
+ }
51
+ }