@lacqjs/nuxt-dict 0.0.2 → 0.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.
@@ -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
- * 2 参:默认存储库;3 参:指定存储库(storeName 为第一个参数)
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
- translate(storeOrType, codeOrType, code) {
8
- if (code !== void 0) {
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
- * 2 参:默认存储库 + 默认分隔符
16
- * 3 参:指定存储库 + 默认分隔符(storeName 为第一个参数)
17
- * 4 参:指定存储库 + 自定义分隔符
18
- * 默认存储库 + 自定义分隔符需显式传 'dicts' 作为 storeName
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
- translatePath(storeOrType, codeOrType, separatorOrCode, separator) {
21
- if (separator !== void 0) {
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,6 +1,6 @@
1
1
  {
2
2
  "name": "@lacqjs/nuxt-dict",
3
- "version": "0.0.2",
3
+ "version": "0.0.3",
4
4
  "description": "Nuxt 数据字典模块,提供扁平/树形字典翻译、多语言国际化、三级缓存与 SSR 预取。",
5
5
  "keywords": [
6
6
  "@lacqjs/nuxt-dict",
@@ -10,9 +10,12 @@
10
10
  ],
11
11
  "license": "MIT",
12
12
  "author": {
13
- "name": "miaozf",
14
- "email": "miaozf@example.com"
13
+ "name": "miaozhongfei",
14
+ "email": "miaozhongfei@example.com"
15
15
  },
16
+ "repository": "miaozhongfei/nuxt-dict",
17
+ "homepage": "https://miaozhongfei.github.io/nuxt-dict/",
18
+ "documentation": "https://miaozhongfei.github.io/nuxt-dict/",
16
19
  "contributors": [],
17
20
  "files": [
18
21
  "dist"
@@ -72,7 +75,8 @@
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:cleanup": "pnpx rimraf docs/.nuxt docs/.output docs/dist docs/node_modules",
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
81
  "release": "pnpm lint && pnpm typecheck && pnpm prepack && changelogen --release && pnpm publish && git push --follow-tags",
78
82
  "release:major": "pnpm lint && pnpm typecheck && pnpm prepack && changelogen --release --major && pnpm publish && git push --follow-tags",
@@ -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
- }