Skip to content

Instantly share code, notes, and snippets.

@bloodyowl
Created November 30, 2012 01:37
Show Gist options
  • Save bloodyowl/4173172 to your computer and use it in GitHub Desktop.
Save bloodyowl/4173172 to your computer and use it in GitHub Desktop.
String#extract
/* relies on Craft.js */
Object.extend(String.prototype, {
extract : function (context){
return this.split(".").fold(function(a,b){ if(a) return b in a ? a[b] : undefined}, context || window )
}
})
"document.body".extract() // HTMLBodyElement
"document.body.foo.bar.baz".extract() // undefined
"foo.bar.baz".extract({
foo : {
bar : {
baz : "foo"
}
}
}) // foo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment