public
Last active

Update NimbleKit's NKLog with proper variable interrogation

  • Download Gist
gistfile1.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
//
// https://gist.github.com/1208947
// Include this JS right after NKit.js in your HTML files.
// Now NKLog() will show you the contents of arrays and objects!
//
 
var NKLog1 = NKLog;
var NKLog = function (arg) {
NKLog1('\n' + NKInterogate(arg));
}
 
function NKInterogate(arg) {
var output;
if (typeof arg === 'object' && arg.length) {
output = '[\n';
for (var i = 0; i < arg.length; i++) {
output += '\t' + NKInterogate(arg[i]).toString().split('\n').join('\n\t') + ',\n';
}
output += ']';
} else if (typeof arg === 'object') {
output = '{\n';
for (k in arg) {
output += '\t' + k + ': ' + NKInterogate(arg[k]).toString().split('\n').join('\n\t') + ',\n';
}
output += '}';
} else if (typeof arg === 'string') {
output = '"' + arg + '"';
} else {
output = arg;
}
return output;
}

JSON.stringify() unfortunately doesn't quite give the output the way I want it, and also doesn't report functions correctly.

NimbleKit's NKLog() does not function like console.log() in Firebug or Webkit Inspector... it would just output [Object object] which wasn't very useful.

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.