用於進階搜尋查詢語法的簡單解析器。
它解析這樣的字串:
from:[email protected],[email protected] to:me subject:vacations date:1/10/2013-15/04/2014 photos
並將其變成這樣的物件:
{
from : [ '[email protected]' , '[email protected]' ] ,
to : 'me' ,
subject : 'vacations' ,
date : {
from : '1/10/2013' ,
to : '15/04/2014'
} ,
text : 'photos' ,
offsets :
[ { keyword : 'from' , value : '[email protected],[email protected]' , offsetStart : 0 , offsetEnd : 32 } ,
{ keyword : 'to' , value : 'me' , offsetStart : 33 , offsetEnd : 38 } ,
{ keyword : 'subject' , value : 'vacations' , offsetStart : 39 , offsetEnd : 56 } ,
{ keyword : 'date' , value : '1/10/2013-15/04/2014' , offsetStart : 57 , offsetEnd : 82 } ,
{ text : 'photos' , offsetStart : 83 , offsetEnd : 89 } ]
}
$ npm install search-query-parser
var searchQuery = require ( 'search-query-parser' ) ;
var query = 'from:[email protected],[email protected] to:me subject:vacations date:1/10/2013-15/04/2014 photos' ;
var options = { keywords : [ 'from' , 'to' , 'subject' ] , ranges : [ 'date' ] }
var searchQueryObj = searchQuery . parse ( query , options ) ;
// searchQueryObj.from is now ['[email protected]', '[email protected]']
// searchQueryObj.to is now 'me'
// searchQueryObj.date is now {from: '1/10/2013', to: '15/04/2014'}
// searchQueryObj.text is now 'photos'
您可以使用 options 參數來配置解析器應接受的關鍵字和範圍。它接受 5 個值:
keywords
,可以用逗號(,)分隔。接受字串數組。ranges
,可用連字號 (-) 分隔。接受字串數組。tokenize
,控製文字搜尋字詞的行為。如果設為true
,非關鍵字文字術語將作為字串數組返回,其中數組中的每個術語都是以空格分隔的單詞,或者是由單引號或雙引號引起來的多單詞術語。alwaysArray
,一個控制會傳回查詢行為的布林值。如果設定為true
,所有符合的關鍵字將始終是數組而不是字串。如果設定為false
如果符合單一值,它們將是字串。預設為false
。offsets
,一個控制回傳查詢行為的布林值。如果設定為true
,查詢將包含偏移量物件。如果設定為false
,查詢將不包含偏移量物件。預設為true
。如果未指定關鍵字或範圍,或給定搜尋查詢中不存在任何關鍵字或範圍,則searchQuery.parse
將在tokenize
為 false 時傳回字串,如果tokenize
為 true 則傳回鍵text
下的字串陣列。
var searchQuery = require ( 'search-query-parser' ) ;
var query = 'a query with "just text"' ;
var parsedQuery = searchQuery . parse ( query ) ;
// parsedQuery is now 'a query with "just text"'
var options = { keywords : [ 'unused' ] } ;
var parsedQueryWithOptions = searchQuery . parse ( query , options ) ;
// parsedQueryWithOptions is now 'a query with "just text"'
var options2 = { tokenize : true } ;
var parsedQueryWithTokens = searchQuery . parse ( query , options2 ) ;
// parsedQueryWithTokens is now: ['a', 'query', 'with', 'just text']
您也可以使用排除語法,例如-from:[email protected] name:hello,world
。當tokenize
設定為true
時,這也適用於非關鍵字文字術語。
{
name : [ 'hello' , 'world' ] ,
exclude : {
from : [ '[email protected]' ]
}
}
有時檢查關鍵字是否包含字串可能會過多且容易出錯;通常更容易簡單地期望所有內容都是數組,即使這意味著經常進行 1 次迭代循環。
var searchQuery = require ( 'search-query-parser' ) ;
var query = 'test:helloworld fun:yay,happy' ;
var options = { keywords : [ 'test' , 'fun' ] } ;
var parsedQueryWithOptions = searchQuery . parse ( query , options ) ;
// parsedQueryWithOptions is now:
// {
// test: 'helloworld',
// fun: ['yay', 'happy']
// }
var optionsAlwaysArray = { keywords : [ 'test' , 'fun' ] , alwaysArray : true } ;
var parsedQueryWithOptions = searchQuery . parse ( query , options ) ;
// parsedQueryWithOptions is now:
// {
// test: ['helloworld'], //No need to check whether test is a string or not!
// fun: ['yay', 'happy']
// }
對於長搜尋查詢,偏移物件可能會變得相當大,如果沒有功能依賴它,這可能會不必要地使用空間。它可以簡單地使用選項offsets: false
關閉。
您可以隨時返回並字串化已解析的搜尋查詢。如果您想操作解析的搜尋查詢對象,這可能會很方便。
var searchQuery = require ( 'search-query-parser' ) ;
var query = 'from:[email protected],[email protected] to:me subject:vacations date:1/10/2013-15/04/2014 photos' ;
var options = { keywords : [ 'from' , 'to' , 'subject' ] , ranges : [ 'date' ] }
var searchQueryObj = searchQuery . parse ( query , options ) ;
searchQueryObj . to = 'you' ;
var newQuery = searchQuery . stringify ( query , options ) ;
// newQuery is now: photos from:[email protected],[email protected] to:you subject:vacations date:1/10/2013-15/04/2014
Typescript 類型可在docs
目錄中用於此程式庫。在此處瀏覽類型文件。
文件是使用node_modules/.bin/typedoc index.d.ts
產生的
這29個測試是使用BDD測試框架should.js編寫的,並使用mocha運行。
運行npm install should
和npm install -g mocha
來安裝它們。
使用make test
運行測試。
麻省理工學院許可證 (MIT)
版權所有 (c) 2014
特此免費授予任何獲得本軟體和相關文件文件(「軟體」)副本的人不受限制地使用本軟體,包括但不限於使用、複製、修改、合併的權利、發布、分發、再授權和/或銷售軟體的副本,並允許向其提供軟體的人員這樣做,但須滿足以下條件:
上述版權聲明和本授權聲明應包含在本軟體的所有副本或主要部分中。
本軟體以「現況」提供,不提供任何明示或暗示的保證,包括但不限於適銷性、特定用途的適用性和不侵權的保證。 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE軟體.