type listType = [string, number][]
type keyByFunction = (tag: [string, number]) => number
const maxBy = (list: listType, keyBy:keyByFunction) => {
return list.reduce((x, y) => {
return keyBy(x) > keyBy(y) ? x : y
})
}
const filterTag:keyByFunction = tag => tag[1]
const getMostFrequent = () => {
const tags = [...document.querySelectorAll('*')]
.map(tag => tag.tagName)
.reduce((acc, cur) => {
acc[cur] = acc[cur] ? acc[cur] + 1 : 1
return acc
}, {})
return maxBy(Object.entries(tags), filterTag)
}
// Todo: querySelectorAll polyfill
type elType = HTMLElement | Document | Element
const getAllTags = (el: elType = document) => {
const children = Array.from(el.children)
.reduce((acc, cur) => [...acc, ...getAllTags(cur)], [])
return children
}