@lacqjs/nuxt-dict 0.0.2 → 0.0.4
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 +79 -0
- package/dist/module.d.mts +3 -1
- package/dist/module.d.ts +3 -1
- package/dist/module.json +1 -1
- package/dist/module.mjs +133 -12
- package/dist/runtime/composables/useDict.d.ts +6 -0
- package/dist/runtime/composables/useDict.js +36 -7
- package/dist/runtime/composables/useDictTree.d.ts +6 -0
- package/dist/runtime/composables/useDictTree.js +29 -14
- package/dist/runtime/composables/useLocale.d.ts +14 -0
- package/dist/runtime/core/adapter.d.ts +27 -1
- package/dist/runtime/core/adapter.js +6 -2
- package/dist/runtime/core/cache/indexeddb-cache.js +3 -1
- package/dist/runtime/core/dict-manager.d.ts +77 -9
- package/dist/runtime/core/dict-manager.js +81 -20
- package/dist/runtime/plugins/dict.js +6 -4
- package/dist/runtime/types/index.d.ts +302 -19
- package/dist/runtime/utils/dict-translator.d.ts +22 -24
- package/dist/runtime/utils/dict-translator.js +93 -20
- package/dist/types.d.mts +5 -1
- package/package.json +15 -10
- package/dist/runtime/composables/useDictOptions.d.ts +0 -13
- package/dist/runtime/composables/useDictOptions.js +0 -17
|
@@ -1,34 +1,32 @@
|
|
|
1
1
|
import type { DictManager } from '../core/dict-manager.js';
|
|
2
|
+
import type { DictTranslator } from '../types/index.js';
|
|
2
3
|
/**
|
|
3
|
-
* 创建字典翻译器,封装 translate / translatePath 方法。
|
|
4
|
+
* 创建字典翻译器,封装 translate / translatePath / translateData 方法。
|
|
4
5
|
* 作为 $dict 注入到 NuxtApp,供组件直接调用翻译。
|
|
5
6
|
* 使用包装层而非直接暴露 DictManager,以控制公开 API 范围。
|
|
6
7
|
*
|
|
8
|
+
* 仓库名统一通过 opts.storeName 指定,不再接受首参为 storeName 的旧式写法。
|
|
9
|
+
*
|
|
10
|
+
* @param {DictManager} manager - 字典管理器实例,提供底层 translate 能力
|
|
11
|
+
* @returns {DictTranslator} 翻译器对象,含 translate / translatePath / translateData 方法
|
|
12
|
+
*
|
|
7
13
|
* @example
|
|
8
14
|
* // 默认存储库 'dicts'
|
|
9
|
-
* $dict.translate('gender',
|
|
10
|
-
* $dict.translatePath('region',
|
|
15
|
+
* $dict.translate('gender', value)
|
|
16
|
+
* $dict.translatePath('region', value)
|
|
17
|
+
*
|
|
18
|
+
* // 指定存储库 + 自定义字段
|
|
19
|
+
* $dict.translate('gender', value, { storeName: 'dicts2', field: 'name' })
|
|
20
|
+
* $dict.translatePath('region', value, { storeName: 'dicts2', field: 'value', separator: ' -> ' })
|
|
11
21
|
*
|
|
12
|
-
* //
|
|
13
|
-
* $dict.translate('
|
|
14
|
-
* $dict.translatePath('dicts2', 'region', code)
|
|
15
|
-
* $dict.translatePath('dicts2', 'region', code, ' -> ')
|
|
22
|
+
* // 默认存储库 + 自定义字段
|
|
23
|
+
* $dict.translate('gender', value, { field: 'name' })
|
|
16
24
|
*
|
|
17
|
-
* //
|
|
18
|
-
* $dict.
|
|
25
|
+
* // 批量翻译数据对象中的字段
|
|
26
|
+
* $dict.translateData(
|
|
27
|
+
* { gender: 'male', status: 1 },
|
|
28
|
+
* { gender: 'gender', status: 'status' }
|
|
29
|
+
* )
|
|
30
|
+
* // → { gender: 'male', gender_label: '男', status: 1, status_label: '启用' }
|
|
19
31
|
*/
|
|
20
|
-
export declare function createDictTranslator(manager: DictManager):
|
|
21
|
-
/**
|
|
22
|
-
* 通过字典类型和编码获取翻译文本。
|
|
23
|
-
* 2 参:默认存储库;3 参:指定存储库(storeName 为第一个参数)
|
|
24
|
-
*/
|
|
25
|
-
translate(storeOrType: string, codeOrType: string | number, code?: string | number): string;
|
|
26
|
-
/**
|
|
27
|
-
* 获取树形字典中某个编码的完整层级路径。
|
|
28
|
-
* 2 参:默认存储库 + 默认分隔符
|
|
29
|
-
* 3 参:指定存储库 + 默认分隔符(storeName 为第一个参数)
|
|
30
|
-
* 4 参:指定存储库 + 自定义分隔符
|
|
31
|
-
* 默认存储库 + 自定义分隔符需显式传 'dicts' 作为 storeName
|
|
32
|
-
*/
|
|
33
|
-
translatePath(storeOrType: string, codeOrType: string | number, separatorOrCode?: string | number, separator?: string): string;
|
|
34
|
-
};
|
|
32
|
+
export declare function createDictTranslator(manager: DictManager): DictTranslator;
|
|
@@ -1,30 +1,103 @@
|
|
|
1
|
+
function translate(manager, type, code, opts) {
|
|
2
|
+
return manager.translate(type, code, opts);
|
|
3
|
+
}
|
|
4
|
+
function translatePath(manager, type, code, opts) {
|
|
5
|
+
return manager.translatePath(type, code, opts);
|
|
6
|
+
}
|
|
7
|
+
function getDictItem(manager, type, code, opts) {
|
|
8
|
+
return manager.getDictItem(type, code, opts);
|
|
9
|
+
}
|
|
10
|
+
function translateData(manager, data, mapping, suffix) {
|
|
11
|
+
const result = { ...data };
|
|
12
|
+
for (const [key, mapValue] of Object.entries(mapping)) {
|
|
13
|
+
const code = data[key];
|
|
14
|
+
const type = typeof mapValue === "string" ? mapValue : mapValue.type;
|
|
15
|
+
const storeName = typeof mapValue === "string" ? void 0 : mapValue.storeName;
|
|
16
|
+
const translated = code !== void 0 && code !== null ? translate(manager, type, code, storeName ? { storeName } : void 0) : "";
|
|
17
|
+
result[key + suffix] = translated;
|
|
18
|
+
}
|
|
19
|
+
return result;
|
|
20
|
+
}
|
|
1
21
|
export function createDictTranslator(manager) {
|
|
2
22
|
return {
|
|
3
23
|
/**
|
|
4
|
-
*
|
|
5
|
-
*
|
|
24
|
+
* 同步翻译字典编码 → 文本。
|
|
25
|
+
*
|
|
26
|
+
* @description 从全局内存缓存中查找编码对应的文本,未命中时返回 code 原样。先通过 useDict 等加载数据后调用。
|
|
27
|
+
* @param {string} type - 字典类型名,如 'gender'、'status'
|
|
28
|
+
* @param {string | number} code - 字典编码值
|
|
29
|
+
* @param {TranslateOptions} [opts] - 可选配置(storeName 指定仓库,field 指定取值字段,默认 'label')
|
|
30
|
+
* @returns {string} 翻译后的文本,缓存未命中时返回 String(code)
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* $dict.translate('gender', 'male')
|
|
34
|
+
* $dict.translate('gender', 'male', { storeName: 'dicts2' })
|
|
35
|
+
* $dict.translate('status', 1, { field: 'color' })
|
|
36
|
+
*/
|
|
37
|
+
translate(type, code, opts) {
|
|
38
|
+
return translate(manager, type, code, opts);
|
|
39
|
+
},
|
|
40
|
+
/**
|
|
41
|
+
* 树形字典中查找编码的完整层级路径。
|
|
42
|
+
*
|
|
43
|
+
* @description 从内存缓存中加载的树形字典数据里,通过 DFS 查找目标编码并回溯完整路径,用分隔符拼接后返回。
|
|
44
|
+
* @param {string} type - 树形字典类型名,如 'region'
|
|
45
|
+
* @param {string | number} code - 叶子节点编码值
|
|
46
|
+
* @param {TranslatePathOptions} [opts] - 可选配置(storeName 指定仓库,field 指定节点取值字段,separator 指定分隔符,默认 ' / ')
|
|
47
|
+
* @returns {string} 用分隔符连接的完整层级路径,未命中时返回 String(code)
|
|
48
|
+
*
|
|
49
|
+
* @example
|
|
50
|
+
* $dict.translatePath('region', '440104')
|
|
51
|
+
* $dict.translatePath('region', '440104', { separator: ' → ' })
|
|
52
|
+
* $dict.translatePath('region', '440104', { storeName: 'dicts2', field: 'value' })
|
|
53
|
+
*/
|
|
54
|
+
translatePath(type, code, opts) {
|
|
55
|
+
return translatePath(manager, type, code, opts);
|
|
56
|
+
},
|
|
57
|
+
/**
|
|
58
|
+
* 批量翻译数据对象中的多个编码字段。
|
|
59
|
+
*
|
|
60
|
+
* @description 传入一个数据对象和字段→字典类型映射表,返回追加了翻译字段的新对象(不修改原对象)。
|
|
61
|
+
* @param {Record<string, unknown>} data - 需要翻译的数据对象,如 `{ gender: 'male', status: 1 }`
|
|
62
|
+
* @param {Record<string, string | { type: string; storeName?: StoreKey }>} mapping - 字段映射表,key 为原字段名,value 为字典类型名(string)或 `{ type, storeName? }` 对象
|
|
63
|
+
* @param {string} [suffix='_label'] - 翻译字段的后缀,默认 `'_label'`,即翻译结果追加到 `原字段名 + suffix` 字段
|
|
64
|
+
* @returns {Record<string, unknown>} 新对象,包含原数据所有字段 + 以 suffix 为后缀的翻译字段
|
|
65
|
+
*
|
|
66
|
+
* @example
|
|
67
|
+
* $dict.translateData(
|
|
68
|
+
* { gender: 'male', status: 1, name: '张三' },
|
|
69
|
+
* { gender: 'gender', status: 'status' }
|
|
70
|
+
* )
|
|
71
|
+
* // → { gender: 'male', gender_label: '男', status: 1, status_label: '启用', name: '张三' }
|
|
72
|
+
*
|
|
73
|
+
* // 跨仓库 + 自定义后缀
|
|
74
|
+
* $dict.translateData(
|
|
75
|
+
* { payStatus: 1 },
|
|
76
|
+
* { payStatus: { type: 'pay_status', storeName: 'payment' } },
|
|
77
|
+
* '_text'
|
|
78
|
+
* )
|
|
79
|
+
* // → { payStatus: 1, payStatus_text: '已支付' }
|
|
6
80
|
*/
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
return manager.translate(codeOrType, code, storeOrType);
|
|
10
|
-
}
|
|
11
|
-
return manager.translate(storeOrType, codeOrType);
|
|
81
|
+
translateData(data, mapping, suffix = "_label") {
|
|
82
|
+
return translateData(manager, data, mapping, suffix);
|
|
12
83
|
},
|
|
13
84
|
/**
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
85
|
+
* 从内存缓存中查找编码对应的完整字典项对象。
|
|
86
|
+
*
|
|
87
|
+
* @description 与 translate 参数一致,但返回整个 DictItem 而非提取单个字段。缓存未命中时返回 undefined。
|
|
88
|
+
* @param {string} type - 字典类型名,如 'gender'、'status'
|
|
89
|
+
* @param {string | number} code - 字典编码值
|
|
90
|
+
* @param {GetDictItemOptions} [opts] - 可选配置(storeName 指定仓库)
|
|
91
|
+
* @returns {DictItem | undefined} 完整的字典项对象,缓存未命中时返回 undefined
|
|
92
|
+
*
|
|
93
|
+
* @example
|
|
94
|
+
* $dict.getDictItem('gender', 'male')
|
|
95
|
+
* // → { value: 'male', label: '男' }
|
|
96
|
+
* $dict.getDictItem('gender', 'male', { storeName: 'dicts2' })
|
|
97
|
+
* // → { value: 'male', label: '男(源2)' }
|
|
19
98
|
*/
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
return manager.translatePath(codeOrType, separatorOrCode, separator, storeOrType);
|
|
23
|
-
}
|
|
24
|
-
if (separatorOrCode !== void 0) {
|
|
25
|
-
return manager.translatePath(codeOrType, separatorOrCode, void 0, storeOrType);
|
|
26
|
-
}
|
|
27
|
-
return manager.translatePath(storeOrType, codeOrType);
|
|
99
|
+
getDictItem(type, code, opts) {
|
|
100
|
+
return getDictItem(manager, type, code, opts);
|
|
28
101
|
}
|
|
29
102
|
};
|
|
30
103
|
}
|
package/dist/types.d.mts
CHANGED
|
@@ -1,3 +1,7 @@
|
|
|
1
|
-
export { type ModuleOptions } from '../dist/runtime/types/index.js'
|
|
1
|
+
export { type DictItem, type DictTranslator, type GetDictItemOptions, type ModuleOptions, type TranslateOptions, type TranslatePathOptions, type TreeNode } from '../dist/runtime/types/index.js'
|
|
2
|
+
|
|
3
|
+
export { type DictManager } from '../dist/runtime/core/dict-manager.js'
|
|
4
|
+
|
|
5
|
+
export { type createDictTranslator } from '../dist/runtime/utils/dict-translator.js'
|
|
2
6
|
|
|
3
7
|
export { default } from './module.mjs'
|
package/package.json
CHANGED
|
@@ -1,19 +1,21 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lacqjs/nuxt-dict",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.4",
|
|
4
4
|
"description": "Nuxt 数据字典模块,提供扁平/树形字典翻译、多语言国际化、三级缓存与 SSR 预取。",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"@lacqjs/nuxt-dict",
|
|
7
|
+
"dict",
|
|
7
8
|
"nuxt",
|
|
8
|
-
"nuxt-dict"
|
|
9
|
-
"dict"
|
|
9
|
+
"nuxt-dict"
|
|
10
10
|
],
|
|
11
|
+
"homepage": "https://miaozhongfei.github.io/nuxt-dict/",
|
|
11
12
|
"license": "MIT",
|
|
12
13
|
"author": {
|
|
13
|
-
"name": "
|
|
14
|
-
"email": "
|
|
14
|
+
"name": "miaozhongfei",
|
|
15
|
+
"email": "miaozhongfei@example.com"
|
|
15
16
|
},
|
|
16
17
|
"contributors": [],
|
|
18
|
+
"repository": "miaozhongfei/nuxt-dict",
|
|
17
19
|
"files": [
|
|
18
20
|
"dist"
|
|
19
21
|
],
|
|
@@ -64,6 +66,7 @@
|
|
|
64
66
|
"pnpm": ">=10.22.0",
|
|
65
67
|
"yarn": ">=1.22.22"
|
|
66
68
|
},
|
|
69
|
+
"documentation": "https://miaozhongfei.github.io/nuxt-dict/",
|
|
67
70
|
"scripts": {
|
|
68
71
|
"cleanup": "pnpx nuxt cleanup || npx nuxt cleanup",
|
|
69
72
|
"clean:install": "pnpm run cleanup && pnpm run docs:cleanup && pnpm run playground:cleanup && pnpm install",
|
|
@@ -72,12 +75,14 @@
|
|
|
72
75
|
"dev:prepare": "nuxt-module-build build --stub && nuxt-module-build prepare && nuxi prepare playground",
|
|
73
76
|
"docs:dev": "nuxi dev docs",
|
|
74
77
|
"docs:build": "nuxi build docs",
|
|
75
|
-
"docs:
|
|
78
|
+
"docs:generate": "nuxi generate docs",
|
|
79
|
+
"docs:cleanup": "pnpx rimraf docs/.nuxt docs/.output docs/dist docs/node_modules docs/.data",
|
|
76
80
|
"playground:cleanup": "pnpx rimraf playground/.nuxt playground/.output playground/dist playground/node_modules",
|
|
77
|
-
"
|
|
78
|
-
"release
|
|
79
|
-
"release:
|
|
80
|
-
"release:e2e
|
|
81
|
+
"demo:base-api:dev": "nuxi dev examples/demo-base-api",
|
|
82
|
+
"release": "pnpm lint && pnpm typecheck && pnpm prepack && changelogen --release && pnpm publish && git push origin dev --follow-tags",
|
|
83
|
+
"release:major": "pnpm lint && pnpm typecheck && pnpm prepack && changelogen --release --major && pnpm publish && git push origin dev --follow-tags",
|
|
84
|
+
"release:e2e": "pnpm lint && pnpm e2e && pnpm typecheck && pnpm prepack && changelogen --release && pnpm publish && git push origin dev --follow-tags",
|
|
85
|
+
"release:e2e:major": "pnpm lint && pnpm e2e && pnpm typecheck && pnpm prepack && changelogen --release --major && pnpm publish && git push origin dev --follow-tags",
|
|
81
86
|
"lint": "oxlint . && oxlint docs --no-error-on-unmatched-pattern",
|
|
82
87
|
"lint:fix": "oxlint --fix . && oxlint --fix docs --no-error-on-unmatched-pattern",
|
|
83
88
|
"fmt": "oxfmt",
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import type { UseDictOptionsReturn, StoreKey } from '../types/index.js';
|
|
2
|
-
/**
|
|
3
|
-
* 以 { label, value } 格式使用字典数据,
|
|
4
|
-
* 直接适配 UI 库的 options 属性。
|
|
5
|
-
*
|
|
6
|
-
* @example
|
|
7
|
-
* // 默认存储库 'dicts'
|
|
8
|
-
* const { options } = useDictOptions('industry')
|
|
9
|
-
* // 指定存储库 'dicts2'
|
|
10
|
-
* const { options } = useDictOptions('dicts2', 'industry')
|
|
11
|
-
*/
|
|
12
|
-
export declare function useDictOptions(type: string): UseDictOptionsReturn;
|
|
13
|
-
export declare function useDictOptions(storeName: StoreKey, type: string): UseDictOptionsReturn;
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import { computed } from "vue";
|
|
2
|
-
import { useDict } from "./useDict.js";
|
|
3
|
-
export function useDictOptions(storeOrType, maybeType) {
|
|
4
|
-
const { data, loading, refresh } = maybeType === void 0 ? useDict(storeOrType) : useDict(storeOrType, maybeType);
|
|
5
|
-
const options = computed(() => {
|
|
6
|
-
if (!data.value) return [];
|
|
7
|
-
return data.value.map((item) => ({
|
|
8
|
-
label: item.label,
|
|
9
|
-
value: item.code
|
|
10
|
-
}));
|
|
11
|
-
});
|
|
12
|
-
return {
|
|
13
|
-
options,
|
|
14
|
-
loading,
|
|
15
|
-
refresh
|
|
16
|
-
};
|
|
17
|
-
}
|