MarioJuniorPro
4/5/2017 - 4:05 PM

Javascript snippets

Javascript snippets


(function(){
    console.clear()

    const elementsFilter = (elem) => { return !['HTML', 'NOSCRIPT'].includes(elem) }

    const elements = Array.from(document.querySelectorAll('*'));

    const elementsMapper = (elem) => elem.tagName

    const elementsReducer = (acc, cur) => {
            if(acc[cur])
                acc[cur]++;
            else 
                acc[cur] = 1;
            return acc
        }

    const elementsReduced = elements
        .map(elementsMapper)
        .filter(elementsFilter) 
        .reduce(elementsReducer, {}) 

    const countSorter = (a, b) => {
      if (a.count > b.count)
        return -1;

      if (a.count < b.count)
        return 1;
      return 0;
    }

    const summarize = (countObject) => {
        let summary = []
        for(let o in countObject){
            summary.push({tagName: o, count: countObject[o]})
        }
        summary.sort(countSorter)
        return summary
    }

    const summary = summarize(elementsReduced)

    console.table(summary, ['tagName', 'count'])
})()