v-dict 1.2.6 → 1.2.7
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 +149 -149
- package/dist/dict-manager.d.ts +8 -1
- package/dist/index.cjs +15 -6
- package/dist/index.js +16 -7
- package/dist/types/dict.d.ts +6 -7
- package/dist/types/index.d.ts +1 -0
- package/dist/types/merge.d.ts +25 -9
- package/dist/types/tool.d.ts +11 -0
- package/dist/util.d.ts +4 -4
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,149 +1,149 @@
|
|
|
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
|
-
// clear dict data
|
|
72
|
-
// dm.clear('REMOTE_STATUS')
|
|
73
|
-
|
|
74
|
-
// clear all dict data
|
|
75
|
-
// dm.clear()
|
|
76
|
-
```
|
|
77
|
-
|
|
78
|
-
### xx.vue
|
|
79
|
-
|
|
80
|
-
```vue
|
|
81
|
-
<template>
|
|
82
|
-
<div>
|
|
83
|
-
{{ statusDict.E }}
|
|
84
|
-
{{ statusDict.map }}
|
|
85
|
-
{{ statusDict.list }}
|
|
86
|
-
{{ statusDict.getLabel(statusDict.E.ENABLED) }}
|
|
87
|
-
{{ statusDict.getItem(statusDict.E.DISABLED) }}
|
|
88
|
-
</div>
|
|
89
|
-
</template>
|
|
90
|
-
|
|
91
|
-
<script setup lang="ts">
|
|
92
|
-
import { useRemoteStatusDict } from './dict'
|
|
93
|
-
import { onMounted } from 'vue'
|
|
94
|
-
|
|
95
|
-
const statusDict = useRemoteStatusDict({
|
|
96
|
-
// Data sharing by default, independent data source when clone is true
|
|
97
|
-
clone: true,
|
|
98
|
-
// Whether the remote dictionary loads data immediately
|
|
99
|
-
immediate: false,
|
|
100
|
-
// whether to reload
|
|
101
|
-
refresh: false
|
|
102
|
-
}) // statusDict is reactive!!!
|
|
103
|
-
|
|
104
|
-
const { E, map, list } = statusDict
|
|
105
|
-
|
|
106
|
-
/*
|
|
107
|
-
E: {
|
|
108
|
-
ENABLED: 'ENABLED',
|
|
109
|
-
DISABLED: 'DISABLED'
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
map: {
|
|
113
|
-
ENABLED: {
|
|
114
|
-
label: 'Enabled',
|
|
115
|
-
value: 'ENABLED',
|
|
116
|
-
color: 'green'
|
|
117
|
-
},
|
|
118
|
-
DISABLED: {
|
|
119
|
-
label: 'Disabled',
|
|
120
|
-
value: 'DISABLED',
|
|
121
|
-
color: 'red'
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
list: [
|
|
126
|
-
{
|
|
127
|
-
label: 'Enabled',
|
|
128
|
-
value: 'ENABLED',
|
|
129
|
-
color: 'green'
|
|
130
|
-
},
|
|
131
|
-
{
|
|
132
|
-
label: 'Disabled',
|
|
133
|
-
value: 'DISABLED',
|
|
134
|
-
color: 'red'
|
|
135
|
-
}
|
|
136
|
-
]
|
|
137
|
-
*/
|
|
138
|
-
|
|
139
|
-
onMounted(async () => {
|
|
140
|
-
await statusDict.load()
|
|
141
|
-
|
|
142
|
-
await statusDict.loadPromise // immediate = true, using loadPromise to wait load
|
|
143
|
-
// do after dict load
|
|
144
|
-
console.log(statusDict.list)
|
|
145
|
-
// clear dict data
|
|
146
|
-
// statusDict.clear()
|
|
147
|
-
})
|
|
148
|
-
</script>
|
|
149
|
-
```
|
|
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
|
+
// clear dict data
|
|
72
|
+
// dm.clear('REMOTE_STATUS')
|
|
73
|
+
|
|
74
|
+
// clear all dict data
|
|
75
|
+
// dm.clear()
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
### xx.vue
|
|
79
|
+
|
|
80
|
+
```vue
|
|
81
|
+
<template>
|
|
82
|
+
<div>
|
|
83
|
+
{{ statusDict.E }}
|
|
84
|
+
{{ statusDict.map }}
|
|
85
|
+
{{ statusDict.list }}
|
|
86
|
+
{{ statusDict.getLabel(statusDict.E.ENABLED) }}
|
|
87
|
+
{{ statusDict.getItem(statusDict.E.DISABLED) }}
|
|
88
|
+
</div>
|
|
89
|
+
</template>
|
|
90
|
+
|
|
91
|
+
<script setup lang="ts">
|
|
92
|
+
import { useRemoteStatusDict } from './dict'
|
|
93
|
+
import { onMounted } from 'vue'
|
|
94
|
+
|
|
95
|
+
const statusDict = useRemoteStatusDict({
|
|
96
|
+
// Data sharing by default, independent data source when clone is true
|
|
97
|
+
clone: true,
|
|
98
|
+
// Whether the remote dictionary loads data immediately
|
|
99
|
+
immediate: false,
|
|
100
|
+
// whether to reload
|
|
101
|
+
refresh: false
|
|
102
|
+
}) // statusDict is reactive!!!
|
|
103
|
+
|
|
104
|
+
const { E, map, list } = statusDict
|
|
105
|
+
|
|
106
|
+
/*
|
|
107
|
+
E: {
|
|
108
|
+
ENABLED: 'ENABLED',
|
|
109
|
+
DISABLED: 'DISABLED'
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
map: {
|
|
113
|
+
ENABLED: {
|
|
114
|
+
label: 'Enabled',
|
|
115
|
+
value: 'ENABLED',
|
|
116
|
+
color: 'green'
|
|
117
|
+
},
|
|
118
|
+
DISABLED: {
|
|
119
|
+
label: 'Disabled',
|
|
120
|
+
value: 'DISABLED',
|
|
121
|
+
color: 'red'
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
list: [
|
|
126
|
+
{
|
|
127
|
+
label: 'Enabled',
|
|
128
|
+
value: 'ENABLED',
|
|
129
|
+
color: 'green'
|
|
130
|
+
},
|
|
131
|
+
{
|
|
132
|
+
label: 'Disabled',
|
|
133
|
+
value: 'DISABLED',
|
|
134
|
+
color: 'red'
|
|
135
|
+
}
|
|
136
|
+
]
|
|
137
|
+
*/
|
|
138
|
+
|
|
139
|
+
onMounted(async () => {
|
|
140
|
+
await statusDict.load()
|
|
141
|
+
|
|
142
|
+
await statusDict.loadPromise // immediate = true, using loadPromise to wait load
|
|
143
|
+
// do after dict load
|
|
144
|
+
console.log(statusDict.list)
|
|
145
|
+
// clear dict data
|
|
146
|
+
// statusDict.clear()
|
|
147
|
+
})
|
|
148
|
+
</script>
|
|
149
|
+
```
|
package/dist/dict-manager.d.ts
CHANGED
|
@@ -1,5 +1,12 @@
|
|
|
1
|
-
import type { CreateDictManagerOptions, DefineDict, ExtraGetter, Fetch } from './types';
|
|
1
|
+
import type { CreateDictManagerOptions, DefineDict, DictValue, ExtraGetter, Fetch } from './types';
|
|
2
2
|
export declare function createDictManager<E extends ExtraGetter, F extends Fetch>(createDictManagerOptions?: CreateDictManagerOptions<E, F>): {
|
|
3
3
|
defineDict: DefineDict<E, F>;
|
|
4
4
|
clear: (code?: string) => void;
|
|
5
|
+
maps: {
|
|
6
|
+
readonly [x: string]: ReadonlyMap<DictValue, {
|
|
7
|
+
readonly [x: string]: any;
|
|
8
|
+
readonly label: string;
|
|
9
|
+
readonly value: DictValue;
|
|
10
|
+
}>;
|
|
11
|
+
};
|
|
5
12
|
};
|
package/dist/index.cjs
CHANGED
|
@@ -40,9 +40,13 @@ function mapToObj(map, obj = {}) {
|
|
|
40
40
|
}
|
|
41
41
|
return obj;
|
|
42
42
|
}
|
|
43
|
-
function checkObjItem(item, key) {
|
|
44
|
-
if (item.value === void 0)
|
|
43
|
+
function checkObjItem(item, key, transformer) {
|
|
44
|
+
if (item.value === void 0) {
|
|
45
45
|
item.value = key;
|
|
46
|
+
}
|
|
47
|
+
if (isFunction(transformer)) {
|
|
48
|
+
item.value = transformer(item.value);
|
|
49
|
+
}
|
|
46
50
|
}
|
|
47
51
|
function mapToList(map, list = []) {
|
|
48
52
|
list.splice(0, list.length, ...map.values());
|
|
@@ -174,7 +178,7 @@ function createDictManager(createDictManagerOptions = {}) {
|
|
|
174
178
|
);
|
|
175
179
|
const { clone, immediate, refresh } = useDictOptions;
|
|
176
180
|
const loadPromise = !clone ? globalLoadPromise : vue.shallowRef(createPromise());
|
|
177
|
-
const mapRef = !clone ? vue.toRef(maps, code) : vue.ref(
|
|
181
|
+
const mapRef = !clone ? vue.toRef(maps, code) : vue.ref();
|
|
178
182
|
const objRef = vue.ref(/* @__PURE__ */ Object.create(null));
|
|
179
183
|
const listRef = vue.ref([]);
|
|
180
184
|
if (!remote || immediate) {
|
|
@@ -205,11 +209,13 @@ function createDictManager(createDictManagerOptions = {}) {
|
|
|
205
209
|
return loadPromise.value;
|
|
206
210
|
}
|
|
207
211
|
function _clear() {
|
|
208
|
-
|
|
212
|
+
var _a;
|
|
213
|
+
(_a = mapRef.value) == null ? void 0 : _a.clear();
|
|
209
214
|
}
|
|
210
215
|
vue.watch(
|
|
211
216
|
mapRef,
|
|
212
217
|
(newValue) => {
|
|
218
|
+
newValue != null ? newValue : newValue = /* @__PURE__ */ new Map();
|
|
213
219
|
mapToObj(newValue, objRef.value);
|
|
214
220
|
mapToList(newValue, listRef.value);
|
|
215
221
|
},
|
|
@@ -217,13 +223,16 @@ function createDictManager(createDictManagerOptions = {}) {
|
|
|
217
223
|
);
|
|
218
224
|
const E = vue.computed(() => {
|
|
219
225
|
const result = {};
|
|
226
|
+
if (!mapRef.value)
|
|
227
|
+
return result;
|
|
220
228
|
for (const key of mapRef.value.keys()) {
|
|
221
229
|
result[key] = key;
|
|
222
230
|
}
|
|
223
231
|
return result;
|
|
224
232
|
});
|
|
225
233
|
function getItem(value) {
|
|
226
|
-
|
|
234
|
+
var _a;
|
|
235
|
+
return value !== null && value !== void 0 ? (_a = mapRef.value) == null ? void 0 : _a.get(value) : null;
|
|
227
236
|
}
|
|
228
237
|
const ctx = {
|
|
229
238
|
map: objRef,
|
|
@@ -249,7 +258,7 @@ function createDictManager(createDictManagerOptions = {}) {
|
|
|
249
258
|
return useDict;
|
|
250
259
|
}
|
|
251
260
|
const defineDict = _defineDict.bind(null, {});
|
|
252
|
-
return { defineDict, clear };
|
|
261
|
+
return { defineDict, clear, maps: vue.readonly(maps) };
|
|
253
262
|
}
|
|
254
263
|
|
|
255
264
|
exports.createDictManager = createDictManager;
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { reactive, shallowRef, toRef, ref, watch, computed } from 'vue';
|
|
1
|
+
import { reactive, readonly, shallowRef, toRef, ref, watch, computed } from 'vue';
|
|
2
2
|
|
|
3
3
|
// src/dict-manager.ts
|
|
4
4
|
|
|
@@ -38,9 +38,13 @@ function mapToObj(map, obj = {}) {
|
|
|
38
38
|
}
|
|
39
39
|
return obj;
|
|
40
40
|
}
|
|
41
|
-
function checkObjItem(item, key) {
|
|
42
|
-
if (item.value === void 0)
|
|
41
|
+
function checkObjItem(item, key, transformer) {
|
|
42
|
+
if (item.value === void 0) {
|
|
43
43
|
item.value = key;
|
|
44
|
+
}
|
|
45
|
+
if (isFunction(transformer)) {
|
|
46
|
+
item.value = transformer(item.value);
|
|
47
|
+
}
|
|
44
48
|
}
|
|
45
49
|
function mapToList(map, list = []) {
|
|
46
50
|
list.splice(0, list.length, ...map.values());
|
|
@@ -172,7 +176,7 @@ function createDictManager(createDictManagerOptions = {}) {
|
|
|
172
176
|
);
|
|
173
177
|
const { clone, immediate, refresh } = useDictOptions;
|
|
174
178
|
const loadPromise = !clone ? globalLoadPromise : shallowRef(createPromise());
|
|
175
|
-
const mapRef = !clone ? toRef(maps, code) : ref(
|
|
179
|
+
const mapRef = !clone ? toRef(maps, code) : ref();
|
|
176
180
|
const objRef = ref(/* @__PURE__ */ Object.create(null));
|
|
177
181
|
const listRef = ref([]);
|
|
178
182
|
if (!remote || immediate) {
|
|
@@ -203,11 +207,13 @@ function createDictManager(createDictManagerOptions = {}) {
|
|
|
203
207
|
return loadPromise.value;
|
|
204
208
|
}
|
|
205
209
|
function _clear() {
|
|
206
|
-
|
|
210
|
+
var _a;
|
|
211
|
+
(_a = mapRef.value) == null ? void 0 : _a.clear();
|
|
207
212
|
}
|
|
208
213
|
watch(
|
|
209
214
|
mapRef,
|
|
210
215
|
(newValue) => {
|
|
216
|
+
newValue != null ? newValue : newValue = /* @__PURE__ */ new Map();
|
|
211
217
|
mapToObj(newValue, objRef.value);
|
|
212
218
|
mapToList(newValue, listRef.value);
|
|
213
219
|
},
|
|
@@ -215,13 +221,16 @@ function createDictManager(createDictManagerOptions = {}) {
|
|
|
215
221
|
);
|
|
216
222
|
const E = computed(() => {
|
|
217
223
|
const result = {};
|
|
224
|
+
if (!mapRef.value)
|
|
225
|
+
return result;
|
|
218
226
|
for (const key of mapRef.value.keys()) {
|
|
219
227
|
result[key] = key;
|
|
220
228
|
}
|
|
221
229
|
return result;
|
|
222
230
|
});
|
|
223
231
|
function getItem(value) {
|
|
224
|
-
|
|
232
|
+
var _a;
|
|
233
|
+
return value !== null && value !== void 0 ? (_a = mapRef.value) == null ? void 0 : _a.get(value) : null;
|
|
225
234
|
}
|
|
226
235
|
const ctx = {
|
|
227
236
|
map: objRef,
|
|
@@ -247,7 +256,7 @@ function createDictManager(createDictManagerOptions = {}) {
|
|
|
247
256
|
return useDict;
|
|
248
257
|
}
|
|
249
258
|
const defineDict = _defineDict.bind(null, {});
|
|
250
|
-
return { defineDict, clear };
|
|
259
|
+
return { defineDict, clear, maps: readonly(maps) };
|
|
251
260
|
}
|
|
252
261
|
|
|
253
262
|
export { createDictManager, defineDictData };
|
package/dist/types/dict.d.ts
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
import type { createPromise } from '../create-promise';
|
|
2
2
|
import type { Merge, MergeValues } from './merge';
|
|
3
|
+
import type { AnyFn, MaybeGetter, MaybePromise, Nil, OptionalRequired, Recordable, Simplify } from './tool';
|
|
4
|
+
export type DictValue = number | string;
|
|
3
5
|
export type DictItem = {
|
|
4
6
|
label: string;
|
|
5
|
-
value:
|
|
7
|
+
value: DictValue;
|
|
6
8
|
};
|
|
7
9
|
export type DictItemRecord = DictItem & Recordable;
|
|
8
|
-
export type DictMap = Map<
|
|
10
|
+
export type DictMap = Map<DictValue, DictItemRecord>;
|
|
9
11
|
export type LoadPromise = ReturnType<typeof createPromise<void>>;
|
|
10
12
|
export type Dict<K extends PropertyKey = PropertyKey, I extends Recordable = DictItem, O extends Recordable = Recordable> = {
|
|
11
13
|
list: I[];
|
|
@@ -22,7 +24,6 @@ export type Dict<K extends PropertyKey = PropertyKey, I extends Recordable = Dic
|
|
|
22
24
|
};
|
|
23
25
|
export type Fetch = (code: string, options?: Recordable) => MaybePromise<DictItemRecord[]>;
|
|
24
26
|
type FetchOptions<F extends Fetch> = Parameters<F>[1] extends infer T ? T extends Nil ? {} : T : {};
|
|
25
|
-
type FetchReturnItem<F extends Fetch> = UnwrapArray<Awaited<ReturnType<F>>> extends infer Item ? If<never, Item, {}> : {};
|
|
26
27
|
export type ExtraGetter<D extends Dict<string> = Dict<string>> = (dict: D) => Recordable;
|
|
27
28
|
export interface CreateDictManagerOptions<E extends ExtraGetter, F extends Fetch> {
|
|
28
29
|
fetch?: F;
|
|
@@ -33,10 +34,8 @@ export type UseDictOptions = {
|
|
|
33
34
|
immediate?: boolean;
|
|
34
35
|
refresh?: boolean;
|
|
35
36
|
} & Recordable;
|
|
36
|
-
type Options<F extends Fetch> = FetchOptions<F> &
|
|
37
|
-
|
|
38
|
-
};
|
|
39
|
-
type CreateDict<D extends Recordable<Recordable>, F extends Fetch> = Dict<keyof D, Simplify<Merge<[DictItem, FetchReturnItem<F>, MergeValues<D>]> extends infer Item ? Item extends never ? DictItem : Item extends Recordable ? OptionalRequired<Item, 'label' | 'value'> : DictItem : DictItem>, Simplify<Options<F>>>;
|
|
37
|
+
type Options<F extends Fetch> = FetchOptions<F> & UseDictOptions;
|
|
38
|
+
type CreateDict<D extends Recordable<Recordable>, F extends Fetch> = Dict<keyof D, Simplify<Merge<[DictItem, MergeValues<D>]> extends infer Item ? Item extends never ? DictItem : Item extends Recordable ? OptionalRequired<Item, 'label' | 'value'> : DictItem : DictItem>, Simplify<Options<F>>>;
|
|
40
39
|
type _UseDict<E extends Recordable, D extends Recordable<Recordable>, F extends Fetch> = (options?: Simplify<Options<F>>) => CreateDict<D, F> & E;
|
|
41
40
|
export type UseDict<E extends Recordable, D extends Recordable<Recordable>, F extends Fetch> = _UseDict<E, D, F> & {
|
|
42
41
|
extend: (extendCode: string, extendOptions?: {
|
package/dist/types/index.d.ts
CHANGED
package/dist/types/merge.d.ts
CHANGED
|
@@ -1,20 +1,36 @@
|
|
|
1
|
-
type
|
|
2
|
-
type
|
|
3
|
-
type
|
|
1
|
+
type TupleUnionKeys<T> = T extends any ? keyof T : never;
|
|
2
|
+
type TupleAllKeys<T extends any[]> = TupleUnionKeys<T[number]>;
|
|
3
|
+
type TupleGetUnionType<T extends any[], K extends PropertyKey> = T extends [
|
|
4
|
+
infer First,
|
|
5
|
+
...infer Rest
|
|
6
|
+
] ? (K extends keyof First ? First[K] : undefined) | TupleGetUnionType<Rest, K> : never;
|
|
4
7
|
type Widen<T> = T extends string ? string : T extends number ? number : T extends boolean ? boolean : T;
|
|
5
8
|
type IsRequiredProperty<T, K extends keyof T> = {} extends Pick<T, K> ? false : true;
|
|
6
9
|
type IsAllRequired<T extends any[], K> = T extends [infer First, ...infer Rest] ? (K extends keyof First ? IsRequiredProperty<First, K> : false) extends true ? IsAllRequired<Rest, K> : false : true;
|
|
7
|
-
type
|
|
8
|
-
[K in
|
|
9
|
-
}[
|
|
10
|
+
type TupleRequiredKeys<T extends any[]> = {
|
|
11
|
+
[K in TupleAllKeys<T>]: IsAllRequired<T, K> extends true ? K : never;
|
|
12
|
+
}[TupleAllKeys<T>];
|
|
10
13
|
export type Merge<T extends any[]> = {
|
|
11
|
-
[K in Exclude<
|
|
14
|
+
[K in Exclude<TupleAllKeys<T>, TupleRequiredKeys<T>>]?: Widen<TupleGetUnionType<T, K>>;
|
|
15
|
+
} & {
|
|
16
|
+
[K in TupleRequiredKeys<T>]: Widen<TupleGetUnionType<T, K>>;
|
|
17
|
+
} extends infer O ? {
|
|
18
|
+
[P in keyof O]: O[P];
|
|
19
|
+
} : never;
|
|
20
|
+
type UnionKeys<T> = T extends any ? keyof T : never;
|
|
21
|
+
type UnionValueOf<T, K extends PropertyKey> = T extends any ? K extends keyof T ? T[K] : undefined : never;
|
|
22
|
+
type UnionIsAlwaysPresent<T, K extends PropertyKey> = [T] extends [infer U] ? U extends any ? K extends keyof U ? true : false : never : never;
|
|
23
|
+
type UnionRequiredKeys<T> = {
|
|
24
|
+
[K in UnionKeys<T>]: UnionIsAlwaysPresent<T, K> extends true ? K : never;
|
|
25
|
+
}[UnionKeys<T>];
|
|
26
|
+
type UnionOptionalKeys<T> = Exclude<UnionKeys<T>, UnionRequiredKeys<T>>;
|
|
27
|
+
export type MergeUnion<T> = {
|
|
28
|
+
[K in UnionOptionalKeys<T>]?: Widen<UnionValueOf<T, K>>;
|
|
12
29
|
} & {
|
|
13
|
-
[K in
|
|
30
|
+
[K in UnionRequiredKeys<T>]: Widen<UnionValueOf<T, K>>;
|
|
14
31
|
} extends infer O ? {
|
|
15
32
|
[P in keyof O]: O[P];
|
|
16
33
|
} : never;
|
|
17
|
-
export type MergeUnion<T> = Merge<[T]>;
|
|
18
34
|
export type MergeValues<T> = MergeUnion<{
|
|
19
35
|
[K in keyof T]: T[K];
|
|
20
36
|
}[keyof T]>;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export type Recordable<T = any> = Record<string, T>;
|
|
2
|
+
export type Nil = undefined | null;
|
|
3
|
+
export type MaybePromise<T> = T | Promise<T>;
|
|
4
|
+
export type Simplify<T> = {
|
|
5
|
+
[KeyType in keyof T]: T[KeyType];
|
|
6
|
+
} & {};
|
|
7
|
+
export type OptionalRequired<T, K extends keyof T> = Partial<T> & Required<Pick<T, K>>;
|
|
8
|
+
export type Getter<T> = () => T;
|
|
9
|
+
export type MaybeGetter<T> = T | Getter<T>;
|
|
10
|
+
export type AnyFn<Return = any, Args extends unknown[] = any[]> = (...args: Args) => Return;
|
|
11
|
+
export type PlainObject<T = any> = Record<PropertyKey, any>;
|
package/dist/util.d.ts
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
import type { DictItemRecord, DictMap, Merge } from './types';
|
|
1
|
+
import type { AnyFn, DictItemRecord, DictMap, DictValue, Merge, PlainObject, Recordable } from './types';
|
|
2
2
|
export declare function isFunction(fn: unknown): fn is AnyFn;
|
|
3
3
|
export declare function clearObj(obj: Recordable): void;
|
|
4
4
|
export declare function mapToObj(map: DictMap, obj?: Recordable<DictItemRecord>): Recordable<DictItemRecord>;
|
|
5
5
|
export declare function objToMap(obj: Recordable<DictItemRecord>): DictMap;
|
|
6
6
|
export declare function mapToList(map: DictMap, list?: DictItemRecord[]): DictItemRecord[];
|
|
7
|
-
export declare function listToMap(list: DictItemRecord[]): Map<
|
|
7
|
+
export declare function listToMap(list: DictItemRecord[]): Map<DictValue, DictItemRecord>;
|
|
8
8
|
type MapOptions = {
|
|
9
|
-
pickValues?:
|
|
10
|
-
omitValues?:
|
|
9
|
+
pickValues?: DictValue[];
|
|
10
|
+
omitValues?: DictValue[];
|
|
11
11
|
};
|
|
12
12
|
export declare function toMap(data: Recordable<DictItemRecord> | DictItemRecord[], options?: MapOptions): DictMap;
|
|
13
13
|
export declare const defineDictData: <T>(data: T) => T;
|