@wzyjs/types 0.0.28

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.
Files changed (76) hide show
  1. package/dist/cjs/common/index.d.ts +6 -0
  2. package/dist/cjs/common/tools/index.d.ts +4 -0
  3. package/dist/cjs/common/tools/number.d.ts +3 -0
  4. package/dist/cjs/common/tools/number.js +34 -0
  5. package/dist/cjs/common/tools/object.d.ts +10 -0
  6. package/dist/cjs/common/tools/object.js +43 -0
  7. package/dist/cjs/common/tools/other.d.ts +26 -0
  8. package/dist/cjs/common/tools/other.js +112 -0
  9. package/dist/cjs/common/tools/string.d.ts +16 -0
  10. package/dist/cjs/common/tools/string.js +137 -0
  11. package/dist/cjs/common/types/index.d.ts +20 -0
  12. package/dist/cjs/fe/dayjs.d.ts +4 -0
  13. package/dist/cjs/fe/element.d.ts +9 -0
  14. package/dist/cjs/fe/index.d.ts +20 -0
  15. package/dist/cjs/fe/middlewares/error.d.ts +3 -0
  16. package/dist/cjs/fe/middlewares/index.d.ts +1 -0
  17. package/dist/cjs/fe/other.d.ts +1 -0
  18. package/dist/cjs/fe/style.d.ts +10 -0
  19. package/dist/cjs/index_all.d.ts +3 -0
  20. package/dist/cjs/index_fe.d.ts +2 -0
  21. package/dist/cjs/index_rd.d.ts +2 -0
  22. package/dist/cjs/index_rd.js +141 -0
  23. package/dist/cjs/rd/database/Collection.d.ts +22 -0
  24. package/dist/cjs/rd/database/Collection.js +202 -0
  25. package/dist/cjs/rd/database/Database.d.ts +10 -0
  26. package/dist/cjs/rd/database/Database.js +25 -0
  27. package/dist/cjs/rd/database/index.d.ts +2 -0
  28. package/dist/cjs/rd/database/types.d.ts +151 -0
  29. package/dist/cjs/rd/database/types.js +25 -0
  30. package/dist/cjs/rd/database/utils.d.ts +3 -0
  31. package/dist/cjs/rd/database/utils.js +30 -0
  32. package/dist/cjs/rd/index.d.ts +3 -0
  33. package/dist/cjs/rd/jsonFile/index.d.ts +6 -0
  34. package/dist/cjs/rd/jsonFile/index.js +29 -0
  35. package/dist/cjs/rd/mail/index.d.ts +1 -0
  36. package/dist/cjs/rd/mail/index.js +31 -0
  37. package/dist/cjs/rd/middlewares/index.d.ts +1 -0
  38. package/dist/cjs/rd/middlewares/logger.d.ts +1 -0
  39. package/dist/esm/common/index.d.ts +6 -0
  40. package/dist/esm/common/tools/index.d.ts +4 -0
  41. package/dist/esm/common/tools/number.d.ts +3 -0
  42. package/dist/esm/common/tools/number.js +30 -0
  43. package/dist/esm/common/tools/object.d.ts +10 -0
  44. package/dist/esm/common/tools/object.js +39 -0
  45. package/dist/esm/common/tools/other.d.ts +26 -0
  46. package/dist/esm/common/tools/other.js +106 -0
  47. package/dist/esm/common/tools/string.d.ts +16 -0
  48. package/dist/esm/common/tools/string.js +120 -0
  49. package/dist/esm/common/types/index.d.ts +20 -0
  50. package/dist/esm/fe/dayjs.d.ts +4 -0
  51. package/dist/esm/fe/dayjs.js +18 -0
  52. package/dist/esm/fe/element.d.ts +9 -0
  53. package/dist/esm/fe/element.js +36 -0
  54. package/dist/esm/fe/index.d.ts +20 -0
  55. package/dist/esm/fe/index.js +16 -0
  56. package/dist/esm/fe/middlewares/error.d.ts +3 -0
  57. package/dist/esm/fe/middlewares/error.js +22 -0
  58. package/dist/esm/fe/middlewares/index.d.ts +1 -0
  59. package/dist/esm/fe/other.d.ts +1 -0
  60. package/dist/esm/fe/other.js +10 -0
  61. package/dist/esm/fe/style.d.ts +10 -0
  62. package/dist/esm/fe/style.js +45 -0
  63. package/dist/esm/index_all.d.ts +3 -0
  64. package/dist/esm/index_fe.d.ts +2 -0
  65. package/dist/esm/index_fe.js +19 -0
  66. package/dist/esm/index_rd.d.ts +2 -0
  67. package/dist/esm/rd/database/Collection.d.ts +22 -0
  68. package/dist/esm/rd/database/Database.d.ts +10 -0
  69. package/dist/esm/rd/database/index.d.ts +2 -0
  70. package/dist/esm/rd/database/types.d.ts +151 -0
  71. package/dist/esm/rd/database/utils.d.ts +3 -0
  72. package/dist/esm/rd/index.d.ts +3 -0
  73. package/dist/esm/rd/jsonFile/index.d.ts +6 -0
  74. package/dist/esm/rd/mail/index.d.ts +1 -0
  75. package/index.ts +40 -0
  76. package/package.json +16 -0
