Commit fae357e7 authored by Jörg Richter's avatar Jörg Richter

Utils: add `fulltextQuery()`

See dmx-platform/dmx-platform#287
parent 636b825c
Pipeline #7875 passed with stage
in 3 seconds
This diff is collapsed.
......@@ -6,7 +6,7 @@ import typeCache from './type-cache'
import permCache from './permission-cache'
import utils from './utils'
console.log('[DMX] Client API 2019/08/10')
console.log('[DMX] Client API 2019/08/15')
export default {
......
......@@ -106,6 +106,35 @@ function setCookie (name, value) {
// ---
const luceneSymbols = [
'+', '-', '&&', '||', '!', '(', ')', '{', '}', '[', ']', '^', '"', '~', '*', '?', ':', '\\', 'AND', 'OR', 'NOT'
]
function containsLuceneSymbol (input) {
return luceneSymbols.some(symbol => input.includes(symbol))
}
/**
* Transforms user input into a Lucene query that can be used for an incremental search.
*
* @return a Lucene query.
* If input is a single character returns empty string to signalize the caller no search should be triggered.
*/
function fulltextQuery (input) {
let query = input.trim()
if (!containsLuceneSymbol(input)) {
query = query.split(/ +/).join(' AND ')
if (query.length === 1) { // don't search single character
query = ''
} else if (query && !input.endsWith(' ')) {
query += '*'
}
}
return query
}
// ---
export default {
instantiateMany,
instantiateChildren,
......@@ -118,5 +147,6 @@ export default {
filter,
isEmpty,
getCookie,
setCookie
setCookie,
fulltextQuery
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment