用于高级搜索查询语法的简单解析器。
它解析这样的字符串:
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
特此免费授予获得本软件和相关文档文件(“软件”)副本的任何人不受限制地使用本软件,包括但不限于使用、复制、修改、合并的权利、发布、分发、再许可和/或销售软件的副本,并允许向其提供软件的人员这样做,但须满足以下条件:
上述版权声明和本许可声明应包含在本软件的所有副本或主要部分中。
本软件按“原样”提供,不提供任何明示或暗示的保证,包括但不限于适销性、特定用途的适用性和不侵权的保证。在任何情况下,作者或版权持有者均不对因本软件或本软件中的使用或其他交易而产生或与之相关的任何索赔、损害或其他责任负责,无论是合同、侵权行为还是其他行为。软件。