function sortAlphaNumeric(a, b) {
// convert to strings and force lowercase
a = typeof a === 'string'
? a.toLowerCase()
: a.toString()
b = typeof b === 'string'
? b.toLowerCase()
: b.toString()
return a.localeCompare(b)
}
function cleanCache(cache, allIds) {
// clean up ids that exist in the index but not in memory
var extra = cache.filter(e => !allIds.includes(e[1]))
extra.forEach(e => cache.splice(cache.indexOf(e), 1))
cache.sort((a, b) => {
return sortAlphaNumeric(a[1], b[1])
})
}
function updateCache(updates, cache, allIds) {
var cacheIds = cache.map(e => e[1])
// update with results from search
updates.forEach(e => {
var i = cacheIds.indexOf(e[1])
if(i === -1) {
cache.push(e)
cacheIds.push(e[1])
} else {
cache.splice(i, 1, e)
}
})
cleanCache(cache, allIds)
return cache
}
module.exports = {
updateCache,
cleanCache,
sortAlphaNumeric,
}