Skip to content

Instantly share code, notes, and snippets.

@cacheflow
Created June 22, 2017 23:32
Show Gist options
  • Save cacheflow/81491b833c3316bcf444cb70bc439023 to your computer and use it in GitHub Desktop.
Save cacheflow/81491b833c3316bcf444cb70bc439023 to your computer and use it in GitHub Desktop.
function getAuthor () {
let selectorsToCheck = ['span', 'a', 'link', 'p', ]
let foundElementsWithAuthorInThem = []
var obj = {}
while(selectorsToCheck.length != 0) {
let ele = Array.from($(selectorsToCheck.shift()))
let authorRegex = /(author|writer)/
ele.forEach((el, index) => {
let attrs = Object.keys(el.attributes)
for(var i = 0; i < attrs.length; i+=1) {
if(authorRegex.test(el.attributes[i].value)) {
foundElementsWithAuthorInThem.push($(el).text())
}
}
})
}
let potentialAuthors = foundElementsWithAuthorInThem.map((el) => {
return {count: 1, name: el}
}).reduce((accumulator, currentVal) => {
accumulator[currentVal.name] = (accumulator[currentVal.name] || 0) + currentVal.count
return accumulator
}, {})
let keys = Object.keys(potentialAuthors)
let partsOfSpeechRegex = /(the|when|a|when|what|where|why|how|who|whom)\s+/
let foundAuhtor;
if(keys.length) {
foundAuhtor = Object.keys(potentialAuthors).filter((key) => {
if(/[a-z]/.test(key) && !partsOfSpeechRegex.test(key)) {
return key
}
})
}
return foundAuhtor
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment