v-dict 1.0.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/README.md ADDED
File without changes
@@ -0,0 +1,4 @@
1
+ import type { CreateDictManager, DefineDict, ExtraGetter } from './type';
2
+ export declare function createDictManager<E extends ExtraGetter>(options?: CreateDictManager<E>): {
3
+ defineDict: DefineDict<E>;
4
+ };
package/dist/index.cjs ADDED
@@ -0,0 +1,141 @@
1
+ 'use strict';
2
+
3
+ var vue = require('vue');
4
+ var lodashEs = require('lodash-es');
5
+
6
+ // src/dict-manager.ts
7
+ var clearObj = (obj) => {
8
+ for (const key of Object.keys(obj)) {
9
+ delete obj[key];
10
+ }
11
+ };
12
+ function mapToObj(map, obj = {}) {
13
+ clearObj(obj);
14
+ map.forEach((value, key) => {
15
+ obj[key] = value;
16
+ });
17
+ return obj;
18
+ }
19
+ function objToMap(obj) {
20
+ const map = /* @__PURE__ */ new Map();
21
+ for (const [key, value] of Object.entries(obj)) {
22
+ if (lodashEs.isUndefined(value.value)) {
23
+ value.value = key;
24
+ }
25
+ map.set(key, value);
26
+ }
27
+ return map;
28
+ }
29
+ function mapToList(map, list = []) {
30
+ list.length = 0;
31
+ list.push(...map.values());
32
+ return list;
33
+ }
34
+ function listToMap(list) {
35
+ const map = /* @__PURE__ */ new Map();
36
+ for (const item of list) {
37
+ map.set(item.value, item);
38
+ }
39
+ return map;
40
+ }
41
+ function toMap(data) {
42
+ return lodashEs.isArray(data) ? listToMap(data) : objToMap(data);
43
+ }
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
+ var defineDictData = (data) => data;
59
+
60
+ // src/dict-manager.ts
61
+ function createDictManager(options) {
62
+ const { fetch: managerFetch, extra: managerExtra } = options != null ? options : {};
63
+ const maps = vue.reactive({});
64
+ const defineDict = (code, options2) => {
65
+ var _a;
66
+ const {
67
+ data,
68
+ remote = false,
69
+ fetch = managerFetch,
70
+ extra
71
+ } = (_a = lodashEs.isFunction(options2) ? options2() : options2) != null ? _a : {};
72
+ const managerLoadPromise = vue.shallowRef(null);
73
+ maps[code] = /* @__PURE__ */ new Map();
74
+ async function loadDict(ctx, mapRef) {
75
+ const dataMap = toMap(lodashEs.cloneDeep(data != null ? data : {}));
76
+ if (remote) {
77
+ await (fetch == null ? void 0 : fetch(code, ctx).then((res) => {
78
+ mapRef.value = toMap(res != null ? res : []);
79
+ dataMap.forEach((value, key) => {
80
+ if (mapRef.value.has(key)) {
81
+ lodashEs.merge(mapRef.value.get(key), value);
82
+ }
83
+ });
84
+ }));
85
+ } else {
86
+ mapRef.value = dataMap;
87
+ }
88
+ }
89
+ return (useDictOptions) => {
90
+ useDictOptions = lodashEs.merge({ clone: false, immediate: false }, useDictOptions);
91
+ const { clone, immediate } = useDictOptions;
92
+ const loadPromise = !clone ? managerLoadPromise : vue.shallowRef(null);
93
+ const mapRef = !clone ? vue.toRef(maps, code) : vue.ref(/* @__PURE__ */ new Map());
94
+ const objRef = vue.ref({});
95
+ const listRef = vue.ref([]);
96
+ vue.watch(
97
+ mapRef,
98
+ (newValue) => {
99
+ mapToObj(newValue, objRef.value);
100
+ mapToList(newValue, listRef.value);
101
+ },
102
+ { deep: true, immediate: true }
103
+ );
104
+ const E = vue.computed(
105
+ () => [...mapRef.value.keys()].reduce((ret, key) => {
106
+ ret[key] = key;
107
+ return ret;
108
+ }, {})
109
+ );
110
+ if (remote) {
111
+ immediate && load();
112
+ } else {
113
+ load();
114
+ }
115
+ function load(ctx2) {
116
+ loadPromise.value = createPromise();
117
+ loadDict(lodashEs.merge({}, useDictOptions, ctx2), mapRef).then(() => {
118
+ var _a2;
119
+ (_a2 = loadPromise.value) == null ? void 0 : _a2.resolve();
120
+ });
121
+ return loadPromise.value;
122
+ }
123
+ const ctx = {
124
+ map: objRef,
125
+ list: listRef,
126
+ E,
127
+ loadPromise
128
+ };
129
+ return vue.reactive({
130
+ ...ctx,
131
+ load,
132
+ ...managerExtra == null ? void 0 : managerExtra(vue.reactive(ctx)),
133
+ ...extra == null ? void 0 : extra(vue.reactive(ctx))
134
+ });
135
+ };
136
+ };
137
+ return { defineDict };
138
+ }
139
+
140
+ exports.createDictManager = createDictManager;
141
+ exports.defineDictData = defineDictData;
@@ -0,0 +1,3 @@
1
+ export type * from './type';
2
+ export * from './dict-manager';
3
+ export { defineDictData } from './util';
package/dist/index.js ADDED
@@ -0,0 +1,138 @@
1
+ import { reactive, shallowRef, toRef, ref, watch, computed } from 'vue';
2
+ import { isFunction, merge, cloneDeep, isArray, isUndefined } from 'lodash-es';
3
+
4
+ // src/dict-manager.ts
5
+ var clearObj = (obj) => {
6
+ for (const key of Object.keys(obj)) {
7
+ delete obj[key];
8
+ }
9
+ };
10
+ function mapToObj(map, obj = {}) {
11
+ clearObj(obj);
12
+ map.forEach((value, key) => {
13
+ obj[key] = value;
14
+ });
15
+ return obj;
16
+ }
17
+ function objToMap(obj) {
18
+ const map = /* @__PURE__ */ new Map();
19
+ for (const [key, value] of Object.entries(obj)) {
20
+ if (isUndefined(value.value)) {
21
+ value.value = key;
22
+ }
23
+ map.set(key, value);
24
+ }
25
+ return map;
26
+ }
27
+ function mapToList(map, list = []) {
28
+ list.length = 0;
29
+ list.push(...map.values());
30
+ return list;
31
+ }
32
+ function listToMap(list) {
33
+ const map = /* @__PURE__ */ new Map();
34
+ for (const item of list) {
35
+ map.set(item.value, item);
36
+ }
37
+ return map;
38
+ }
39
+ function toMap(data) {
40
+ return isArray(data) ? listToMap(data) : objToMap(data);
41
+ }
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
+ var defineDictData = (data) => data;
57
+
58
+ // src/dict-manager.ts
59
+ function createDictManager(options) {
60
+ const { fetch: managerFetch, extra: managerExtra } = options != null ? options : {};
61
+ const maps = reactive({});
62
+ const defineDict = (code, options2) => {
63
+ var _a;
64
+ const {
65
+ data,
66
+ remote = false,
67
+ fetch = managerFetch,
68
+ extra
69
+ } = (_a = isFunction(options2) ? options2() : options2) != null ? _a : {};
70
+ const managerLoadPromise = shallowRef(null);
71
+ maps[code] = /* @__PURE__ */ new Map();
72
+ async function loadDict(ctx, mapRef) {
73
+ const dataMap = toMap(cloneDeep(data != null ? data : {}));
74
+ if (remote) {
75
+ await (fetch == null ? void 0 : fetch(code, ctx).then((res) => {
76
+ mapRef.value = toMap(res != null ? res : []);
77
+ dataMap.forEach((value, key) => {
78
+ if (mapRef.value.has(key)) {
79
+ merge(mapRef.value.get(key), value);
80
+ }
81
+ });
82
+ }));
83
+ } else {
84
+ mapRef.value = dataMap;
85
+ }
86
+ }
87
+ return (useDictOptions) => {
88
+ useDictOptions = merge({ clone: false, immediate: false }, useDictOptions);
89
+ const { clone, immediate } = useDictOptions;
90
+ const loadPromise = !clone ? managerLoadPromise : shallowRef(null);
91
+ const mapRef = !clone ? toRef(maps, code) : ref(/* @__PURE__ */ new Map());
92
+ const objRef = ref({});
93
+ const listRef = ref([]);
94
+ watch(
95
+ mapRef,
96
+ (newValue) => {
97
+ mapToObj(newValue, objRef.value);
98
+ mapToList(newValue, listRef.value);
99
+ },
100
+ { deep: true, immediate: true }
101
+ );
102
+ const E = computed(
103
+ () => [...mapRef.value.keys()].reduce((ret, key) => {
104
+ ret[key] = key;
105
+ return ret;
106
+ }, {})
107
+ );
108
+ if (remote) {
109
+ immediate && load();
110
+ } else {
111
+ load();
112
+ }
113
+ function load(ctx2) {
114
+ loadPromise.value = createPromise();
115
+ loadDict(merge({}, useDictOptions, ctx2), mapRef).then(() => {
116
+ var _a2;
117
+ (_a2 = loadPromise.value) == null ? void 0 : _a2.resolve();
118
+ });
119
+ return loadPromise.value;
120
+ }
121
+ const ctx = {
122
+ map: objRef,
123
+ list: listRef,
124
+ E,
125
+ loadPromise
126
+ };
127
+ return reactive({
128
+ ...ctx,
129
+ load,
130
+ ...managerExtra == null ? void 0 : managerExtra(reactive(ctx)),
131
+ ...extra == null ? void 0 : extra(reactive(ctx))
132
+ });
133
+ };
134
+ };
135
+ return { defineDict };
136
+ }
137
+
138
+ export { createDictManager, defineDictData };
package/dist/type.d.ts ADDED
@@ -0,0 +1,47 @@
1
+ import type { O } from 'ts-toolbelt';
2
+ import type { Except, Simplify, ValueOf } from 'type-fest';
3
+ import type { createPromise } from './util';
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 = Simplify<DictItem & Recordable>;
14
+ export type DictMap = Map<string, DictItemRecord>;
15
+ export type Fetch = (code: string, ctx: any) => Promise<DictItemRecord[]>;
16
+ export interface CreateDictManager<E extends ExtraGetter> {
17
+ fetch?: Fetch;
18
+ extra?: E;
19
+ }
20
+ export type ExtraGetter<D extends Dict<string> = Dict<string>> = (dict: Except<D, 'load'>) => Recordable;
21
+ export type UseDictOptions = {
22
+ clone?: boolean;
23
+ immediate?: boolean;
24
+ } & Recordable;
25
+ export interface DefineDict<Extra extends ExtraGetter> {
26
+ <R extends boolean, F extends Fetch, D extends Recordable<Recordable>, E extends ExtraGetter>(code: string, options?: MaybeGetter<{
27
+ remote?: R;
28
+ fetch?: F;
29
+ data?: D;
30
+ extra?: E;
31
+ }>): (options?: UseDictOptions & GetFetchCtx<F>) => CreateDict<D, F> & ReturnType<Extra> & ReturnType<E>;
32
+ }
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> = {
36
+ list: Simplify<DictItem & ExtraItem>[];
37
+ E: {
38
+ [K in Key]: K;
39
+ };
40
+ map: {
41
+ [K in Key]: Simplify<DictItem & ExtraItem>;
42
+ } & Recordable<Simplify<DictItem & ExtraItem>>;
43
+ loadPromise: Promise<void>;
44
+ load: (ctx: Ctx) => Promise<void>;
45
+ };
46
+ export type LoadPromise = ReturnType<typeof createPromise<void>> | undefined;
47
+ export {};
package/dist/util.d.ts ADDED
@@ -0,0 +1,23 @@
1
+ import type { WritableDeep } from 'type-fest';
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
+ }>;
8
+ 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
+ }[];
14
+ export declare function listToMap(list: DictItemRecord[]): DictMap;
15
+ 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
+ export declare const defineDictData: <const T extends {
22
+ label: string;
23
+ } & Recordable, const K extends string>(data: Record<K, T>) => Record<K, WritableDeep<T>>;
package/package.json ADDED
@@ -0,0 +1,48 @@
1
+ {
2
+ "name": "v-dict",
3
+ "version": "1.0.0",
4
+ "type": "module",
5
+ "description": "vue3 dict manager",
6
+ "main": "dist/index.js",
7
+ "module": "dist/index.mjs",
8
+ "typings": "dist/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "import": "./dist/index.js",
12
+ "require": "./dist/index.cjs",
13
+ "types": "./lib/index.d.ts"
14
+ },
15
+ "./*": "./*"
16
+ },
17
+ "files": [
18
+ "dist"
19
+ ],
20
+ "keywords": [],
21
+ "author": "",
22
+ "license": "MIT",
23
+ "dependencies": {
24
+ "lodash-es": "^4.17.21"
25
+ },
26
+ "peerDependencies": {
27
+ "vue": "^3.0.0"
28
+ },
29
+ "devDependencies": {
30
+ "@changesets/cli": "^2.27.1",
31
+ "@types/lodash-es": "^4.17.12",
32
+ "@types/node": "^20.11.0",
33
+ "ts-toolbelt": "^9.6.0",
34
+ "tsup": "^8.0.1",
35
+ "type-fest": "^4.9.0",
36
+ "typescript": "^5.3.3",
37
+ "vue": "^3.4.10"
38
+ },
39
+ "scripts": {
40
+ "watch": "tsup --watch",
41
+ "build": "tsup",
42
+ "build:type": "tsc -p ./tsconfig.json",
43
+ "changeset": "changeset",
44
+ "changeset:version": "changeset version",
45
+ "changeset:release:only": "changeset publish --registry=https://registry.npmjs.com/",
46
+ "changeset:release": "pnpm build && pnpm changeset:release:only"
47
+ }
48
+ }