paypal-spotlight
Version:
A smart launcher for PayPal.
149 lines (128 loc) • 3.65 kB
JavaScript
// TODO To auto-load from config files
const getPluginMetadata = plugins => {
const allPluginCommands = plugins
.reduce((acc, val) => [...acc, ...val.commands], [])
.filter(val => val !== '')
const pluginCommandMap = {}
const singleEntryCommandMap = {}
plugins.forEach(plugin => {
const { commands, isSingleEntryCommand } = plugin
if (commands) {
commands.forEach(c => {
singleEntryCommandMap[c] = isSingleEntryCommand
pluginCommandMap[c] = plugin
})
}
})
return {
pluginCommands: allPluginCommands,
pluginObjects: plugins,
pluginCommandMap,
singleEntryCommandMap,
}
}
const getSearchKeywords = () =>
global.paypalSpotlight.pluginMetadata.pluginCommands || []
const getPluginObjects = () =>
global.paypalSpotlight.pluginMetadata.pluginObjects || []
const getPluginByCommand = command => {
const plugin = global.paypalSpotlight.pluginMetadata.pluginCommandMap[command]
if (!plugin) {
throw new TypeError(`Plugin '${command}' not found.`)
}
return plugin
}
const isSingleEntryCommand = q =>
global.paypalSpotlight.pluginMetadata.singleEntryCommandMap[q]
function shouldHaveSpace(keyword) {
return global.paypalSpotlight.pluginMetadata.singleEntryCommandMap[keyword]
? ''
: ' '
}
const isDebugID = query => {
return (
/^[a-z0-9]{13}$/.test(query) && /[a-z]/.test(query) && /[0-9]/.test(query)
)
}
const isRallyText = query => /^(US|L2|TA|DE)\d{5,10}$/i.test(query)
const isJiraText = query => /^[A-Z]{2}-\d+$/g.test(query)
const isReservedSearch = query => {
return getSearchKeywords().some(key =>
query.startsWith(key + shouldHaveSpace(key)),
)
}
const isDefaultSearch = query => {
return !isReservedSearch(query)
}
const isQuerySearchedWith = (query, keyword) => {
return new RegExp(`^${keyword} .+`).test(query)
}
const isBecomingReservedWords = words => {
const similarKeywords = getSearchKeywords().reduce((acc, val) => {
val.startsWith(words) &&
val + shouldHaveSpace(val) !== words &&
acc.push(val)
return acc
}, [])
return similarKeywords
}
const getReservedKeywords = query => {
if (isReservedSearch(query)) {
return getSearchKeywords()
.map(k => k + shouldHaveSpace(k))
.filter(key => query.startsWith(key))
}
return null
}
function determinePrefix(q) {
const keywords = getReservedKeywords(q)
if (!!keywords && keywords.length > 0) {
const matchKeyword = keywords[0]
if (isSingleEntryCommand(matchKeyword)) return ''
return matchKeyword
}
return ''
}
function determineSuffix(q) {
const similarKeywords = isBecomingReservedWords(q)
let suffix = ''
if (similarKeywords.length && q !== '') {
const keyword = similarKeywords[0] + shouldHaveSpace(similarKeywords[0])
suffix = keyword.replace(q, '')
}
return suffix
}
function determineDisplayText(q) {
const keyword = determinePrefix(q)
if (keyword === '') {
return q
}
return q.replace(keyword, '')
}
function calculateResultGroup(result) {
return result.reduce((acc, value, index, arr) => {
if (index === 0) return acc + 1
const prevPluginName = arr[index - 1].plugin.name
const pluginName = arr[index].plugin.name
if (prevPluginName !== pluginName) return acc + 1
return acc
}, 0)
}
module.exports = {
isDebugID,
isRallyText,
isJiraText,
isDefaultSearch,
isBecomingReservedWords,
getReservedKeywords,
isQuerySearchedWith,
determinePrefix,
determineSuffix,
determineDisplayText,
calculateResultGroup,
getSearchKeywords,
getPluginObjects,
shouldHaveSpace,
getPluginMetadata,
getPluginByCommand,
}