ss search
v1.12.0
还是不相信?通过此交互式演示亲身体验其强大功能。
它与其他搜索库相比如何?使用此交互式基准测试自己;)
ss-search 在 npm 上可用。安装它:
npm install ss-search
import { search } from 'ss-search'
const data = [
{
number : 1 ,
text : 'A search function should be fast' ,
} ,
{
number : 2 ,
text : 'A search function should provide accurate results' ,
} ,
]
const searchKeys = [ 'text' ]
const searchText = 'fast search'
const results = search ( data , searchKeys , searchText )
// results: [{ number: 1, text: "A search function should be fast" }]
就是这么简单。无需配置,即可运行。
支持几乎所有数据类型[布尔、数字、字符串、对象、数组]。
// This dataset will be used as a common starting point for our type examples
const data = [
{
boolean : true ,
number : 1 ,
string : 'search' ,
object : { nestedProperty : 'nested value' } ,
array : [ 'value1' , 'value2' ] ,
arrayObjects : [ { arrayObjectProperty : 'array object value' } ] ,
} ,
]
const results = search ( data , [ 'boolean' ] , 'true' )
// results: will return our original dataset
const results = search ( data , [ 'number' ] , '1' )
// results: will return our original dataset
const results = search ( data , [ 'string' ] , 'search' )
// results: will return our original dataset
提供引用对象的键将使用 JSON.stringify 对该对象进行字符串化
const results = search ( data , [ 'object' ] , 'property' )
// results: will return our original dataset as it matches the property key "nestedProperty" of our object
如果您想访问对象的嵌套属性以仅提取单个值
const results = search ( data , [ 'object.nestedProperty' ] , 'property' )
// results: will return an empty array as we extracted the value of our nested object
// if we had searched for "nested value" we would of had the original dataset
提供引用数组的键将使用 JSON.stringify 对该数组进行字符串化
const results = search ( data , [ 'array' ] , 'value2' )
// results: will return our original dataset
如果您有一个要搜索其所有属性的对象数组
const results = search ( data , [ 'arrayObjects' ] , 'arrayObjectProperty' )
// results: will return an our original dataset as it's treated just like a regular array
// thus the arrayObjectProperty is part of the searchable text
如果您有一个对象数组,您只想搜索特定属性
const results = search ( data , [ 'arrayObjects[arrayObjectProperty]' ] , 'arrayObjectProperty' )
// results: will return an empty array as we extracted the value of our nested array of objects
// if we had searched for "value object" we would of had the original dataset
search
功能选项使用以下选项自定义您的搜索体验:
选项参数 | 价值 | 描述 |
---|---|---|
withScore | true | 当设置为true 时,搜索函数将返回一个对象数组,每个对象包含匹配的元素及其相应的分数。分数表示元素与搜索文本的匹配程度,分数越高表示匹配越接近。即使搜索不匹配,也会返回 0 分。 |
withScore | false | 当设置为false 或未提供时,该函数将返回匹配元素的数组,但不包含其分数。 |
没有withScore
选项:
const data = [ { name : 'John' } , { name : 'Jane' } , { name : 'Doe' } ]
const result = search ( data , [ 'name' ] , 'John' )
console . log ( result ) // [{ name: 'John' }]
使用withScore
选项:
const data = [ { name : 'John' } , { name : 'Jane' } , { name : 'Doe' } ]
const result = search ( data , [ 'name' ] , 'John' , { withScore : true } )
console . log ( result )
// [
// { element: { name: 'John' }, score: 1 },
// { element: { name: 'Jane' }, score: 0 },
// { element: { name: 'Doe' }, score: 0 }
// ]
为了更好地管理 monorepo 之间的依赖关系,我使用 NX。
安装依赖项: npm i
启动 Web 应用程序: npm run web-app:serve
测试库: npm run test:all