@terzogenito/json-utils 1.0.8 → 1.0.9
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/README.md +192 -1
- package/index.js +100 -7
- package/package.json +28 -31
package/README.md
CHANGED
|
@@ -1 +1,192 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Documentation
|
|
2
|
+
|
|
3
|
+
## Overview
|
|
4
|
+
This module provides various functions for reading, validating, and processing JSON files and URL content.
|
|
5
|
+
|
|
6
|
+
## Function List
|
|
7
|
+
|
|
8
|
+
### 1. `getData(path)`
|
|
9
|
+
```javascript
|
|
10
|
+
console.log(await app.getData('./test-data.json'));
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
**Description**: Reads and returns data from a file asynchronously.
|
|
14
|
+
|
|
15
|
+
**Parameters**:
|
|
16
|
+
- `path` (String): Path to the file to be read
|
|
17
|
+
|
|
18
|
+
---
|
|
19
|
+
|
|
20
|
+
### 2. `getString(path)`
|
|
21
|
+
```javascript
|
|
22
|
+
console.log(app.getString('test-data.json'));
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
**Description**: Reads a file and returns its content as a string.
|
|
26
|
+
|
|
27
|
+
**Parameters**:
|
|
28
|
+
- `path` (String): Path to the file to be read
|
|
29
|
+
|
|
30
|
+
---
|
|
31
|
+
|
|
32
|
+
### 3. `getFile(path, callback)`
|
|
33
|
+
```javascript
|
|
34
|
+
app.getFile('test-data.json', data => {
|
|
35
|
+
console.log(app.isValid(data));
|
|
36
|
+
});
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
**Description**: Reads a file and processes the data using a callback function.
|
|
40
|
+
|
|
41
|
+
**Parameters**:
|
|
42
|
+
- `path` (String): Path to the file to be read
|
|
43
|
+
- `callback` (Function): Function to be called with the file data
|
|
44
|
+
|
|
45
|
+
---
|
|
46
|
+
|
|
47
|
+
### 4. `isValid(data)`
|
|
48
|
+
```javascript
|
|
49
|
+
app.getFile('test-data.json', data => {
|
|
50
|
+
console.log(app.isValid(data));
|
|
51
|
+
});
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
**Description**: Validates whether the data is valid JSON.
|
|
55
|
+
|
|
56
|
+
**Parameters**:
|
|
57
|
+
- `data` (Any): Data to be validated
|
|
58
|
+
|
|
59
|
+
---
|
|
60
|
+
|
|
61
|
+
### 5. `toString(data)`
|
|
62
|
+
```javascript
|
|
63
|
+
app.getFile('test-data.json', data => {
|
|
64
|
+
console.log(app.toString(data));
|
|
65
|
+
});
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
**Description**: Converts JSON data to a string.
|
|
69
|
+
|
|
70
|
+
**Parameters**:
|
|
71
|
+
- `data` (Object): JSON data to be converted
|
|
72
|
+
|
|
73
|
+
---
|
|
74
|
+
|
|
75
|
+
### 6. `readJSON(jsonString)`
|
|
76
|
+
```javascript
|
|
77
|
+
console.log(app.readJSON('{"name":"John","age":30,"isActive":true}'));
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
**Description**: Reads and parses a JSON string into a JavaScript object.
|
|
81
|
+
|
|
82
|
+
**Parameters**:
|
|
83
|
+
- `jsonString` (String): JSON string to be parsed
|
|
84
|
+
|
|
85
|
+
---
|
|
86
|
+
|
|
87
|
+
### 7. `isJSON(jsonString)`
|
|
88
|
+
```javascript
|
|
89
|
+
console.log(app.isJSON('{"name":"John","age":30,"isActive":true}'));
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
**Description**: Checks if a string is valid JSON.
|
|
93
|
+
|
|
94
|
+
**Parameters**:
|
|
95
|
+
- `jsonString` (String): String to be checked
|
|
96
|
+
|
|
97
|
+
---
|
|
98
|
+
|
|
99
|
+
### 8. `isJSONObject(obj)`
|
|
100
|
+
```javascript
|
|
101
|
+
console.log(app.isJSONObject({"name": "John","age": 30,"isActive": true}));
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
**Description**: Checks if an object is a valid JSON object.
|
|
105
|
+
|
|
106
|
+
**Parameters**:
|
|
107
|
+
- `obj` (Object): Object to be checked
|
|
108
|
+
|
|
109
|
+
---
|
|
110
|
+
|
|
111
|
+
### 9. `beautifyJSON(jsonString, indent)`
|
|
112
|
+
```javascript
|
|
113
|
+
console.log(app.beautifyJSON(jsonString));
|
|
114
|
+
console.log(app.beautifyJSON(jsonString, 4));
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
**Description**: Formats a JSON string with customizable indentation.
|
|
118
|
+
|
|
119
|
+
**Parameters**:
|
|
120
|
+
- `jsonString` (String): JSON string to be formatted
|
|
121
|
+
- `indent` (Number, optional): Number of spaces for indentation (default: 2)
|
|
122
|
+
|
|
123
|
+
---
|
|
124
|
+
|
|
125
|
+
### 10. `beautify(jsonObject)`
|
|
126
|
+
```javascript
|
|
127
|
+
console.log(app.beautify(sampleJSONObject));
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
**Description**: Formats a JSON object into a readable string.
|
|
131
|
+
|
|
132
|
+
**Parameters**:
|
|
133
|
+
- `jsonObject` (Object): JSON object to be formatted
|
|
134
|
+
|
|
135
|
+
---
|
|
136
|
+
|
|
137
|
+
### 11. `getURL(url, callback)`
|
|
138
|
+
```javascript
|
|
139
|
+
app.getURL("https://example.com/data.txt", data => {
|
|
140
|
+
console.log(data);
|
|
141
|
+
});
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
**Description**: Fetches string content from a URL.
|
|
145
|
+
|
|
146
|
+
**Parameters**:
|
|
147
|
+
- `url` (String): Target URL
|
|
148
|
+
- `callback` (Function): Callback function that receives the data
|
|
149
|
+
|
|
150
|
+
---
|
|
151
|
+
|
|
152
|
+
### 12. `getJSON(url, callback)`
|
|
153
|
+
```javascript
|
|
154
|
+
app.getJSON("https://example.com/data.json", data => {
|
|
155
|
+
console.log(data);
|
|
156
|
+
});
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
**Description**: Fetches and parses JSON content from a URL.
|
|
160
|
+
|
|
161
|
+
**Parameters**:
|
|
162
|
+
- `url` (String): URL pointing to JSON file
|
|
163
|
+
- `callback` (Function): Callback function that receives JSON data
|
|
164
|
+
|
|
165
|
+
## Usage Examples
|
|
166
|
+
|
|
167
|
+
```javascript
|
|
168
|
+
const app = require('./index');
|
|
169
|
+
|
|
170
|
+
// Reading local file
|
|
171
|
+
const data = await app.getData('./data.json');
|
|
172
|
+
|
|
173
|
+
// Validating JSON
|
|
174
|
+
const isValid = app.isJSON('{"key": "value"}');
|
|
175
|
+
|
|
176
|
+
// Formatting JSON
|
|
177
|
+
const beautified = app.beautifyJSON('{"key":"value"}', 4);
|
|
178
|
+
|
|
179
|
+
// Fetching data from URL
|
|
180
|
+
app.getJSON("https://api.example.com/data", jsonData => {
|
|
181
|
+
console.log(jsonData);
|
|
182
|
+
});
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
## Installation
|
|
186
|
+
```bash
|
|
187
|
+
npm install @terzogenito/json-utils
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
## Requirements
|
|
191
|
+
- Node.js 12 or higher
|
|
192
|
+
- Internet connection (for URL functions)
|
package/index.js
CHANGED
|
@@ -1,4 +1,67 @@
|
|
|
1
|
-
const
|
|
1
|
+
const fs = require('fs');
|
|
2
|
+
const https = require('https');
|
|
3
|
+
|
|
4
|
+
function getString(filePath) {
|
|
5
|
+
return fs.readFileSync(filePath, 'utf-8');
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
function getFile(filePath, callback) {
|
|
9
|
+
fs.readFile(filePath, 'utf8', (err, data) => {
|
|
10
|
+
if (err) {
|
|
11
|
+
return;
|
|
12
|
+
} else {
|
|
13
|
+
callback(data);
|
|
14
|
+
}
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function loadFile(filePath) {
|
|
19
|
+
return new Promise((resolve, reject) => {
|
|
20
|
+
fs.readFile(filePath, 'utf8', (err, data) => {
|
|
21
|
+
if (err) {
|
|
22
|
+
reject(err);
|
|
23
|
+
} else {
|
|
24
|
+
resolve(data);
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
async function getData(filePath) {
|
|
31
|
+
try {
|
|
32
|
+
const data = await loadFile(filePath);
|
|
33
|
+
return data;
|
|
34
|
+
} catch {
|
|
35
|
+
return null;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function getURL(url, callback) {
|
|
40
|
+
https.get(url, (response) => {
|
|
41
|
+
let data = '';
|
|
42
|
+
response.on('data', chunk => {
|
|
43
|
+
data += chunk;
|
|
44
|
+
});
|
|
45
|
+
response.on('end', () => {
|
|
46
|
+
callback(data || null);
|
|
47
|
+
});
|
|
48
|
+
}).on('error', (err) => {
|
|
49
|
+
callback(null);
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function isUrl(input) {
|
|
54
|
+
const urlPattern = /^(https?:\/\/|ftp:\/\/|www\.)/i;
|
|
55
|
+
return urlPattern.test(input);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function getContent(target, callback) {
|
|
59
|
+
if (isUrl(target)) {
|
|
60
|
+
getURL(target, callback);
|
|
61
|
+
} else {
|
|
62
|
+
getFile(target, callback);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
2
65
|
|
|
3
66
|
function readJSON(jsonString) {
|
|
4
67
|
return JSON.parse(jsonString);
|
|
@@ -12,8 +75,7 @@ function isValid(jsonString) {
|
|
|
12
75
|
try {
|
|
13
76
|
JSON.parse(jsonString);
|
|
14
77
|
return true;
|
|
15
|
-
} catch
|
|
16
|
-
console.log("The string is not valid JSON.");
|
|
78
|
+
} catch {
|
|
17
79
|
return false;
|
|
18
80
|
}
|
|
19
81
|
}
|
|
@@ -27,30 +89,61 @@ function isJSON(jsonObject) {
|
|
|
27
89
|
}
|
|
28
90
|
}
|
|
29
91
|
|
|
92
|
+
function isJSONObject(jsonObject) {
|
|
93
|
+
try {
|
|
94
|
+
return typeof jsonObject === 'object' && jsonObject !== null && jsonObject !== undefined;
|
|
95
|
+
} catch (error) {
|
|
96
|
+
return false;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
30
100
|
function getJSON(target, callback) {
|
|
31
|
-
|
|
32
|
-
if (
|
|
101
|
+
getContent(target, data => {
|
|
102
|
+
if (!data) {
|
|
103
|
+
return callback(null);
|
|
104
|
+
}
|
|
105
|
+
if (!isValid(data)) {
|
|
106
|
+
return callback(null);
|
|
107
|
+
}
|
|
108
|
+
try {
|
|
33
109
|
callback(readJSON(data));
|
|
110
|
+
} catch {
|
|
111
|
+
callback(null);
|
|
34
112
|
}
|
|
35
113
|
});
|
|
36
114
|
}
|
|
37
115
|
|
|
38
|
-
function beautifyJSON(jsonString, indent) {
|
|
116
|
+
function beautifyJSON(jsonString, indent = 2) {
|
|
39
117
|
try {
|
|
118
|
+
if (typeof jsonString !== 'string') return null;
|
|
40
119
|
const jsonObject = JSON.parse(jsonString);
|
|
120
|
+
return JSON.stringify(jsonObject, null, indent);
|
|
121
|
+
} catch {
|
|
122
|
+
return null;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function beautify(jsonObject, indent) {
|
|
127
|
+
try {
|
|
41
128
|
if (!indent) indent = 2;
|
|
42
129
|
return JSON.stringify(jsonObject, null, indent);
|
|
43
130
|
} catch (error) {
|
|
44
|
-
console.error("Invalid JSON string:", error);
|
|
45
131
|
return null;
|
|
46
132
|
}
|
|
47
133
|
}
|
|
48
134
|
|
|
49
135
|
module.exports = {
|
|
136
|
+
getString,
|
|
137
|
+
getFile,
|
|
138
|
+
getData,
|
|
139
|
+
getURL,
|
|
140
|
+
getContent,
|
|
50
141
|
readJSON,
|
|
51
142
|
toString,
|
|
52
143
|
isValid,
|
|
53
144
|
isJSON,
|
|
145
|
+
isJSONObject,
|
|
54
146
|
getJSON,
|
|
55
147
|
beautifyJSON,
|
|
148
|
+
beautify,
|
|
56
149
|
};
|
package/package.json
CHANGED
|
@@ -1,31 +1,28 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@terzogenito/json-utils",
|
|
3
|
-
"version": "1.0.
|
|
4
|
-
"description": "Check JSON Format",
|
|
5
|
-
"main": "index.js",
|
|
6
|
-
"files": [
|
|
7
|
-
"index.js"
|
|
8
|
-
],
|
|
9
|
-
"scripts": {
|
|
10
|
-
"start": "node test.js",
|
|
11
|
-
"test": "echo \"Error: no test specified\" && exit 1"
|
|
12
|
-
},
|
|
13
|
-
"publishConfig": {
|
|
14
|
-
"access": "public"
|
|
15
|
-
},
|
|
16
|
-
"engines": {
|
|
17
|
-
"node": ">=21.6.1"
|
|
18
|
-
},
|
|
19
|
-
"keywords": [
|
|
20
|
-
"nodejs",
|
|
21
|
-
"module",
|
|
22
|
-
"json",
|
|
23
|
-
"check",
|
|
24
|
-
"utils"
|
|
25
|
-
],
|
|
26
|
-
"author": "Terzogenito",
|
|
27
|
-
"license": "MIT"
|
|
28
|
-
|
|
29
|
-
"@terzogenito/file-utils": "^1.0.0"
|
|
30
|
-
}
|
|
31
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "@terzogenito/json-utils",
|
|
3
|
+
"version": "1.0.9",
|
|
4
|
+
"description": "Check JSON Format",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"files": [
|
|
7
|
+
"index.js"
|
|
8
|
+
],
|
|
9
|
+
"scripts": {
|
|
10
|
+
"start": "node test.js",
|
|
11
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
12
|
+
},
|
|
13
|
+
"publishConfig": {
|
|
14
|
+
"access": "public"
|
|
15
|
+
},
|
|
16
|
+
"engines": {
|
|
17
|
+
"node": ">=21.6.1"
|
|
18
|
+
},
|
|
19
|
+
"keywords": [
|
|
20
|
+
"nodejs",
|
|
21
|
+
"module",
|
|
22
|
+
"json",
|
|
23
|
+
"check",
|
|
24
|
+
"utils"
|
|
25
|
+
],
|
|
26
|
+
"author": "Terzogenito",
|
|
27
|
+
"license": "MIT"
|
|
28
|
+
}
|