Skip to content

Instantly share code, notes, and snippets.

@raimohanska
Last active August 29, 2015 14:15
Show Gist options
  • Select an option

  • Save raimohanska/41debaaa8d92d5667365 to your computer and use it in GitHub Desktop.

Select an option

Save raimohanska/41debaaa8d92d5667365 to your computer and use it in GitHub Desktop.
depth.js
#! /usr/bin/env node
var fs = require("fs")
var files = process.argv.slice(2)
if (!files.length) {
console.log("USAGE: depth.js file1 [file2 ...]")
}
files.forEach(function(filename) {
var object = JSON.parse(fs.readFileSync(filename))
console.log(filename, depth(object, ""))
function depth(object, path) {
var max = 0;
var maxKey = ""
for (var key in object) {
if (typeof object == "object" && object.hasOwnProperty(key)) {
var childDepth = depth(object[key], key)
var localDepth = childDepth.depth + 1
if (localDepth > max) {
max = localDepth
maxKey = childDepth.key
}
}
}
return { depth: max, key : path + "." + maxKey }
}
})
@raimohanska
Copy link
Copy Markdown
Author

utility for measuring maximum depth of JSON files

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment