You've already forked search-query-parser
Added offsets and alwaysArray boolean controls
This commit is contained in:
@@ -6,9 +6,12 @@
|
||||
|
||||
exports.parse = function (string, options) {
|
||||
|
||||
// Set an empty options object when none provided
|
||||
// Set a default options object when none is provided
|
||||
if (!options) {
|
||||
options = {};
|
||||
options = {offsets: true};
|
||||
} else {
|
||||
// If options offsets was't passed, set it to true
|
||||
options.offsets = (typeof options.offsets === 'undefined' ? true : options.offsets)
|
||||
}
|
||||
|
||||
if (!string) {
|
||||
@@ -26,7 +29,11 @@ exports.parse = function (string, options) {
|
||||
// Otherwise parse the advanced query syntax
|
||||
else {
|
||||
// Our object to store the query object
|
||||
var query = {text: [], offsets: []};
|
||||
var query = {text: []};
|
||||
// When offsets is true, create their array
|
||||
if (options.offsets) {
|
||||
query.offsets = [];
|
||||
}
|
||||
var exclusion = {};
|
||||
var terms = [];
|
||||
// Get a list of search terms respecting single and double quotes
|
||||
@@ -113,7 +120,10 @@ exports.parse = function (string, options) {
|
||||
if (term.text) {
|
||||
// We add it as pure text
|
||||
query.text.push(term.text);
|
||||
query.offsets.push(term);
|
||||
// When offsets is true, push a new offset
|
||||
if (options.offsets) {
|
||||
query.offsets.push(term);
|
||||
}
|
||||
}
|
||||
// We got an advanced search syntax
|
||||
else {
|
||||
@@ -138,12 +148,15 @@ exports.parse = function (string, options) {
|
||||
var isRange = !(-1 === options.ranges.indexOf(key));
|
||||
// When the key matches a keyword
|
||||
if (isKeyword) {
|
||||
query.offsets.push({
|
||||
keyword: key,
|
||||
value: term.value,
|
||||
offsetStart: isExclusion ? term.offsetStart + 1 : term.offsetStart,
|
||||
offsetEnd: term.offsetEnd
|
||||
});
|
||||
// When offsets is true, push a new offset
|
||||
if (options.offsets) {
|
||||
query.offsets.push({
|
||||
keyword: key,
|
||||
value: term.value,
|
||||
offsetStart: isExclusion ? term.offsetStart + 1 : term.offsetStart,
|
||||
offsetEnd: term.offsetEnd
|
||||
});
|
||||
}
|
||||
|
||||
var value = term.value;
|
||||
// When value is a thing
|
||||
@@ -217,8 +230,13 @@ exports.parse = function (string, options) {
|
||||
}
|
||||
// Got only a single value this time
|
||||
else {
|
||||
// Record its value as a string
|
||||
query[key] = value;
|
||||
if (options.alwaysArray) {
|
||||
// ...but we always return an array if option alwaysArray is true
|
||||
query[key] = [value];
|
||||
} else {
|
||||
// Record its value as a string
|
||||
query[key] = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -226,7 +244,10 @@ exports.parse = function (string, options) {
|
||||
}
|
||||
// The key allows a range
|
||||
else if (isRange) {
|
||||
query.offsets.push(term);
|
||||
// When offsets is true, push a new offset
|
||||
if (options.offsets) {
|
||||
query.offsets.push(term);
|
||||
}
|
||||
|
||||
var value = term.value;
|
||||
// Range are separated with a dash
|
||||
@@ -253,11 +274,14 @@ exports.parse = function (string, options) {
|
||||
var text = term.keyword + ':' + term.value;
|
||||
query.text.push(text);
|
||||
|
||||
query.offsets.push({
|
||||
text: text,
|
||||
offsetStart: term.offsetStart,
|
||||
offsetEnd: term.offsetEnd
|
||||
});
|
||||
// When offsets is true, push a new offset
|
||||
if (options.offsets) {
|
||||
query.offsets.push({
|
||||
text: text,
|
||||
offsetStart: term.offsetStart,
|
||||
offsetEnd: term.offsetEnd
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user