@terzogenito/json-utils 1.0.7 → 1.0.8
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/index.js +2 -71
- package/package.json +5 -2
package/index.js
CHANGED
|
@@ -1,68 +1,4 @@
|
|
|
1
|
-
const
|
|
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
|
-
console.error("Error reading the file:", err);
|
|
12
|
-
return;
|
|
13
|
-
} else {
|
|
14
|
-
callback(data);
|
|
15
|
-
}
|
|
16
|
-
});
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
function loadFile(filePath) {
|
|
20
|
-
return new Promise((resolve, reject) => {
|
|
21
|
-
fs.readFile(filePath, 'utf8', (err, data) => {
|
|
22
|
-
if (err) {
|
|
23
|
-
reject(err);
|
|
24
|
-
} else {
|
|
25
|
-
resolve(data);
|
|
26
|
-
}
|
|
27
|
-
});
|
|
28
|
-
});
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
async function getData(filePath) {
|
|
32
|
-
try {
|
|
33
|
-
const data = await loadFile(filePath);
|
|
34
|
-
return data;
|
|
35
|
-
} catch (err) {
|
|
36
|
-
console.error("Error reading the file:", err);
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
function getURL(url, callback) {
|
|
41
|
-
https.get(url, (response) => {
|
|
42
|
-
let data = '';
|
|
43
|
-
response.on('data', (chunk) => {
|
|
44
|
-
data += chunk;
|
|
45
|
-
});
|
|
46
|
-
response.on('end', () => {
|
|
47
|
-
callback(data);
|
|
48
|
-
});
|
|
49
|
-
}).on('error', (err) => {
|
|
50
|
-
console.error("Error fetching the URL:", err);
|
|
51
|
-
});
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
function isUrl(input) {
|
|
55
|
-
const urlPattern = /^(https?:\/\/|ftp:\/\/|www\.)/i;
|
|
56
|
-
return urlPattern.test(input);
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
function getContent(target, callback) {
|
|
60
|
-
if (isUrl(target)) {
|
|
61
|
-
getURL(target, callback);
|
|
62
|
-
} else {
|
|
63
|
-
getFile(target, callback);
|
|
64
|
-
}
|
|
65
|
-
}
|
|
1
|
+
const fileUtils = require('@terzogenito/file-utils');
|
|
66
2
|
|
|
67
3
|
function readJSON(jsonString) {
|
|
68
4
|
return JSON.parse(jsonString);
|
|
@@ -92,7 +28,7 @@ function isJSON(jsonObject) {
|
|
|
92
28
|
}
|
|
93
29
|
|
|
94
30
|
function getJSON(target, callback) {
|
|
95
|
-
getContent(target, data => {
|
|
31
|
+
fileUtils.getContent(target, data => {
|
|
96
32
|
if (isValid(data)) {
|
|
97
33
|
callback(readJSON(data));
|
|
98
34
|
}
|
|
@@ -111,11 +47,6 @@ function beautifyJSON(jsonString, indent) {
|
|
|
111
47
|
}
|
|
112
48
|
|
|
113
49
|
module.exports = {
|
|
114
|
-
getString,
|
|
115
|
-
getFile,
|
|
116
|
-
getData,
|
|
117
|
-
getURL,
|
|
118
|
-
getContent,
|
|
119
50
|
readJSON,
|
|
120
51
|
toString,
|
|
121
52
|
isValid,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@terzogenito/json-utils",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.8",
|
|
4
4
|
"description": "Check JSON Format",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"files": [
|
|
@@ -24,5 +24,8 @@
|
|
|
24
24
|
"utils"
|
|
25
25
|
],
|
|
26
26
|
"author": "Terzogenito",
|
|
27
|
-
"license": "MIT"
|
|
27
|
+
"license": "MIT",
|
|
28
|
+
"dependencies": {
|
|
29
|
+
"@terzogenito/file-utils": "^1.0.0"
|
|
30
|
+
}
|
|
28
31
|
}
|