all files / src/hyperscript/ parseSelector.ts

100% Statements 20/20
87.5% Branches 7/8
100% Functions 1/1
100% Lines 20/20
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36   22× 22× 22×   22×   22× 22×   22× 108×   108× 65×   43×   43× 22× 21× 17×       22×            
const classIdSplit = /([\.#]?[a-zA-Z0-9\u007F-\uFFFF_:-]+)/
 
export function parseSelector(selector: string) {
  let tagName: string | void
  let id = ''
  const classes: Array<string> = []
 
  const tagParts = selector.split(classIdSplit)
 
  let part: string | void
  let type
 
  for (let i = 0; i < tagParts.length; i++) {
    part = tagParts[i]
 
    if (!part)
      continue
 
    type = part.charAt(0)
 
    if (!tagName) {
      tagName = part
    } else if (type === '.') {
      classes.push(part.substring(1, part.length))
    } else Eif (type === '#') {
      id = part.substring(1, part.length)
    }
  }
 
  return {
    tagName: tagName as string,
    id,
    className: classes.join(' '),
  }
}