boma 2.0.4 → 2.0.6
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/dist/index.d.ts +5 -0
- package/dist/index.js +17 -16
- package/package.json +1 -1
- package/readme.md +21 -2
package/dist/index.d.ts
CHANGED
|
@@ -10,6 +10,7 @@ export interface ReadJSONProps {
|
|
|
10
10
|
filePath: string;
|
|
11
11
|
parseJSON?: boolean;
|
|
12
12
|
createIfNotFound?: boolean | SerializableObject | SerializableArray;
|
|
13
|
+
silent?: boolean;
|
|
13
14
|
}
|
|
14
15
|
export interface SaveJSONProps {
|
|
15
16
|
filePath: string;
|
|
@@ -17,6 +18,7 @@ export interface SaveJSONProps {
|
|
|
17
18
|
format?: boolean;
|
|
18
19
|
logSaving?: boolean;
|
|
19
20
|
replaceNonSerializable?: boolean;
|
|
21
|
+
silent?: boolean;
|
|
20
22
|
}
|
|
21
23
|
export interface addToJSONProps {
|
|
22
24
|
filePath: string;
|
|
@@ -24,6 +26,7 @@ export interface addToJSONProps {
|
|
|
24
26
|
format?: boolean;
|
|
25
27
|
logSaving?: boolean;
|
|
26
28
|
replaceNonSerializable?: boolean;
|
|
29
|
+
silent?: boolean;
|
|
27
30
|
}
|
|
28
31
|
export type ErrorWithCode = Error & {
|
|
29
32
|
code: string;
|
|
@@ -41,6 +44,8 @@ export interface SerializationIssue {
|
|
|
41
44
|
export declare const isErrorWithCode: (error: unknown) => error is ErrorWithCode;
|
|
42
45
|
export declare const isErrorWithMessage: (error: unknown) => error is ErrorWithMessage;
|
|
43
46
|
export declare const isSyntaxError: (error: unknown) => error is SyntaxError;
|
|
47
|
+
export declare const isObjectLike: (value: unknown) => value is object;
|
|
48
|
+
export declare const isPlainMergeableObject: (value: unknown) => value is Record<string, unknown>;
|
|
44
49
|
export declare const getSerializationIssues: (value: unknown, path?: string, ancestors?: WeakMap<object, string>, issues?: SerializationIssue[]) => SerializationIssue[];
|
|
45
50
|
export declare const isSerializable: (value: unknown) => value is Serializable;
|
|
46
51
|
export declare const sanitizeNonSerializable: (value: unknown, path?: string, ancestors?: WeakMap<object, string>) => Serializable;
|
package/dist/index.js
CHANGED
|
@@ -12,10 +12,10 @@ const getDate = () => {
|
|
|
12
12
|
const castedDate = new Date();
|
|
13
13
|
return castedDate.toLocaleTimeString('ru-RU', { hour: '2-digit', minute: '2-digit' });
|
|
14
14
|
};
|
|
15
|
-
const isObjectLike = (value) => {
|
|
15
|
+
export const isObjectLike = (value) => {
|
|
16
16
|
return typeof value === 'object' && value !== null;
|
|
17
17
|
};
|
|
18
|
-
const isPlainMergeableObject = (value) => {
|
|
18
|
+
export const isPlainMergeableObject = (value) => {
|
|
19
19
|
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
20
20
|
};
|
|
21
21
|
const pathJoin = (basePath, key) => {
|
|
@@ -35,7 +35,7 @@ const getIssueMessage = (issue) => {
|
|
|
35
35
|
}
|
|
36
36
|
return `Field "${issue.path}" has non-serializable value of type "${issue.kind}"`;
|
|
37
37
|
};
|
|
38
|
-
export const getSerializationIssues = (value, path = '
|
|
38
|
+
export const getSerializationIssues = (value, path = '[OBJECT]', ancestors = new WeakMap(), issues = []) => {
|
|
39
39
|
if (value === null) {
|
|
40
40
|
return issues;
|
|
41
41
|
}
|
|
@@ -150,10 +150,7 @@ export const sanitizeNonSerializable = (value, path = '$', ancestors = new WeakM
|
|
|
150
150
|
if (typeof value === 'number') {
|
|
151
151
|
return Number.isFinite(value) ? value : 'non-finite-number';
|
|
152
152
|
}
|
|
153
|
-
if (
|
|
154
|
-
typeof value === 'function' ||
|
|
155
|
-
typeof value === 'symbol' ||
|
|
156
|
-
typeof value === 'bigint') {
|
|
153
|
+
if (['undefined', 'function', 'symbol', 'bigint'].includes(typeof value)) {
|
|
157
154
|
return typeFlagFromValue(value);
|
|
158
155
|
}
|
|
159
156
|
if (Array.isArray(value)) {
|
|
@@ -185,11 +182,11 @@ const logSerializationIssues = (issues) => {
|
|
|
185
182
|
}
|
|
186
183
|
};
|
|
187
184
|
export const saveJSON = (saveInput) => {
|
|
188
|
-
const { filePath, objToSave, format = false, logSaving = false, replaceNonSerializable = false, } = saveInput;
|
|
185
|
+
const { filePath, objToSave, format = false, logSaving = false, replaceNonSerializable = false, silent = true, } = saveInput;
|
|
189
186
|
const issues = getSerializationIssues(objToSave);
|
|
190
187
|
if (issues.length > 0) {
|
|
191
|
-
logSerializationIssues(issues);
|
|
192
|
-
if (!replaceNonSerializable) {
|
|
188
|
+
!silent && logSerializationIssues(issues);
|
|
189
|
+
if (!replaceNonSerializable && !silent) {
|
|
193
190
|
const firstIssue = issues[0];
|
|
194
191
|
const errorMessage = `[${getDate()}] Boma got non JSON-serializable object at saveJSON! ${getIssueMessage(firstIssue)}`;
|
|
195
192
|
console.error(errorMessage);
|
|
@@ -202,7 +199,7 @@ export const saveJSON = (saveInput) => {
|
|
|
202
199
|
try {
|
|
203
200
|
const json = format ? JSON.stringify(valueToSave, null, 2) : JSON.stringify(valueToSave);
|
|
204
201
|
writeFileSync(filePath, json, 'utf8');
|
|
205
|
-
logSaving && console.log(`[${getDate()}] Write file ${filePath} successfully`);
|
|
202
|
+
!silent && logSaving && console.log(`[${getDate()}] Write file ${filePath} successfully`);
|
|
206
203
|
}
|
|
207
204
|
catch (error) {
|
|
208
205
|
if (isErrorWithCode(error)) {
|
|
@@ -212,7 +209,7 @@ export const saveJSON = (saveInput) => {
|
|
|
212
209
|
}
|
|
213
210
|
};
|
|
214
211
|
export const readJSON = (props) => {
|
|
215
|
-
const { filePath, createIfNotFound = false, parseJSON = true } = props;
|
|
212
|
+
const { filePath, createIfNotFound = false, parseJSON = true, silent = true } = props;
|
|
216
213
|
try {
|
|
217
214
|
const savedfile = readFileSync(filePath, 'utf8');
|
|
218
215
|
if (parseJSON) {
|
|
@@ -225,7 +222,7 @@ export const readJSON = (props) => {
|
|
|
225
222
|
switch (err.code) {
|
|
226
223
|
case 'ENOENT':
|
|
227
224
|
if (createIfNotFound) {
|
|
228
|
-
console.log('Try to create: ', filePath);
|
|
225
|
+
!silent && console.log('Try to create: ', filePath);
|
|
229
226
|
try {
|
|
230
227
|
const initialContent = typeof createIfNotFound === 'boolean' ? '{}' : JSON.stringify(createIfNotFound);
|
|
231
228
|
writeFileSync(filePath, initialContent, 'utf8');
|
|
@@ -235,7 +232,7 @@ export const readJSON = (props) => {
|
|
|
235
232
|
}
|
|
236
233
|
return typeof createIfNotFound === 'boolean' ? {} : createIfNotFound;
|
|
237
234
|
}
|
|
238
|
-
console.
|
|
235
|
+
console.info("Boma can't find file: ", filePath);
|
|
239
236
|
return parseJSON ? {} : null;
|
|
240
237
|
case 'EACCES':
|
|
241
238
|
console.error(`Access denied for ${filePath}`);
|
|
@@ -254,8 +251,8 @@ export const readJSON = (props) => {
|
|
|
254
251
|
}
|
|
255
252
|
};
|
|
256
253
|
export const addToJSON = (saveInput) => {
|
|
257
|
-
const { filePath, dataToAdd, format = false, logSaving = false, replaceNonSerializable = false, } = saveInput;
|
|
258
|
-
const oldJSON = readJSON({ filePath, createIfNotFound: true });
|
|
254
|
+
const { filePath, dataToAdd, format = false, logSaving = false, replaceNonSerializable = false, silent = true, } = saveInput;
|
|
255
|
+
const oldJSON = readJSON({ filePath, createIfNotFound: true, silent });
|
|
259
256
|
if (oldJSON === null || typeof oldJSON !== 'object') {
|
|
260
257
|
return saveJSON({
|
|
261
258
|
filePath,
|
|
@@ -263,6 +260,7 @@ export const addToJSON = (saveInput) => {
|
|
|
263
260
|
format,
|
|
264
261
|
logSaving,
|
|
265
262
|
replaceNonSerializable,
|
|
263
|
+
silent
|
|
266
264
|
});
|
|
267
265
|
}
|
|
268
266
|
if (Array.isArray(oldJSON) && Array.isArray(dataToAdd)) {
|
|
@@ -272,6 +270,7 @@ export const addToJSON = (saveInput) => {
|
|
|
272
270
|
format,
|
|
273
271
|
logSaving,
|
|
274
272
|
replaceNonSerializable,
|
|
273
|
+
silent
|
|
275
274
|
});
|
|
276
275
|
return;
|
|
277
276
|
}
|
|
@@ -282,6 +281,7 @@ export const addToJSON = (saveInput) => {
|
|
|
282
281
|
format,
|
|
283
282
|
logSaving,
|
|
284
283
|
replaceNonSerializable,
|
|
284
|
+
silent
|
|
285
285
|
});
|
|
286
286
|
return;
|
|
287
287
|
}
|
|
@@ -292,6 +292,7 @@ export const addToJSON = (saveInput) => {
|
|
|
292
292
|
format,
|
|
293
293
|
logSaving,
|
|
294
294
|
replaceNonSerializable,
|
|
295
|
+
silent
|
|
295
296
|
});
|
|
296
297
|
return;
|
|
297
298
|
}
|
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -6,7 +6,12 @@
|
|
|
6
6
|
# Usage
|
|
7
7
|
|
|
8
8
|
```JS
|
|
9
|
-
const logs = readJSON({
|
|
9
|
+
const logs = readJSON({
|
|
10
|
+
filePath: '/support.log',
|
|
11
|
+
createIfNotFound: {},
|
|
12
|
+
parseJSON: true,
|
|
13
|
+
silent: true, // Do not log warnings (only file not found will be logged)
|
|
14
|
+
});
|
|
10
15
|
/*
|
|
11
16
|
logs = {
|
|
12
17
|
any: {}, //
|
|
@@ -18,7 +23,8 @@ saveJSON({
|
|
|
18
23
|
filePath: '/test.json',
|
|
19
24
|
objToSave: { any: {}, key: {} },
|
|
20
25
|
format: true,
|
|
21
|
-
logSaving: false
|
|
26
|
+
logSaving: false,
|
|
27
|
+
silent: true, // Do not log warnings
|
|
22
28
|
}); // Format for save line break's
|
|
23
29
|
/* Will save:
|
|
24
30
|
{
|
|
@@ -40,6 +46,8 @@ addToJSON({
|
|
|
40
46
|
```
|
|
41
47
|
|
|
42
48
|
## Types
|
|
49
|
+
Main types:
|
|
50
|
+
|
|
43
51
|
```typescript
|
|
44
52
|
export type SerializablePrimitive = string | number | boolean | null;
|
|
45
53
|
export type SerializableArray = Serializable[];
|
|
@@ -58,6 +66,7 @@ export interface SaveJSONProps {
|
|
|
58
66
|
format?: boolean
|
|
59
67
|
logSaving?: boolean;
|
|
60
68
|
replaceNonSerializable?: boolean; // Заменять несереализуемые значения их типом
|
|
69
|
+
silent?: boolean; // Не выводим предупреждения
|
|
61
70
|
}
|
|
62
71
|
|
|
63
72
|
export interface addToJSONProps {
|
|
@@ -65,5 +74,15 @@ export interface addToJSONProps {
|
|
|
65
74
|
dataToAdd: SerializableObject | SerializableArray;
|
|
66
75
|
format?: boolean
|
|
67
76
|
logSaving?: boolean;
|
|
77
|
+
silent?: boolean; // Не выводим предупреждения
|
|
68
78
|
}
|
|
79
|
+
|
|
80
|
+
// We also export some helpers:
|
|
81
|
+
export const isSerializable = (value: unknown) => boolean;
|
|
82
|
+
export const isObjectLike = (value: unknown) => boolean;
|
|
83
|
+
export const isPlainMergeableObject = (value: unknown) => boolean;
|
|
84
|
+
|
|
85
|
+
export const isErrorWithCode = (error: unknown) => boolean;
|
|
86
|
+
export const isErrorWithMessage = (error: unknown) => boolean;
|
|
87
|
+
export const isSyntaxError = (error: unknown) => boolean;
|
|
69
88
|
```
|