v-dict 1.0.1 → 1.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/README.md CHANGED
@@ -0,0 +1,140 @@
1
+ <!--
2
+ * @Date: 2024-01-12 02:58:43
3
+ * @Author: fangruiyi
4
+ * @LastEditors: fangruiyi
5
+ * @Description:
6
+ -->
7
+
8
+ # Vue3 Dict Manager
9
+
10
+ ## Installation
11
+
12
+ ```sh
13
+ npm i v-dict
14
+ ```
15
+
16
+ ## Examples
17
+
18
+ ### dict.ts
19
+
20
+ ```ts
21
+ import { createDictManager, defineDictData } from 'v-dict'
22
+
23
+ export const dm = createDictManager({
24
+ // method to fetch remote dict
25
+ fetch: (code) =>
26
+ Promise.resolve([
27
+ { label: 'xx', value: 'xx' },
28
+ { label: 'xx', value: 'xx' }
29
+ ]),
30
+ // extra attr
31
+ extra: ({ loadPromise, load, list, map, E }) => {
32
+ return {
33
+ getLabel: (value: string) => map[value]?.label
34
+ }
35
+ }
36
+ })
37
+
38
+ // same api for local dict or remote dict
39
+ // local
40
+ export const useStatusDict = dm.defineDict('STATUS', {
41
+ data: defineDictData({
42
+ ENABLED: {
43
+ label: 'Enabled',
44
+ // extra attr
45
+ color: 'green'
46
+ },
47
+ DISABLED: {
48
+ label: 'Disabled',
49
+ color: 'red'
50
+ }
51
+ })
52
+ })
53
+ // remote
54
+ export const useRemoteStatusDict = dm.defineDict('REMOTE_STATUS', {
55
+ remote: true,
56
+ // overwrite dm fetch
57
+ fetch: (code) =>
58
+ // code = 'REMOTE_STATUS'
59
+ Promise.resolve([
60
+ { label: 'Enabled', value: 'ENABLED', color: 'green' },
61
+ { label: 'Disabled', value: 'DISABLED' color: 'red' }
62
+ ]),
63
+ // merge dm extra
64
+ extra: ({ loadPromise, load, list, map, E }) => {
65
+ return {
66
+ getItem: (value: string) => map[value]
67
+ }
68
+ }
69
+ })
70
+
71
+ ```
72
+
73
+ ### xx.vue
74
+
75
+ ```vue
76
+ <template>
77
+ <div>
78
+ {{ statusDict.E }}
79
+ {{ statusDict.map }}
80
+ {{ statusDict.list }}
81
+ {{ statusDict.getLabel(E.ENABLED) }}
82
+ {{ statusDict.getItem(E.DISABLED) }}
83
+ </div>
84
+ </template>
85
+
86
+ <script setup lang="ts">
87
+ import { useRemoteStatusDict } from './dict'
88
+ import { onMounted } from 'vue'
89
+
90
+ const statusDict = useRemoteStatusDict({
91
+ // Data sharing by default, independent data source when clone is true
92
+ clone: true,
93
+ // Whether the remote dictionary loads data immediately
94
+ immediate: false
95
+ }) // statusDict is reactive!!!
96
+
97
+ const { E, map, list } = statusDict
98
+
99
+ /*
100
+ E: {
101
+ ENABLED: ENABLED,
102
+ DISABLED: DISABLED
103
+ }
104
+
105
+ map: {
106
+ ENABLED: {
107
+ label: 'Enabled',
108
+ value: 'ENABLED',
109
+ color: 'green'
110
+ },
111
+ DISABLED: {
112
+ label: 'Disabled',
113
+ value: 'DISABLED',
114
+ color: 'red'
115
+ }
116
+ }
117
+
118
+ list: [
119
+ {
120
+ label: 'Enabled',
121
+ value: 'ENABLED',
122
+ color: 'green'
123
+ },
124
+ {
125
+ label: 'Disabled',
126
+ value: 'DISABLED',
127
+ color: 'red'
128
+ }
129
+ ]
130
+ */
131
+
132
+ onMounted(async () => {
133
+ await statusDict.load()
134
+
135
+ await statusDict.loadPromise // immediate = true, using loadPromise to wait load
136
+ // do after dict load
137
+ console.log(statusDict.list)
138
+ })
139
+ </script>
140
+ ```
package/dist/index.cjs CHANGED
@@ -63,20 +63,20 @@ var defineDictData = (data) => data;
63
63
  function createDictManager(managerOptions = {}) {
64
64
  const { fetch: managerFetch, extra: managerExtra } = managerOptions;
65
65
  const maps = vue.reactive({});
66
- const defineDict = (code, options) => {
66
+ const defineDict = (code, defineDictOptions) => {
67
67
  var _a;
68
68
  const {
69
69
  data = {},
70
70
  remote = false,
71
71
  fetch = managerFetch,
72
72
  extra
73
- } = (_a = lodashEs.isFunction(options) ? options() : options) != null ? _a : {};
74
- const managerLoadPromise = vue.shallowRef(null);
73
+ } = (_a = lodashEs.isFunction(defineDictOptions) ? defineDictOptions() : defineDictOptions) != null ? _a : {};
74
+ const managerLoadPromise = vue.shallowRef(createPromise());
75
75
  maps[code] = /* @__PURE__ */ new Map();
76
- async function loadDict(ctx, mapRef) {
76
+ async function loadDict(options, mapRef) {
77
77
  const dataMap = toMap(lodashEs.cloneDeep(data));
78
78
  if (remote) {
79
- await (fetch == null ? void 0 : fetch(code, ctx).then((res) => {
79
+ await (fetch == null ? void 0 : fetch(code, options).then((res) => {
80
80
  mapRef.value = toMap(res != null ? res : []);
81
81
  dataMap.forEach((value, key) => {
82
82
  if (mapRef.value.has(key)) {
@@ -91,7 +91,7 @@ function createDictManager(managerOptions = {}) {
91
91
  return (useDictOptions) => {
92
92
  useDictOptions = lodashEs.merge({ clone: false, immediate: false }, useDictOptions);
93
93
  const { clone, immediate } = useDictOptions;
94
- const loadPromise = !clone ? managerLoadPromise : vue.shallowRef(null);
94
+ const loadPromise = !clone ? managerLoadPromise : vue.shallowRef(createPromise());
95
95
  const mapRef = !clone ? vue.toRef(maps, code) : vue.ref(/* @__PURE__ */ new Map());
96
96
  const objRef = vue.ref({});
97
97
  const listRef = vue.ref([]);
@@ -115,9 +115,9 @@ function createDictManager(managerOptions = {}) {
115
115
  } else {
116
116
  load();
117
117
  }
118
- function load(ctx2) {
118
+ function load(options) {
119
119
  loadPromise.value = createPromise();
120
- loadDict(lodashEs.merge({}, useDictOptions, ctx2), mapRef).then(() => {
120
+ loadDict(lodashEs.merge({}, useDictOptions, options), mapRef).then(() => {
121
121
  var _a2;
122
122
  (_a2 = loadPromise.value) == null ? void 0 : _a2.resolve();
123
123
  });
@@ -127,12 +127,12 @@ function createDictManager(managerOptions = {}) {
127
127
  map: objRef,
128
128
  list: listRef,
129
129
  E,
130
- loadPromise
130
+ loadPromise,
131
+ load
131
132
  };
132
133
  const reactiveCtx = vue.reactive(ctx);
133
134
  return vue.reactive({
134
135
  ...ctx,
135
- load,
136
136
  ...managerExtra == null ? void 0 : managerExtra(reactiveCtx),
137
137
  ...extra == null ? void 0 : extra(reactiveCtx)
138
138
  });
package/dist/index.js CHANGED
@@ -61,20 +61,20 @@ var defineDictData = (data) => data;
61
61
  function createDictManager(managerOptions = {}) {
62
62
  const { fetch: managerFetch, extra: managerExtra } = managerOptions;
63
63
  const maps = reactive({});
64
- const defineDict = (code, options) => {
64
+ const defineDict = (code, defineDictOptions) => {
65
65
  var _a;
66
66
  const {
67
67
  data = {},
68
68
  remote = false,
69
69
  fetch = managerFetch,
70
70
  extra
71
- } = (_a = isFunction(options) ? options() : options) != null ? _a : {};
72
- const managerLoadPromise = shallowRef(null);
71
+ } = (_a = isFunction(defineDictOptions) ? defineDictOptions() : defineDictOptions) != null ? _a : {};
72
+ const managerLoadPromise = shallowRef(createPromise());
73
73
  maps[code] = /* @__PURE__ */ new Map();
74
- async function loadDict(ctx, mapRef) {
74
+ async function loadDict(options, mapRef) {
75
75
  const dataMap = toMap(cloneDeep(data));
76
76
  if (remote) {
77
- await (fetch == null ? void 0 : fetch(code, ctx).then((res) => {
77
+ await (fetch == null ? void 0 : fetch(code, options).then((res) => {
78
78
  mapRef.value = toMap(res != null ? res : []);
79
79
  dataMap.forEach((value, key) => {
80
80
  if (mapRef.value.has(key)) {
@@ -89,7 +89,7 @@ function createDictManager(managerOptions = {}) {
89
89
  return (useDictOptions) => {
90
90
  useDictOptions = merge({ clone: false, immediate: false }, useDictOptions);
91
91
  const { clone, immediate } = useDictOptions;
92
- const loadPromise = !clone ? managerLoadPromise : shallowRef(null);
92
+ const loadPromise = !clone ? managerLoadPromise : shallowRef(createPromise());
93
93
  const mapRef = !clone ? toRef(maps, code) : ref(/* @__PURE__ */ new Map());
94
94
  const objRef = ref({});
95
95
  const listRef = ref([]);
@@ -113,9 +113,9 @@ function createDictManager(managerOptions = {}) {
113
113
  } else {
114
114
  load();
115
115
  }
116
- function load(ctx2) {
116
+ function load(options) {
117
117
  loadPromise.value = createPromise();
118
- loadDict(merge({}, useDictOptions, ctx2), mapRef).then(() => {
118
+ loadDict(merge({}, useDictOptions, options), mapRef).then(() => {
119
119
  var _a2;
120
120
  (_a2 = loadPromise.value) == null ? void 0 : _a2.resolve();
121
121
  });
@@ -125,12 +125,12 @@ function createDictManager(managerOptions = {}) {
125
125
  map: objRef,
126
126
  list: listRef,
127
127
  E,
128
- loadPromise
128
+ loadPromise,
129
+ load
129
130
  };
130
131
  const reactiveCtx = reactive(ctx);
131
132
  return reactive({
132
133
  ...ctx,
133
- load,
134
134
  ...managerExtra == null ? void 0 : managerExtra(reactiveCtx),
135
135
  ...extra == null ? void 0 : extra(reactiveCtx)
136
136
  });
package/dist/type.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import type { O } from 'ts-toolbelt';
2
- import type { Except, Simplify, ValueOf } from 'type-fest';
2
+ import type { Simplify, ValueOf } from 'type-fest';
3
3
  import type { createPromise } from './create-promise';
4
4
  export type Recordable<T = any> = Record<string, T>;
5
5
  export type Getter<T> = () => T;
@@ -10,29 +10,33 @@ export type DictItem = {
10
10
  label: string;
11
11
  value: string;
12
12
  };
13
- export type DictItemRecord = Simplify<DictItem & Recordable>;
13
+ export type DictItemRecord = {
14
+ label: string;
15
+ value: string;
16
+ } & Recordable;
14
17
  export type DictMap = Map<string, DictItemRecord>;
15
- export type Fetch = (code: string, ctx: any) => Promise<DictItemRecord[]>;
18
+ export type Fetch = (code: string, options?: any) => Promise<DictItemRecord[]>;
16
19
  export interface CreateDictManager<E extends ExtraGetter> {
17
20
  fetch?: Fetch;
18
21
  extra?: E;
19
22
  }
20
- export type ExtraGetter<D extends Dict<string> = Dict<string>> = (dict: Except<D, 'load'>) => Recordable;
23
+ export type ExtraGetter<D extends Dict<string> = Dict<string>> = (dict: D) => Recordable;
21
24
  export type UseDictOptions = {
22
25
  clone?: boolean;
23
26
  immediate?: boolean;
24
27
  } & Recordable;
28
+ type ExtractFetchOptions<F extends Fetch> = Parameters<F>[1] extends infer T ? T extends undefined ? {} : T : never;
25
29
  export interface DefineDict<Extra extends ExtraGetter> {
26
30
  <R extends boolean, F extends Fetch, D extends Recordable<Recordable>, E extends ExtraGetter>(code: string, options?: MaybeGetter<{
27
31
  remote?: R;
28
32
  fetch?: F;
29
33
  data?: D;
30
34
  extra?: E;
31
- }>): (options?: UseDictOptions & GetFetchCtx<F>) => CreateDict<D, F> & ReturnType<Extra> & ReturnType<E>;
35
+ }>): (options?: Simplify<UseDictOptions & ExtractFetchOptions<F>>) => Simplify<CreateDict<D, F> & ReturnType<Extra> & ReturnType<E>>;
32
36
  }
33
- type GetFetchCtx<F extends Fetch> = Parameters<F>[1] extends undefined ? {} : Parameters<F>[1];
34
- type CreateDict<D extends Recordable<Recordable>, F extends Fetch> = Dict<keyof D, O.Merge<UnwrapArray<Awaited<ReturnType<F>>>, ValueOf<D>>, UseDictOptions & GetFetchCtx<F>>;
35
- export type Dict<Key extends PropertyKey = PropertyKey, ExtraItem extends Recordable = Recordable, Ctx = any> = {
37
+ type CreateDict<D extends Recordable<Recordable>, F extends Fetch> = Dict<keyof D, O.Merge<UnwrapArray<Awaited<ReturnType<F>>>, ValueOf<D>>, UseDictOptions & ExtractFetchOptions<F>>;
38
+ export type LoadPromise = ReturnType<typeof createPromise<void>>;
39
+ export type Dict<Key extends PropertyKey = PropertyKey, ExtraItem extends Recordable = Recordable, Options = Recordable> = {
36
40
  list: Simplify<DictItem & ExtraItem>[];
37
41
  E: {
38
42
  [K in Key]: K;
@@ -40,8 +44,7 @@ export type Dict<Key extends PropertyKey = PropertyKey, ExtraItem extends Record
40
44
  map: {
41
45
  [K in Key]: Simplify<DictItem & ExtraItem>;
42
46
  } & Recordable<Simplify<DictItem & ExtraItem>>;
43
- loadPromise: Promise<void>;
44
- load: (ctx: Ctx) => Promise<void>;
47
+ loadPromise: LoadPromise;
48
+ load: (options?: Options) => LoadPromise;
45
49
  };
46
- export type LoadPromise = ReturnType<typeof createPromise<void>>;
47
50
  export {};
package/dist/util.d.ts CHANGED
@@ -1,16 +1,8 @@
1
1
  import type { WritableDeep } from 'type-fest';
2
2
  import type { DictItemRecord, DictMap, Recordable } from './type';
3
- export declare function mapToObj(map: DictMap, obj?: Recordable<DictItemRecord>): Recordable<{
4
- [x: string]: any;
5
- label: string;
6
- value: string;
7
- }>;
3
+ export declare function mapToObj(map: DictMap, obj?: Recordable<DictItemRecord>): Recordable<DictItemRecord>;
8
4
  export declare function objToMap(obj: Recordable<DictItemRecord>): DictMap;
9
- export declare function mapToList(map: DictMap, list?: DictItemRecord[]): {
10
- [x: string]: any;
11
- label: string;
12
- value: string;
13
- }[];
5
+ export declare function mapToList(map: DictMap, list?: DictItemRecord[]): DictItemRecord[];
14
6
  export declare function listToMap(list: DictItemRecord[]): DictMap;
15
7
  export declare function toMap(data: Recordable<DictItemRecord> | DictItemRecord[]): DictMap;
16
8
  export declare const defineDictData: <const T extends {
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "v-dict",
3
- "version": "1.0.1",
3
+ "version": "1.0.4",
4
4
  "type": "module",
5
- "description": "vue3 dict manager",
5
+ "description": "Vue3 Dict Manager",
6
6
  "main": "dist/index.js",
7
7
  "module": "dist/index.mjs",
8
8
  "typings": "dist/index.d.ts",