Last active
December 12, 2015 07:29
-
-
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
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
/** | |
* @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