@terzogenito/json-utils 1.0.3 → 1.0.4

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 +31 -2
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  const fs = require('fs');
2
+ const https = require('https');
2
3
 
3
4
  function readJSON(jsonString) {
4
5
  return JSON.parse(jsonString);
@@ -19,6 +20,33 @@ function getFile(filePath, callback) {
19
20
  });
20
21
  }
21
22
 
23
+ function getURL(url, callback) {
24
+ https.get(url, (response) => {
25
+ let data = '';
26
+ response.on('data', (chunk) => {
27
+ data += chunk;
28
+ });
29
+ response.on('end', () => {
30
+ callback(data);
31
+ });
32
+ }).on('error', (err) => {
33
+ console.error("Error fetching the URL:", err);
34
+ });
35
+ }
36
+
37
+ function isUrl(input) {
38
+ const urlPattern = /^(https?:\/\/|ftp:\/\/|www\.)/i;
39
+ return urlPattern.test(input);
40
+ }
41
+
42
+ function getContent(target, callback) {
43
+ if (isUrl(target)) {
44
+ getURL(target, callback);
45
+ } else {
46
+ getFile(target, callback);
47
+ }
48
+ }
49
+
22
50
  function loadFile(filePath) {
23
51
  return new Promise((resolve, reject) => {
24
52
  fs.readFile(filePath, 'utf8', (err, data) => {
@@ -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
  }
@@ -82,6 +110,7 @@ module.exports = {
82
110
  readJSON,
83
111
  toString,
84
112
  getFile,
113
+ getURL,
85
114
  getData,
86
115
  isValid,
87
116
  isJSON,
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.4",
4
4
  "description": "Check JSON Format",
5
5
  "main": "index.js",
6
6
  "files": [