Updating to use default false for search pre-processing.

This commit is contained in:
mike12345567 2021-07-23 13:44:46 +01:00
parent e96f45464a
commit bf4fb71257
1 changed files with 18 additions and 15 deletions

View File

@ -9,25 +9,22 @@ const fetch = require("node-fetch")
* @param options The preprocess options * @param options The preprocess options
* @returns {string|*} * @returns {string|*}
*/ */
const preprocess = ( const preprocess = (value, { escape, lowercase, wrap } = {}) => {
value,
options = { escape: true, lowercase: true, wrap: true }
) => {
// Determine if type needs wrapped // Determine if type needs wrapped
const originalType = typeof value const originalType = typeof value
// Convert to lowercase // Convert to lowercase
if (value && options.lowercase) { if (value && lowercase) {
value = value.toLowerCase ? value.toLowerCase() : value value = value.toLowerCase ? value.toLowerCase() : value
} }
// Escape characters // Escape characters
if (options.escape && originalType === "string") { if (escape && originalType === "string") {
value = `${value}`.replace(/[ #+\-&|!(){}\]^"~*?:\\]/g, "\\$&") value = `${value}`.replace(/[ #+\-&|!(){}\]^"~*?:\\]/g, "\\$&")
} }
// Wrap in quotes // Wrap in quotes
if (options.wrap) { if (wrap) {
value = originalType === "number" ? value : `"${value}"` value = originalType === "number" ? value : `"${value}"`
} }
@ -132,12 +129,12 @@ class QueryBuilder {
buildSearchQuery() { buildSearchQuery() {
let query = "*:*" let query = "*:*"
const allPreProcessingOpts = { escape: true, lowercase: true, wrap: true }
function build(structure, queryFn) { function build(structure, queryFn) {
for (let [key, value] of Object.entries(structure)) { for (let [key, value] of Object.entries(structure)) {
key = preprocess(key.replace(/ /, "_"), { key = preprocess(key.replace(/ /, "_"), {
wrap: false, escape: true,
lowercase: false,
}) })
const expression = queryFn(key, value) const expression = queryFn(key, value)
if (expression == null) { if (expression == null) {
@ -153,7 +150,10 @@ class QueryBuilder {
if (!value) { if (!value) {
return null return null
} }
value = preprocess(value) value = preprocess(value, {
escape: true,
lowercase: true,
})
return `${key}:${value}*` return `${key}:${value}*`
}) })
} }
@ -168,8 +168,8 @@ class QueryBuilder {
if (value.high == null || value.high === "") { if (value.high == null || value.high === "") {
return null return null
} }
const low = preprocess(value.low) const low = preprocess(value.low, allPreProcessingOpts)
const high = preprocess(value.high) const high = preprocess(value.high, allPreProcessingOpts)
return `${key}:[${low} TO ${high}]` return `${key}:[${low} TO ${high}]`
}) })
} }
@ -178,7 +178,10 @@ class QueryBuilder {
if (!value) { if (!value) {
return null return null
} }
value = preprocess(value) value = preprocess(value, {
escape: true,
lowercase: true,
})
return `${key}:${value}~` return `${key}:${value}~`
}) })
} }
@ -187,7 +190,7 @@ class QueryBuilder {
if (!value) { if (!value) {
return null return null
} }
return `${key}:${preprocess(value)}` return `${key}:${preprocess(value, allPreProcessingOpts)}`
}) })
} }
if (this.query.notEqual) { if (this.query.notEqual) {
@ -195,7 +198,7 @@ class QueryBuilder {
if (!value) { if (!value) {
return null return null
} }
return `!${key}:${preprocess(value)}` return `!${key}:${preprocess(value, allPreProcessingOpts)}`
}) })
} }
if (this.query.empty) { if (this.query.empty) {