v-dict 1.0.0 → 1.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.
- package/dist/create-promise.d.ts +7 -0
- package/dist/dict-manager.d.ts +1 -1
- package/dist/index.cjs +40 -36
- package/dist/index.js +40 -36
- package/dist/type.d.ts +15 -12
- package/dist/util.d.ts +2 -15
- package/package.json +5 -2
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export type Resolve<T> = (value: T | PromiseLike<T>) => void;
|
|
2
|
+
export type Reject = (reason?: any) => void;
|
|
3
|
+
export type CreatePromiseReturn<T> = Promise<T> & {
|
|
4
|
+
resolve: Resolve<T>;
|
|
5
|
+
reject: Reject;
|
|
6
|
+
};
|
|
7
|
+
export declare function createPromise<T = any>(executor?: (resolve: Resolve<T>, reject: Reject) => void): CreatePromiseReturn<T>;
|
package/dist/dict-manager.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import type { CreateDictManager, DefineDict, ExtraGetter } from './type';
|
|
2
|
-
export declare function createDictManager<E extends ExtraGetter>(
|
|
2
|
+
export declare function createDictManager<E extends ExtraGetter>(managerOptions?: CreateDictManager<E>): {
|
|
3
3
|
defineDict: DefineDict<E>;
|
|
4
4
|
};
|
package/dist/index.cjs
CHANGED
|
@@ -4,6 +4,22 @@ var vue = require('vue');
|
|
|
4
4
|
var lodashEs = require('lodash-es');
|
|
5
5
|
|
|
6
6
|
// src/dict-manager.ts
|
|
7
|
+
|
|
8
|
+
// src/create-promise.ts
|
|
9
|
+
var noop = () => {
|
|
10
|
+
};
|
|
11
|
+
function createPromise(executor) {
|
|
12
|
+
let resolve = noop;
|
|
13
|
+
let reject = noop;
|
|
14
|
+
const promise = new Promise((_resolve, _reject) => {
|
|
15
|
+
resolve = _resolve;
|
|
16
|
+
reject = _reject;
|
|
17
|
+
executor == null ? void 0 : executor(_resolve, _reject);
|
|
18
|
+
});
|
|
19
|
+
promise.resolve = resolve;
|
|
20
|
+
promise.reject = reject;
|
|
21
|
+
return promise;
|
|
22
|
+
}
|
|
7
23
|
var clearObj = (obj) => {
|
|
8
24
|
for (const key of Object.keys(obj)) {
|
|
9
25
|
delete obj[key];
|
|
@@ -41,40 +57,26 @@ function listToMap(list) {
|
|
|
41
57
|
function toMap(data) {
|
|
42
58
|
return lodashEs.isArray(data) ? listToMap(data) : objToMap(data);
|
|
43
59
|
}
|
|
44
|
-
var noop = () => {
|
|
45
|
-
};
|
|
46
|
-
function createPromise(executor) {
|
|
47
|
-
let resolve = noop;
|
|
48
|
-
let reject = noop;
|
|
49
|
-
const promise = new Promise((_resolve, _reject) => {
|
|
50
|
-
resolve = _resolve;
|
|
51
|
-
reject = _reject;
|
|
52
|
-
executor == null ? void 0 : executor(_resolve, _reject);
|
|
53
|
-
});
|
|
54
|
-
promise.resolve = resolve;
|
|
55
|
-
promise.reject = reject;
|
|
56
|
-
return promise;
|
|
57
|
-
}
|
|
58
60
|
var defineDictData = (data) => data;
|
|
59
61
|
|
|
60
62
|
// src/dict-manager.ts
|
|
61
|
-
function createDictManager(
|
|
62
|
-
const { fetch: managerFetch, extra: managerExtra } =
|
|
63
|
+
function createDictManager(managerOptions = {}) {
|
|
64
|
+
const { fetch: managerFetch, extra: managerExtra } = managerOptions;
|
|
63
65
|
const maps = vue.reactive({});
|
|
64
|
-
const defineDict = (code,
|
|
66
|
+
const defineDict = (code, defineDictOptions) => {
|
|
65
67
|
var _a;
|
|
66
68
|
const {
|
|
67
|
-
data,
|
|
69
|
+
data = {},
|
|
68
70
|
remote = false,
|
|
69
71
|
fetch = managerFetch,
|
|
70
72
|
extra
|
|
71
|
-
} = (_a = lodashEs.isFunction(
|
|
72
|
-
const managerLoadPromise = vue.shallowRef(
|
|
73
|
+
} = (_a = lodashEs.isFunction(defineDictOptions) ? defineDictOptions() : defineDictOptions) != null ? _a : {};
|
|
74
|
+
const managerLoadPromise = vue.shallowRef(createPromise());
|
|
73
75
|
maps[code] = /* @__PURE__ */ new Map();
|
|
74
|
-
async function loadDict(
|
|
75
|
-
const dataMap = toMap(lodashEs.cloneDeep(data
|
|
76
|
+
async function loadDict(options, mapRef) {
|
|
77
|
+
const dataMap = toMap(lodashEs.cloneDeep(data));
|
|
76
78
|
if (remote) {
|
|
77
|
-
await (fetch == null ? void 0 : fetch(code,
|
|
79
|
+
await (fetch == null ? void 0 : fetch(code, options).then((res) => {
|
|
78
80
|
mapRef.value = toMap(res != null ? res : []);
|
|
79
81
|
dataMap.forEach((value, key) => {
|
|
80
82
|
if (mapRef.value.has(key)) {
|
|
@@ -89,7 +91,7 @@ function createDictManager(options) {
|
|
|
89
91
|
return (useDictOptions) => {
|
|
90
92
|
useDictOptions = lodashEs.merge({ clone: false, immediate: false }, useDictOptions);
|
|
91
93
|
const { clone, immediate } = useDictOptions;
|
|
92
|
-
const loadPromise = !clone ? managerLoadPromise : vue.shallowRef(
|
|
94
|
+
const loadPromise = !clone ? managerLoadPromise : vue.shallowRef(createPromise());
|
|
93
95
|
const mapRef = !clone ? vue.toRef(maps, code) : vue.ref(/* @__PURE__ */ new Map());
|
|
94
96
|
const objRef = vue.ref({});
|
|
95
97
|
const listRef = vue.ref([]);
|
|
@@ -101,20 +103,21 @@ function createDictManager(options) {
|
|
|
101
103
|
},
|
|
102
104
|
{ deep: true, immediate: true }
|
|
103
105
|
);
|
|
104
|
-
const E = vue.computed(
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
}
|
|
109
|
-
|
|
106
|
+
const E = vue.computed(() => {
|
|
107
|
+
const result = {};
|
|
108
|
+
for (const key of mapRef.value.keys()) {
|
|
109
|
+
result[key] = key;
|
|
110
|
+
}
|
|
111
|
+
return result;
|
|
112
|
+
});
|
|
110
113
|
if (remote) {
|
|
111
114
|
immediate && load();
|
|
112
115
|
} else {
|
|
113
116
|
load();
|
|
114
117
|
}
|
|
115
|
-
function load(
|
|
118
|
+
function load(options) {
|
|
116
119
|
loadPromise.value = createPromise();
|
|
117
|
-
loadDict(lodashEs.merge({}, useDictOptions,
|
|
120
|
+
loadDict(lodashEs.merge({}, useDictOptions, options), mapRef).then(() => {
|
|
118
121
|
var _a2;
|
|
119
122
|
(_a2 = loadPromise.value) == null ? void 0 : _a2.resolve();
|
|
120
123
|
});
|
|
@@ -124,13 +127,14 @@ function createDictManager(options) {
|
|
|
124
127
|
map: objRef,
|
|
125
128
|
list: listRef,
|
|
126
129
|
E,
|
|
127
|
-
loadPromise
|
|
130
|
+
loadPromise,
|
|
131
|
+
load
|
|
128
132
|
};
|
|
133
|
+
const reactiveCtx = vue.reactive(ctx);
|
|
129
134
|
return vue.reactive({
|
|
130
135
|
...ctx,
|
|
131
|
-
|
|
132
|
-
...
|
|
133
|
-
...extra == null ? void 0 : extra(vue.reactive(ctx))
|
|
136
|
+
...managerExtra == null ? void 0 : managerExtra(reactiveCtx),
|
|
137
|
+
...extra == null ? void 0 : extra(reactiveCtx)
|
|
134
138
|
});
|
|
135
139
|
};
|
|
136
140
|
};
|
package/dist/index.js
CHANGED
|
@@ -2,6 +2,22 @@ import { reactive, shallowRef, toRef, ref, watch, computed } from 'vue';
|
|
|
2
2
|
import { isFunction, merge, cloneDeep, isArray, isUndefined } from 'lodash-es';
|
|
3
3
|
|
|
4
4
|
// src/dict-manager.ts
|
|
5
|
+
|
|
6
|
+
// src/create-promise.ts
|
|
7
|
+
var noop = () => {
|
|
8
|
+
};
|
|
9
|
+
function createPromise(executor) {
|
|
10
|
+
let resolve = noop;
|
|
11
|
+
let reject = noop;
|
|
12
|
+
const promise = new Promise((_resolve, _reject) => {
|
|
13
|
+
resolve = _resolve;
|
|
14
|
+
reject = _reject;
|
|
15
|
+
executor == null ? void 0 : executor(_resolve, _reject);
|
|
16
|
+
});
|
|
17
|
+
promise.resolve = resolve;
|
|
18
|
+
promise.reject = reject;
|
|
19
|
+
return promise;
|
|
20
|
+
}
|
|
5
21
|
var clearObj = (obj) => {
|
|
6
22
|
for (const key of Object.keys(obj)) {
|
|
7
23
|
delete obj[key];
|
|
@@ -39,40 +55,26 @@ function listToMap(list) {
|
|
|
39
55
|
function toMap(data) {
|
|
40
56
|
return isArray(data) ? listToMap(data) : objToMap(data);
|
|
41
57
|
}
|
|
42
|
-
var noop = () => {
|
|
43
|
-
};
|
|
44
|
-
function createPromise(executor) {
|
|
45
|
-
let resolve = noop;
|
|
46
|
-
let reject = noop;
|
|
47
|
-
const promise = new Promise((_resolve, _reject) => {
|
|
48
|
-
resolve = _resolve;
|
|
49
|
-
reject = _reject;
|
|
50
|
-
executor == null ? void 0 : executor(_resolve, _reject);
|
|
51
|
-
});
|
|
52
|
-
promise.resolve = resolve;
|
|
53
|
-
promise.reject = reject;
|
|
54
|
-
return promise;
|
|
55
|
-
}
|
|
56
58
|
var defineDictData = (data) => data;
|
|
57
59
|
|
|
58
60
|
// src/dict-manager.ts
|
|
59
|
-
function createDictManager(
|
|
60
|
-
const { fetch: managerFetch, extra: managerExtra } =
|
|
61
|
+
function createDictManager(managerOptions = {}) {
|
|
62
|
+
const { fetch: managerFetch, extra: managerExtra } = managerOptions;
|
|
61
63
|
const maps = reactive({});
|
|
62
|
-
const defineDict = (code,
|
|
64
|
+
const defineDict = (code, defineDictOptions) => {
|
|
63
65
|
var _a;
|
|
64
66
|
const {
|
|
65
|
-
data,
|
|
67
|
+
data = {},
|
|
66
68
|
remote = false,
|
|
67
69
|
fetch = managerFetch,
|
|
68
70
|
extra
|
|
69
|
-
} = (_a = isFunction(
|
|
70
|
-
const managerLoadPromise = shallowRef(
|
|
71
|
+
} = (_a = isFunction(defineDictOptions) ? defineDictOptions() : defineDictOptions) != null ? _a : {};
|
|
72
|
+
const managerLoadPromise = shallowRef(createPromise());
|
|
71
73
|
maps[code] = /* @__PURE__ */ new Map();
|
|
72
|
-
async function loadDict(
|
|
73
|
-
const dataMap = toMap(cloneDeep(data
|
|
74
|
+
async function loadDict(options, mapRef) {
|
|
75
|
+
const dataMap = toMap(cloneDeep(data));
|
|
74
76
|
if (remote) {
|
|
75
|
-
await (fetch == null ? void 0 : fetch(code,
|
|
77
|
+
await (fetch == null ? void 0 : fetch(code, options).then((res) => {
|
|
76
78
|
mapRef.value = toMap(res != null ? res : []);
|
|
77
79
|
dataMap.forEach((value, key) => {
|
|
78
80
|
if (mapRef.value.has(key)) {
|
|
@@ -87,7 +89,7 @@ function createDictManager(options) {
|
|
|
87
89
|
return (useDictOptions) => {
|
|
88
90
|
useDictOptions = merge({ clone: false, immediate: false }, useDictOptions);
|
|
89
91
|
const { clone, immediate } = useDictOptions;
|
|
90
|
-
const loadPromise = !clone ? managerLoadPromise : shallowRef(
|
|
92
|
+
const loadPromise = !clone ? managerLoadPromise : shallowRef(createPromise());
|
|
91
93
|
const mapRef = !clone ? toRef(maps, code) : ref(/* @__PURE__ */ new Map());
|
|
92
94
|
const objRef = ref({});
|
|
93
95
|
const listRef = ref([]);
|
|
@@ -99,20 +101,21 @@ function createDictManager(options) {
|
|
|
99
101
|
},
|
|
100
102
|
{ deep: true, immediate: true }
|
|
101
103
|
);
|
|
102
|
-
const E = computed(
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
}
|
|
107
|
-
|
|
104
|
+
const E = computed(() => {
|
|
105
|
+
const result = {};
|
|
106
|
+
for (const key of mapRef.value.keys()) {
|
|
107
|
+
result[key] = key;
|
|
108
|
+
}
|
|
109
|
+
return result;
|
|
110
|
+
});
|
|
108
111
|
if (remote) {
|
|
109
112
|
immediate && load();
|
|
110
113
|
} else {
|
|
111
114
|
load();
|
|
112
115
|
}
|
|
113
|
-
function load(
|
|
116
|
+
function load(options) {
|
|
114
117
|
loadPromise.value = createPromise();
|
|
115
|
-
loadDict(merge({}, useDictOptions,
|
|
118
|
+
loadDict(merge({}, useDictOptions, options), mapRef).then(() => {
|
|
116
119
|
var _a2;
|
|
117
120
|
(_a2 = loadPromise.value) == null ? void 0 : _a2.resolve();
|
|
118
121
|
});
|
|
@@ -122,13 +125,14 @@ function createDictManager(options) {
|
|
|
122
125
|
map: objRef,
|
|
123
126
|
list: listRef,
|
|
124
127
|
E,
|
|
125
|
-
loadPromise
|
|
128
|
+
loadPromise,
|
|
129
|
+
load
|
|
126
130
|
};
|
|
131
|
+
const reactiveCtx = reactive(ctx);
|
|
127
132
|
return reactive({
|
|
128
133
|
...ctx,
|
|
129
|
-
|
|
130
|
-
...
|
|
131
|
-
...extra == null ? void 0 : extra(reactive(ctx))
|
|
134
|
+
...managerExtra == null ? void 0 : managerExtra(reactiveCtx),
|
|
135
|
+
...extra == null ? void 0 : extra(reactiveCtx)
|
|
132
136
|
});
|
|
133
137
|
};
|
|
134
138
|
};
|
package/dist/type.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { O } from 'ts-toolbelt';
|
|
2
|
-
import type {
|
|
3
|
-
import type { createPromise } from './
|
|
2
|
+
import type { Simplify, ValueOf } from 'type-fest';
|
|
3
|
+
import type { createPromise } from './create-promise';
|
|
4
4
|
export type Recordable<T = any> = Record<string, T>;
|
|
5
5
|
export type Getter<T> = () => T;
|
|
6
6
|
export type MaybeGetter<T> = T | Getter<T>;
|
|
@@ -10,29 +10,33 @@ export type DictItem = {
|
|
|
10
10
|
label: string;
|
|
11
11
|
value: string;
|
|
12
12
|
};
|
|
13
|
-
export type DictItemRecord =
|
|
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,
|
|
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:
|
|
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 &
|
|
35
|
+
}>): (options?: Simplify<UseDictOptions & ExtractFetchOptions<F>>) => Simplify<CreateDict<D, F> & ReturnType<Extra> & ReturnType<E>>;
|
|
32
36
|
}
|
|
33
|
-
type
|
|
34
|
-
type
|
|
35
|
-
export type Dict<Key extends PropertyKey = PropertyKey, ExtraItem extends Recordable = Recordable,
|
|
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:
|
|
44
|
-
load: (
|
|
47
|
+
loadPromise: LoadPromise;
|
|
48
|
+
load: (options: Options) => LoadPromise;
|
|
45
49
|
};
|
|
46
|
-
export type LoadPromise = ReturnType<typeof createPromise<void>> | undefined;
|
|
47
50
|
export {};
|
package/dist/util.d.ts
CHANGED
|
@@ -1,23 +1,10 @@
|
|
|
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
|
-
export type CreatePromiseReturn<T> = Promise<T> & {
|
|
17
|
-
resolve: (value: T | PromiseLike<T>) => void;
|
|
18
|
-
reject: (reason?: any) => void;
|
|
19
|
-
};
|
|
20
|
-
export declare function createPromise<T = any>(executor?: (resolve: (value: T | PromiseLike<T>) => void, reject: (reason?: any) => void) => void): CreatePromiseReturn<T>;
|
|
21
8
|
export declare const defineDictData: <const T extends {
|
|
22
9
|
label: string;
|
|
23
10
|
} & Recordable, const K extends string>(data: Record<K, T>) => Record<K, WritableDeep<T>>;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "v-dict",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.3",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "vue3 dict manager",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -17,7 +17,10 @@
|
|
|
17
17
|
"files": [
|
|
18
18
|
"dist"
|
|
19
19
|
],
|
|
20
|
-
"keywords": [
|
|
20
|
+
"keywords": [
|
|
21
|
+
"vue",
|
|
22
|
+
"dict"
|
|
23
|
+
],
|
|
21
24
|
"author": "",
|
|
22
25
|
"license": "MIT",
|
|
23
26
|
"dependencies": {
|