v-dict 1.1.2 → 1.2.0
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/dist/dict-manager.d.ts +1 -1
- package/dist/index.cjs +78 -43
- package/dist/index.d.ts +1 -1
- package/dist/index.js +79 -44
- package/dist/types/dict.d.ts +64 -0
- package/dist/types/index.d.ts +1 -0
- package/dist/types/util.d.ts +12 -0
- package/dist/util.d.ts +6 -6
- package/package.json +2 -3
- package/dist/type.d.ts +0 -52
package/dist/dict-manager.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { CreateDictManager, DefineDict, ExtraGetter } from './
|
|
1
|
+
import type { CreateDictManager, DefineDict, ExtraGetter } from './types';
|
|
2
2
|
export declare function createDictManager<E extends ExtraGetter>(managerOptions?: CreateDictManager<E>): {
|
|
3
3
|
defineDict: DefineDict<E>;
|
|
4
4
|
clear: (code?: string) => void;
|
package/dist/index.cjs
CHANGED
|
@@ -38,11 +38,12 @@ function mapToObj(map, obj = {}) {
|
|
|
38
38
|
}
|
|
39
39
|
function objToMap(obj) {
|
|
40
40
|
const map = /* @__PURE__ */ new Map();
|
|
41
|
-
for (const
|
|
42
|
-
|
|
43
|
-
|
|
41
|
+
for (const key of Object.keys(obj)) {
|
|
42
|
+
const item = obj[key];
|
|
43
|
+
if (lodashEs.isUndefined(item.value)) {
|
|
44
|
+
item.value = key;
|
|
44
45
|
}
|
|
45
|
-
map.set(key,
|
|
46
|
+
map.set(key, item);
|
|
46
47
|
}
|
|
47
48
|
return map;
|
|
48
49
|
}
|
|
@@ -58,39 +59,62 @@ function listToMap(list) {
|
|
|
58
59
|
}
|
|
59
60
|
return map;
|
|
60
61
|
}
|
|
61
|
-
function toMap(data
|
|
62
|
-
|
|
62
|
+
function toMap(data, {
|
|
63
|
+
pickValues,
|
|
64
|
+
omitValues
|
|
65
|
+
} = {}) {
|
|
66
|
+
if (lodashEs.isArray(data)) {
|
|
67
|
+
if (pickValues == null ? void 0 : pickValues.length) {
|
|
68
|
+
data = data.filter((item) => pickValues.includes(item.value));
|
|
69
|
+
}
|
|
70
|
+
if (omitValues == null ? void 0 : omitValues.length) {
|
|
71
|
+
data = data.filter((item) => !omitValues.includes(item.value));
|
|
72
|
+
}
|
|
73
|
+
return listToMap(data);
|
|
74
|
+
}
|
|
75
|
+
if (pickValues == null ? void 0 : pickValues.length) {
|
|
76
|
+
data = lodashEs.pick(data, ...pickValues);
|
|
77
|
+
}
|
|
78
|
+
if (omitValues == null ? void 0 : omitValues.length) {
|
|
79
|
+
data = lodashEs.omit(data, ...omitValues);
|
|
80
|
+
}
|
|
81
|
+
return objToMap(data);
|
|
63
82
|
}
|
|
64
83
|
var defineDictData = (data) => data;
|
|
65
84
|
|
|
66
85
|
// src/dict-manager.ts
|
|
86
|
+
var warn = (msg) => console.warn(`[v-dict]: ${msg}`);
|
|
67
87
|
function createDictManager(managerOptions = {}) {
|
|
68
88
|
const { fetch: managerFetch, extra: managerExtra } = managerOptions;
|
|
69
|
-
const maps = vue.reactive(
|
|
89
|
+
const maps = vue.reactive(/* @__PURE__ */ Object.create(null));
|
|
90
|
+
const defineDictOptionsMap = /* @__PURE__ */ new Map();
|
|
70
91
|
function clear(code) {
|
|
92
|
+
var _a;
|
|
71
93
|
if (code) {
|
|
72
|
-
|
|
73
|
-
dictMap && dictMap.clear();
|
|
94
|
+
(_a = maps[code]) == null ? void 0 : _a.clear();
|
|
74
95
|
return;
|
|
75
96
|
}
|
|
76
97
|
clearObj(maps);
|
|
77
98
|
}
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
99
|
+
function _defineDict(internalOptions, code, defineDictOptions) {
|
|
100
|
+
if (maps[code]) {
|
|
101
|
+
warn(`code "${code}" already exists`);
|
|
102
|
+
}
|
|
103
|
+
const { pickValues, omitValues } = internalOptions;
|
|
104
|
+
const options = Object.assign(
|
|
105
|
+
{ data: {}, remote: false, fetch: managerFetch },
|
|
106
|
+
lodashEs.isFunction(defineDictOptions) ? defineDictOptions() : defineDictOptions
|
|
107
|
+
);
|
|
108
|
+
defineDictOptionsMap.set(code, options);
|
|
109
|
+
const { data, remote, fetch, extra } = options;
|
|
86
110
|
const globalLoadPromise = vue.shallowRef(null);
|
|
87
111
|
maps[code] = /* @__PURE__ */ new Map();
|
|
88
|
-
async function loadDict(
|
|
89
|
-
var
|
|
90
|
-
const dataMap = toMap(lodashEs.cloneDeep(data));
|
|
112
|
+
async function loadDict(options2, mapRef) {
|
|
113
|
+
var _a;
|
|
114
|
+
const dataMap = toMap(lodashEs.cloneDeep(data), { pickValues, omitValues });
|
|
91
115
|
if (remote) {
|
|
92
|
-
const res = (
|
|
93
|
-
mapRef.value = toMap(res);
|
|
116
|
+
const res = (_a = await (fetch == null ? void 0 : fetch(code, options2))) != null ? _a : [];
|
|
117
|
+
mapRef.value = toMap(res, { pickValues, omitValues });
|
|
94
118
|
dataMap.forEach((value, key) => {
|
|
95
119
|
if (mapRef.value.has(key)) {
|
|
96
120
|
lodashEs.merge(mapRef.value.get(key), value);
|
|
@@ -100,28 +124,16 @@ function createDictManager(managerOptions = {}) {
|
|
|
100
124
|
mapRef.value = dataMap;
|
|
101
125
|
}
|
|
102
126
|
}
|
|
103
|
-
|
|
104
|
-
useDictOptions =
|
|
127
|
+
function useDict(useDictOptions) {
|
|
128
|
+
useDictOptions = Object.assign(
|
|
129
|
+
{ clone: false, immediate: true, refresh: false },
|
|
130
|
+
useDictOptions
|
|
131
|
+
);
|
|
105
132
|
const { clone, immediate, refresh } = useDictOptions;
|
|
106
133
|
const loadPromise = !clone ? globalLoadPromise : vue.shallowRef(createPromise());
|
|
107
134
|
const mapRef = !clone ? vue.toRef(maps, code) : vue.ref(/* @__PURE__ */ new Map());
|
|
108
|
-
const objRef = vue.ref(
|
|
135
|
+
const objRef = vue.ref(/* @__PURE__ */ Object.create(null));
|
|
109
136
|
const listRef = vue.ref([]);
|
|
110
|
-
vue.watch(
|
|
111
|
-
mapRef,
|
|
112
|
-
(newValue) => {
|
|
113
|
-
mapToObj(newValue, objRef.value);
|
|
114
|
-
mapToList(newValue, listRef.value);
|
|
115
|
-
},
|
|
116
|
-
{ deep: true, immediate: true }
|
|
117
|
-
);
|
|
118
|
-
const E = vue.computed(() => {
|
|
119
|
-
const result = {};
|
|
120
|
-
for (const key of mapRef.value.keys()) {
|
|
121
|
-
result[key] = key;
|
|
122
|
-
}
|
|
123
|
-
return result;
|
|
124
|
-
});
|
|
125
137
|
if (!remote || immediate) {
|
|
126
138
|
if (clone) {
|
|
127
139
|
load();
|
|
@@ -140,10 +152,10 @@ function createDictManager(managerOptions = {}) {
|
|
|
140
152
|
globalLoadPromise.value = createPromise();
|
|
141
153
|
}
|
|
142
154
|
}
|
|
143
|
-
function load(
|
|
155
|
+
function load(options2) {
|
|
144
156
|
const oldLoadPromise = loadPromise.value;
|
|
145
157
|
loadPromise.value = createPromise();
|
|
146
|
-
loadDict(
|
|
158
|
+
loadDict(Object.assign({}, useDictOptions, options2), mapRef).then(() => {
|
|
147
159
|
oldLoadPromise.resolve();
|
|
148
160
|
loadPromise.value.resolve();
|
|
149
161
|
});
|
|
@@ -152,6 +164,21 @@ function createDictManager(managerOptions = {}) {
|
|
|
152
164
|
function _clear() {
|
|
153
165
|
mapRef.value.clear();
|
|
154
166
|
}
|
|
167
|
+
vue.watch(
|
|
168
|
+
mapRef,
|
|
169
|
+
(newValue) => {
|
|
170
|
+
mapToObj(newValue, objRef.value);
|
|
171
|
+
mapToList(newValue, listRef.value);
|
|
172
|
+
},
|
|
173
|
+
{ deep: true, immediate: true }
|
|
174
|
+
);
|
|
175
|
+
const E = vue.computed(() => {
|
|
176
|
+
const result = {};
|
|
177
|
+
for (const key of mapRef.value.keys()) {
|
|
178
|
+
result[key] = key;
|
|
179
|
+
}
|
|
180
|
+
return result;
|
|
181
|
+
});
|
|
155
182
|
const ctx = {
|
|
156
183
|
map: objRef,
|
|
157
184
|
list: listRef,
|
|
@@ -166,8 +193,16 @@ function createDictManager(managerOptions = {}) {
|
|
|
166
193
|
...managerExtra == null ? void 0 : managerExtra(reactiveCtx),
|
|
167
194
|
...extra == null ? void 0 : extra(reactiveCtx)
|
|
168
195
|
});
|
|
196
|
+
}
|
|
197
|
+
const dictCode = code;
|
|
198
|
+
useDict.extend = (code2, extendOptions) => {
|
|
199
|
+
const { pickValues: pickValues2, omitValues: omitValues2 } = extendOptions != null ? extendOptions : {};
|
|
200
|
+
const options2 = defineDictOptionsMap.get(dictCode);
|
|
201
|
+
return _defineDict.bind(null, { pickValues: pickValues2, omitValues: omitValues2 }, code2, options2);
|
|
169
202
|
};
|
|
170
|
-
|
|
203
|
+
return useDict;
|
|
204
|
+
}
|
|
205
|
+
const defineDict = _defineDict.bind(null, {});
|
|
171
206
|
return { defineDict, clear };
|
|
172
207
|
}
|
|
173
208
|
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { reactive, shallowRef, toRef, ref, watch, computed } from 'vue';
|
|
2
|
-
import { isFunction,
|
|
2
|
+
import { isFunction, cloneDeep, merge, isArray, pick, omit, isUndefined } from 'lodash-es';
|
|
3
3
|
|
|
4
4
|
// src/dict-manager.ts
|
|
5
5
|
|
|
@@ -36,11 +36,12 @@ function mapToObj(map, obj = {}) {
|
|
|
36
36
|
}
|
|
37
37
|
function objToMap(obj) {
|
|
38
38
|
const map = /* @__PURE__ */ new Map();
|
|
39
|
-
for (const
|
|
40
|
-
|
|
41
|
-
|
|
39
|
+
for (const key of Object.keys(obj)) {
|
|
40
|
+
const item = obj[key];
|
|
41
|
+
if (isUndefined(item.value)) {
|
|
42
|
+
item.value = key;
|
|
42
43
|
}
|
|
43
|
-
map.set(key,
|
|
44
|
+
map.set(key, item);
|
|
44
45
|
}
|
|
45
46
|
return map;
|
|
46
47
|
}
|
|
@@ -56,39 +57,62 @@ function listToMap(list) {
|
|
|
56
57
|
}
|
|
57
58
|
return map;
|
|
58
59
|
}
|
|
59
|
-
function toMap(data
|
|
60
|
-
|
|
60
|
+
function toMap(data, {
|
|
61
|
+
pickValues,
|
|
62
|
+
omitValues
|
|
63
|
+
} = {}) {
|
|
64
|
+
if (isArray(data)) {
|
|
65
|
+
if (pickValues == null ? void 0 : pickValues.length) {
|
|
66
|
+
data = data.filter((item) => pickValues.includes(item.value));
|
|
67
|
+
}
|
|
68
|
+
if (omitValues == null ? void 0 : omitValues.length) {
|
|
69
|
+
data = data.filter((item) => !omitValues.includes(item.value));
|
|
70
|
+
}
|
|
71
|
+
return listToMap(data);
|
|
72
|
+
}
|
|
73
|
+
if (pickValues == null ? void 0 : pickValues.length) {
|
|
74
|
+
data = pick(data, ...pickValues);
|
|
75
|
+
}
|
|
76
|
+
if (omitValues == null ? void 0 : omitValues.length) {
|
|
77
|
+
data = omit(data, ...omitValues);
|
|
78
|
+
}
|
|
79
|
+
return objToMap(data);
|
|
61
80
|
}
|
|
62
81
|
var defineDictData = (data) => data;
|
|
63
82
|
|
|
64
83
|
// src/dict-manager.ts
|
|
84
|
+
var warn = (msg) => console.warn(`[v-dict]: ${msg}`);
|
|
65
85
|
function createDictManager(managerOptions = {}) {
|
|
66
86
|
const { fetch: managerFetch, extra: managerExtra } = managerOptions;
|
|
67
|
-
const maps = reactive(
|
|
87
|
+
const maps = reactive(/* @__PURE__ */ Object.create(null));
|
|
88
|
+
const defineDictOptionsMap = /* @__PURE__ */ new Map();
|
|
68
89
|
function clear(code) {
|
|
90
|
+
var _a;
|
|
69
91
|
if (code) {
|
|
70
|
-
|
|
71
|
-
dictMap && dictMap.clear();
|
|
92
|
+
(_a = maps[code]) == null ? void 0 : _a.clear();
|
|
72
93
|
return;
|
|
73
94
|
}
|
|
74
95
|
clearObj(maps);
|
|
75
96
|
}
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
97
|
+
function _defineDict(internalOptions, code, defineDictOptions) {
|
|
98
|
+
if (maps[code]) {
|
|
99
|
+
warn(`code "${code}" already exists`);
|
|
100
|
+
}
|
|
101
|
+
const { pickValues, omitValues } = internalOptions;
|
|
102
|
+
const options = Object.assign(
|
|
103
|
+
{ data: {}, remote: false, fetch: managerFetch },
|
|
104
|
+
isFunction(defineDictOptions) ? defineDictOptions() : defineDictOptions
|
|
105
|
+
);
|
|
106
|
+
defineDictOptionsMap.set(code, options);
|
|
107
|
+
const { data, remote, fetch, extra } = options;
|
|
84
108
|
const globalLoadPromise = shallowRef(null);
|
|
85
109
|
maps[code] = /* @__PURE__ */ new Map();
|
|
86
|
-
async function loadDict(
|
|
87
|
-
var
|
|
88
|
-
const dataMap = toMap(cloneDeep(data));
|
|
110
|
+
async function loadDict(options2, mapRef) {
|
|
111
|
+
var _a;
|
|
112
|
+
const dataMap = toMap(cloneDeep(data), { pickValues, omitValues });
|
|
89
113
|
if (remote) {
|
|
90
|
-
const res = (
|
|
91
|
-
mapRef.value = toMap(res);
|
|
114
|
+
const res = (_a = await (fetch == null ? void 0 : fetch(code, options2))) != null ? _a : [];
|
|
115
|
+
mapRef.value = toMap(res, { pickValues, omitValues });
|
|
92
116
|
dataMap.forEach((value, key) => {
|
|
93
117
|
if (mapRef.value.has(key)) {
|
|
94
118
|
merge(mapRef.value.get(key), value);
|
|
@@ -98,28 +122,16 @@ function createDictManager(managerOptions = {}) {
|
|
|
98
122
|
mapRef.value = dataMap;
|
|
99
123
|
}
|
|
100
124
|
}
|
|
101
|
-
|
|
102
|
-
useDictOptions =
|
|
125
|
+
function useDict(useDictOptions) {
|
|
126
|
+
useDictOptions = Object.assign(
|
|
127
|
+
{ clone: false, immediate: true, refresh: false },
|
|
128
|
+
useDictOptions
|
|
129
|
+
);
|
|
103
130
|
const { clone, immediate, refresh } = useDictOptions;
|
|
104
131
|
const loadPromise = !clone ? globalLoadPromise : shallowRef(createPromise());
|
|
105
132
|
const mapRef = !clone ? toRef(maps, code) : ref(/* @__PURE__ */ new Map());
|
|
106
|
-
const objRef = ref(
|
|
133
|
+
const objRef = ref(/* @__PURE__ */ Object.create(null));
|
|
107
134
|
const listRef = ref([]);
|
|
108
|
-
watch(
|
|
109
|
-
mapRef,
|
|
110
|
-
(newValue) => {
|
|
111
|
-
mapToObj(newValue, objRef.value);
|
|
112
|
-
mapToList(newValue, listRef.value);
|
|
113
|
-
},
|
|
114
|
-
{ deep: true, immediate: true }
|
|
115
|
-
);
|
|
116
|
-
const E = computed(() => {
|
|
117
|
-
const result = {};
|
|
118
|
-
for (const key of mapRef.value.keys()) {
|
|
119
|
-
result[key] = key;
|
|
120
|
-
}
|
|
121
|
-
return result;
|
|
122
|
-
});
|
|
123
135
|
if (!remote || immediate) {
|
|
124
136
|
if (clone) {
|
|
125
137
|
load();
|
|
@@ -138,10 +150,10 @@ function createDictManager(managerOptions = {}) {
|
|
|
138
150
|
globalLoadPromise.value = createPromise();
|
|
139
151
|
}
|
|
140
152
|
}
|
|
141
|
-
function load(
|
|
153
|
+
function load(options2) {
|
|
142
154
|
const oldLoadPromise = loadPromise.value;
|
|
143
155
|
loadPromise.value = createPromise();
|
|
144
|
-
loadDict(
|
|
156
|
+
loadDict(Object.assign({}, useDictOptions, options2), mapRef).then(() => {
|
|
145
157
|
oldLoadPromise.resolve();
|
|
146
158
|
loadPromise.value.resolve();
|
|
147
159
|
});
|
|
@@ -150,6 +162,21 @@ function createDictManager(managerOptions = {}) {
|
|
|
150
162
|
function _clear() {
|
|
151
163
|
mapRef.value.clear();
|
|
152
164
|
}
|
|
165
|
+
watch(
|
|
166
|
+
mapRef,
|
|
167
|
+
(newValue) => {
|
|
168
|
+
mapToObj(newValue, objRef.value);
|
|
169
|
+
mapToList(newValue, listRef.value);
|
|
170
|
+
},
|
|
171
|
+
{ deep: true, immediate: true }
|
|
172
|
+
);
|
|
173
|
+
const E = computed(() => {
|
|
174
|
+
const result = {};
|
|
175
|
+
for (const key of mapRef.value.keys()) {
|
|
176
|
+
result[key] = key;
|
|
177
|
+
}
|
|
178
|
+
return result;
|
|
179
|
+
});
|
|
153
180
|
const ctx = {
|
|
154
181
|
map: objRef,
|
|
155
182
|
list: listRef,
|
|
@@ -164,8 +191,16 @@ function createDictManager(managerOptions = {}) {
|
|
|
164
191
|
...managerExtra == null ? void 0 : managerExtra(reactiveCtx),
|
|
165
192
|
...extra == null ? void 0 : extra(reactiveCtx)
|
|
166
193
|
});
|
|
194
|
+
}
|
|
195
|
+
const dictCode = code;
|
|
196
|
+
useDict.extend = (code2, extendOptions) => {
|
|
197
|
+
const { pickValues: pickValues2, omitValues: omitValues2 } = extendOptions != null ? extendOptions : {};
|
|
198
|
+
const options2 = defineDictOptionsMap.get(dictCode);
|
|
199
|
+
return _defineDict.bind(null, { pickValues: pickValues2, omitValues: omitValues2 }, code2, options2);
|
|
167
200
|
};
|
|
168
|
-
|
|
201
|
+
return useDict;
|
|
202
|
+
}
|
|
203
|
+
const defineDict = _defineDict.bind(null, {});
|
|
169
204
|
return { defineDict, clear };
|
|
170
205
|
}
|
|
171
206
|
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import type { Merge, Simplify, ValueOf } from 'type-fest';
|
|
2
|
+
import type { createPromise } from '../create-promise';
|
|
3
|
+
import type { MergeUnionObject } from './util';
|
|
4
|
+
export type DictItem = {
|
|
5
|
+
label: string;
|
|
6
|
+
value: string;
|
|
7
|
+
};
|
|
8
|
+
export type DictItemRecord = {
|
|
9
|
+
label: string;
|
|
10
|
+
value: string;
|
|
11
|
+
[x: string]: any;
|
|
12
|
+
};
|
|
13
|
+
export type DictMap = Map<string, DictItemRecord>;
|
|
14
|
+
export type Fetch = (code: string, options?: any) => MaybePromise<DictItemRecord[]>;
|
|
15
|
+
export interface CreateDictManager<E extends ExtraGetter> {
|
|
16
|
+
fetch?: Fetch;
|
|
17
|
+
extra?: E;
|
|
18
|
+
}
|
|
19
|
+
export type ExtraGetter<D extends Dict<string> = Dict<string>> = (dict: D) => Recordable;
|
|
20
|
+
export type UseDictOptions = {
|
|
21
|
+
clone?: boolean;
|
|
22
|
+
immediate?: boolean;
|
|
23
|
+
refresh?: boolean;
|
|
24
|
+
[x: string]: any;
|
|
25
|
+
};
|
|
26
|
+
type ExtractFetchOptions<F extends Fetch> = Parameters<F>[1] extends infer T ? T extends undefined ? {} : T : never;
|
|
27
|
+
type ExtractFetchReturn<F extends Fetch> = UnwrapArray<Awaited<ReturnType<F>>> extends infer Item ? Item extends never ? {} : Item : never;
|
|
28
|
+
type _UseDict<ME extends ExtraGetter, E extends ExtraGetter, D extends Recordable, F extends Fetch> = (options?: Simplify<UseDictOptions & ExtractFetchOptions<F>>) => Simplify<CreateDict<D, F> & ReturnType<ME> & ReturnType<E>>;
|
|
29
|
+
type UseDict<ME extends ExtraGetter, E extends ExtraGetter, D extends Recordable, F extends Fetch> = _UseDict<ME, E, D, F> & {
|
|
30
|
+
extend: (code: string, extendOptions?: {
|
|
31
|
+
pickValues?: Simplify<keyof D>[];
|
|
32
|
+
omitValues?: Simplify<keyof D>[];
|
|
33
|
+
}) => UseDict<ME, E, D, F>;
|
|
34
|
+
};
|
|
35
|
+
export interface DefineDict<ME extends ExtraGetter> {
|
|
36
|
+
<R extends boolean, F extends Fetch, D extends Recordable<{
|
|
37
|
+
label: string;
|
|
38
|
+
[x: string]: any;
|
|
39
|
+
}>, E extends ExtraGetter>(code: string, options?: MaybeGetter<{
|
|
40
|
+
remote?: R;
|
|
41
|
+
fetch?: F;
|
|
42
|
+
data?: D;
|
|
43
|
+
extra?: E;
|
|
44
|
+
}>): UseDict<ME, E, D, F>;
|
|
45
|
+
}
|
|
46
|
+
type CreateDict<D extends Recordable<Recordable>, F extends Fetch> = Dict<keyof D, Merge<ExtractFetchReturn<F>, MergeUnionObject<ValueOf<D>>>, UseDictOptions & ExtractFetchOptions<F>>;
|
|
47
|
+
export type LoadPromise = ReturnType<typeof createPromise<void>>;
|
|
48
|
+
export type Dict<Key extends PropertyKey = PropertyKey, Item extends Recordable = DictItem, Options = Recordable> = {
|
|
49
|
+
list: Simplify<{
|
|
50
|
+
value: string;
|
|
51
|
+
} & Item>[];
|
|
52
|
+
E: {
|
|
53
|
+
[K in Key]: K;
|
|
54
|
+
};
|
|
55
|
+
map: {
|
|
56
|
+
[K in Key]: Simplify<{
|
|
57
|
+
value: string;
|
|
58
|
+
} & Item>;
|
|
59
|
+
};
|
|
60
|
+
loadPromise: LoadPromise;
|
|
61
|
+
load: (options?: Options) => LoadPromise;
|
|
62
|
+
clear: () => void;
|
|
63
|
+
};
|
|
64
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './dict';
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { Merge, Simplify } from 'type-fest';
|
|
2
|
+
type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (k: infer I) => void ? I : never;
|
|
3
|
+
type LastOf<T extends object> = UnionToIntersection<T extends any ? () => T : never> extends () => infer R ? R : never;
|
|
4
|
+
type Push<T extends any[], V> = [...T, V];
|
|
5
|
+
type UnionToTuple<T extends object, L = LastOf<T>, N = [T] extends [never] ? true : false> = true extends N ? [] : Push<UnionToTuple<Exclude<T, L>>, L>;
|
|
6
|
+
type MissingProperties<T, U> = Exclude<keyof T, keyof U>;
|
|
7
|
+
type CompareDataWithTarget<Data extends any[], Target> = {
|
|
8
|
+
[K in keyof Data]: MissingProperties<Target, Data[K]>;
|
|
9
|
+
};
|
|
10
|
+
type ExtractOptionalKey<T extends object> = CompareDataWithTarget<UnionToTuple<T>, UnionToIntersection<T>>[number];
|
|
11
|
+
export type MergeUnionObject<T extends object> = Simplify<Merge<UnionToIntersection<T>, Partial<Pick<UnionToIntersection<T>, ExtractOptionalKey<T>>>>>;
|
|
12
|
+
export {};
|
package/dist/util.d.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type { DictItemRecord, DictMap, Recordable } from './type';
|
|
1
|
+
import type { DictItemRecord, DictMap } from './types';
|
|
3
2
|
export declare function clearObj(obj: Recordable): void;
|
|
4
3
|
export declare function mapToObj(map: DictMap, obj?: Recordable<DictItemRecord>): Recordable<DictItemRecord>;
|
|
5
4
|
export declare function objToMap(obj: Recordable<DictItemRecord>): DictMap;
|
|
6
5
|
export declare function mapToList(map: DictMap, list?: DictItemRecord[]): DictItemRecord[];
|
|
7
6
|
export declare function listToMap(list: DictItemRecord[]): DictMap;
|
|
8
|
-
export declare function toMap(data: Recordable<DictItemRecord> | DictItemRecord[]
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
}
|
|
7
|
+
export declare function toMap(data: Recordable<DictItemRecord> | DictItemRecord[], { pickValues, omitValues }?: {
|
|
8
|
+
pickValues?: string[];
|
|
9
|
+
omitValues?: string[];
|
|
10
|
+
}): DictMap;
|
|
11
|
+
export declare const defineDictData: <T>(data: T) => T;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "v-dict",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Vue3 Dict Manager",
|
|
6
6
|
"repository": {
|
|
@@ -32,7 +32,6 @@
|
|
|
32
32
|
"dependencies": {
|
|
33
33
|
"lodash-es": "^4.17.21",
|
|
34
34
|
"type-fest": "^4.9.0",
|
|
35
|
-
"ts-toolbelt": "^9.6.0",
|
|
36
35
|
"@types/lodash-es": "^4.17.12"
|
|
37
36
|
},
|
|
38
37
|
"peerDependencies": {
|
|
@@ -42,7 +41,7 @@
|
|
|
42
41
|
"@changesets/cli": "^2.27.1",
|
|
43
42
|
"@types/node": "^20.11.0",
|
|
44
43
|
"tsup": "^8.0.1",
|
|
45
|
-
"typescript": "^5.
|
|
44
|
+
"typescript": "^5.4.0",
|
|
46
45
|
"vue": "^3.4.10"
|
|
47
46
|
},
|
|
48
47
|
"scripts": {
|
package/dist/type.d.ts
DELETED
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
import type { O } from 'ts-toolbelt';
|
|
2
|
-
import type { Simplify, ValueOf } from 'type-fest';
|
|
3
|
-
import type { createPromise } from './create-promise';
|
|
4
|
-
export type Recordable<T = any> = Record<string, T>;
|
|
5
|
-
export type Getter<T> = () => T;
|
|
6
|
-
export type MaybeGetter<T> = T | Getter<T>;
|
|
7
|
-
export type UnwrapArray<T> = T extends (infer TItem)[] ? TItem : T;
|
|
8
|
-
export type StringUnion<T> = T | (string & {});
|
|
9
|
-
export type DictItem = {
|
|
10
|
-
label: string;
|
|
11
|
-
value: string;
|
|
12
|
-
};
|
|
13
|
-
export type DictItemRecord = {
|
|
14
|
-
label: string;
|
|
15
|
-
value: string;
|
|
16
|
-
} & Recordable;
|
|
17
|
-
export type DictMap = Map<string, DictItemRecord>;
|
|
18
|
-
export type Fetch = (code: string, options?: any) => Promise<DictItemRecord[]>;
|
|
19
|
-
export interface CreateDictManager<E extends ExtraGetter> {
|
|
20
|
-
fetch?: Fetch;
|
|
21
|
-
extra?: E;
|
|
22
|
-
}
|
|
23
|
-
export type ExtraGetter<D extends Dict<string> = Dict<string>> = (dict: D) => Recordable;
|
|
24
|
-
export type UseDictOptions = {
|
|
25
|
-
clone?: boolean;
|
|
26
|
-
immediate?: boolean;
|
|
27
|
-
refresh?: boolean;
|
|
28
|
-
} & Recordable;
|
|
29
|
-
type ExtractFetchOptions<F extends Fetch> = Parameters<F>[1] extends infer T ? T extends undefined ? {} : T : never;
|
|
30
|
-
export interface DefineDict<Extra extends ExtraGetter> {
|
|
31
|
-
<R extends boolean, F extends Fetch, D extends Recordable<Recordable>, E extends ExtraGetter>(code: string, options?: MaybeGetter<{
|
|
32
|
-
remote?: R;
|
|
33
|
-
fetch?: F;
|
|
34
|
-
data?: D;
|
|
35
|
-
extra?: E;
|
|
36
|
-
}>): (options?: Simplify<UseDictOptions & ExtractFetchOptions<F>>) => Simplify<CreateDict<D, F> & ReturnType<Extra> & ReturnType<E>>;
|
|
37
|
-
}
|
|
38
|
-
type CreateDict<D extends Recordable<Recordable>, F extends Fetch> = Dict<keyof D, O.Merge<UnwrapArray<Awaited<ReturnType<F>>>, ValueOf<D>>, UseDictOptions & ExtractFetchOptions<F>>;
|
|
39
|
-
export type LoadPromise = ReturnType<typeof createPromise<void>>;
|
|
40
|
-
export type Dict<Key extends PropertyKey = PropertyKey, ExtraItem extends Recordable = Recordable, Options = Recordable> = {
|
|
41
|
-
list: Simplify<DictItem & ExtraItem>[];
|
|
42
|
-
E: {
|
|
43
|
-
[K in Key]: K;
|
|
44
|
-
};
|
|
45
|
-
map: {
|
|
46
|
-
[K in Key]: Simplify<DictItem & ExtraItem>;
|
|
47
|
-
} & Recordable<Simplify<DictItem & ExtraItem>>;
|
|
48
|
-
loadPromise: LoadPromise;
|
|
49
|
-
load: (options?: Options) => LoadPromise;
|
|
50
|
-
clear: () => void;
|
|
51
|
-
};
|
|
52
|
-
export {};
|