Skip to content

Instantly share code, notes, and snippets.

@williammalo williammalo/README.md forked from 140bytes/LICENSE.txt
Last active Oct 5, 2015

Embed
What would you like to do?
blockifyIndents

Turns indentation into {curly brackets} Great for a yml to json converter or something. Credit to @maettig for writing all* the code.

Turn this:

if(3==3)
    alert("obviously!")
    alert("lol!")

Into:

if(3==3){
    alert("obviously!")
    alert("lol!")
}

jsperf showing the different methods:

http://jsperf.com/blockifyindents

*almost

function(a,b,c){
for(
b=1; //loop starts at 1
(c=RegExp('(\n\t{'+b+++'}).*(\\1.*)*','g')) //for every indent level (b)
.test(a=a.replace(c,'{$&}')); //put curly braces around blocks
); //if regex test returns false, end loop
return a //return the blockified string
}
function(a,b,c){for(b=1;(c=RegExp('(\n\t{'+b+++'}).*(\\1.*)*','g')).test(a=a.replace(c,'{$&}')););return a}
//shorter version (Only if indents are smaller than 9!!!)
function(a,b){for(b=9;--b;)a=a.replace(RegExp('(\n\t{'+b+'}).*(\\1.*)*','g'),'{$&}');return a}
{
"name": "blockifyIndents",
"description": "Turns indentation into {curly brackets}.",
"keywords": [
"indentation",
"tabs",
"block",
"whitespace",
"yml"
]
}
<!DOCTYPE html>
<title>Foo</title>
<div id = code>
if(3==3)
alert("obviously!")
alert("lol!")
</div>
<script>
blockifyIndents = function(a,b,c){for(b=1;(c=RegExp('(\n\t{'+b+++'}).*(\\1.*)*','g')).test(a=a.replace(c,'{$&}')););return a}
document.write(blockifyIndents(document.getElementById("code").innerHTML))
</script>
@maettig

This comment has been minimized.

Copy link

commented Jun 6, 2012

You can save a few bytes. Also your version leaked the variable e to the global scope. You may find my dumpGlobalLeaks helpful.

function(a,e,i,j,o){for(i in a=a.split("\n"))for(e=~~o-(o=/\t*/.exec(a[i])[0].length),j=e<0?-e:e;j--;)a[i-1]+=e<0?"{":"}";return a.join("\n")}
@williammalo

This comment has been minimized.

Copy link
Owner Author

commented Jun 6, 2012

@maettig
=D smart bit of code there!
Thanks!

@maettig

This comment has been minimized.

Copy link

commented Jun 6, 2012

Here is an other idea I had. I'm not sure if it works in all cases. Maybe we need to replace the first .* with [^\t]*. Do you have more test cases?

function(a,b){for(b=9;--b;)a=a.replace(RegExp('(\n\t{'+b+'}).*(\\1.*)*','g'),'{$&}');return a}
@williammalo

This comment has been minimized.

Copy link
Owner Author

commented Jun 6, 2012

@maettig
Holy shit that's genius!
But it only works up to a certain indent level...
Aside from that it seems to work with all the tests I did! :D

@maettig

This comment has been minimized.

Copy link

commented Jun 6, 2012

You can either replace 9 with 99 (which is very slow, see this jsPerf test case) or use something like the following to make it work with all indention levels.

function(a,b,c){for(b=1;(c=RegExp('(\n\t{'+b+++'}).*(\\1.*)*','g')).test(a);)a=a.replace(c,'{$&}');return a}
@williammalo

This comment has been minimized.

Copy link
Owner Author

commented Jun 6, 2012

@maettig
Sweet! I'll test it a bit and I'll probably make it the official version.
I also added safari and chrome to the jsperf.

@williammalo

This comment has been minimized.

Copy link
Owner Author

commented Jun 6, 2012

@maettig
Congratulations! Your version (edited to remove 1 byte) is now the official function!
Thanks a bunch!

@maettig

This comment has been minimized.

Copy link

commented Jun 6, 2012

This is a funny hack. You are executing test() and replace() in reverse order. But it works. It's even faster. I wonder why.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.