Skip to content

Instantly share code, notes, and snippets.

@luiscronicl
Last active December 12, 2015 07:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save luiscronicl/4737441 to your computer and use it in GitHub Desktop.
Save luiscronicl/4737441 to your computer and use it in GitHub Desktop.
Function for parsing one Object which represents a directory structure into an array
/**
* @param JSON incoming The JSON with all the strings
* @param array templates The temporary holder of the templates
* @param parentName string Helper string for storing the name of the route
* @return array Array with the parsed routes
* @example parse([dirA: ['example', 'example2'], dirB: ['example3', subdir:['s1','s2']])
* returns ['dirA/example','dirA/example2','dirB/example3','dirB/subdir/s1','dirb/subdir/s2']
*/
function parse(incoming, templates, parentName){
var i;
templates = templates || [];
parentName = parentName || "";
if(parentName != "" && parentName.charAt(parentName.length-1) != "/"){
parentName += "/";
}
for(i in incoming){
switch (typeof incoming[i]){
case 'object':
parse(incoming[i], templates, parentName +($.isNumeric(i)?"":i));
break;
case 'string':
templates.push(parentName+ incoming[i]);
break;
}
}
return templates;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment