Created
August 21, 2017 14:20
-
-
Save mateusvahl/1c90f217308dd42005a3545f534ec59e to your computer and use it in GitHub Desktop.
This file contains hidden or 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
const parseElementExpression = (str) => { | |
const elements = /([\#|\.][a-zA-Z]+)/; | |
const itens = str.split(elements).filter(x => x); | |
const getClassOrIdNames = (idOrClass) => | |
itens | |
.filter(_ => _.charAt(0) === idOrClass) | |
.map(_ => _.slice(1)); | |
const tag = itens.filter(_ => (_.charAt(0) !== '#') && (_.charAt(0) !== '.')); | |
const id = getClassOrIdNames('#'); | |
const classes = getClassOrIdNames('.'); | |
const attrs = {id: id, 'class': classes}; | |
return [tag, attrs]; | |
} | |
const [tag, attrs] = parseElementExpression("div#hellow.xyz.abc"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment