-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
3,833 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# v0.1.3 | ||
|
||
- Bug: Handle different structure between items in array. E.g: [{a: true}, {b: true}] | ||
- Feature: Show type of the value for each path. If multiple types, show separated by a `|`. | ||
- Tests: Added tests to cover the basic cases. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
function summarizePaths(jsonObj, prefix = "", paths = {}) { | ||
if (Array.isArray(jsonObj)) { | ||
// Handle arrays: Add "[]" to the path and process the first element (if exists) | ||
if (jsonObj.length > 0) { | ||
const path = `${prefix}[]`; | ||
if (!!prefix) { | ||
if (!paths[prefix]) { | ||
paths[prefix] = []; | ||
} | ||
paths[prefix].push(jsonObj); | ||
} | ||
for (let i = 0; i < jsonObj.length; i++) { | ||
summarizePaths(jsonObj[i], path, paths); | ||
} | ||
} | ||
} else if (typeof jsonObj === "object" && jsonObj !== null) { | ||
const path = `${prefix}`; | ||
if (!!prefix) { | ||
if (!paths[path]) { | ||
paths[path] = []; | ||
} | ||
paths[path].push(jsonObj); | ||
} | ||
Object.keys(jsonObj).forEach((key) => { | ||
const k = key.includes(" ") ? `"${key}"` : key; | ||
summarizePaths(jsonObj[key], `${path}.${k}`, paths); | ||
}); | ||
} else { | ||
// base case: add the value to the path | ||
if (!paths[prefix]) { | ||
paths[prefix] = []; | ||
} | ||
paths[prefix].push(jsonObj); | ||
} | ||
return paths; | ||
} | ||
|
||
function pathsToLines(paths) { | ||
const lines = []; | ||
for (const path in paths) { | ||
const values = paths[path]; | ||
const valueTypes = values.map((value) => { | ||
if (Array.isArray(value)) { | ||
return "array"; | ||
} else if (value === null) { | ||
return "null"; | ||
} else { | ||
return typeof value; | ||
} | ||
}); | ||
const uniqueValueTypes = [...new Set(valueTypes)]; | ||
const valueType = | ||
uniqueValueTypes.length === 1 | ||
? uniqueValueTypes[0] | ||
: `${uniqueValueTypes.join(" | ")}`; | ||
lines.push(`${path}: ${valueType}`); | ||
} | ||
return lines; | ||
} | ||
|
||
function processJson(jsonObject) { | ||
try { | ||
const paths = summarizePaths(jsonObject); | ||
const lines = pathsToLines(paths); | ||
for (const line of lines) { | ||
console.log(line); | ||
} | ||
} catch (error) { | ||
console.error("Error processing JSON:", error.message); | ||
} | ||
} | ||
|
||
module.exports = { summarizePaths, pathsToLines, processJson }; |
Oops, something went wrong.