@@ -0,0 +1,202 @@
1
+ 'use strict';
2
+
3
+ var lodash = require('lodash');
4
+ var utils = require('./utils.js');
5
+ var types = require('./types.js');
6
+
7
+ class Collection {
8
+ db;
9
+ collection;
10
+ constructor(db, name) {
11
+ this.db = db;
12
+ this.collection = db.collection(name);
13
+ }
14
+ joinConditions(conditions) {
15
+ let record = this.collection;
16
+ if (!conditions) {
17
+ return record;
18
+ }
19
+ // 如果是根据 _id 查询
20
+ if ('_id' in conditions) {
21
+ record = record.doc(conditions._id);
22
+ // 如果是根据 条件 查询
23
+ }
24
+ else {
25
+ const { page, order, where, options } = conditions;
26
+ if (options) {
27
+ record = record.options(options);
28
+ }
29
+ if (where) {
30
+ record = record.where(where);
31
+ }
32
+ if (order) {
33
+ record = record.orderBy(order.field, order.type || 'desc');
34
+ }
35
+ if (page?.current && page?.size) {
36
+ record = record.skip((page.current - 1) * page.size);
37
+ }
38
+ if (page?.size) {
39
+ record = record.limit(page.size);
40
+ }
41
+ }
42
+ if (Array.isArray(conditions.field)) {
43
+ record = record.field(conditions.field.reduce((o, i) => ({ ...o, [i]: true }), {}));
44
+ }
45
+ else if (conditions.field) {
46
+ record = record.field(conditions.field);
47
+ }
48
+ return record;
49
+ }
50
+ request(params) {
51
+ switch (params.method) {
52
+ case types.Method.Add:
53
+ return this.joinConditions().add(params.data);
54
+ case types.Method.Find:
55
+ case types.Method.FindList:
56
+ return this.joinConditions(params.conditions).get();
57
+ case types.Method.Delete:
58
+ return this.joinConditions(params.conditions).remove();
59
+ case types.Method.Update:
60
+ if (params.isSet && '_id' in params.conditions) {
61
+ return this.joinConditions(params.conditions).set(params.data);
62
+ }
63
+ return this.joinConditions(params.conditions).update(params.data);
64
+ case types.Method.Count:
65
+ return this.joinConditions(params.conditions).count();
66
+ default:
67
+ throw new Error('method is not exist');
68
+ }
69
+ }
70
+ // 处理出参
71
+ async handleRes(params, res) {
72
+ if (res) {
73
+ delete res.requestId;
74
+ }
75
+ let data = res;
76
+ switch (params.method) {
77
+ case types.Method.Add:
78
+ const addRes = res;
79
+ if (addRes.id && params.isReturn) {
80
+ data = await this.find({ _id: addRes.id, field: params.conditions?.field });
81
+ }
82
+ break;
83
+ case types.Method.Update:
84
+ const updateRes = res;
85
+ if (updateRes.updated && params.isReturn && '_id' in params.conditions) {
86
+ data = await this.find({ _id: params.conditions._id, field: params.conditions?.field });
87
+ }
88
+ break;
89
+ case types.Method.Find:
90
+ const getRes = res;
91
+ data = getRes?.data[0] || null;
92
+ break;
93
+ case types.Method.FindList:
94
+ const getListRes = res;
95
+ const countRes = await this.request({
96
+ method: types.Method.Count,
97
+ conditions: params.conditions,
98
+ });
99
+ data = {
100
+ list: getListRes.data,
101
+ total: countRes.total,
102
+ };
103
+ break;
104
+ }
105
+ return data;
106
+ }
107
+ // 处理数据
108
+ async handleData(params) {
109
+ switch (params.method) {
110
+ case types.Method.Add:
111
+ params.data = utils.appendTime(params.data, 'create');
112
+ params.data = utils.appendTime(params.data, 'update');
113
+ break;
114
+ case types.Method.Update:
115
+ if ('_id' in params.data) {
116
+ delete params.data._id;
117
+ }
118
+ break;
119
+ }
120
+ return params;
121
+ }
122
+ // 处理条件
123
+ handleConditions(params) {
124
+ if (params.method !== types.Method.Add) {
125
+ params.conditions = lodash.merge(lodash.cloneDeep(utils.defaultConditions), params.conditions);
126
+ }
127
+ // 处理 where 条件
128
+ if (params.conditions && 'where' in params.conditions) {
129
+ const { command: _ } = this.db;
130
+ for (const key in params.conditions.where) {
131
+ const whereValue = params.conditions.where[key];
132
+ if (lodash.isObject(whereValue)) {
133
+ const { type, value } = whereValue;
134
+ switch (type) {
135
+ case types.Command.In:
136
+ params.conditions.where[key] = _.in(value);
137
+ break;
138
+ case types.Command.Nin:
139
+ params.conditions.where[key] = _.nin(value);
140
+ break;
141
+ case types.Command.Gt:
142
+ params.conditions.where[key] = _.gt(value);
143
+ break;
144
+ case types.Command.Gte:
145
+ params.conditions.where[key] = _.gte(value);
146
+ break;
147
+ case types.Command.Lt:
148
+ params.conditions.where[key] = _.lt(value);
149
+ break;
150
+ case types.Command.Lte:
151
+ params.conditions.where[key] = _.lte(value);
152
+ break;
153
+ case types.Command.Eq:
154
+ params.conditions.where[key] = _.eq(value);
155
+ break;
156
+ case types.Command.Neq:
157
+ params.conditions.where[key] = _.neq(value);
158
+ break;
159
+ case types.Command.Gtlt:
160
+ params.conditions.where[key] = _.and(_.gt(value[0]), _.lt(value[1]));
161
+ break;
162
+ default:
163
+ const check = type;
164
+ console.error(666, '处理 where 条件 出现未定义的情况', check);
165
+ break;
166
+ }
167
+ }
168
+ }
169
+ }
170
+ return params;
171
+ }
172
+ async exec(params) {
173
+ params = await this.handleData(params);
174
+ params = await this.handleConditions(params);
175
+ const res = await this.request(params);
176
+ return this.handleRes(params, res);
177
+ }
178
+ add(data, isReturn, conditions) {
179
+ return this.exec({ method: types.Method.Add, conditions, data, isReturn });
180
+ }
181
+ // 删
182
+ delete(conditions) {
183
+ return this.exec({ method: types.Method.Delete, conditions });
184
+ }
185
+ update(conditions, data, isReturn, isSet) {
186
+ return this.exec({ method: types.Method.Update, conditions, data, isReturn, isSet });
187
+ }
188
+ // 查
189
+ find(conditions) {
190
+ return this.exec({ method: types.Method.Find, conditions });
191
+ }
192
+ // 查列表
193
+ findList(conditions) {
194
+ return this.exec({ method: types.Method.FindList, conditions });
195
+ }
196
+ // 查
197
+ count(conditions) {
198
+ return this.exec({ method: types.Method.Count, conditions });
199
+ }
200
+ }
201
+
202
+ exports.Collection = Collection;
@@ -0,0 +1,10 @@
1
+ import tcb, { IKeyValue } from '@cloudbase/node-sdk';
2
+ import { Config } from './types';
3
+ import { Collection } from './Collection';
4
+ export declare class Database {
5
+ private readonly db;
6
+ private readonly instance;
7
+ constructor(config: Config);
8
+ collection<D extends IKeyValue>(name: string): Collection<D>;
9
+ createCollection(name: string): tcb.IBaseRes;
10
+ }
@@ -0,0 +1,25 @@
1
+ 'use strict';
2
+
3
+ var nodeSdk = require('@cloudbase/node-sdk');
4
+ var Collection = require('./Collection.js');
5
+
6
+ class Database {
7
+ db;
8
+ instance = {};
9
+ constructor(config) {
10
+ this.db = nodeSdk.init(config).database();
11
+ }
12
+ // 封装了较多功能的 collection
13
+ collection(name) {
14
+ if (!this.instance[name]) {
15
+ this.instance[name] = new Collection.Collection(this.db, name);
16
+ }
17
+ return this.instance[name];
18
+ }
19
+ // 创建 collection
20
+ createCollection(name) {
21
+ return this.db.createCollection(name);
22
+ }
23
+ }
24
+
25
+ exports.Database = Database;
@@ -0,0 +1,2 @@
1
+ export { Database } from './Database';
2
+ export { Conditions, IdConditions, WhereConditions, Record } from './types';
@@ -0,0 +1,151 @@
1
+ import { Database, ICountRes } from '@cloudbase/node-sdk';
2
+ export interface Config {
3
+ env: string;
4
+ secretId: string;
5
+ secretKey: string;
6
+ }
7
+ export type IdConditions = {
8
+ _id: string;
9
+ field?: string[] | {
10
+ [key: string]: false;
11
+ };
12
+ options?: {
13
+ timeout: number;
14
+ multiple?: false;
15
+ };
16
+ };
17
+ export declare enum Command {
18
+ Eq = "eq",
19
+ Neq = "neq",
20
+ Lt = "lt",
21
+ Lte = "lte",
22
+ Gt = "gt",
23
+ Gte = "gte",
24
+ In = "in",
25
+ Nin = "nin",
26
+ Gtlt = "gtlt"
27
+ }
28
+ export type CommandWhere = {
29
+ type: Command.Eq;
30
+ value: any;
31
+ } | {
32
+ type: Command.Neq;
33
+ value: any;
34
+ } | {
35
+ type: Command.Gt;
36
+ value: number;
37
+ } | {
38
+ type: Command.Gte;
39
+ value: number;
40
+ } | {
41
+ type: Command.Lt;
42
+ value: number;
43
+ } | {
44
+ type: Command.Lte;
45
+ value: number;
46
+ } | {
47
+ type: Command.Gtlt;
48
+ value: number[];
49
+ } | {
50
+ type: Command.In;
51
+ value: any[];
52
+ } | {
53
+ type: Command.Nin;
54
+ value: any[];
55
+ };
56
+ export type WhereConditions = {
57
+ where?: {
58
+ [key: string]: any | CommandWhere;
59
+ };
60
+ field?: string[] | {
61
+ [key: string]: false;
62
+ };
63
+ page?: {
64
+ size?: number;
65
+ current?: number;
66
+ };
67
+ order?: {
68
+ field: string;
69
+ type?: 'asc' | 'desc';
70
+ };
71
+ options?: {
72
+ timeout?: number;
73
+ multiple?: boolean;
74
+ };
75
+ };
76
+ export type Conditions = IdConditions | WhereConditions;
77
+ export declare enum Method {
78
+ Add = "add",
79
+ Find = "find",
80
+ FindList = "findList",
81
+ Delete = "delete",
82
+ Update = "update",
83
+ Count = "count"
84
+ }
85
+ export declare namespace Add {
86
+ type Params<D> = {
87
+ method: Method.Add;
88
+ data: D;
89
+ conditions?: Pick<IdConditions, 'field'>;
90
+ isReturn?: true;
91
+ };
92
+ type Res = {
93
+ id: string;
94
+ };
95
+ }
96
+ export declare namespace Find {
97
+ type Params = {
98
+ method: Method.Find;
99
+ conditions?: Conditions;
100
+ };
101
+ type Res<D> = Record<D> | undefined;
102
+ }
103
+ export declare namespace FindList {
104
+ type Params = {
105
+ method: Method.FindList;
106
+ conditions?: WhereConditions;
107
+ };
108
+ type Res<D> = {
109
+ list?: Record<D>[];
110
+ total?: number;
111
+ };
112
+ }
113
+ export declare namespace Delete {
114
+ type Params = {
115
+ method: Method.Delete;
116
+ conditions: Conditions;
117
+ };
118
+ type Res = {
119
+ deleted: number | string;
120
+ };
121
+ }
122
+ export declare namespace Update {
123
+ type Params<D> = {
124
+ method: Method.Update;
125
+ conditions: Conditions;
126
+ data: Partial<Record<D>>;
127
+ isReturn?: true;
128
+ isSet?: true;
129
+ };
130
+ type Res<D> = {
131
+ updated?: number;
132
+ doc?: D;
133
+ };
134
+ }
135
+ export declare namespace Count {
136
+ type Params = {
137
+ method: Method.Count;
138
+ conditions?: WhereConditions;
139
+ };
140
+ type Res = {
141
+ total?: number;
142
+ };
143
+ }
144
+ export type MethodParams<D> = Add.Params<D> | Find.Params | FindList.Params | Delete.Params | Update.Params<D> | Count.Params;
145
+ export type MethodRes<D> = Add.Res | Find.Res<D> | FindList.Res<D> | Delete.Res | Update.Res<D> | Count.Res;
146
+ export type IRes = Database.IAddRes | Database.IGetRes | Database.IDeleteResult | Database.IUpdateResult | ICountRes;
147
+ export type Record<D> = D & {
148
+ _id: string;
149
+ createAt: number;
150
+ updateAt: number;
151
+ };
@@ -0,0 +1,25 @@
1
+ 'use strict';
2
+
3
+ // 查询数据库的条件 - 比较的特殊条件
4
+ exports.Command = void 0;
5
+ (function (Command) {
6
+ Command["Eq"] = "eq";
7
+ Command["Neq"] = "neq";
8
+ Command["Lt"] = "lt";
9
+ Command["Lte"] = "lte";
10
+ Command["Gt"] = "gt";
11
+ Command["Gte"] = "gte";
12
+ Command["In"] = "in";
13
+ Command["Nin"] = "nin";
14
+ Command["Gtlt"] = "gtlt";
15
+ })(exports.Command || (exports.Command = {}));
16
+ // 查询的方法
17
+ exports.Method = void 0;
18
+ (function (Method) {
19
+ Method["Add"] = "add";
20
+ Method["Find"] = "find";
21
+ Method["FindList"] = "findList";
22
+ Method["Delete"] = "delete";
23
+ Method["Update"] = "update";
24
+ Method["Count"] = "count";
25
+ })(exports.Method || (exports.Method = {}));
@@ -0,0 +1,3 @@
1
+ import { Conditions } from './types';
2
+ export declare const appendTime: <D>(data: D, type: 'create' | 'update') => D;
3
+ export declare const defaultConditions: Conditions;
@@ -0,0 +1,30 @@
1
+ 'use strict';
2
+
3
+ // 给记录添加时间
4
+ const appendTime = (data, type) => {
5
+ if (!data) {
6
+ return {};
7
+ }
8
+ return {
9
+ ...data,
10
+ [type + 'At']: Date.now(),
11
+ };
12
+ };
13
+ // 默认条件
14
+ const defaultConditions = {
15
+ order: {
16
+ field: 'createAt',
17
+ type: 'desc',
18
+ },
19
+ options: {
20
+ timeout: 10000,
21
+ multiple: true,
22
+ },
23
+ page: {
24
+ current: 1,
25
+ size: 10,
26
+ },
27
+ };
28
+
29
+ exports.appendTime = appendTime;
30
+ exports.defaultConditions = defaultConditions;
@@ -0,0 +1,3 @@
1
+ export * from './database';
2
+ export * from './jsonFile';
3
+ export * from './mail';
@@ -0,0 +1,6 @@
1
+ export declare class JsonFile {
2
+ filePath: string;
3
+ constructor(filePath: string);
4
+ get(key: string): Promise<unknown>;
5
+ set(key: string, value: any): void;
6
+ }
@@ -0,0 +1,29 @@
1
+ 'use strict';
2
+
3
+ const fs = require('fs');
4
+ const path = require('path');
5
+ // 实现一个文件系统读写数据库
6
+ class JsonFile {
7
+ filePath;
8
+ constructor(filePath) {
9
+ this.filePath = path.resolve(__dirname, filePath);
10
+ }
11
+ get(key) {
12
+ return new Promise((resolve) => {
13
+ fs.readFile(this.filePath, (err, data) => {
14
+ const json = data ? JSON.parse(data) : {};
15
+ resolve(json[key]);
16
+ });
17
+ });
18
+ }
19
+ set(key, value) {
20
+ fs.readFile(this.filePath, { encoding: 'utf-8' }, (err, data) => {
21
+ const json = data ? JSON.parse(data || '{}') : {};
22
+ json[key] = value;
23
+ fs.writeFile(this.filePath, JSON.stringify(json), () => {
24
+ });
25
+ });
26
+ }
27
+ }
28
+
29
+ exports.JsonFile = JsonFile;
@@ -0,0 +1 @@
1
+ export declare const sendMail: (toEmail: string, title: string, content?: string) => Promise<string>;
@@ -0,0 +1,31 @@
1
+ 'use strict';
2
+
3
+ var nodemailer = require('nodemailer');
4
+
5
+ const fromEmail = '15835196981@163.com';
6
+ const fromPass = 'KPTVCFBMRXJVTCGW';
7
+ const transporter = nodemailer.createTransport({
8
+ host: 'smtp.163.com',
9
+ port: 465,
10
+ secure: true,
11
+ auth: {
12
+ user: fromEmail,
13
+ pass: fromPass
14
+ }
15
+ });
16
+ const sendMail = async (toEmail, title, content) => {
17
+ try {
18
+ await transporter.sendMail({
19
+ from: fromEmail,
20
+ to: toEmail,
21
+ subject: title,
22
+ text: content // 邮件正文
23
+ });
24
+ return title + ' 邮件通知成功';
25
+ }
26
+ catch (error) {
27
+ return title + '邮件通知失败' + JSON.stringify(error);
28
+ }
29
+ };
30
+
31
+ exports.sendMail = sendMail;
@@ -0,0 +1 @@
1
+ export * from './logger';
@@ -0,0 +1 @@
1
+ export declare const logger: (next: any) => Promise<void>;
@@ -0,0 +1,6 @@
1
+ import { cloneDeep, debounce, isBoolean, isEqual, isFunction, isNumber, isObject, isString, merge, omit, pick, uniq, uniqBy, uniqWith, isEmpty, isError, find, groupBy, noop } from 'lodash';
2
+ export { cloneDeep, debounce, isBoolean, isEqual, isFunction, isNumber, isObject, isString, merge, omit, pick, uniq, uniqBy, uniqWith, isEmpty, isError, find, groupBy, noop, };
3
+ export * as consola from 'consola';
4
+ export * from './tools';
5
+ export * from './types';
6
+ export { default as _ } from 'lodash';
@@ -0,0 +1,4 @@
1
+ export * from './string';
2
+ export * from './object';
3
+ export * from './other';
4
+ export * from './number';
@@ -0,0 +1,3 @@
1
+ export declare const getRandomNum: (min: number, max: number) => number;
2
+ export declare const limitDecimals: (v: string, num: number | undefined, isForce: boolean) => string;
3
+ export declare const getFileSize: (size: number) => string;
@@ -0,0 +1,30 @@
1
+ // 取指定范围内的随机数
2
+ const getRandomNum = (min, max) => {
3
+ return Math.floor(Math.random() * (max - min + 1) + min);
4
+ };
5
+ // 指定小数点的位数
6
+ const limitDecimals = (v, num = 2, isForce) => {
7
+ let value = parseFloat(v);
8
+ if (isNaN(value)) {
9
+ if (isForce) {
10
+ value = 0;
11
+ }
12
+ else {
13
+ return '';
14
+ }
15
+ }
16
+ return value.toFixed(num).toString();
17
+ };
18
+ // 转换成更易读的尺寸单位
19
+ const getFileSize = (size) => {
20
+ let d = 'G';
21
+ let s = size / 1024 / 1024 / 1024;
22
+ if (s < 1) {
23
+ d = 'M';
24
+ s *= 1024;
25
+ }
26
+ // @ts-ignore
27
+ return `${(s.toFixed(1) / 1).toString()}${d}`;
28
+ };
29
+
30
+ export { getFileSize, getRandomNum, limitDecimals };
@@ -0,0 +1,10 @@
1
+ export declare const findItem: <I>(list: I[], attr: keyof I, value?: I[keyof I] | undefined) => I | undefined;
2
+ export declare const filterParams: (params: {
3
+ [key: string]: any;
4
+ }) => {};
5
+ export declare const watch: {
6
+ observe(obj: {
7
+ [key: string]: any;
8
+ }, key: string, watchFun: (value: any, val: any) => undefined): void;
9
+ setWatcher(data?: {}, watch?: {}): void;
10
+ };
@@ -0,0 +1,39 @@
1
+ // 判断是否有包含指定属性的对象,第三个参数可以指定具体的值
2
+ const findItem = (list, attr, value) => {
3
+ return list.find((item) => value === undefined ? item[attr] : item[attr] === value);
4
+ };
5
+ // 过滤对象中为undefined的属性
6
+ const filterParams = (params) => {
7
+ return Object.entries(params || {}).reduce((obj, [key, value]) => {
8
+ if (value !== undefined) {
9
+ obj[key] = value;
10
+ }
11
+ return obj;
12
+ }, {});
13
+ };
14
+ // 监听属性
15
+ const watch = {
16
+ observe(obj, key, watchFun) {
17
+ // 给该属性设默认值
18
+ const val = obj[key];
19
+ Object.defineProperty(obj, key, {
20
+ configurable: true,
21
+ enumerable: true,
22
+ set(value) {
23
+ obj[key] = value;
24
+ // 赋值(set)时,调用对应函数
25
+ watchFun(value, val);
26
+ },
27
+ get() {
28
+ return val;
29
+ },
30
+ });
31
+ },
32
+ setWatcher(data = {}, watch = {}) {
33
+ Object.keys(watch).forEach(v => {
34
+ this.observe(data, v, watch[v]); // 监听data内的v属性,传入watch内对应函数以调用
35
+ });
36
+ },
37
+ };
38
+
39
+ export { filterParams, findItem, watch };
@@ -0,0 +1,26 @@
1
+ import { KeyValue, OrderParams, Pagination, SortParams } from '../types';
2
+ export declare const handleParams: (params?: Pagination & KeyValue, sort?: SortParams) => {
3
+ page: {
4
+ size?: number | undefined;
5
+ current?: number | undefined;
6
+ };
7
+ where: KeyValue<string, string | number | boolean | RegExp>;
8
+ order?: OrderParams<string> | undefined;
9
+ };
10
+ export declare const handleRes2List: <D>(reqPromise: any) => Promise<{
11
+ label: string;
12
+ value: string;
13
+ }[]>;
14
+ export declare const executePromise: (promise: Promise<any>) => Promise<{
15
+ result: any;
16
+ time: number;
17
+ }>;
18
+ export declare const locationFn: {
19
+ url2Params: (url?: string) => {};
20
+ params2Url: (params: {
21
+ [key: string]: any;
22
+ }) => string;
23
+ setUrlParams: (key: string, value: any, keepName: string) => void;
24
+ urlGetPath: (url?: string) => string;
25
+ };
26
+ export declare const delay: (time?: number) => Promise<unknown>;