Created
March 16, 2015 17:52
-
-
Save mientjan/ce073078a195607f39a9 to your computer and use it in GitHub Desktop.
Handlebar: DumpHelper
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
function dump(context, options) { | |
function type(value){ | |
var type = ''; | |
if( Object.prototype.toString.call( value ) === '[object Array]' ){ | |
type = 'array'; | |
} else if(typeof value == 'object' ) { | |
type = 'object'; | |
} else { | |
type = 'primitive'; | |
} | |
return type; | |
} | |
function loop(value, callback){ | |
if( Object.prototype.toString.call( value ) === '[object Array]' ) { | |
for(var i = 0; i < value.length; i++) | |
{ | |
callback(value[i], i); | |
} | |
} else if(typeof value == 'object' ) { | |
for(var i in value) | |
{ | |
callback(value[i], i); | |
} | |
} else { | |
callback(value, 0); | |
} | |
} | |
var dump; | |
dump = function(context){ | |
var self = this; | |
var ret = ""; | |
ret += '<ul>'; | |
loop(context, function(value, index){ | |
ret += '<li>'; | |
ret += 'index: ' + index; | |
ret += '<br />'; | |
if( type(value) != 'primitive' ){ | |
ret += dump(value); | |
} else { | |
ret += 'value:' + value; | |
} | |
data += '</li>'; | |
}); | |
ret += '</ul>'; | |
return ret; | |
} | |
return dump(context); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment