Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
parse indentation of string

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
}
function(a,b){for(a in b=a.split("\n"))b[a]=/\t*/.exec(b[a])[0].length;return b}
//@maettig's version
function(a,b){b=[];a.replace(/^\t*/gm,function(c){b.push(c.length)});return b}
{
"name": "getIndent",
"description": "Returns an array of the indentation level of each line of a string.",
"keywords": [
"indentation",
"yaml",
"whitespace",
"haml",
"jade"
]
}
<!DOCTYPE html>
<title>Foo</title>
<b id=foo></b>
<script>
foo = document.getElementById("foo")
getIndent = function(a,b){for(a in b=a.split("\n"))b[a]=/\t*/.exec(b[a])[0].length;return b}
foo.innerHTML = getIndent("\t\t\ttext\n\tmore text\nnot indented")
</script>
@tsaniel

This comment has been minimized.

Show comment Hide comment
@tsaniel

tsaniel May 21, 2012

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}

tsaniel commented May 21, 2012

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}

@atk

This comment has been minimized.

Show comment Hide comment
@atk

atk May 21, 2012

Save 5 more bytes by reusing variables:

function(a,b){for(a in b=a.split("\n"))b[a]=/\t*/.exec(b[a])[0].length;return b}

atk commented May 21, 2012

Save 5 more bytes by reusing variables:

function(a,b){for(a in b=a.split("\n"))b[a]=/\t*/.exec(b[a])[0].length;return b}

@williammalo

This comment has been minimized.

Show comment Hide comment
@williammalo

williammalo May 21, 2012

@tsaniel ah thanks!
@atk Smart! Thanks!

Owner

williammalo commented May 21, 2012

@tsaniel ah thanks!
@atk Smart! Thanks!

@maettig

This comment has been minimized.

Show comment Hide comment
@maettig

maettig May 24, 2012

I tried to come up with my own solution and ended with something 2 bytes smaller. This abuses replace like it's a loop. Edit: Also it's a bit more reliable since your version returns [1] instead of [0] for "a\t".

function(a,b){b=[];a.replace(/^\t*/gm,function(c){b.push(c.length)});return b}

maettig commented May 24, 2012

I tried to come up with my own solution and ended with something 2 bytes smaller. This abuses replace like it's a loop. Edit: Also it's a bit more reliable since your version returns [1] instead of [0] for "a\t".

function(a,b){b=[];a.replace(/^\t*/gm,function(c){b.push(c.length)});return b}
@williammalo

This comment has been minimized.

Show comment Hide comment
@williammalo

williammalo May 24, 2012

@maettig
ah! very cool!

Owner

williammalo commented May 24, 2012

@maettig
ah! very cool!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment