@hzab/data-model 1.7.0 → 1.7.2

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/CHANGELOG.md CHANGED
@@ -1,3 +1,11 @@
1
+ # @hzab/data-model@1.7.2
2
+
3
+ feat: axios.js getCancelTokenSource 获取 axios.CancelToken.source, 用于取消请求
4
+
5
+ # @hzab/data-model@1.7.1
6
+
7
+ feat: sortKey/orderByColumn 支持对象格式,支持定义数据类型、自定义数据处理函数;自定义数据对比函数
8
+
1
9
  # @hzab/data-model@1.7.0
2
10
 
3
11
  feat: 本地数据支持排序查询列表数据
package/README.md CHANGED
@@ -188,10 +188,21 @@ function Demo({ orgId }) {
188
188
 
189
189
  ### query 配置
190
190
 
191
- | 参数 | 类型 | 必填 | 默认值 | 说明 |
192
- | --------------------- | -------------------- | ---- | ------ | --------------------------------------------------------- |
193
- | sortKey/orderByColumn | string/Array[string] | 否 | - | 排序字段,支持多维度排序,按照传入的顺序排优先级 |
194
- | sortType/isAsc | string/Object | 否 | - | 排序规则,支持对象配置各字段排序规则 正序: asc 倒序: desc |
191
+ | 参数 | 类型 | 必填 | 默认值 | 说明 |
192
+ | --------------------- | ----------------------------------------- | ---- | ------ | --------------------------------------------------------- |
193
+ | sortKey/orderByColumn | string/Object/Array[string]/Array[Object] | 否 | - | 排序字段,支持多维度排序,按照传入的顺序排优先级 |
194
+ | sortType/isAsc | string/Object | 否 | - | 排序规则,支持对象配置各字段排序规则 正序: asc 倒序: desc |
195
+
196
+ ### sortKey/orderByColumn Object 配置
197
+
198
+ | 参数 | 类型 | 必填 | 默认值 | 说明 |
199
+ | ------------- | -------- | ---- | ------ | -------------------------------------------------------------------------------------------------------------------------- |
200
+ | path | string | 否 | - | path 取值路径字符串 |
201
+ | type | string | 否 | - | 值类型 用于进行内部相关转换 date |
202
+ | customFormat | Function | 否 | - | 自定义数据处理函数 |
203
+ | customCompare | Function | 否 | - | 自定义数据对比函数 (a, b)=>number,如果 a 小于 b,返回值为负数,如果 a 大于 b,返回值为正数,如果两个元素相等,返回值为 0。NaN 被视为 0。 |
204
+
205
+ - type 目前支持的类型: date
195
206
 
196
207
  # 组件开发流程
197
208
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hzab/data-model",
3
- "version": "1.7.0",
3
+ "version": "1.7.2",
4
4
  "description": "data model",
5
5
  "main": "src",
6
6
  "scripts": {
@@ -43,6 +43,7 @@
43
43
  "js-cookie": ">=3.0.5"
44
44
  },
45
45
  "dependencies": {
46
+ "dayjs": "^1.11.7",
46
47
  "axios": ">=1.6.2",
47
48
  "js-cookie": ">=3.0.5",
48
49
  "nanoid": "^3.3.7"
package/src/ArrayUtils.js CHANGED
@@ -1,5 +1,6 @@
1
1
  import { nanoid } from "nanoid";
2
2
  import _ from "lodash";
3
+ import dayjs from "dayjs";
3
4
 
4
5
  export const STATE_CODE = {
5
6
  SUC: 200,
@@ -91,39 +92,49 @@ export class ArrayUtils {
91
92
  if (!sortKey && !sortType) {
92
93
  return _list;
93
94
  }
94
- const keys = Array.isArray(sortKey) ? sortKey : [sortKey];
95
+ const paths = Array.isArray(sortKey) ? sortKey : [sortKey];
95
96
  _list.sort((a, b) => {
96
- return this.compareData(a, b, { keys, sortType });
97
+ return this.compareData(a, b, { paths, sortType });
97
98
  });
98
99
  return _list;
99
100
  }
100
101
 
101
102
  /**
102
- * 根据传入的 keys 比较对象目标数据先后顺序
103
+ * 根据传入的 paths 比较对象目标数据先后顺序
103
104
  * @param dataA
104
105
  * @param dataB
105
106
  * @param opt
106
- * @param opt.keys 数据 key 数组
107
- * @param opt.keyIdx 当前数据下标
107
+ * @param opt.paths 数据 path 数组
108
+ * @param opt.pathIdx 当前数据下标
108
109
  * @param opt.sortType 排序规则
109
110
  * @returns
110
111
  */
111
- compareData(dataA, dataB, opt = { keys, keyIdx: 0, sortType: "desc" }) {
112
- const { keys, keyIdx = 0, sortType = "desc" } = opt;
113
- const key = keys[keyIdx];
112
+ compareData(dataA, dataB, opt = { paths, pathIdx: 0, sortType: "desc" }) {
113
+ const { paths, pathIdx = 0, sortType = "desc" } = opt;
114
+ const path = paths[pathIdx];
115
+ let _path = path;
114
116
 
117
+ if (path && typeof path === "object") {
118
+ // 外部自定义比较函数
119
+ if (typeof path.customCompare === "function") {
120
+ return direction * path.customCompare(dataA, dataB, opt);
121
+ }
122
+ _path = path.path;
123
+ }
124
+
125
+ /** 排序规则 */
115
126
  let direction = 1;
116
127
  // 兼容 string 及 object 类型
117
128
  let _sortType = sortType;
118
129
  if (typeof _sortType === "object") {
119
- _sortType = _sortType[key];
130
+ _sortType = _sortType[_path];
120
131
  }
121
132
  if (_sortType === "asc") {
122
133
  direction = -1;
123
134
  }
124
135
 
125
- const valueA = _.get(dataA, key);
126
- const valueB = _.get(dataB, key);
136
+ let valueA = this.handleValByKey(dataA, path);
137
+ let valueB = this.handleValByKey(dataB, path);
127
138
  if (valueA < valueB) {
128
139
  return -1 * direction;
129
140
  }
@@ -131,13 +142,42 @@ export class ArrayUtils {
131
142
  return 1 * direction;
132
143
  }
133
144
 
134
- if (keyIdx + 1 >= keys.length) {
145
+ if (pathIdx + 1 >= paths.length) {
135
146
  return 0;
136
147
  }
137
148
 
138
149
  if (valueA == valueB) {
139
150
  // 继续使用次选排序规则排序
140
- return this.compareData(dataA, dataB, { ...opt, keyIdx: keyIdx + 1 });
151
+ return this.compareData(dataA, dataB, { ...opt, pathIdx: pathIdx + 1 });
152
+ }
153
+ }
154
+
155
+ /**
156
+ * 根据 path 配置对数据进行处理
157
+ * @param {Object} data 数据
158
+ * @param {string/Object} path 取值路径字符串 或 取值配置
159
+ * @param {string} path.path 取值路径字符串
160
+ * @param {string} path.type 值类型 用于进行相关转换
161
+ * @param {Function} path.customFormat 自定义数据处理函数
162
+ */
163
+ handleValByKey(data, path) {
164
+ if (typeof path === "string") {
165
+ return _.get(data, path);
166
+ }
167
+ if (path === null || path === undefined) {
168
+ return;
169
+ }
170
+ if (typeof path === "object") {
171
+ let val = _.get(data, path.path);
172
+ // 日期格式转为时间戳进行比较
173
+ if (path?.type === "date") {
174
+ val = dayjs(val).valueOf();
175
+ }
176
+ // 外部自定义数据格式函数,便于排序比较
177
+ if (typeof path?.customFormat === "function") {
178
+ val = customFormat(val);
179
+ }
180
+ return val;
141
181
  }
142
182
  }
143
183
 
package/src/axios.js CHANGED
@@ -113,8 +113,20 @@ export function setAxAuthorization(token) {
113
113
  setAxHeaders("Authorization", token);
114
114
  }
115
115
 
116
+ /**
117
+ * 设置 baseUrl
118
+ * @param {*} url
119
+ */
116
120
  export function setAxBaseUrl(url) {
117
121
  setAxDefaults("baseURL", url);
118
122
  }
119
123
 
124
+ /**
125
+ * 获取 axios.CancelToken.source, 用于取消请求
126
+ * @returns
127
+ */
128
+ export function getCancelTokenSource() {
129
+ return axios.CancelToken.source();
130
+ }
131
+
120
132
  export default axios;