boma 1.0.0 → 2.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 CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2025 Gratio-tech
3
+ Copyright (c) 2025 Nick G.
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/dist/index.d.ts CHANGED
@@ -9,6 +9,18 @@ export interface ReadJSONProps {
9
9
  parseJSON?: boolean;
10
10
  createIfNotFound?: boolean | SerializableObject | SerializableArray;
11
11
  }
12
+ export interface SaveJSONProps {
13
+ filePath: string;
14
+ objToSave: Serializable;
15
+ format?: boolean;
16
+ logSaving?: boolean;
17
+ }
18
+ export interface addToJSONProps {
19
+ filePath: string;
20
+ dataToAdd: SerializableObject | SerializableArray;
21
+ format?: boolean;
22
+ logSaving?: boolean;
23
+ }
12
24
  export type ErrorWithCode = Error & {
13
25
  code: string;
14
26
  };
@@ -18,6 +30,7 @@ export type ErrorWithMessage = Error & {
18
30
  export declare const isErrorWithCode: (error: unknown) => error is ErrorWithCode;
19
31
  export declare const isErrorWithMessage: (error: unknown) => error is ErrorWithMessage;
20
32
  export declare const isSyntaxError: (error: unknown) => error is SyntaxError;
21
- export declare const saveJSON: (filePath: string, objToSave: Serializable, format?: boolean) => void;
33
+ export declare const isSerializable: (value: unknown, seen?: WeakSet<object>) => value is Serializable;
34
+ export declare const saveJSON: (saveInput: SaveJSONProps) => void;
22
35
  export declare const readJSON: (props: ReadJSONProps) => any;
23
- export declare const addToJSON: (filePath: string, objToAdd: SerializableObject | SerializableArray) => void;
36
+ export declare const addToJSON: (saveInput: addToJSONProps) => void;
package/dist/index.js CHANGED
@@ -8,14 +8,51 @@ export const isErrorWithMessage = (error) => {
8
8
  export const isSyntaxError = (error) => {
9
9
  return error instanceof SyntaxError;
10
10
  };
11
- export const saveJSON = (filePath, objToSave, format) => {
11
+ const getDate = () => {
12
+ const castedDate = new Date();
13
+ return castedDate.toLocaleTimeString('ru-RU', { hour: '2-digit', minute: '2-digit' });
14
+ };
15
+ export const isSerializable = (value, seen = new WeakSet()) => {
16
+ if (value === null || typeof value === "string" || typeof value === "number" || typeof value === "boolean") {
17
+ return true;
18
+ }
19
+ if (Array.isArray(value)) {
20
+ for (const el of value) {
21
+ if (!isSerializable(el, seen)) {
22
+ return false;
23
+ }
24
+ }
25
+ return true;
26
+ }
27
+ if (typeof value === "object") {
28
+ if (seen.has(value)) {
29
+ return false;
30
+ }
31
+ seen.add(value);
32
+ const obj = value;
33
+ for (const key in obj) {
34
+ if (!isSerializable(obj[key], seen)) {
35
+ return false;
36
+ }
37
+ }
38
+ return true;
39
+ }
40
+ return false;
41
+ };
42
+ export const saveJSON = (saveInput) => {
43
+ const { filePath, objToSave, format = false, logSaving = false } = saveInput;
44
+ if (!isSerializable(objToSave)) {
45
+ console.error(`[${getDate()}] Boma get non JSON-serializable object at saveJSON!`);
46
+ throw new Error(`[${getDate()}] Boma get non JSON-serializable object at saveJSON!`);
47
+ }
12
48
  try {
13
49
  const json = format ? JSON.stringify(objToSave, null, 2) : JSON.stringify(objToSave);
14
50
  writeFileSync(filePath, json, 'utf8');
51
+ logSaving && console.log(`[${getDate()}] Write file ${filePath} successfully`);
15
52
  }
16
53
  catch (error) {
17
54
  if (isErrorWithCode(error)) {
18
- console.error(`Filesystem error for ${filePath}:`, error);
55
+ console.error(`[${getDate()}] Boma get filesystem error for ${filePath}:`, error);
19
56
  }
20
57
  throw error;
21
58
  }
@@ -62,19 +99,20 @@ export const readJSON = (props) => {
62
99
  return null;
63
100
  }
64
101
  };
65
- export const addToJSON = (filePath, objToAdd) => {
102
+ export const addToJSON = (saveInput) => {
103
+ const { filePath, dataToAdd, format = false, logSaving = false } = saveInput;
66
104
  const oldJSON = readJSON({ filePath, createIfNotFound: true });
67
105
  if (oldJSON === null || typeof oldJSON !== 'object') {
68
- return saveJSON(filePath, objToAdd);
106
+ return saveJSON({ filePath, objToSave: dataToAdd, logSaving });
69
107
  }
70
- if (Array.isArray(oldJSON) && Array.isArray(objToAdd)) {
71
- saveJSON(filePath, [...oldJSON, ...objToAdd]);
108
+ if (Array.isArray(oldJSON) && Array.isArray(dataToAdd)) {
109
+ saveJSON({ filePath, objToSave: [...oldJSON, ...dataToAdd], format, logSaving });
72
110
  }
73
- else if (!Array.isArray(oldJSON) && !Array.isArray(objToAdd)) {
74
- saveJSON(filePath, { ...oldJSON, ...objToAdd });
111
+ else if (!Array.isArray(oldJSON) && !Array.isArray(dataToAdd)) {
112
+ saveJSON({ filePath, objToSave: { ...oldJSON, ...dataToAdd }, format, logSaving });
75
113
  }
76
- else if (Array.isArray(objToAdd) && Object.keys(oldJSON).length === 0) {
77
- saveJSON(filePath, [...objToAdd]);
114
+ else if (Array.isArray(dataToAdd) && Object.keys(oldJSON).length === 0) {
115
+ saveJSON({ filePath, objToSave: [...dataToAdd], format, logSaving });
78
116
  }
79
117
  else {
80
118
  throw new Error('Cannot merge array with object');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "boma",
3
- "version": "1.0.0",
3
+ "version": "2.0.0",
4
4
  "description": "Primitive JSON helper",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
@@ -21,12 +21,13 @@
21
21
  }
22
22
  }
23
23
  },
24
- "author": "Nick",
24
+ "author": "Nick G.",
25
25
  "license": "MIT",
26
26
  "bugs": {
27
27
  "url": "https://github.com/Psychosynthesis/Boma/issues"
28
28
  },
29
29
  "homepage": "https://github.com/Psychosynthesis/Boma#readme",
30
+ "keywords": [ "json", "helper" ],
30
31
  "devDependencies": {
31
32
  "@types/node": "^22.13.10",
32
33
  "typescript": "^5.8.x"
package/readme.md CHANGED
@@ -1,2 +1,23 @@
1
1
  # Boma JSON helper
2
2
  Super-simple helper for working with JSON
3
+
4
+ Work only in synchronous mode.
5
+
6
+ # Usage
7
+
8
+ ```JS
9
+ const logs = readJSON({ filePath: '/support.log', createIfNotFound: {}, parseJSON: true });
10
+ /*
11
+ logs = {
12
+ any: {}, //
13
+ key: {}
14
+ }
15
+ */
16
+
17
+ saveJSON(filePath: string, objToSave: Serializable, format?: boolean); // Format for save line break's
18
+
19
+ addToJSON('/support.log', { 6sdf89g7dghg: { any: "data" } });
20
+ /* Will save:
21
+ "{ "any": {}, "key": {}, "6sdf89g7dghg": { "any": "data" } }"
22
+ */
23
+ ```