lyb-js 1.6.38 → 1.6.39
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Data/LibJsPinyinSearch.d.ts +21 -0
- package/Data/LibJsPinyinSearch.js +36 -0
- package/Data/index.d.ts +1 -0
- package/Data/index.js +1 -0
- package/Math/LibJsPiecewiseLerp.d.ts +25 -0
- package/Math/LibJsPiecewiseLerp.js +33 -0
- package/Math/index.d.ts +1 -0
- package/Math/index.js +1 -0
- package/README.md +60 -0
- package/package.json +3 -2
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/** @description 可参与拼音搜索的数据项 */
|
|
2
|
+
export interface LibJsPinyinSearchItem {
|
|
3
|
+
[key: string]: unknown;
|
|
4
|
+
}
|
|
5
|
+
/**
|
|
6
|
+
* @description 根据指定字段执行拼音模糊搜索
|
|
7
|
+
* @param data 待搜索的数据数组
|
|
8
|
+
* @param value 搜索关键字;为空字符串时返回原数组
|
|
9
|
+
* @param keys 参与搜索的字段名数组
|
|
10
|
+
* @returns 匹配成功的数据项数组,按原数组顺序返回且自动去重
|
|
11
|
+
* @example
|
|
12
|
+
* const list = [
|
|
13
|
+
* { id: 1, name: "张三", city: "上海" },
|
|
14
|
+
* { id: 2, name: "李四", city: "北京" },
|
|
15
|
+
* ];
|
|
16
|
+
*
|
|
17
|
+
* console.log(libJsPinyinSearch(list, "zhang", ["name"]));
|
|
18
|
+
* // [{ id: 1, name: "张三", city: "上海" }]
|
|
19
|
+
* @link 使用方法:https://www.npmjs.com/package/lyb-js#libjspinyinsearch-拼音模糊搜索
|
|
20
|
+
*/
|
|
21
|
+
export declare const libJsPinyinSearch: <T extends LibJsPinyinSearchItem>(data: T[], value: string, keys: string[]) => T[];
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { match as pinyinMatch } from "pinyin-pro";
|
|
2
|
+
/**
|
|
3
|
+
* @description 根据指定字段执行拼音模糊搜索
|
|
4
|
+
* @param data 待搜索的数据数组
|
|
5
|
+
* @param value 搜索关键字;为空字符串时返回原数组
|
|
6
|
+
* @param keys 参与搜索的字段名数组
|
|
7
|
+
* @returns 匹配成功的数据项数组,按原数组顺序返回且自动去重
|
|
8
|
+
* @example
|
|
9
|
+
* const list = [
|
|
10
|
+
* { id: 1, name: "张三", city: "上海" },
|
|
11
|
+
* { id: 2, name: "李四", city: "北京" },
|
|
12
|
+
* ];
|
|
13
|
+
*
|
|
14
|
+
* console.log(libJsPinyinSearch(list, "zhang", ["name"]));
|
|
15
|
+
* // [{ id: 1, name: "张三", city: "上海" }]
|
|
16
|
+
* @link 使用方法:https://www.npmjs.com/package/lyb-js#libjspinyinsearch-拼音模糊搜索
|
|
17
|
+
*/
|
|
18
|
+
export const libJsPinyinSearch = (data, value, keys) => {
|
|
19
|
+
if (value === "")
|
|
20
|
+
return data;
|
|
21
|
+
const result = [];
|
|
22
|
+
const seen = new Set();
|
|
23
|
+
for (const item of data) {
|
|
24
|
+
for (const key of keys) {
|
|
25
|
+
const fieldValue = String(item[key] ?? "");
|
|
26
|
+
if (pinyinMatch(fieldValue, value, { precision: "start" })) {
|
|
27
|
+
if (!seen.has(item)) {
|
|
28
|
+
seen.add(item);
|
|
29
|
+
result.push(item);
|
|
30
|
+
}
|
|
31
|
+
break;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return result;
|
|
36
|
+
};
|
package/Data/index.d.ts
CHANGED
|
@@ -2,6 +2,7 @@ export * from "./LibJsChunkArray.js";
|
|
|
2
2
|
export * from "./LibJsDeepJSONParse.js";
|
|
3
3
|
export * from "./LibJsGroupArrayByKey.js";
|
|
4
4
|
export * from "./LibJsMatchEmail.js";
|
|
5
|
+
export * from "./LibJsPinyinSearch.js";
|
|
5
6
|
export * from "./libJsPickUnique.js";
|
|
6
7
|
export * from "./LibJsShuffleArray.js";
|
|
7
8
|
export * from "./LibJsStepArray.js";
|
package/Data/index.js
CHANGED
|
@@ -2,6 +2,7 @@ export * from "./LibJsChunkArray.js";
|
|
|
2
2
|
export * from "./LibJsDeepJSONParse.js";
|
|
3
3
|
export * from "./LibJsGroupArrayByKey.js";
|
|
4
4
|
export * from "./LibJsMatchEmail.js";
|
|
5
|
+
export * from "./LibJsPinyinSearch.js";
|
|
5
6
|
export * from "./libJsPickUnique.js";
|
|
6
7
|
export * from "./LibJsShuffleArray.js";
|
|
7
8
|
export * from "./LibJsStepArray.js";
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/** @description 分段线性插值节点 */
|
|
2
|
+
export interface LibJsPiecewiseLerpPoint {
|
|
3
|
+
/** 横轴位置,需按升序传入 */
|
|
4
|
+
h: number;
|
|
5
|
+
/** 当前节点对应的数值 */
|
|
6
|
+
v: number;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* @description 按多个节点做分段线性插值,区间外返回兜底值
|
|
10
|
+
* @param points 插值节点数组,需按 h 升序传入
|
|
11
|
+
* @param currentValue 当前横轴值
|
|
12
|
+
* @param edgeValue 当前值落在全部区间外时返回的兜底值
|
|
13
|
+
* @returns 区间内返回插值结果,区间外返回 edgeValue;结果保留两位小数
|
|
14
|
+
* @example
|
|
15
|
+
* const points = [
|
|
16
|
+
* { h: 0, v: 0 },
|
|
17
|
+
* { h: 6, v: 30 },
|
|
18
|
+
* { h: 12, v: 100 },
|
|
19
|
+
* ];
|
|
20
|
+
*
|
|
21
|
+
* console.log(LibJsPiecewiseLerp(points, 3, 0)); // 15
|
|
22
|
+
* console.log(LibJsPiecewiseLerp(points, 9, 0)); // 65
|
|
23
|
+
* @link 使用方法:https://www.npmjs.com/package/lyb-js#libjspiecewiselerp-分段线性插值
|
|
24
|
+
*/
|
|
25
|
+
export declare const LibJsPiecewiseLerp: (points: LibJsPiecewiseLerpPoint[], currentValue: number, edgeValue: number) => number;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @description 按多个节点做分段线性插值,区间外返回兜底值
|
|
3
|
+
* @param points 插值节点数组,需按 h 升序传入
|
|
4
|
+
* @param currentValue 当前横轴值
|
|
5
|
+
* @param edgeValue 当前值落在全部区间外时返回的兜底值
|
|
6
|
+
* @returns 区间内返回插值结果,区间外返回 edgeValue;结果保留两位小数
|
|
7
|
+
* @example
|
|
8
|
+
* const points = [
|
|
9
|
+
* { h: 0, v: 0 },
|
|
10
|
+
* { h: 6, v: 30 },
|
|
11
|
+
* { h: 12, v: 100 },
|
|
12
|
+
* ];
|
|
13
|
+
*
|
|
14
|
+
* console.log(LibJsPiecewiseLerp(points, 3, 0)); // 15
|
|
15
|
+
* console.log(LibJsPiecewiseLerp(points, 9, 0)); // 65
|
|
16
|
+
* @link 使用方法:https://www.npmjs.com/package/lyb-js#libjspiecewiselerp-分段线性插值
|
|
17
|
+
*/
|
|
18
|
+
export const LibJsPiecewiseLerp = (points, currentValue, edgeValue) => {
|
|
19
|
+
if (!points.length)
|
|
20
|
+
return edgeValue;
|
|
21
|
+
if (currentValue < points[0].h || currentValue >= points[points.length - 1].h) {
|
|
22
|
+
return edgeValue;
|
|
23
|
+
}
|
|
24
|
+
for (let i = 0; i < points.length - 1; i++) {
|
|
25
|
+
const curr = points[i];
|
|
26
|
+
const next = points[i + 1];
|
|
27
|
+
if (currentValue >= curr.h && currentValue < next.h) {
|
|
28
|
+
const ratio = (currentValue - curr.h) / (next.h - curr.h);
|
|
29
|
+
return +(curr.v + (next.v - curr.v) * ratio).toFixed(2);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
return edgeValue;
|
|
33
|
+
};
|
package/Math/index.d.ts
CHANGED
package/Math/index.js
CHANGED
package/README.md
CHANGED
|
@@ -119,6 +119,7 @@ console.log(libJsGetRowValue({ user: { name: "Tom" } }, "user.name")); // "Tom"
|
|
|
119
119
|
- [LibJsDeepJSONParse-深度解析JSON](#libjsdeepjsonparse-深度解析json)
|
|
120
120
|
- [LibJsGroupArrayByKey-分类汇总](#libjsgrouparraybykey-分类汇总)
|
|
121
121
|
- [LibJsMatchEmail-匹配E-Mail](#libjsmatchemail-匹配e-mail)
|
|
122
|
+
- [LibJsPinyinSearch-拼音模糊搜索](#libjspinyinsearch-拼音模糊搜索)
|
|
122
123
|
- [libJsPickUnique-随机选择未使用元素](#libjspickunique-随机选择未使用元素)
|
|
123
124
|
- [LibJsShuffleArray-数组乱序](#libjsshufflearray-数组乱序)
|
|
124
125
|
- [LibJsStepArray-数组偏移](#libjssteparray-数组偏移)
|
|
@@ -147,6 +148,7 @@ console.log(libJsGetRowValue({ user: { name: "Tom" } }, "user.name")); // "Tom"
|
|
|
147
148
|
- [LibJsDecimal-高精度计算](#libjsdecimal-高精度计算)
|
|
148
149
|
- [LibJsLerp-线性插值](#libjslerp-线性插值)
|
|
149
150
|
- [LibJsNormalizeInRange-范围归一化](#libjsnormalizeinrange-范围归一化)
|
|
151
|
+
- [LibJsPiecewiseLerp-分段线性插值](#libjspiecewiselerp-分段线性插值)
|
|
150
152
|
|
|
151
153
|
### Misc-杂项
|
|
152
154
|
|
|
@@ -425,6 +427,33 @@ const result = libJsMatchEmail("user@g", ["@gmail.com", "@github.com"]);
|
|
|
425
427
|
console.log(result); // ["user@gmail.com", "user@github.com"]
|
|
426
428
|
```
|
|
427
429
|
|
|
430
|
+
### LibJsPinyinSearch-拼音模糊搜索
|
|
431
|
+
|
|
432
|
+
按指定字段做拼音模糊搜索,适合中文姓名、城市名、标签名这类检索场景。
|
|
433
|
+
|
|
434
|
+
```ts
|
|
435
|
+
import { libJsPinyinSearch } from "lyb-js/Data/LibJsPinyinSearch";
|
|
436
|
+
|
|
437
|
+
const list = [
|
|
438
|
+
{ id: 1, name: "张三", city: "上海" },
|
|
439
|
+
{ id: 2, name: "李四", city: "北京" },
|
|
440
|
+
{ id: 3, name: "王五", city: "深圳" },
|
|
441
|
+
];
|
|
442
|
+
|
|
443
|
+
console.log(libJsPinyinSearch(list, "zhang", ["name"]));
|
|
444
|
+
// [{ id: 1, name: "张三", city: "上海" }]
|
|
445
|
+
```
|
|
446
|
+
|
|
447
|
+
```ts
|
|
448
|
+
const contacts = [
|
|
449
|
+
{ id: 1, name: "赵敏", department: "市场部" },
|
|
450
|
+
{ id: 2, name: "周杰伦", department: "内容中心" },
|
|
451
|
+
];
|
|
452
|
+
|
|
453
|
+
console.log(libJsPinyinSearch(contacts, "shi", ["department"]));
|
|
454
|
+
// [{ id: 1, name: "赵敏", department: "市场部" }]
|
|
455
|
+
```
|
|
456
|
+
|
|
428
457
|
### libJsPickUnique-随机选择未使用元素
|
|
429
458
|
|
|
430
459
|
从候选数组中随机选一个还没有被使用过的元素。
|
|
@@ -670,6 +699,37 @@ console.log(LibJsNormalizeInRange(0, 100, 50)); // 0.5
|
|
|
670
699
|
console.log(LibJsNormalizeInRange(0, 100, 120)); // 1
|
|
671
700
|
```
|
|
672
701
|
|
|
702
|
+
### LibJsPiecewiseLerp-分段线性插值
|
|
703
|
+
|
|
704
|
+
根据多个节点做分段线性插值;当值落在全部区间外时返回兜底值。
|
|
705
|
+
|
|
706
|
+
```ts
|
|
707
|
+
import {
|
|
708
|
+
LibJsPiecewiseLerp,
|
|
709
|
+
type LibJsPiecewiseLerpPoint,
|
|
710
|
+
} from "lyb-js/Math/LibJsPiecewiseLerp";
|
|
711
|
+
|
|
712
|
+
const points: LibJsPiecewiseLerpPoint[] = [
|
|
713
|
+
{ h: 0, v: 0 },
|
|
714
|
+
{ h: 6, v: 30 },
|
|
715
|
+
{ h: 12, v: 100 },
|
|
716
|
+
];
|
|
717
|
+
|
|
718
|
+
console.log(LibJsPiecewiseLerp(points, 3, 0)); // 15
|
|
719
|
+
console.log(LibJsPiecewiseLerp(points, 9, 0)); // 65
|
|
720
|
+
```
|
|
721
|
+
|
|
722
|
+
```ts
|
|
723
|
+
const brightnessPoints = [
|
|
724
|
+
{ h: 8, v: 0.2 },
|
|
725
|
+
{ h: 12, v: 0.8 },
|
|
726
|
+
{ h: 18, v: 0.4 },
|
|
727
|
+
];
|
|
728
|
+
|
|
729
|
+
console.log(LibJsPiecewiseLerp(brightnessPoints, 10, 0)); // 0.5
|
|
730
|
+
console.log(LibJsPiecewiseLerp(brightnessPoints, 20, -1)); // -1
|
|
731
|
+
```
|
|
732
|
+
|
|
673
733
|
## Misc-杂项
|
|
674
734
|
|
|
675
735
|
### LibJsClassObservable-类属性监听器
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "lyb-js",
|
|
3
|
-
"version": "1.6.
|
|
3
|
+
"version": "1.6.39",
|
|
4
4
|
"description": "JavaScript utility library",
|
|
5
5
|
"license": "ISC",
|
|
6
6
|
"type": "module",
|
|
@@ -60,6 +60,7 @@
|
|
|
60
60
|
],
|
|
61
61
|
"dependencies": {
|
|
62
62
|
"dayjs": "^1.11.13",
|
|
63
|
-
"decimal.js": "^10.4.3"
|
|
63
|
+
"decimal.js": "^10.4.3",
|
|
64
|
+
"pinyin-pro": "^3.27.0"
|
|
64
65
|
}
|
|
65
66
|
}
|