search-query-parser
Advanced tools
Comparing version 0.0.3 to 0.0.4
@@ -30,4 +30,30 @@ /*! | ||
var query = {text: []}; | ||
// Get a list of search term. Reverse to ensure proper order when pop()'ing. | ||
var terms = string.split(' ').reverse(); | ||
// Get a list of search terms respecting single and double quotes | ||
var terms = string.match(/(\S+:'(?:[^'\\]|\\.)*')|(\S+:"(?:[^"\\]|\\.)*")|\S+|\S+:\S+/g); | ||
for (var i = 0; i < terms.length; i++) { | ||
var sepIndex = terms[i].indexOf(':'); | ||
if(sepIndex !== -1) { | ||
var split = terms[i].split(':'), | ||
key = terms[i].slice(0, sepIndex), | ||
val = terms[i].slice(sepIndex + 1); | ||
// Strip surrounding quotes | ||
val = val.replace(/^\"|\"$|^\'|\'$/g, ''); | ||
// Strip backslashes respecting escapes | ||
val = (val + '').replace(/\\(.?)/g, function (s, n1) { | ||
switch (n1) { | ||
case '\\': | ||
return '\\'; | ||
case '0': | ||
return '\u0000'; | ||
case '': | ||
return ''; | ||
default: | ||
return n1; | ||
} | ||
}); | ||
terms[i] = key + ':' + val; | ||
} | ||
}; | ||
// Reverse to ensure proper order when pop()'ing. | ||
terms.reverse(); | ||
// For each search term | ||
@@ -34,0 +60,0 @@ while (term = terms.pop()) { |
{ | ||
"name": "search-query-parser", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"description": "Parser for advanced search query syntax", | ||
@@ -11,3 +11,3 @@ "main": "index.js", | ||
"type": "git", | ||
"url": "git://github.com/retraceio/search-query-parser" | ||
"url": "git://github.com/nepsilon/search-query-parser" | ||
}, | ||
@@ -24,9 +24,9 @@ "keywords": [ | ||
"name": "Julien Buty", | ||
"email": "julien@retraceio.net" | ||
"email": "julien@nepsilon.net" | ||
}, | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/retraceio/search-query-parser/issues" | ||
"url": "https://github.com/nepsilon/search-query-parser/issues" | ||
}, | ||
"homepage": "https://github.com/retraceio/search-query-parser", | ||
"homepage": "https://github.com/nepsilon/search-query-parser", | ||
"devDependencies": { | ||
@@ -33,0 +33,0 @@ "should": "^3.2.0", |
@@ -53,2 +53,3 @@ # Search Query Syntax Parser | ||
* `ranges`, that can be separated by a hyphen (-) | ||
Both values take an array of strings, as in the example just above. | ||
@@ -58,7 +59,7 @@ | ||
The 15 tests are written using the BDD testing framework should.js, and run with mocha. | ||
The 17 tests are written using the BDD testing framework should.js, and run with mocha. | ||
Run `npm install should` and `npm install -g mocha` to install them both. | ||
Run tests with `mocha -R spec`. | ||
Run tests with `make test`. | ||
@@ -69,3 +70,3 @@ ## License | ||
Copyright (c) 2014 retraceio <hi@retrace.io> | ||
Copyright (c) 2014 retraceio <julien@nepsilon.net> | ||
@@ -72,0 +73,0 @@ Permission is hereby granted, free of charge, to any person obtaining a copy |
@@ -204,2 +204,24 @@ var assert = require('assert') | ||
it('should not split on spaces inside single and double quotes', function () { | ||
var searchQuery = 'name:"Bob Saget" description:\'Banana Sandwiche\''; | ||
var options = {keywords: ['name', 'description']}; | ||
var parsedSearchQuery = searchquery.parse(searchQuery, options); | ||
parsedSearchQuery.should.be.an.Object; | ||
parsedSearchQuery.should.have.property('name', 'Bob Saget'); | ||
parsedSearchQuery.should.have.property('description', 'Banana Sandwiche'); | ||
}); | ||
it('should correctly handle escaped single and double quotes', function () { | ||
var searchQuery = 'case1:"This \\"is\\" \'a\' test" case2:\'This "is" \\\'a\\\' test\''; | ||
var options = {keywords: ['case1', 'case2']}; | ||
var parsedSearchQuery = searchquery.parse(searchQuery, options); | ||
parsedSearchQuery.should.be.an.Object; | ||
parsedSearchQuery.should.have.property('case1', 'This "is" \'a\' test'); | ||
parsedSearchQuery.should.have.property('case2', 'This "is" \'a\' test'); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
149042
5377
87