@terzogenito/json-utils 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.
@@ -0,0 +1,28 @@
1
+ name: Publish to npm
2
+
3
+ on:
4
+ push:
5
+ branches:
6
+ - main
7
+
8
+ jobs:
9
+ publish:
10
+ runs-on: ubuntu-latest
11
+
12
+ steps:
13
+ - name: Checkout code
14
+ uses: actions/checkout@v3
15
+
16
+ - name: Setup Node.js
17
+ uses: actions/setup-node@v3
18
+ with:
19
+ node-version: 16
20
+ registry-url: https://registry.npmjs.org/
21
+
22
+ - name: Install dependencies
23
+ run: npm install
24
+
25
+ - name: Publish to npm
26
+ run: npm publish
27
+ env:
28
+ NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 terzogenito
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 @@
1
+ # json-utils
package/data.json ADDED
@@ -0,0 +1,5 @@
1
+ {
2
+ "name": "John",
3
+ "age": 30,
4
+ "isActive": true
5
+ }
package/index.js ADDED
@@ -0,0 +1,90 @@
1
+ const fs = require('fs');
2
+
3
+ function readJSON(jsonString) {
4
+ return JSON.parse(jsonString);
5
+ }
6
+
7
+ function toString(jsonObject) {
8
+ return JSON.stringify(jsonObject);
9
+ }
10
+
11
+ function getFile(filePath, callback) {
12
+ fs.readFile(filePath, 'utf8', (err, data) => {
13
+ if (err) {
14
+ console.error("Error reading the file:", err);
15
+ return;
16
+ } else {
17
+ callback(data);
18
+ }
19
+ });
20
+ }
21
+
22
+ function loadFile(filePath) {
23
+ return new Promise((resolve, reject) => {
24
+ fs.readFile(filePath, 'utf8', (err, data) => {
25
+ if (err) {
26
+ reject(err);
27
+ } else {
28
+ resolve(data);
29
+ }
30
+ });
31
+ });
32
+ }
33
+
34
+ async function getData(filePath) {
35
+ try {
36
+ const data = await loadFile(filePath);
37
+ return data;
38
+ } catch (err) {
39
+ console.error("Error reading the file:", err);
40
+ }
41
+ }
42
+
43
+ function isValid(jsonString) {
44
+ try {
45
+ JSON.parse(jsonString);
46
+ return true;
47
+ } catch (error) {
48
+ console.log("The string is not valid JSON.");
49
+ return false;
50
+ }
51
+ }
52
+
53
+ function isJSON(jsonObject) {
54
+ try {
55
+ const parsedData = JSON.parse(jsonObject);
56
+ return typeof parsedData === 'object' && parsedData !== null;
57
+ } catch (error) {
58
+ return false;
59
+ }
60
+ }
61
+
62
+ function getJSON(filePath, callback) {
63
+ getFile(filePath, data => {
64
+ if (isValid(data)) {
65
+ callback(readJSON(data));
66
+ }
67
+ });
68
+ }
69
+
70
+ function beautifyJSON(jsonString, indent) {
71
+ try {
72
+ const jsonObject = JSON.parse(jsonString);
73
+ if (!indent) indent = 2;
74
+ return JSON.stringify(jsonObject, null, indent);
75
+ } catch (error) {
76
+ console.error("Invalid JSON string:", error);
77
+ return null;
78
+ }
79
+ }
80
+
81
+ module.exports = {
82
+ readJSON,
83
+ toString,
84
+ getFile,
85
+ getData,
86
+ isValid,
87
+ isJSON,
88
+ getJSON,
89
+ beautifyJSON,
90
+ };
package/package.json ADDED
@@ -0,0 +1,16 @@
1
+ {
2
+ "name": "@terzogenito/json-utils",
3
+ "version": "1.0.0",
4
+ "description": "Check JSON Format",
5
+ "main": "index.js",
6
+ "scripts": {
7
+ "start": "node test.js",
8
+ "test": "echo \"Error: no test specified\" && exit 1"
9
+ },
10
+ "publishConfig": {
11
+ "access": "public"
12
+ },
13
+ "keywords": ["nodejs", "module", "json", "check", "utils"],
14
+ "author": "Terzogenito",
15
+ "license": "MIT"
16
+ }
package/test.js ADDED
@@ -0,0 +1,22 @@
1
+ const app = require('./index');
2
+
3
+ var sampleData = '{"name":"John","age":30,"isActive":true}';
4
+
5
+ (async () => {
6
+ console.log(await app.getData('./data.json'));
7
+ })();
8
+
9
+ app.getFile('data.json',data=>{
10
+ console.log(app.isValid(data));
11
+ });
12
+
13
+ app.getJSON('data.json',data=>{
14
+ console.log(app.toString(data));
15
+ });
16
+
17
+ console.log(app.readJSON(sampleData));
18
+
19
+ console.log(app.isJSON(sampleData));
20
+
21
+ console.log(app.beautifyJSON(sampleData));
22
+ console.log(app.beautifyJSON(sampleData, 4));