Last active
August 29, 2015 14:15
-
-
Save raimohanska/41debaaa8d92d5667365 to your computer and use it in GitHub Desktop.
depth.js
This file contains hidden or 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
#! /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 } | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
utility for measuring maximum depth of JSON files