This function returns an array of the indentation level of each line of a string.
I made this as part of a yaml parser I'm trying to do =D
you use it like this:
getIndent("\t\t\ttext\n\tmore text\nnot indented")
and it returns this:
[3,1,0]
function(a,b){ | |
for(a in b=a.split("\n")) //for each line | |
b[a]=/\t*/.exec(b[a])[0].length; //replace string with indentation level | |
return b //return the array | |
} |
{ | |
"name": "getIndent", | |
"description": "Returns an array of the indentation level of each line of a string.", | |
"keywords": [ | |
"indentation", | |
"yaml", | |
"whitespace", | |
"haml", | |
"jade" | |
] | |
} |
Save 5 more bytes by reusing variables:
|
I tried to come up with my own solution and ended with something 2 bytes smaller. This abuses function(a,b){b=[];a.replace(/^\t*/gm,function(c){b.push(c.length)});return b} |
@maettig |
This comment has been minimized.
Save 2 bytes:
function(a,b,i){b=[];for(i in a=a.split("\n"))b[i]=/\t*/.exec(a[i])[0].length;return b}