Skip to content

Commit 7db861b

Browse files
committed
Improve partially quoted results
1 parent 1c1ff5c commit 7db861b

4 files changed

Lines changed: 17 additions & 3 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ You can create a `SearchEngine` instance with specific options that will be used
4848

4949
```javascript
5050
const engine = new SearchEngine({
51-
excludeKeys: ['password', 'private.info'],
51+
excludeKeys: ['name', 'tags'],
5252
allowNumericString: false,
5353
allowKeyValueMatching: true,
5454
matchChildKeysAsValues: false

lib/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@ const REGEX_CHAR = '*'
55
const RANGE_CHAR = '~'
66
const GROUP_START = '('
77
const GROUP_END = ')'
8+
const QUOTES = '"'
89
const EMPTY_QUOTES_STR = '""'
910
const KEY_SEPARATOR = '.'
1011
const NEGATED_PREFIX = 'not'
1112
const RANGE_REGEXP = /^[^-\d]*(-?\d+(\.\d+)?)?[^-\d]*-[^-\d]*(-?\d+(\.\d+)?)?[^-\d]*$/
12-
const TOKENIZER = new RegExp(` *(${NEGATED_PREFIX})? *(\\${GROUP_START})| *(${NEGATED_PREFIX} +)?(?:((?:\\\\.|[^ ${GROUP_START}${GROUP_END}\\\\${REGEX_CHAR}${RANGE_CHAR}${TOKEN_SEPARATOR}])+) *([${REGEX_CHAR}${RANGE_CHAR}]?${TOKEN_SEPARATOR}))? *("((?:\\\\.|[^"\\\\])+)"|(?:\\\\.|[^ ${GROUP_START}${GROUP_END}\\\\])+)? *(and|or|\\${GROUP_END}|$)`, 'g')
13+
const TOKENIZER = new RegExp(` *(${NEGATED_PREFIX})? *(\\${GROUP_START})| *(${NEGATED_PREFIX} +)?(?:((?:\\\\.|[^ ${GROUP_START}${GROUP_END}\\\\${REGEX_CHAR}${RANGE_CHAR}${TOKEN_SEPARATOR}])+) *([${REGEX_CHAR}${RANGE_CHAR}]?${TOKEN_SEPARATOR}))? *(${QUOTES}((?:\\\\.|[^${QUOTES}\\\\])+)${QUOTES}?|(?:\\\\.|[^ ${GROUP_START}${GROUP_END}\\\\])+)? *(and|or|\\${GROUP_END}|$)`, 'g')
1314
const TOKEN = { GROUP_NEGATED: 1, GROUP_START: 2, NEGATED: 3, KEY: 4, TYPE: 5, VALUE: 6, QUOTED_VALUE: 7, OPERATOR: 8 }
1415
const UNKNOWN = -1
1516
const EMPTY_STR = ''

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@ecromaneli/search-engine",
3-
"version": "2.0.3",
3+
"version": "2.1.0",
44
"description": "A powerful object search engine with advanced query syntax",
55
"main": "dist/npm/index.min.js",
66
"files": [ "dist/npm/index.min.*", "dist/npm/index.d.*", "LICENSE" ],

test/index.test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,19 @@ describe('Search Engine', () => {
359359
expect(results.length).toBe(1)
360360
expect(results[0].id).toBe(2)
361361
})
362+
363+
test('Partially quoted values', () => {
364+
let query = 'name:"Smith'
365+
let results = Search.search(testData, query)
366+
expect(results.length).toBe(1)
367+
expect(results[0].id).toBe(1)
368+
369+
query = '"John Smith" or "Jane Doe'
370+
results = Search.search(testData, query)
371+
expect(results.length).toBe(2)
372+
expect(results[0].id).toBe(1)
373+
expect(results[1].id).toBe(2)
374+
})
362375

363376
test('Quoted values with boolean operators inside', () => {
364377
const query = 'name:"and"' // Looking for literal "and" in name

0 commit comments

Comments
 (0)