@jckj/dict 0.0.0-stage → 1.0.3
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/LICENSE +21 -0
- package/README.md +111 -3
- package/dist/dict.es.js +61 -0
- package/dist/dict.umd.js +1 -0
- package/dist/jckj.svg +6 -0
- package/index.d.ts +81 -0
- package/package.json +36 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 hzqhxy
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,111 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
## 字典管理模块 API 文档
|
|
2
|
+
|
|
3
|
+
### 模块概述
|
|
4
|
+
|
|
5
|
+
该模块提供了一个用于管理字典数据的 Dict 类及对应的工厂函数 createDictFactory。通过将原始的字典配置数据转化为字典实例,可以便捷地进行多维度的数据查询,如通过值反查标签、获取索引位置、校验值合法性等,非常适合在前端开发中用于管理枚举类型、下拉选项及表格状态渲染等场景。
|
|
6
|
+
|
|
7
|
+
### 工厂函数
|
|
8
|
+
|
|
9
|
+
**createDictFactory<T>(data)**
|
|
10
|
+
用于创建字典实例的工厂函数。
|
|
11
|
+
|
|
12
|
+
- 类型参数:
|
|
13
|
+
- T:字典数据类型,必须符合 DictData 接口。
|
|
14
|
+
- 参数:
|
|
15
|
+
- data: T:字典数据对象。
|
|
16
|
+
- 返回值:
|
|
17
|
+
- Dict<T>:字典实例。
|
|
18
|
+
|
|
19
|
+
### 类:Dict<T>
|
|
20
|
+
|
|
21
|
+
字典管理类,封装了字典数据的多种查询映射关系。
|
|
22
|
+
|
|
23
|
+
#### 构造函数
|
|
24
|
+
|
|
25
|
+
**constructor(data: T)**
|
|
26
|
+
|
|
27
|
+
- 参数:
|
|
28
|
+
- data: T:符合 DictData 接口的原始字典数据对象。
|
|
29
|
+
- 描述:
|
|
30
|
+
实例化时,会遍历传入的数据对象,为每个字典项创建 DictItem 实例,并构建动态属性、列表、以及多种映射关系(值到标签、值到字典项、值到 DictItem 实例)。
|
|
31
|
+
|
|
32
|
+
#### 实例属性
|
|
33
|
+
|
|
34
|
+
- [key: StrNum]: any:动态属性索引签名。允许通过字典项的键名直接访问对应的 DictItem 实例,例如 dict['ACTIVE']。
|
|
35
|
+
|
|
36
|
+
#### 实例方法
|
|
37
|
+
|
|
38
|
+
1. **getList(): readonly DictItemVo[]**
|
|
39
|
+
|
|
40
|
+
- 返回值:readonly DictItemVo[]
|
|
41
|
+
- 描述:获取字典项的只读列表数组。
|
|
42
|
+
|
|
43
|
+
2. **getMap(): ReadonlyMap<StrNum, string>**
|
|
44
|
+
|
|
45
|
+
- 返回值:ReadonlyMap<StrNum, string>
|
|
46
|
+
- 描述:获取“值”到“标签”的只读映射表。
|
|
47
|
+
|
|
48
|
+
3. **getItemMap(): ReadonlyMap<StrNum, DictItemVo>**
|
|
49
|
+
|
|
50
|
+
- 返回值:ReadonlyMap<StrNum, DictItemVo>
|
|
51
|
+
- 描述:获取“值”到“原始字典项对象”的只读映射表。
|
|
52
|
+
|
|
53
|
+
4. **getDictItemMap(): ReadonlyMap<StrNum, DictItem>**
|
|
54
|
+
|
|
55
|
+
- 返回值:ReadonlyMap<StrNum, DictItem>
|
|
56
|
+
- 描述:获取“值”到“DictItem 实例”的只读映射表。
|
|
57
|
+
|
|
58
|
+
5. **getIndex(value: StrNum): number**
|
|
59
|
+
|
|
60
|
+
- 参数:
|
|
61
|
+
value: StrNum:要查询的字典值。
|
|
62
|
+
- 返回值:number。返回该值在字典列表中的索引位置,未找到时返回 -1。
|
|
63
|
+
|
|
64
|
+
6. **getLabelForValue(value: StrNum): string | undefined**
|
|
65
|
+
|
|
66
|
+
- 参数:
|
|
67
|
+
value: StrNum:要查询的字典值。
|
|
68
|
+
- 返回值:string | undefined。返回对应的标签文本,未找到时返回 undefined。
|
|
69
|
+
|
|
70
|
+
7. **getLabel(key: StrNum): string | undefined**
|
|
71
|
+
|
|
72
|
+
- 参数:
|
|
73
|
+
key: StrNum:要查询的字典键名。
|
|
74
|
+
- 返回值:string | undefined。返回对应的标签文本,未找到时返回 undefined。
|
|
75
|
+
|
|
76
|
+
8. **includes<V>(value: NoInfer<V>): boolean**
|
|
77
|
+
|
|
78
|
+
- 类型参数:
|
|
79
|
+
V:继承自 StrNum 的具体值类型。
|
|
80
|
+
- 参数:
|
|
81
|
+
value: NoInfer<V>:要检查的目标值。
|
|
82
|
+
- 返回值:boolean。检查目标值是否存在于字典中。
|
|
83
|
+
|
|
84
|
+
### 使用示例
|
|
85
|
+
|
|
86
|
+
```typescript
|
|
87
|
+
import { createDictFactory } from "./dict";
|
|
88
|
+
|
|
89
|
+
// 1. 定义原始字典数据
|
|
90
|
+
const userStatusData = {
|
|
91
|
+
ACTIVE: { label: "活跃", value: 1 },
|
|
92
|
+
INACTIVE: { label: "未激活", value: 2 },
|
|
93
|
+
BANNED: { label: "封禁", value: 3 },
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
// 2. 创建字典实例
|
|
97
|
+
const statusDict = createDictFactory(userStatusData);
|
|
98
|
+
|
|
99
|
+
// 3. 动态属性访问
|
|
100
|
+
console.log(statusDict.ACTIVE.getLabel()); // 输出: '活跃'
|
|
101
|
+
|
|
102
|
+
// 4. 值反查标签
|
|
103
|
+
console.log(statusDict.getLabelForValue(2)); // 输出: '未激活'
|
|
104
|
+
|
|
105
|
+
// 5. 获取索引
|
|
106
|
+
console.log(statusDict.getIndex(3)); // 输出: 2
|
|
107
|
+
|
|
108
|
+
// 6. 校验合法性
|
|
109
|
+
const allowedStatus = [1, 2] as const;
|
|
110
|
+
console.log(statusDict.includes(3, allowedStatus)); // 输出: fals
|
|
111
|
+
```
|
package/dist/dict.es.js
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
// src/lib/dictItem.ts
|
|
2
|
+
var e = class {
|
|
3
|
+
index = 0;
|
|
4
|
+
label;
|
|
5
|
+
value;
|
|
6
|
+
item;
|
|
7
|
+
constructor(e) {
|
|
8
|
+
this.item = e, this.label = e.label, this.value = e.value;
|
|
9
|
+
}
|
|
10
|
+
getAttr(e) {
|
|
11
|
+
return this.item[e];
|
|
12
|
+
}
|
|
13
|
+
_setIndex(e) {
|
|
14
|
+
this.index = e;
|
|
15
|
+
}
|
|
16
|
+
getIndex() {
|
|
17
|
+
return this.index;
|
|
18
|
+
}
|
|
19
|
+
getLabel() {
|
|
20
|
+
return this.label;
|
|
21
|
+
}
|
|
22
|
+
getValue() {
|
|
23
|
+
return this.value;
|
|
24
|
+
}
|
|
25
|
+
getItem() {
|
|
26
|
+
return this.item;
|
|
27
|
+
}
|
|
28
|
+
}, t = class {
|
|
29
|
+
list = [];
|
|
30
|
+
map = /* @__PURE__ */ new Map();
|
|
31
|
+
itemMap = /* @__PURE__ */ new Map();
|
|
32
|
+
dictItemMap = /* @__PURE__ */ new Map();
|
|
33
|
+
constructor(t) {
|
|
34
|
+
for (let n in t) if (Object.hasOwn(t, n)) {
|
|
35
|
+
let r = t[n], i = new e(r);
|
|
36
|
+
i._setIndex(this.list.length), this[n] = i, this.list.push(r), this.map.set(i.getValue(), i.getLabel()), this.itemMap.set(i.getValue(), i.getItem()), this.dictItemMap.set(i.getValue(), i);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
getList() {
|
|
40
|
+
return this.list;
|
|
41
|
+
}
|
|
42
|
+
getMap() {
|
|
43
|
+
return this.map;
|
|
44
|
+
}
|
|
45
|
+
getItemMap() {
|
|
46
|
+
return this.itemMap;
|
|
47
|
+
}
|
|
48
|
+
getDictItemMap() {
|
|
49
|
+
return this.dictItemMap;
|
|
50
|
+
}
|
|
51
|
+
getIndex(e) {
|
|
52
|
+
return this.dictItemMap.get(e)?.getIndex() ?? -1;
|
|
53
|
+
}
|
|
54
|
+
getLabelForValue(e) {
|
|
55
|
+
return this.map.get(e);
|
|
56
|
+
}
|
|
57
|
+
includes(e, t) {
|
|
58
|
+
return t.includes(e);
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
export { t as Dict };
|
package/dist/dict.umd.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
(function(e,t){typeof exports==`object`&&typeof module<`u`?t(exports):typeof define==`function`&&define.amd?define([`exports`],t):(e=typeof globalThis<`u`?globalThis:e||self,t(e.dict={}))})(this,function(e){Object.defineProperty(e,Symbol.toStringTag,{value:`Module`});var t=class{index=0;label;value;item;constructor(e){this.item=e,this.label=e.label,this.value=e.value}getAttr(e){return this.item[e]}_setIndex(e){this.index=e}getIndex(){return this.index}getLabel(){return this.label}getValue(){return this.value}getItem(){return this.item}};e.Dict=class{list=[];map=new Map;itemMap=new Map;dictItemMap=new Map;constructor(e){for(let n in e)if(Object.hasOwn(e,n)){let r=e[n],i=new t(r);i._setIndex(this.list.length),this[n]=i,this.list.push(r),this.map.set(i.getValue(),i.getLabel()),this.itemMap.set(i.getValue(),i.getItem()),this.dictItemMap.set(i.getValue(),i)}}getList(){return this.list}getMap(){return this.map}getItemMap(){return this.itemMap}getDictItemMap(){return this.dictItemMap}getIndex(e){return this.dictItemMap.get(e)?.getIndex()??-1}getLabelForValue(e){return this.map.get(e)}includes(e,t){return t.includes(e)}}});
|
package/dist/jckj.svg
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
<svg version="1.1" id="layout_1" xmlns="http://www.w3.org/2000/svg" x="0" y="0" viewBox="0 0 148.28 132.18">
|
|
2
|
+
<g>
|
|
3
|
+
<path fill="#00A7EA" d="M139.08,62.84c-16.53,0-33.05,0-49.58,0c-3.85-8.99-12.32-14.83-21.36-14.83c-11.72,0-22.8,9.82-22.8,22.8 c0,12.59,10.21,22.8,22.8,22.8c9.89,0,18.31-6.3,21.47-15.11h33.83v24.91h15.65"/>
|
|
4
|
+
<path fill="#1D2088" d="M100.86,84.77c-5.44,12.84-18.16,21.84-32.97,21.84c-19.77,0-35.8-16.03-35.8-35.8s16.03-35.8,35.8-35.8 c14.3,0,26.63,8.38,32.37,20.5l18.27-7.5c-8.7-19.3-28.1-32.73-50.64-32.73c-30.67,0-55.53,24.86-55.53,55.53 s24.86,55.53,55.53,55.53c23.17,0,43.02-14.19,51.34-34.36L100.86,84.77z"/>
|
|
5
|
+
</g>
|
|
6
|
+
</svg>
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import type { DictData, DictItemVo, StrNum } from "./src/lib/type";
|
|
2
|
+
|
|
3
|
+
declare class DictItem<T extends DictItemVo = DictItemVo> {
|
|
4
|
+
constructor(item: T);
|
|
5
|
+
|
|
6
|
+
getAttr<K extends Exclude<keyof T, "label" | "value">>(key: K): T[K];
|
|
7
|
+
|
|
8
|
+
getIndex(): number;
|
|
9
|
+
|
|
10
|
+
getLabel(): StrNum;
|
|
11
|
+
|
|
12
|
+
getValue(): StrNum;
|
|
13
|
+
|
|
14
|
+
getItem(): T;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* 字典类,用于管理字典数据并提供多种查询方式
|
|
19
|
+
* @template T - 字典数据类型,必须符合DictData接口
|
|
20
|
+
*/
|
|
21
|
+
export declare class Dict<T extends DictData = DictData> {
|
|
22
|
+
/**
|
|
23
|
+
* 创建字典实例
|
|
24
|
+
* @param data - 字典数据对象
|
|
25
|
+
*/
|
|
26
|
+
constructor(data: T);
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* 获取字典项列表
|
|
30
|
+
* @returns 字典项数组
|
|
31
|
+
*/
|
|
32
|
+
getList(): readonly DictItemVo[];
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* 获取值到标签的映射
|
|
36
|
+
* @returns 值到标签的Map对象
|
|
37
|
+
*/
|
|
38
|
+
getMap(): ReadonlyMap<StrNum, string>;
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* 获取值到字典项的映射
|
|
42
|
+
* @returns 值到字典项的Map对象
|
|
43
|
+
*/
|
|
44
|
+
getItemMap(): ReadonlyMap<StrNum, DictItemVo>;
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* 获取值到DictItem对象的映射
|
|
48
|
+
* @returns 值到DictItem的Map对象
|
|
49
|
+
*/
|
|
50
|
+
getDictItemMap(): ReadonlyMap<StrNum, DictItem>;
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* 根据值获取索引位置
|
|
54
|
+
* @param value - 要查询的值
|
|
55
|
+
* @returns 索引位置,未找到返回-1
|
|
56
|
+
*/
|
|
57
|
+
getIndex(value: StrNum): number;
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* 根据值获取标签
|
|
61
|
+
* @param value - 要查询的值
|
|
62
|
+
* @returns 对应的标签,未找到返回undefined
|
|
63
|
+
*/
|
|
64
|
+
getLabelForValue(value: StrNum): string | undefined;
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* 检查值是否包含在给定的值数组中
|
|
68
|
+
* @param value - 要检查的值
|
|
69
|
+
* @param values - 值数组
|
|
70
|
+
* @returns 是否包含
|
|
71
|
+
*/
|
|
72
|
+
includes<V extends StrNum>(value: NoInfer<V>, values: readonly V[]): boolean;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* 创建字典的工厂函数
|
|
77
|
+
* @template T - 字典数据类型,必须符合DictData接口
|
|
78
|
+
* @param data - 字典数据对象
|
|
79
|
+
* @returns 字典实例
|
|
80
|
+
*/
|
|
81
|
+
export declare function createDictFactory<T extends DictData>(data: T): Dict<T>;
|
package/package.json
CHANGED
|
@@ -1,6 +1,39 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jckj/dict",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
|
|
3
|
+
"version": "1.0.3",
|
|
4
|
+
"publishConfig": {
|
|
5
|
+
"access": "public",
|
|
6
|
+
"registry": "https://registry.npmjs.org/"
|
|
7
|
+
},
|
|
8
|
+
"type": "module",
|
|
9
|
+
"files": [
|
|
10
|
+
"dist",
|
|
11
|
+
"index.d.ts",
|
|
12
|
+
"README.md",
|
|
13
|
+
"LICENSE"
|
|
14
|
+
],
|
|
15
|
+
"main": "dist/dict.umd.js",
|
|
16
|
+
"module": "dist/dict.es.js",
|
|
17
|
+
"types": "index.d.ts",
|
|
18
|
+
"description": "A lightweight TypeScript dictionary library featuring convenient and efficient data structure operations.",
|
|
19
|
+
"repository": {
|
|
20
|
+
"type": "git",
|
|
21
|
+
"url": "https://gitee.com/hzqhxy/dict"
|
|
22
|
+
},
|
|
23
|
+
"keywords": [
|
|
24
|
+
"dictionary",
|
|
25
|
+
"dict",
|
|
26
|
+
"util"
|
|
27
|
+
],
|
|
28
|
+
"author": "chuz1 <jckjsjz@163.com>",
|
|
29
|
+
"license": "MIT",
|
|
30
|
+
"devDependencies": {
|
|
31
|
+
"typescript": "~7.0.2",
|
|
32
|
+
"vite": "^8.3.6"
|
|
33
|
+
},
|
|
34
|
+
"scripts": {
|
|
35
|
+
"dev": "vite",
|
|
36
|
+
"build": "tsc && vite build",
|
|
37
|
+
"preview": "vite preview"
|
|
38
|
+
}
|
|
6
39
|
}
|