@fastcar/core 0.3.7 → 0.3.8

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fastcar/core",
3
- "version": "0.3.7",
3
+ "version": "0.3.8",
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> {
@@ -32,22 +32,35 @@ export default class DataMap<K, V extends Object> extends Map<K, V> {
32
32
  if (!sorts || sorts?.length == 0) {
33
33
  return list;
34
34
  }
35
- let total = sorts.length;
35
+
36
36
  list.sort((a, b) => {
37
37
  let resultNum = 0;
38
38
  sorts.some((f, index) => {
39
+ let cindex = index + 1;
39
40
  let field = f.field;
40
41
  let aValue = Reflect.get(a, field);
41
42
  let bValue = Reflect.get(b, field);
42
- let flag = f.compare ? f.compare(aValue, bValue) : aValue > bValue;
43
- resultNum = flag ? total - index : index - total;
43
+
44
+ if (f.compare) {
45
+ let flag = f.compare(aValue, bValue);
46
+ if (flag == 0) {
47
+ return false;
48
+ }
49
+ resultNum = flag > 0 ? cindex : -cindex;
50
+ } else {
51
+ if (aValue == bValue) {
52
+ return false;
53
+ }
54
+
55
+ resultNum = aValue > bValue ? cindex : -cindex;
56
+ }
44
57
 
45
58
  //降序则倒着
46
59
  if (f.order) {
47
60
  resultNum = -resultNum;
48
61
  }
49
62
 
50
- return !!flag;
63
+ return true;
51
64
  });
52
65
  return resultNum;
53
66
  });
@@ -23,20 +23,31 @@ class DataMap extends Map {
23
23
  if (!sorts || sorts?.length == 0) {
24
24
  return list;
25
25
  }
26
- let total = sorts.length;
27
26
  list.sort((a, b) => {
28
27
  let resultNum = 0;
29
28
  sorts.some((f, index) => {
29
+ let cindex = index + 1;
30
30
  let field = f.field;
31
31
  let aValue = Reflect.get(a, field);
32
32
  let bValue = Reflect.get(b, field);
33
- let flag = f.compare ? f.compare(aValue, bValue) : aValue > bValue;
34
- resultNum = flag ? total - index : index - total;
33
+ if (f.compare) {
34
+ let flag = f.compare(aValue, bValue);
35
+ if (flag == 0) {
36
+ return false;
37
+ }
38
+ resultNum = flag > 0 ? cindex : -cindex;
39
+ }
40
+ else {
41
+ if (aValue == bValue) {
42
+ return false;
43
+ }
44
+ resultNum = aValue > bValue ? cindex : -cindex;
45
+ }
35
46
  //降序则倒着
36
47
  if (f.order) {
37
48
  resultNum = -resultNum;
38
49
  }
39
- return !!flag;
50
+ return true;
40
51
  });
41
52
  return resultNum;
42
53
  });