zurich 中文文档教程
苏黎世???
基于 Jaccard 索引的字符串距离 TS 库,用于模糊搜索等。
简单的 API、精确、有用的类型。
安装
使用npm
npm i zurich
:
distance
- 获取两个字符串之间的距离
获取两个字符串之间的(二元组或三元组)距离(通过杰卡德索引测量
)
distance(
a: string,
b: string,
options?: {
n?: number = 2,
caseSensitive?: boolean = false
}
): number
用法
基本
options
对象是可选的,该函数默认使用二元语法。
import { distance } from "zurich";
const d1 = distance("dog", "dog"); // 0
const d2 = distance("toad", "road"); // 0.5
const d3 = distance("dog", "monkey"); // 1
Trigrams
使用 trigrams 获取距离,并使用 {n: 3}
选项
const d = distance("toad", "road", { n: 3 }); // 0.6666666666666667
区分
大小写 在计算距离时考虑大小写,通过在选项中设置 { caseSensitive: true }
const d1 = distance("dog", "dog", { caseSensitive: true }); // 0
const d2 = distance("dog", "DOG", { caseSensitive: true }); // 1
bestMatch
- 获取字符串数组中最接近的字符串
从字符串数组中获取与另一个字符串最接近的字符串
签名
bestMatch(
str: string,
other: string[],
options?: {
n?: number = 2,
caseSensitive?: boolean = false,
returnCount?: number = 1
}
): (string | null) | (string[] | null)
用法
基本
import { bestMatch } from "zurich";
const match1 = bestMatch("dog", ["zog", "hog", "bog"]); // 'zog'
const match2 = bestMatch("dog", ["zebras", "hedgehog", "warthog"]); // 'warthog'
const match3 = bestMatch("dog", []); // null
三元
const match = bestMatch("tigers", ["liger", "tiger", "tigers"], { n: 3 }); // 'tigers'
组 区分大小写
const match = bestMatch("DOG", ["dog", "DOG"], { n: 3, caseSensitive: true }); // 'DOG'
返回多个有序匹配
返回有序数组最接近的匹配项。 bestMatch
的返回类型适配returnCount
; returnCount == 1
返回一个字符串
,returnCount > 1
返回字符串[]
。
const match1 = bestMatch("dog", ["zog", "hog", "dog"], { returnCount: 2 }); // ['dog', 'zog']
const match2 = bestMatch("dog", ["zog", "hog", "dog"], { returnCount: 5 }); // ['dog', 'zog', 'hog']
const match3 = bestMatch("dog", ["zog", "hog", "dog"], { returnCount: 2 }); // ['dog']
bestObjMatchByKey
- 获取数组中最接近的对象
从对象数组中获取与给定键的字符串最匹配的对象
签名
bestObjMatchByKey<T extends object>(
str: string,
other: T[],
key: keyof T,
options?: {
n?: number = 2,
caseSensitive?: boolean = false,
returnCount?: number = 1
}
): (T | null) | (T[] | null)
用法
n
bestObjMatchByKey 的 >、caseSensitive
和 returnCount
选项的工作方式与 distance
和 bestMatch< /code>
import { bestObjMatchByKey } from "zurich";
const match1 = bestObjMatchByKey("dog", [{ animal: "dog" }], "animal"); // '{ animal: 'dog' }
const match2 = bestObjMatchByKey(
"dog",
[{ animal: "hog" }, { species: "dog" }],
"animal"
); // '{ animal: 'hog' }
const match3 = bestObjMatchByKey(
"dog",
[{ thing: "log" }, { species: "dog" }],
"animal"
); // null
性能说明
请参阅 perf
文件夹以了解做出某些决定的原因。
- 请参阅 distance-currying 文件夹,了解为什么
distance
不支持柯里化。 - 有关如何进行 50-700 次
bestMatch
的详细信息,请参阅 bestMatch-bucket-sort 文件夹快点。
贡献
欢迎 Pull 请求。对于重大更改,请先打开一个问题来讨论您想要更改的内容。
请确保适当更新测试。
许可证
Zurich ????
Jaccard Index-based string distance TS library, for fuzzy search and more.
Simple API, precise, helpful types.
Installation
Using npm
:
npm i zurich
distance
- getting the distance between two strings
Get the (bigram or trigram) distance between two strings (measured by jaccard index)
Signature
distance(
a: string,
b: string,
options?: {
n?: number = 2,
caseSensitive?: boolean = false
}
): number
Usage
Basic
The options
object is optional, and the function defaults the using bi-grams.
import { distance } from "zurich";
const d1 = distance("dog", "dog"); // 0
const d2 = distance("toad", "road"); // 0.5
const d3 = distance("dog", "monkey"); // 1
Trigrams
Get distance using trigrams, with the {n: 3}
option
const d = distance("toad", "road", { n: 3 }); // 0.6666666666666667
Case sensitive
Take casing into account when calculating distance, by setting { caseSensitive: true }
in options
const d1 = distance("dog", "dog", { caseSensitive: true }); // 0
const d2 = distance("dog", "DOG", { caseSensitive: true }); // 1
bestMatch
- getting the closest string in an array of strings
Get the string(s) matching closest to a another string, from an array of strings
Signature
bestMatch(
str: string,
other: string[],
options?: {
n?: number = 2,
caseSensitive?: boolean = false,
returnCount?: number = 1
}
): (string | null) | (string[] | null)
Usage
Basic
import { bestMatch } from "zurich";
const match1 = bestMatch("dog", ["zog", "hog", "bog"]); // 'zog'
const match2 = bestMatch("dog", ["zebras", "hedgehog", "warthog"]); // 'warthog'
const match3 = bestMatch("dog", []); // null
Trigrams
const match = bestMatch("tigers", ["liger", "tiger", "tigers"], { n: 3 }); // 'tigers'
Case sensitive
const match = bestMatch("DOG", ["dog", "DOG"], { n: 3, caseSensitive: true }); // 'DOG'
Returning multiple ordered matches
Return an ordered array of closest matches. The return type of bestMatch
adapts to returnCount
; returnCount == 1
returns a string
, returnCount > 1
returns string[]
.
const match1 = bestMatch("dog", ["zog", "hog", "dog"], { returnCount: 2 }); // ['dog', 'zog']
const match2 = bestMatch("dog", ["zog", "hog", "dog"], { returnCount: 5 }); // ['dog', 'zog', 'hog']
const match3 = bestMatch("dog", ["zog", "hog", "dog"], { returnCount: 2 }); // ['dog']
bestObjMatchByKey
- getting the closest object in an array
Get the object(s) matching closest to a string for a given key, from an array of objects
Signature
bestObjMatchByKey<T extends object>(
str: string,
other: T[],
key: keyof T,
options?: {
n?: number = 2,
caseSensitive?: boolean = false,
returnCount?: number = 1
}
): (T | null) | (T[] | null)
Usage
The n
, caseSensitive
, and returnCount
options for bestObjMatchByKey
, work the same as for distance
and bestMatch
import { bestObjMatchByKey } from "zurich";
const match1 = bestObjMatchByKey("dog", [{ animal: "dog" }], "animal"); // '{ animal: 'dog' }
const match2 = bestObjMatchByKey(
"dog",
[{ animal: "hog" }, { species: "dog" }],
"animal"
); // '{ animal: 'hog' }
const match3 = bestObjMatchByKey(
"dog",
[{ thing: "log" }, { species: "dog" }],
"animal"
); // null
Performance notes
See the perf
-folder to see why certain decisions have been made.
- See the distance-currying-folder for an explanation of why
distance
is not curry-capable. - See the bestMatch-bucket-sort-folder for details on how
bestMatch
was made 50-700 times faster.
Contributing
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Please make sure to update tests as appropriate.