@fastcar/core 0.3.7 → 0.3.9

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/index.d.ts CHANGED
@@ -260,6 +260,8 @@ export class ValidError {
260
260
  }
261
261
 
262
262
  export class DataMap<K, V extends Object> extends Map<K, V> {
263
+ init(list: Array<V>, key: string): void;
264
+
263
265
  toValues(): V[];
264
266
 
265
267
  toKeys(): K[];
@@ -275,5 +277,5 @@ export class DataMap<K, V extends Object> extends Map<K, V> {
275
277
  * @params atts代表属性键值对匹配
276
278
  *
277
279
  */
278
- findByAtts(atts: { [key: string]: any }): V[];
280
+ findByAtts(atts: { [key: string]: any | any[] }): V[];
279
281
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fastcar/core",
3
- "version": "0.3.7",
3
+ "version": "0.3.9",
4
4
  "homepage": "https://github.com/williamDazhangyu/fast-car",
5
5
  "main": "target/index.js",
6
6
  "author": "william_zhong",
@@ -1,7 +1,7 @@
1
1
  export type FIELDTYPE = {
2
2
  field: string;
3
3
  order?: boolean; //是否为倒序 order true为倒序
4
- compare?: (a: any, b: any) => boolean;
4
+ compare?: (a: any, b: any) => -1 | 0 | 1; // -1 0 1 //-1为小 0为相等 1为大
5
5
  };
6
6
 
7
7
  export default class DataMap<K, V extends Object> extends Map<K, V> {
@@ -9,6 +9,13 @@ export default class DataMap<K, V extends Object> extends Map<K, V> {
9
9
  super();
10
10
  }
11
11
 
12
+ init(list: Array<V>, key: string) {
13
+ this.clear();
14
+ list.forEach((item) => {
15
+ this.set(Reflect.get(item, key) as K, item);
16
+ });
17
+ }
18
+
12
19
  toValues(): V[] {
13
20
  return [...this.values()];
14
21
  }
@@ -32,22 +39,35 @@ export default class DataMap<K, V extends Object> extends Map<K, V> {
32
39
  if (!sorts || sorts?.length == 0) {
33
40
  return list;
34
41
  }
35
- let total = sorts.length;
42
+
36
43
  list.sort((a, b) => {
37
44
  let resultNum = 0;
38
45
  sorts.some((f, index) => {
46
+ let cindex = index + 1;
39
47
  let field = f.field;
40
48
  let aValue = Reflect.get(a, field);
41
49
  let bValue = Reflect.get(b, field);
42
- let flag = f.compare ? f.compare(aValue, bValue) : aValue > bValue;
43
- resultNum = flag ? total - index : index - total;
50
+
51
+ if (f.compare) {
52
+ let flag = f.compare(aValue, bValue);
53
+ if (flag == 0) {
54
+ return false;
55
+ }
56
+ resultNum = flag > 0 ? cindex : -cindex;
57
+ } else {
58
+ if (aValue == bValue) {
59
+ return false;
60
+ }
61
+
62
+ resultNum = aValue > bValue ? cindex : -cindex;
63
+ }
44
64
 
45
65
  //降序则倒着
46
66
  if (f.order) {
47
67
  resultNum = -resultNum;
48
68
  }
49
69
 
50
- return !!flag;
70
+ return true;
51
71
  });
52
72
  return resultNum;
53
73
  });
@@ -70,7 +90,7 @@ export default class DataMap<K, V extends Object> extends Map<K, V> {
70
90
  //这边判断 是不是一个复合属性
71
91
  if (Reflect.has(item, key)) {
72
92
  let itemV = Reflect.get(item, key);
73
- return itemV == v;
93
+ return Array.isArray(v) ? v.includes(itemV) : itemV == v;
74
94
  } else {
75
95
  let keyList = key.split(".");
76
96
  if (keyList.length > 1) {
@@ -87,7 +107,7 @@ export default class DataMap<K, V extends Object> extends Map<K, V> {
87
107
  return false;
88
108
  }
89
109
 
90
- return tmpV == v;
110
+ return Array.isArray(v) ? v.includes(tmpV) : tmpV == v;
91
111
  }
92
112
  }
93
113
 
@@ -4,6 +4,12 @@ class DataMap extends Map {
4
4
  constructor() {
5
5
  super();
6
6
  }
7
+ init(list, key) {
8
+ this.clear();
9
+ list.forEach((item) => {
10
+ this.set(Reflect.get(item, key), item);
11
+ });
12
+ }
7
13
  toValues() {
8
14
  return [...this.values()];
9
15
  }
@@ -23,20 +29,31 @@ class DataMap extends Map {
23
29
  if (!sorts || sorts?.length == 0) {
24
30
  return list;
25
31
  }
26
- let total = sorts.length;
27
32
  list.sort((a, b) => {
28
33
  let resultNum = 0;
29
34
  sorts.some((f, index) => {
35
+ let cindex = index + 1;
30
36
  let field = f.field;
31
37
  let aValue = Reflect.get(a, field);
32
38
  let bValue = Reflect.get(b, field);
33
- let flag = f.compare ? f.compare(aValue, bValue) : aValue > bValue;
34
- resultNum = flag ? total - index : index - total;
39
+ if (f.compare) {
40
+ let flag = f.compare(aValue, bValue);
41
+ if (flag == 0) {
42
+ return false;
43
+ }
44
+ resultNum = flag > 0 ? cindex : -cindex;
45
+ }
46
+ else {
47
+ if (aValue == bValue) {
48
+ return false;
49
+ }
50
+ resultNum = aValue > bValue ? cindex : -cindex;
51
+ }
35
52
  //降序则倒着
36
53
  if (f.order) {
37
54
  resultNum = -resultNum;
38
55
  }
39
- return !!flag;
56
+ return true;
40
57
  });
41
58
  return resultNum;
42
59
  });
@@ -56,7 +73,7 @@ class DataMap extends Map {
56
73
  //这边判断 是不是一个复合属性
57
74
  if (Reflect.has(item, key)) {
58
75
  let itemV = Reflect.get(item, key);
59
- return itemV == v;
76
+ return Array.isArray(v) ? v.includes(itemV) : itemV == v;
60
77
  }
61
78
  else {
62
79
  let keyList = key.split(".");
@@ -72,7 +89,7 @@ class DataMap extends Map {
72
89
  if (!f) {
73
90
  return false;
74
91
  }
75
- return tmpV == v;
92
+ return Array.isArray(v) ? v.includes(tmpV) : tmpV == v;
76
93
  }
77
94
  }
78
95
  return false;