Created
May 4, 2012 00:31
-
-
Save Trindaz/2590689 to your computer and use it in GitHub Desktop.
Clothe 'naked' values of an object by wrapping in " chars for postponed evaluation
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
//clothe a hub file that has 'naked' expressions | |
//e.g. turn {key:$('body p')} into {key:"$('body p')"} | |
function clothe(contents){ | |
closers = /[\}\]\)\/"']/ | |
openers = /[\{\[\(\/"']/ | |
closing = { | |
"{": "}", | |
"[": "]", | |
"(": ")", | |
"/": "/", | |
'"': '"', | |
"'": "'" | |
} | |
contents = contents.split(""); | |
var beforeKey = true; | |
var inKey = false; | |
var beforeValue = false; | |
var inValue = false; | |
var inArray = false; | |
var delimiterStack = []; | |
function inDelimited(){ | |
return delimiterStack.length > 0; | |
} | |
function toggleDelimiter(d){ | |
if(openers.exec(d) && !closers.exec(d)){ | |
pushDelimiter(d); | |
}else if(openers.exec(d) && closers.exec(d)){ | |
if(topDelimiter()){ | |
if(topDelimiter()==d){ | |
popDelimiterIfValid(d); | |
}else{ | |
pushDelimiter(d); | |
} | |
}else{ | |
pushDelimiter(d); | |
} | |
}else if(closers.exec(d)){ | |
popDelimiterIfValid(d); | |
} | |
} | |
function topDelimiter(){ | |
if(delimiterStack.length>=0){ | |
return delimiterStack[delimiterStack.length-1]; | |
}else{ | |
return undefined; | |
} | |
} | |
function pushDelimiter(d){ | |
delimiterStack.push(d); | |
} | |
function popDelimiterIfValid(d){ | |
if(delimiterStack.length>0) | |
if(closing[delimiterStack[delimiterStack.length-1]]==d) | |
delimiterStack.pop(d); | |
} | |
function rTrimmedRightBound(rightBound){ | |
while(rightBound>0){ | |
if(!/\s/g.exec(contents[--rightBound])){ | |
return rightBound+1; | |
} | |
} | |
} | |
for(var i=0; i<contents.length; i++){ | |
function delimiterCheck(c){ | |
if(c=='"'){ | |
toggleDelimiter('"'); | |
contents.splice(i, 0, '\\'); | |
i++; | |
}else if(openers.exec(c) || closers.exec(c)){ | |
toggleDelimiter(c) | |
} | |
} | |
if(beforeKey){ | |
if(/[a-zA-Z0-9$_!]/.exec(contents[i])){ | |
beforeKey = false; | |
inKey = true; | |
} | |
}else if(inKey){ | |
if(contents[i]==":"){ | |
inKey = false; | |
beforeValue = true; | |
} | |
}else if(beforeValue){ | |
if(/[a-zA-Z0-9$_!'"\(\/]/.exec(contents[i])){ | |
contents.splice(i, 0, '"'); | |
i++; | |
beforeValue = false; | |
inValue = true; | |
delimiterCheck(contents[i]); | |
}else if(/\{/.exec(contents[i])){ | |
beforeKey = true; | |
beforeValue = false; | |
}else if(/\[/.exec(contents[i])){ | |
beforeValue = false; | |
inArray = true; | |
} | |
}else if(inArray && !inValue){ | |
if(/[a-zA-Z0-9$_!'"\(\/]/.exec(contents[i])){ | |
contents.splice(i, 0, '"'); | |
i++; | |
beforeValue = false; | |
inValue = true; | |
delimiterCheck(contents[i]); | |
} | |
}else if(inValue){ | |
if(!inDelimited() && /[\},\]]/.exec(contents[i])){ | |
contents.splice(rTrimmedRightBound(i), 0, '"'); | |
i++; | |
inValue = false; | |
if(/\]/.exec(contents[i])){ | |
inArray = false; | |
} | |
beforeKey = !inArray; | |
}else{ | |
delimiterCheck(contents[i]); | |
} | |
} | |
} | |
return contents.join(""); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment