@terzogenito/json-utils 1.0.4 → 1.0.5
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 +29 -28
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -1,14 +1,6 @@
|
|
|
1
1
|
const fs = require('fs');
|
|
2
2
|
const https = require('https');
|
|
3
3
|
|
|
4
|
-
function readJSON(jsonString) {
|
|
5
|
-
return JSON.parse(jsonString);
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
function toString(jsonObject) {
|
|
9
|
-
return JSON.stringify(jsonObject);
|
|
10
|
-
}
|
|
11
|
-
|
|
12
4
|
function getFile(filePath, callback) {
|
|
13
5
|
fs.readFile(filePath, 'utf8', (err, data) => {
|
|
14
6
|
if (err) {
|
|
@@ -20,6 +12,27 @@ function getFile(filePath, callback) {
|
|
|
20
12
|
});
|
|
21
13
|
}
|
|
22
14
|
|
|
15
|
+
function loadFile(filePath) {
|
|
16
|
+
return new Promise((resolve, reject) => {
|
|
17
|
+
fs.readFile(filePath, 'utf8', (err, data) => {
|
|
18
|
+
if (err) {
|
|
19
|
+
reject(err);
|
|
20
|
+
} else {
|
|
21
|
+
resolve(data);
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
async function getData(filePath) {
|
|
28
|
+
try {
|
|
29
|
+
const data = await loadFile(filePath);
|
|
30
|
+
return data;
|
|
31
|
+
} catch (err) {
|
|
32
|
+
console.error("Error reading the file:", err);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
23
36
|
function getURL(url, callback) {
|
|
24
37
|
https.get(url, (response) => {
|
|
25
38
|
let data = '';
|
|
@@ -47,25 +60,12 @@ function getContent(target, callback) {
|
|
|
47
60
|
}
|
|
48
61
|
}
|
|
49
62
|
|
|
50
|
-
function
|
|
51
|
-
return
|
|
52
|
-
fs.readFile(filePath, 'utf8', (err, data) => {
|
|
53
|
-
if (err) {
|
|
54
|
-
reject(err);
|
|
55
|
-
} else {
|
|
56
|
-
resolve(data);
|
|
57
|
-
}
|
|
58
|
-
});
|
|
59
|
-
});
|
|
63
|
+
function readJSON(jsonString) {
|
|
64
|
+
return JSON.parse(jsonString);
|
|
60
65
|
}
|
|
61
66
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
const data = await loadFile(filePath);
|
|
65
|
-
return data;
|
|
66
|
-
} catch (err) {
|
|
67
|
-
console.error("Error reading the file:", err);
|
|
68
|
-
}
|
|
67
|
+
function toString(jsonObject) {
|
|
68
|
+
return JSON.stringify(jsonObject);
|
|
69
69
|
}
|
|
70
70
|
|
|
71
71
|
function isValid(jsonString) {
|
|
@@ -107,11 +107,12 @@ function beautifyJSON(jsonString, indent) {
|
|
|
107
107
|
}
|
|
108
108
|
|
|
109
109
|
module.exports = {
|
|
110
|
-
readJSON,
|
|
111
|
-
toString,
|
|
112
110
|
getFile,
|
|
113
|
-
getURL,
|
|
114
111
|
getData,
|
|
112
|
+
getURL,
|
|
113
|
+
getContent,
|
|
114
|
+
readJSON,
|
|
115
|
+
toString,
|
|
115
116
|
isValid,
|
|
116
117
|
isJSON,
|
|
117
118
|
getJSON,
|