@terzogenito/json-utils 1.0.3 → 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.
Files changed (2) hide show
  1. package/index.js +42 -12
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -1,12 +1,5 @@
1
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
- }
2
+ const https = require('https');
10
3
 
11
4
  function getFile(filePath, callback) {
12
5
  fs.readFile(filePath, 'utf8', (err, data) => {
@@ -40,6 +33,41 @@ async function getData(filePath) {
40
33
  }
41
34
  }
42
35
 
36
+ function getURL(url, callback) {
37
+ https.get(url, (response) => {
38
+ let data = '';
39
+ response.on('data', (chunk) => {
40
+ data += chunk;
41
+ });
42
+ response.on('end', () => {
43
+ callback(data);
44
+ });
45
+ }).on('error', (err) => {
46
+ console.error("Error fetching the URL:", err);
47
+ });
48
+ }
49
+
50
+ function isUrl(input) {
51
+ const urlPattern = /^(https?:\/\/|ftp:\/\/|www\.)/i;
52
+ return urlPattern.test(input);
53
+ }
54
+
55
+ function getContent(target, callback) {
56
+ if (isUrl(target)) {
57
+ getURL(target, callback);
58
+ } else {
59
+ getFile(target, callback);
60
+ }
61
+ }
62
+
63
+ function readJSON(jsonString) {
64
+ return JSON.parse(jsonString);
65
+ }
66
+
67
+ function toString(jsonObject) {
68
+ return JSON.stringify(jsonObject);
69
+ }
70
+
43
71
  function isValid(jsonString) {
44
72
  try {
45
73
  JSON.parse(jsonString);
@@ -59,8 +87,8 @@ function isJSON(jsonObject) {
59
87
  }
60
88
  }
61
89
 
62
- function getJSON(filePath, callback) {
63
- getFile(filePath, data => {
90
+ function getJSON(target, callback) {
91
+ getContent(target, data => {
64
92
  if (isValid(data)) {
65
93
  callback(readJSON(data));
66
94
  }
@@ -79,10 +107,12 @@ function beautifyJSON(jsonString, indent) {
79
107
  }
80
108
 
81
109
  module.exports = {
82
- readJSON,
83
- toString,
84
110
  getFile,
85
111
  getData,
112
+ getURL,
113
+ getContent,
114
+ readJSON,
115
+ toString,
86
116
  isValid,
87
117
  isJSON,
88
118
  getJSON,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@terzogenito/json-utils",
3
- "version": "1.0.3",
3
+ "version": "1.0.5",
4
4
  "description": "Check JSON Format",
5
5
  "main": "index.js",
6
6
  "files": [