@univerjs/sheets-sort 1.5.1 → 1.6.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/lib/cjs/index.js CHANGED
@@ -11,54 +11,93 @@ let SortType = /* @__PURE__ */ function(SortType) {
11
11
  }({});
12
12
 
13
13
  // src/controllers/utils.ts
14
- const removeStringSymbol = (str) => {
15
- return str.replace(/-/gi, "").replace(/'/gi, "");
16
- };
17
- const compareNull = (a1, a2) => {
18
- const isA1Null = a1 === null || a1 === "";
19
- const isA2Null = a2 === null || a2 === "";
20
- if (isA1Null && isA2Null) return 0;
21
- if (isA1Null) return 1;
22
- if (isA2Null) return -1;
23
- return null;
24
- };
25
- const compareNumber = (a1, a2, type) => {
26
- const isA1Num = typeof a1 === "number";
27
- const isA2Num = typeof a2 === "number";
28
- if (isA1Num && isA2Num) {
29
- if (a1 < a2) return type === "asc" ? -1 : 1;
30
- if (a1 > a2) return type === "asc" ? 1 : -1;
31
- return 0;
14
+ const HASH_CHAR_CODE = 35;
15
+ const removeStringSymbol = (str) => str.replace(/-/gi, "").replace(/'/gi, "");
16
+ const isNullValue = (cell) => !cell || cell.v == null && cell.p == null;
17
+ /**
18
+ * Get the value kind of a cell. The kind decides the group the cell is sorted in, so it must not
19
+ * depend on the stringified value: a `TRUE` Boolean cell is a logical value, not the text "TRUE".
20
+ */
21
+ function getSortRank(cell) {
22
+ var _paragraph$body2;
23
+ if (!cell) return 4;
24
+ const value = cell.v;
25
+ const paragraph = cell.p;
26
+ if (value == null) {
27
+ var _paragraph$body;
28
+ return (paragraph === null || paragraph === void 0 || (_paragraph$body = paragraph.body) === null || _paragraph$body === void 0 ? void 0 : _paragraph$body.dataStream) ? 1 : 4;
32
29
  }
33
- if (isA1Num) return type === "asc" ? 1 : -1;
34
- if (isA2Num) return type === "asc" ? -1 : 1;
35
- return null;
36
- };
37
- const compareString = (a1, a2, type) => {
38
- const isA1Str = typeof a1 === "string";
39
- const isA2Str = typeof a2 === "string";
40
- if (isA1Str) a1 = removeStringSymbol(a1.toLocaleLowerCase());
41
- if (isA2Str) a2 = removeStringSymbol(a2.toLocaleLowerCase());
42
- if (!isA1Str && !isA2Str) return null;
43
- if (isA1Str && isA2Str) {
44
- const a1AsString = a1;
45
- const a2AsString = a2;
46
- if (a1AsString < a2AsString) return type === "asc" ? -1 : 1;
47
- if (a1AsString > a2AsString) return type === "asc" ? 1 : -1;
48
- return 0;
30
+ if (paragraph === null || paragraph === void 0 || (_paragraph$body2 = paragraph.body) === null || _paragraph$body2 === void 0 ? void 0 : _paragraph$body2.dataStream) return 1;
31
+ switch (cell.t) {
32
+ case _univerjs_core.CellValueType.NUMBER: return getNumberRank(value);
33
+ case _univerjs_core.CellValueType.STRING: return getStringRank(value);
34
+ case _univerjs_core.CellValueType.BOOLEAN: return 2;
35
+ case _univerjs_core.CellValueType.FORCE_STRING: return value === "" ? 4 : 1;
36
+ default: return getUntypedCellRank(value);
49
37
  }
50
- if (isA1Str) return type === "asc" ? 1 : -1;
51
- if (isA2Str) return type === "asc" ? -1 : 1;
52
- return null;
53
- };
54
- const isNullValue = (cell) => {
55
- if (!cell) return true;
56
- if (Object.keys(cell).length === 0) return true;
57
- if ((cell === null || cell === void 0 ? void 0 : cell.v) == null && (cell === null || cell === void 0 ? void 0 : cell.p) == null) return true;
58
- return false;
59
- };
38
+ }
39
+ function getNumberRank(value) {
40
+ if (typeof value !== "number") return Number.isNaN(Number.parseFloat(`${value}`)) ? 1 : 0;
41
+ return Number.isNaN(value) ? 1 : 0;
42
+ }
43
+ function getStringRank(value) {
44
+ if (typeof value === "number") return 0;
45
+ if (value === "") return 4;
46
+ if (typeof value === "string" && value.charCodeAt(0) === HASH_CHAR_CODE && _univerjs_engine_formula.ERROR_TYPE_SET.has(value)) return 3;
47
+ return 1;
48
+ }
49
+ function getUntypedCellRank(value) {
50
+ if (typeof value === "number") return Number.isNaN(value) ? 1 : 0;
51
+ if (typeof value === "boolean") return 2;
52
+ if (value === "") return 4;
53
+ return 1;
54
+ }
55
+ function readNumber(cell) {
56
+ const value = cell === null || cell === void 0 ? void 0 : cell.v;
57
+ return typeof value === "number" ? value : Number.parseFloat(`${value}`);
58
+ }
59
+ function readText(cell) {
60
+ var _cell$p;
61
+ const richText = cell === null || cell === void 0 || (_cell$p = cell.p) === null || _cell$p === void 0 || (_cell$p = _cell$p.body) === null || _cell$p === void 0 ? void 0 : _cell$p.dataStream;
62
+ if (richText) return richText;
63
+ const value = cell === null || cell === void 0 ? void 0 : cell.v;
64
+ return typeof value === "string" ? value : `${value}`;
65
+ }
66
+ function readLogical(cell) {
67
+ const value = cell === null || cell === void 0 ? void 0 : cell.v;
68
+ if (value === 1 || value === true) return 1;
69
+ if (value === 0 || value === false) return 0;
70
+ return cell ? Number((0, _univerjs_sheets.getCellValue)(_univerjs_core.CellValueType.BOOLEAN, cell)) : 0;
71
+ }
72
+ function compareText(a, b) {
73
+ const left = removeStringSymbol(a.toLocaleLowerCase());
74
+ const right = removeStringSymbol(b.toLocaleLowerCase());
75
+ if (left < right) return -1;
76
+ if (left > right) return 1;
77
+ return 0;
78
+ }
79
+ /**
80
+ * Compare two cells the same way Excel sorts a range: by value kind first, then by value inside the
81
+ * kind. Blank cells are always sorted last, whichever direction is used.
82
+ */
83
+ function compareCells(type, a, b) {
84
+ const rankA = getSortRank(a);
85
+ const rankB = getSortRank(b);
86
+ if (rankA === 4 || rankB === 4) {
87
+ if (rankA === rankB) return 0;
88
+ return rankA === 4 ? 1 : -1;
89
+ }
90
+ const direction = type === "asc" ? 1 : -1;
91
+ if (rankA !== rankB) return direction * (rankA - rankB);
92
+ switch (rankA) {
93
+ case 0: return direction * Math.sign(readNumber(a) - readNumber(b));
94
+ case 2: return direction * Math.sign(readLogical(a) - readLogical(b));
95
+ case 3: return 0;
96
+ default: return direction * compareText(readText(a), readText(b));
97
+ }
98
+ }
60
99
 
61
- // \0@oxc-project+runtime@0.157.0/helpers/esm/typeof.js
100
+ // \0@oxc-project+runtime@0.158.0/helpers/esm/typeof.js
62
101
  function _typeof(o) {
63
102
  "@babel/helpers - typeof";
64
103
  return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(o) {
@@ -68,7 +107,7 @@ function _typeof(o) {
68
107
  }, _typeof(o);
69
108
  }
70
109
 
71
- // \0@oxc-project+runtime@0.157.0/helpers/esm/toPrimitive.js
110
+ // \0@oxc-project+runtime@0.158.0/helpers/esm/toPrimitive.js
72
111
  function toPrimitive(t, e) {
73
112
  if ("object" != _typeof(t) || !t) return t;
74
113
  var r;
@@ -80,13 +119,13 @@ function toPrimitive(t, e) {
80
119
  return ("string" === e ? String : Number)(t);
81
120
  }
82
121
 
83
- // \0@oxc-project+runtime@0.157.0/helpers/esm/toPropertyKey.js
122
+ // \0@oxc-project+runtime@0.158.0/helpers/esm/toPropertyKey.js
84
123
  function toPropertyKey(t) {
85
124
  var i = toPrimitive(t, "string");
86
125
  return "symbol" == _typeof(i) ? i : i + "";
87
126
  }
88
127
 
89
- // \0@oxc-project+runtime@0.157.0/helpers/esm/defineProperty.js
128
+ // \0@oxc-project+runtime@0.158.0/helpers/esm/defineProperty.js
90
129
  function _defineProperty(e, r, t) {
91
130
  return (r = toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
92
131
  value: t,
@@ -96,14 +135,14 @@ function _defineProperty(e, r, t) {
96
135
  }) : e[r] = t, e;
97
136
  }
98
137
 
99
- // \0@oxc-project+runtime@0.157.0/helpers/esm/decorateParam.js
138
+ // \0@oxc-project+runtime@0.158.0/helpers/esm/decorateParam.js
100
139
  function __decorateParam(paramIndex, decorator) {
101
140
  return function(target, key) {
102
141
  decorator(target, key, paramIndex);
103
142
  };
104
143
  }
105
144
 
106
- // \0@oxc-project+runtime@0.157.0/helpers/esm/decorate.js
145
+ // \0@oxc-project+runtime@0.158.0/helpers/esm/decorate.js
107
146
  function __decorate(decorators, target, key, desc) {
108
147
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
109
148
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
@@ -274,6 +313,21 @@ function reorderFnGenerator(orderRules, valueCompare) {
274
313
  }
275
314
 
276
315
  // src/controllers/sheets-sort.controller.ts
316
+ /**
317
+ * Copyright 2023-present DreamNum Co., Ltd.
318
+ *
319
+ * Licensed under the Apache License, Version 2.0 (the "License");
320
+ * you may not use this file except in compliance with the License.
321
+ * You may obtain a copy of the License at
322
+ *
323
+ * http://www.apache.org/licenses/LICENSE-2.0
324
+ *
325
+ * Unless required by applicable law or agreed to in writing, software
326
+ * distributed under the License is distributed on an "AS IS" BASIS,
327
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
328
+ * See the License for the specific language governing permissions and
329
+ * limitations under the License.
330
+ */
277
331
  let SheetsSortController = class SheetsSortController extends _univerjs_core.Disposable {
278
332
  constructor(_commandService, _sortService) {
279
333
  super();
@@ -286,42 +340,14 @@ let SheetsSortController = class SheetsSortController extends _univerjs_core.Dis
286
340
  [SortRangeCommand].forEach((command) => this.disposeWithMe(this._commandService.registerCommand(command)));
287
341
  }
288
342
  _registerCompareFns() {
289
- const commonFn = (type, a, b) => {
290
- const valueA = this._getCommonValue(a);
291
- const valueB = this._getCommonValue(b);
292
- const compareTypeFns = [
293
- compareNull,
294
- compareString,
295
- compareNumber
296
- ];
297
- for (let i = 0; i < compareTypeFns.length; i++) {
298
- const res = compareTypeFns[i](valueA, valueB, type);
299
- if (res !== null) return res;
300
- }
301
- return null;
302
- };
303
- this._sortService.registerCompareFn(commonFn);
304
- }
305
- _getCommonValue(a) {
306
- var _a$p;
307
- if (isNullValue(a)) return null;
308
- const richTextValue = a === null || a === void 0 || (_a$p = a.p) === null || _a$p === void 0 || (_a$p = _a$p.body) === null || _a$p === void 0 ? void 0 : _a$p.dataStream;
309
- if (richTextValue) return richTextValue;
310
- if ((a === null || a === void 0 ? void 0 : a.t) === _univerjs_core.CellValueType.NUMBER) return Number.parseFloat(`${a.v}`);
311
- if ((a === null || a === void 0 ? void 0 : a.t) === _univerjs_core.CellValueType.STRING) {
312
- if (typeof a.v === "number") return a.v;
313
- return `${a.v}`;
314
- }
315
- if ((a === null || a === void 0 ? void 0 : a.t) === _univerjs_core.CellValueType.BOOLEAN) return `${a.v}`;
316
- if ((a === null || a === void 0 ? void 0 : a.t) === _univerjs_core.CellValueType.FORCE_STRING) return Number.parseFloat(`${a.v}`);
317
- return `${a === null || a === void 0 ? void 0 : a.v}`;
343
+ this._sortService.registerCompareFn(compareCells);
318
344
  }
319
345
  };
320
346
  SheetsSortController = __decorate([__decorateParam(0, _univerjs_core.ICommandService), __decorateParam(1, (0, _univerjs_core.Inject)(SheetsSortService))], SheetsSortController);
321
347
 
322
348
  // package.json
323
349
  var name = "@univerjs/sheets-sort";
324
- var version = "1.5.1";
350
+ var version = "1.6.0";
325
351
 
326
352
  // src/config/config.ts
327
353
  /**
package/lib/es/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { CellValueType, CommandType, DependentOn, Disposable, ICommandService, IConfigService, IUniverInstanceService, Inject, Injector, Plugin, Rectangle, UniverInstanceType, merge, sequenceExecute } from "@univerjs/core";
2
- import { ReorderRangeCommand, UniverSheetsPlugin, getSheetCommandTarget } from "@univerjs/sheets";
3
- import { FormulaDataModel, UniverFormulaEnginePlugin } from "@univerjs/engine-formula";
2
+ import { ReorderRangeCommand, UniverSheetsPlugin, getCellValue, getSheetCommandTarget } from "@univerjs/sheets";
3
+ import { ERROR_TYPE_SET, FormulaDataModel, UniverFormulaEnginePlugin } from "@univerjs/engine-formula";
4
4
 
5
5
  // src/services/interface.ts
6
6
  let SortType = /* @__PURE__ */ function(SortType) {
@@ -10,54 +10,93 @@ let SortType = /* @__PURE__ */ function(SortType) {
10
10
  }({});
11
11
 
12
12
  // src/controllers/utils.ts
13
- const removeStringSymbol = (str) => {
14
- return str.replace(/-/gi, "").replace(/'/gi, "");
15
- };
16
- const compareNull = (a1, a2) => {
17
- const isA1Null = a1 === null || a1 === "";
18
- const isA2Null = a2 === null || a2 === "";
19
- if (isA1Null && isA2Null) return 0;
20
- if (isA1Null) return 1;
21
- if (isA2Null) return -1;
22
- return null;
23
- };
24
- const compareNumber = (a1, a2, type) => {
25
- const isA1Num = typeof a1 === "number";
26
- const isA2Num = typeof a2 === "number";
27
- if (isA1Num && isA2Num) {
28
- if (a1 < a2) return type === "asc" ? -1 : 1;
29
- if (a1 > a2) return type === "asc" ? 1 : -1;
30
- return 0;
13
+ const HASH_CHAR_CODE = 35;
14
+ const removeStringSymbol = (str) => str.replace(/-/gi, "").replace(/'/gi, "");
15
+ const isNullValue = (cell) => !cell || cell.v == null && cell.p == null;
16
+ /**
17
+ * Get the value kind of a cell. The kind decides the group the cell is sorted in, so it must not
18
+ * depend on the stringified value: a `TRUE` Boolean cell is a logical value, not the text "TRUE".
19
+ */
20
+ function getSortRank(cell) {
21
+ var _paragraph$body2;
22
+ if (!cell) return 4;
23
+ const value = cell.v;
24
+ const paragraph = cell.p;
25
+ if (value == null) {
26
+ var _paragraph$body;
27
+ return (paragraph === null || paragraph === void 0 || (_paragraph$body = paragraph.body) === null || _paragraph$body === void 0 ? void 0 : _paragraph$body.dataStream) ? 1 : 4;
31
28
  }
32
- if (isA1Num) return type === "asc" ? 1 : -1;
33
- if (isA2Num) return type === "asc" ? -1 : 1;
34
- return null;
35
- };
36
- const compareString = (a1, a2, type) => {
37
- const isA1Str = typeof a1 === "string";
38
- const isA2Str = typeof a2 === "string";
39
- if (isA1Str) a1 = removeStringSymbol(a1.toLocaleLowerCase());
40
- if (isA2Str) a2 = removeStringSymbol(a2.toLocaleLowerCase());
41
- if (!isA1Str && !isA2Str) return null;
42
- if (isA1Str && isA2Str) {
43
- const a1AsString = a1;
44
- const a2AsString = a2;
45
- if (a1AsString < a2AsString) return type === "asc" ? -1 : 1;
46
- if (a1AsString > a2AsString) return type === "asc" ? 1 : -1;
47
- return 0;
29
+ if (paragraph === null || paragraph === void 0 || (_paragraph$body2 = paragraph.body) === null || _paragraph$body2 === void 0 ? void 0 : _paragraph$body2.dataStream) return 1;
30
+ switch (cell.t) {
31
+ case CellValueType.NUMBER: return getNumberRank(value);
32
+ case CellValueType.STRING: return getStringRank(value);
33
+ case CellValueType.BOOLEAN: return 2;
34
+ case CellValueType.FORCE_STRING: return value === "" ? 4 : 1;
35
+ default: return getUntypedCellRank(value);
48
36
  }
49
- if (isA1Str) return type === "asc" ? 1 : -1;
50
- if (isA2Str) return type === "asc" ? -1 : 1;
51
- return null;
52
- };
53
- const isNullValue = (cell) => {
54
- if (!cell) return true;
55
- if (Object.keys(cell).length === 0) return true;
56
- if ((cell === null || cell === void 0 ? void 0 : cell.v) == null && (cell === null || cell === void 0 ? void 0 : cell.p) == null) return true;
57
- return false;
58
- };
37
+ }
38
+ function getNumberRank(value) {
39
+ if (typeof value !== "number") return Number.isNaN(Number.parseFloat(`${value}`)) ? 1 : 0;
40
+ return Number.isNaN(value) ? 1 : 0;
41
+ }
42
+ function getStringRank(value) {
43
+ if (typeof value === "number") return 0;
44
+ if (value === "") return 4;
45
+ if (typeof value === "string" && value.charCodeAt(0) === HASH_CHAR_CODE && ERROR_TYPE_SET.has(value)) return 3;
46
+ return 1;
47
+ }
48
+ function getUntypedCellRank(value) {
49
+ if (typeof value === "number") return Number.isNaN(value) ? 1 : 0;
50
+ if (typeof value === "boolean") return 2;
51
+ if (value === "") return 4;
52
+ return 1;
53
+ }
54
+ function readNumber(cell) {
55
+ const value = cell === null || cell === void 0 ? void 0 : cell.v;
56
+ return typeof value === "number" ? value : Number.parseFloat(`${value}`);
57
+ }
58
+ function readText(cell) {
59
+ var _cell$p;
60
+ const richText = cell === null || cell === void 0 || (_cell$p = cell.p) === null || _cell$p === void 0 || (_cell$p = _cell$p.body) === null || _cell$p === void 0 ? void 0 : _cell$p.dataStream;
61
+ if (richText) return richText;
62
+ const value = cell === null || cell === void 0 ? void 0 : cell.v;
63
+ return typeof value === "string" ? value : `${value}`;
64
+ }
65
+ function readLogical(cell) {
66
+ const value = cell === null || cell === void 0 ? void 0 : cell.v;
67
+ if (value === 1 || value === true) return 1;
68
+ if (value === 0 || value === false) return 0;
69
+ return cell ? Number(getCellValue(CellValueType.BOOLEAN, cell)) : 0;
70
+ }
71
+ function compareText(a, b) {
72
+ const left = removeStringSymbol(a.toLocaleLowerCase());
73
+ const right = removeStringSymbol(b.toLocaleLowerCase());
74
+ if (left < right) return -1;
75
+ if (left > right) return 1;
76
+ return 0;
77
+ }
78
+ /**
79
+ * Compare two cells the same way Excel sorts a range: by value kind first, then by value inside the
80
+ * kind. Blank cells are always sorted last, whichever direction is used.
81
+ */
82
+ function compareCells(type, a, b) {
83
+ const rankA = getSortRank(a);
84
+ const rankB = getSortRank(b);
85
+ if (rankA === 4 || rankB === 4) {
86
+ if (rankA === rankB) return 0;
87
+ return rankA === 4 ? 1 : -1;
88
+ }
89
+ const direction = type === "asc" ? 1 : -1;
90
+ if (rankA !== rankB) return direction * (rankA - rankB);
91
+ switch (rankA) {
92
+ case 0: return direction * Math.sign(readNumber(a) - readNumber(b));
93
+ case 2: return direction * Math.sign(readLogical(a) - readLogical(b));
94
+ case 3: return 0;
95
+ default: return direction * compareText(readText(a), readText(b));
96
+ }
97
+ }
59
98
 
60
- // \0@oxc-project+runtime@0.157.0/helpers/esm/typeof.js
99
+ // \0@oxc-project+runtime@0.158.0/helpers/esm/typeof.js
61
100
  function _typeof(o) {
62
101
  "@babel/helpers - typeof";
63
102
  return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(o) {
@@ -67,7 +106,7 @@ function _typeof(o) {
67
106
  }, _typeof(o);
68
107
  }
69
108
 
70
- // \0@oxc-project+runtime@0.157.0/helpers/esm/toPrimitive.js
109
+ // \0@oxc-project+runtime@0.158.0/helpers/esm/toPrimitive.js
71
110
  function toPrimitive(t, e) {
72
111
  if ("object" != _typeof(t) || !t) return t;
73
112
  var r;
@@ -79,13 +118,13 @@ function toPrimitive(t, e) {
79
118
  return ("string" === e ? String : Number)(t);
80
119
  }
81
120
 
82
- // \0@oxc-project+runtime@0.157.0/helpers/esm/toPropertyKey.js
121
+ // \0@oxc-project+runtime@0.158.0/helpers/esm/toPropertyKey.js
83
122
  function toPropertyKey(t) {
84
123
  var i = toPrimitive(t, "string");
85
124
  return "symbol" == _typeof(i) ? i : i + "";
86
125
  }
87
126
 
88
- // \0@oxc-project+runtime@0.157.0/helpers/esm/defineProperty.js
127
+ // \0@oxc-project+runtime@0.158.0/helpers/esm/defineProperty.js
89
128
  function _defineProperty(e, r, t) {
90
129
  return (r = toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
91
130
  value: t,
@@ -95,14 +134,14 @@ function _defineProperty(e, r, t) {
95
134
  }) : e[r] = t, e;
96
135
  }
97
136
 
98
- // \0@oxc-project+runtime@0.157.0/helpers/esm/decorateParam.js
137
+ // \0@oxc-project+runtime@0.158.0/helpers/esm/decorateParam.js
99
138
  function __decorateParam(paramIndex, decorator) {
100
139
  return function(target, key) {
101
140
  decorator(target, key, paramIndex);
102
141
  };
103
142
  }
104
143
 
105
- // \0@oxc-project+runtime@0.157.0/helpers/esm/decorate.js
144
+ // \0@oxc-project+runtime@0.158.0/helpers/esm/decorate.js
106
145
  function __decorate(decorators, target, key, desc) {
107
146
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
108
147
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
@@ -273,6 +312,21 @@ function reorderFnGenerator(orderRules, valueCompare) {
273
312
  }
274
313
 
275
314
  // src/controllers/sheets-sort.controller.ts
315
+ /**
316
+ * Copyright 2023-present DreamNum Co., Ltd.
317
+ *
318
+ * Licensed under the Apache License, Version 2.0 (the "License");
319
+ * you may not use this file except in compliance with the License.
320
+ * You may obtain a copy of the License at
321
+ *
322
+ * http://www.apache.org/licenses/LICENSE-2.0
323
+ *
324
+ * Unless required by applicable law or agreed to in writing, software
325
+ * distributed under the License is distributed on an "AS IS" BASIS,
326
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
327
+ * See the License for the specific language governing permissions and
328
+ * limitations under the License.
329
+ */
276
330
  let SheetsSortController = class SheetsSortController extends Disposable {
277
331
  constructor(_commandService, _sortService) {
278
332
  super();
@@ -285,42 +339,14 @@ let SheetsSortController = class SheetsSortController extends Disposable {
285
339
  [SortRangeCommand].forEach((command) => this.disposeWithMe(this._commandService.registerCommand(command)));
286
340
  }
287
341
  _registerCompareFns() {
288
- const commonFn = (type, a, b) => {
289
- const valueA = this._getCommonValue(a);
290
- const valueB = this._getCommonValue(b);
291
- const compareTypeFns = [
292
- compareNull,
293
- compareString,
294
- compareNumber
295
- ];
296
- for (let i = 0; i < compareTypeFns.length; i++) {
297
- const res = compareTypeFns[i](valueA, valueB, type);
298
- if (res !== null) return res;
299
- }
300
- return null;
301
- };
302
- this._sortService.registerCompareFn(commonFn);
303
- }
304
- _getCommonValue(a) {
305
- var _a$p;
306
- if (isNullValue(a)) return null;
307
- const richTextValue = a === null || a === void 0 || (_a$p = a.p) === null || _a$p === void 0 || (_a$p = _a$p.body) === null || _a$p === void 0 ? void 0 : _a$p.dataStream;
308
- if (richTextValue) return richTextValue;
309
- if ((a === null || a === void 0 ? void 0 : a.t) === CellValueType.NUMBER) return Number.parseFloat(`${a.v}`);
310
- if ((a === null || a === void 0 ? void 0 : a.t) === CellValueType.STRING) {
311
- if (typeof a.v === "number") return a.v;
312
- return `${a.v}`;
313
- }
314
- if ((a === null || a === void 0 ? void 0 : a.t) === CellValueType.BOOLEAN) return `${a.v}`;
315
- if ((a === null || a === void 0 ? void 0 : a.t) === CellValueType.FORCE_STRING) return Number.parseFloat(`${a.v}`);
316
- return `${a === null || a === void 0 ? void 0 : a.v}`;
342
+ this._sortService.registerCompareFn(compareCells);
317
343
  }
318
344
  };
319
345
  SheetsSortController = __decorate([__decorateParam(0, ICommandService), __decorateParam(1, Inject(SheetsSortService))], SheetsSortController);
320
346
 
321
347
  // package.json
322
348
  var name = "@univerjs/sheets-sort";
323
- var version = "1.5.1";
349
+ var version = "1.6.0";
324
350
 
325
351
  // src/config/config.ts
326
352
  /**
package/lib/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { CellValueType, CommandType, DependentOn, Disposable, ICommandService, IConfigService, IUniverInstanceService, Inject, Injector, Plugin, Rectangle, UniverInstanceType, merge, sequenceExecute } from "@univerjs/core";
2
- import { ReorderRangeCommand, UniverSheetsPlugin, getSheetCommandTarget } from "@univerjs/sheets";
3
- import { FormulaDataModel, UniverFormulaEnginePlugin } from "@univerjs/engine-formula";
2
+ import { ReorderRangeCommand, UniverSheetsPlugin, getCellValue, getSheetCommandTarget } from "@univerjs/sheets";
3
+ import { ERROR_TYPE_SET, FormulaDataModel, UniverFormulaEnginePlugin } from "@univerjs/engine-formula";
4
4
 
5
5
  // src/services/interface.ts
6
6
  let SortType = /* @__PURE__ */ function(SortType) {
@@ -10,54 +10,93 @@ let SortType = /* @__PURE__ */ function(SortType) {
10
10
  }({});
11
11
 
12
12
  // src/controllers/utils.ts
13
- const removeStringSymbol = (str) => {
14
- return str.replace(/-/gi, "").replace(/'/gi, "");
15
- };
16
- const compareNull = (a1, a2) => {
17
- const isA1Null = a1 === null || a1 === "";
18
- const isA2Null = a2 === null || a2 === "";
19
- if (isA1Null && isA2Null) return 0;
20
- if (isA1Null) return 1;
21
- if (isA2Null) return -1;
22
- return null;
23
- };
24
- const compareNumber = (a1, a2, type) => {
25
- const isA1Num = typeof a1 === "number";
26
- const isA2Num = typeof a2 === "number";
27
- if (isA1Num && isA2Num) {
28
- if (a1 < a2) return type === "asc" ? -1 : 1;
29
- if (a1 > a2) return type === "asc" ? 1 : -1;
30
- return 0;
13
+ const HASH_CHAR_CODE = 35;
14
+ const removeStringSymbol = (str) => str.replace(/-/gi, "").replace(/'/gi, "");
15
+ const isNullValue = (cell) => !cell || cell.v == null && cell.p == null;
16
+ /**
17
+ * Get the value kind of a cell. The kind decides the group the cell is sorted in, so it must not
18
+ * depend on the stringified value: a `TRUE` Boolean cell is a logical value, not the text "TRUE".
19
+ */
20
+ function getSortRank(cell) {
21
+ var _paragraph$body2;
22
+ if (!cell) return 4;
23
+ const value = cell.v;
24
+ const paragraph = cell.p;
25
+ if (value == null) {
26
+ var _paragraph$body;
27
+ return (paragraph === null || paragraph === void 0 || (_paragraph$body = paragraph.body) === null || _paragraph$body === void 0 ? void 0 : _paragraph$body.dataStream) ? 1 : 4;
31
28
  }
32
- if (isA1Num) return type === "asc" ? 1 : -1;
33
- if (isA2Num) return type === "asc" ? -1 : 1;
34
- return null;
35
- };
36
- const compareString = (a1, a2, type) => {
37
- const isA1Str = typeof a1 === "string";
38
- const isA2Str = typeof a2 === "string";
39
- if (isA1Str) a1 = removeStringSymbol(a1.toLocaleLowerCase());
40
- if (isA2Str) a2 = removeStringSymbol(a2.toLocaleLowerCase());
41
- if (!isA1Str && !isA2Str) return null;
42
- if (isA1Str && isA2Str) {
43
- const a1AsString = a1;
44
- const a2AsString = a2;
45
- if (a1AsString < a2AsString) return type === "asc" ? -1 : 1;
46
- if (a1AsString > a2AsString) return type === "asc" ? 1 : -1;
47
- return 0;
29
+ if (paragraph === null || paragraph === void 0 || (_paragraph$body2 = paragraph.body) === null || _paragraph$body2 === void 0 ? void 0 : _paragraph$body2.dataStream) return 1;
30
+ switch (cell.t) {
31
+ case CellValueType.NUMBER: return getNumberRank(value);
32
+ case CellValueType.STRING: return getStringRank(value);
33
+ case CellValueType.BOOLEAN: return 2;
34
+ case CellValueType.FORCE_STRING: return value === "" ? 4 : 1;
35
+ default: return getUntypedCellRank(value);
48
36
  }
49
- if (isA1Str) return type === "asc" ? 1 : -1;
50
- if (isA2Str) return type === "asc" ? -1 : 1;
51
- return null;
52
- };
53
- const isNullValue = (cell) => {
54
- if (!cell) return true;
55
- if (Object.keys(cell).length === 0) return true;
56
- if ((cell === null || cell === void 0 ? void 0 : cell.v) == null && (cell === null || cell === void 0 ? void 0 : cell.p) == null) return true;
57
- return false;
58
- };
37
+ }
38
+ function getNumberRank(value) {
39
+ if (typeof value !== "number") return Number.isNaN(Number.parseFloat(`${value}`)) ? 1 : 0;
40
+ return Number.isNaN(value) ? 1 : 0;
41
+ }
42
+ function getStringRank(value) {
43
+ if (typeof value === "number") return 0;
44
+ if (value === "") return 4;
45
+ if (typeof value === "string" && value.charCodeAt(0) === HASH_CHAR_CODE && ERROR_TYPE_SET.has(value)) return 3;
46
+ return 1;
47
+ }
48
+ function getUntypedCellRank(value) {
49
+ if (typeof value === "number") return Number.isNaN(value) ? 1 : 0;
50
+ if (typeof value === "boolean") return 2;
51
+ if (value === "") return 4;
52
+ return 1;
53
+ }
54
+ function readNumber(cell) {
55
+ const value = cell === null || cell === void 0 ? void 0 : cell.v;
56
+ return typeof value === "number" ? value : Number.parseFloat(`${value}`);
57
+ }
58
+ function readText(cell) {
59
+ var _cell$p;
60
+ const richText = cell === null || cell === void 0 || (_cell$p = cell.p) === null || _cell$p === void 0 || (_cell$p = _cell$p.body) === null || _cell$p === void 0 ? void 0 : _cell$p.dataStream;
61
+ if (richText) return richText;
62
+ const value = cell === null || cell === void 0 ? void 0 : cell.v;
63
+ return typeof value === "string" ? value : `${value}`;
64
+ }
65
+ function readLogical(cell) {
66
+ const value = cell === null || cell === void 0 ? void 0 : cell.v;
67
+ if (value === 1 || value === true) return 1;
68
+ if (value === 0 || value === false) return 0;
69
+ return cell ? Number(getCellValue(CellValueType.BOOLEAN, cell)) : 0;
70
+ }
71
+ function compareText(a, b) {
72
+ const left = removeStringSymbol(a.toLocaleLowerCase());
73
+ const right = removeStringSymbol(b.toLocaleLowerCase());
74
+ if (left < right) return -1;
75
+ if (left > right) return 1;
76
+ return 0;
77
+ }
78
+ /**
79
+ * Compare two cells the same way Excel sorts a range: by value kind first, then by value inside the
80
+ * kind. Blank cells are always sorted last, whichever direction is used.
81
+ */
82
+ function compareCells(type, a, b) {
83
+ const rankA = getSortRank(a);
84
+ const rankB = getSortRank(b);
85
+ if (rankA === 4 || rankB === 4) {
86
+ if (rankA === rankB) return 0;
87
+ return rankA === 4 ? 1 : -1;
88
+ }
89
+ const direction = type === "asc" ? 1 : -1;
90
+ if (rankA !== rankB) return direction * (rankA - rankB);
91
+ switch (rankA) {
92
+ case 0: return direction * Math.sign(readNumber(a) - readNumber(b));
93
+ case 2: return direction * Math.sign(readLogical(a) - readLogical(b));
94
+ case 3: return 0;
95
+ default: return direction * compareText(readText(a), readText(b));
96
+ }
97
+ }
59
98
 
60
- // \0@oxc-project+runtime@0.157.0/helpers/esm/typeof.js
99
+ // \0@oxc-project+runtime@0.158.0/helpers/esm/typeof.js
61
100
  function _typeof(o) {
62
101
  "@babel/helpers - typeof";
63
102
  return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(o) {
@@ -67,7 +106,7 @@ function _typeof(o) {
67
106
  }, _typeof(o);
68
107
  }
69
108
 
70
- // \0@oxc-project+runtime@0.157.0/helpers/esm/toPrimitive.js
109
+ // \0@oxc-project+runtime@0.158.0/helpers/esm/toPrimitive.js
71
110
  function toPrimitive(t, e) {
72
111
  if ("object" != _typeof(t) || !t) return t;
73
112
  var r;
@@ -79,13 +118,13 @@ function toPrimitive(t, e) {
79
118
  return ("string" === e ? String : Number)(t);
80
119
  }
81
120
 
82
- // \0@oxc-project+runtime@0.157.0/helpers/esm/toPropertyKey.js
121
+ // \0@oxc-project+runtime@0.158.0/helpers/esm/toPropertyKey.js
83
122
  function toPropertyKey(t) {
84
123
  var i = toPrimitive(t, "string");
85
124
  return "symbol" == _typeof(i) ? i : i + "";
86
125
  }
87
126
 
88
- // \0@oxc-project+runtime@0.157.0/helpers/esm/defineProperty.js
127
+ // \0@oxc-project+runtime@0.158.0/helpers/esm/defineProperty.js
89
128
  function _defineProperty(e, r, t) {
90
129
  return (r = toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
91
130
  value: t,
@@ -95,14 +134,14 @@ function _defineProperty(e, r, t) {
95
134
  }) : e[r] = t, e;
96
135
  }
97
136
 
98
- // \0@oxc-project+runtime@0.157.0/helpers/esm/decorateParam.js
137
+ // \0@oxc-project+runtime@0.158.0/helpers/esm/decorateParam.js
99
138
  function __decorateParam(paramIndex, decorator) {
100
139
  return function(target, key) {
101
140
  decorator(target, key, paramIndex);
102
141
  };
103
142
  }
104
143
 
105
- // \0@oxc-project+runtime@0.157.0/helpers/esm/decorate.js
144
+ // \0@oxc-project+runtime@0.158.0/helpers/esm/decorate.js
106
145
  function __decorate(decorators, target, key, desc) {
107
146
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
108
147
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
@@ -273,6 +312,21 @@ function reorderFnGenerator(orderRules, valueCompare) {
273
312
  }
274
313
 
275
314
  // src/controllers/sheets-sort.controller.ts
315
+ /**
316
+ * Copyright 2023-present DreamNum Co., Ltd.
317
+ *
318
+ * Licensed under the Apache License, Version 2.0 (the "License");
319
+ * you may not use this file except in compliance with the License.
320
+ * You may obtain a copy of the License at
321
+ *
322
+ * http://www.apache.org/licenses/LICENSE-2.0
323
+ *
324
+ * Unless required by applicable law or agreed to in writing, software
325
+ * distributed under the License is distributed on an "AS IS" BASIS,
326
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
327
+ * See the License for the specific language governing permissions and
328
+ * limitations under the License.
329
+ */
276
330
  let SheetsSortController = class SheetsSortController extends Disposable {
277
331
  constructor(_commandService, _sortService) {
278
332
  super();
@@ -285,42 +339,14 @@ let SheetsSortController = class SheetsSortController extends Disposable {
285
339
  [SortRangeCommand].forEach((command) => this.disposeWithMe(this._commandService.registerCommand(command)));
286
340
  }
287
341
  _registerCompareFns() {
288
- const commonFn = (type, a, b) => {
289
- const valueA = this._getCommonValue(a);
290
- const valueB = this._getCommonValue(b);
291
- const compareTypeFns = [
292
- compareNull,
293
- compareString,
294
- compareNumber
295
- ];
296
- for (let i = 0; i < compareTypeFns.length; i++) {
297
- const res = compareTypeFns[i](valueA, valueB, type);
298
- if (res !== null) return res;
299
- }
300
- return null;
301
- };
302
- this._sortService.registerCompareFn(commonFn);
303
- }
304
- _getCommonValue(a) {
305
- var _a$p;
306
- if (isNullValue(a)) return null;
307
- const richTextValue = a === null || a === void 0 || (_a$p = a.p) === null || _a$p === void 0 || (_a$p = _a$p.body) === null || _a$p === void 0 ? void 0 : _a$p.dataStream;
308
- if (richTextValue) return richTextValue;
309
- if ((a === null || a === void 0 ? void 0 : a.t) === CellValueType.NUMBER) return Number.parseFloat(`${a.v}`);
310
- if ((a === null || a === void 0 ? void 0 : a.t) === CellValueType.STRING) {
311
- if (typeof a.v === "number") return a.v;
312
- return `${a.v}`;
313
- }
314
- if ((a === null || a === void 0 ? void 0 : a.t) === CellValueType.BOOLEAN) return `${a.v}`;
315
- if ((a === null || a === void 0 ? void 0 : a.t) === CellValueType.FORCE_STRING) return Number.parseFloat(`${a.v}`);
316
- return `${a === null || a === void 0 ? void 0 : a.v}`;
342
+ this._sortService.registerCompareFn(compareCells);
317
343
  }
318
344
  };
319
345
  SheetsSortController = __decorate([__decorateParam(0, ICommandService), __decorateParam(1, Inject(SheetsSortService))], SheetsSortController);
320
346
 
321
347
  // package.json
322
348
  var name = "@univerjs/sheets-sort";
323
- var version = "1.5.1";
349
+ var version = "1.6.0";
324
350
 
325
351
  // src/config/config.ts
326
352
  /**
@@ -15,12 +15,10 @@
15
15
  */
16
16
  import { Disposable, ICommandService } from '@univerjs/core';
17
17
  import { SheetsSortService } from '../services/sheets-sort.service';
18
- export type ICommonComparableCellValue = number | string | null;
19
18
  export declare class SheetsSortController extends Disposable {
20
19
  private readonly _commandService;
21
20
  private readonly _sortService;
22
21
  constructor(_commandService: ICommandService, _sortService: SheetsSortService);
23
22
  private _initCommands;
24
23
  private _registerCompareFns;
25
- private _getCommonValue;
26
24
  }
@@ -14,14 +14,28 @@
14
14
  * limitations under the License.
15
15
  */
16
16
  import type { ICellData, Nullable } from '@univerjs/core';
17
- import type { ICommonComparableCellValue } from './sheets-sort.controller';
18
17
  import { SortType } from '../services/interface';
19
- export declare enum ORDER {
20
- POSITIVE = 1,
21
- NEGATIVE = -1,
22
- ZERO = 0
18
+ /**
19
+ * Sort order of the value kinds recognized by range sorting. The order matches Excel's default
20
+ * ascending order: numbers, text, logical values, errors, and blank cells.
21
+ */
22
+ export declare enum SortRank {
23
+ NUMBER = 0,
24
+ TEXT = 1,
25
+ LOGICAL = 2,
26
+ ERROR = 3,
27
+ BLANK = 4
23
28
  }
24
- export declare const compareNull: (a1: ICommonComparableCellValue, a2: ICommonComparableCellValue) => ORDER | null;
25
- export declare const compareNumber: (a1: ICommonComparableCellValue, a2: ICommonComparableCellValue, type: SortType) => ORDER | null;
26
- export declare const compareString: (a1: ICommonComparableCellValue, a2: ICommonComparableCellValue, type: SortType) => ORDER | null;
29
+ type SortableCell = Nullable<ICellData>;
27
30
  export declare const isNullValue: (cell: Nullable<ICellData>) => boolean;
31
+ /**
32
+ * Get the value kind of a cell. The kind decides the group the cell is sorted in, so it must not
33
+ * depend on the stringified value: a `TRUE` Boolean cell is a logical value, not the text "TRUE".
34
+ */
35
+ export declare function getSortRank(cell: SortableCell): SortRank;
36
+ /**
37
+ * Compare two cells the same way Excel sorts a range: by value kind first, then by value inside the
38
+ * kind. Blank cells are always sorted last, whichever direction is used.
39
+ */
40
+ export declare function compareCells(type: SortType, a: SortableCell, b: SortableCell): number;
41
+ export {};
package/lib/umd/index.js CHANGED
@@ -1 +1 @@
1
- (function(e,t){typeof exports==`object`&&typeof module<`u`?t(exports,require("@univerjs/core"),require("@univerjs/sheets"),require("@univerjs/engine-formula")):typeof define==`function`&&define.amd?define([`exports`,`@univerjs/core`,`@univerjs/sheets`,`@univerjs/engine-formula`],t):(e=typeof globalThis<`u`?globalThis:e||self,t(e.UniverSheetsSort={},e.UniverCore,e.UniverSheets,e.UniverEngineFormula))})(this,function(e,t,n,r){Object.defineProperty(e,Symbol.toStringTag,{value:`Module`});let i=/* @__PURE__ */ function(e){return e.DESC=`desc`,e.ASC=`asc`,e}({}),a=e=>e.replace(/-/gi,``).replace(/'/gi,``),o=(e,t)=>{let n=e===null||e===``,r=t===null||t===``;return n&&r?0:n?1:r?-1:null},s=(e,t,n)=>{let r=typeof e==`number`,i=typeof t==`number`;return r&&i?e<t?n===`asc`?-1:1:e>t?n===`asc`?1:-1:0:r?n===`asc`?1:-1:i?n===`asc`?-1:1:null},c=(e,t,n)=>{let r=typeof e==`string`,i=typeof t==`string`;if(r&&(e=a(e.toLocaleLowerCase())),i&&(t=a(t.toLocaleLowerCase())),!r&&!i)return null;if(r&&i){let r=e,i=t;return r<i?n===`asc`?-1:1:r>i?n===`asc`?1:-1:0}return r?n===`asc`?1:-1:i?n===`asc`?-1:1:null},l=e=>!e||Object.keys(e).length===0||(e==null?void 0:e.v)==null&&(e==null?void 0:e.p)==null;function u(e){"@babel/helpers - typeof";return u=typeof Symbol==`function`&&typeof Symbol.iterator==`symbol`?function(e){return typeof e}:function(e){return e&&typeof Symbol==`function`&&e.constructor===Symbol&&e!==Symbol.prototype?`symbol`:typeof e},u(e)}function d(e,t){if(u(e)!=`object`||!e)return e;var n;if(typeof Symbol<`u`&&(n=e[Symbol.toPrimitive])!==void 0){var r=n.call(e,t||`default`);if(u(r)!=`object`)return r;throw TypeError(`@@toPrimitive must return a primitive value.`)}return(t===`string`?String:Number)(e)}function f(e){var t=d(e,`string`);return u(t)==`symbol`?t:t+``}function p(e,t,n){return(t=f(t))in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function m(e,t){return function(n,r){t(n,r,e)}}function h(e,t,n,r){var i=arguments.length,a=i<3?t:r===null?r=Object.getOwnPropertyDescriptor(t,n):r,o;if(typeof Reflect==`object`&&typeof Reflect.decorate==`function`)a=Reflect.decorate(e,t,n,r);else for(var s=e.length-1;s>=0;s--)(o=e[s])&&(a=(i<3?o(a):i>3?o(t,n,a):o(t,n))||a);return i>3&&a&&Object.defineProperty(t,n,a),a}let g=class extends t.Disposable{constructor(e,t,n){super(),this._univerInstanceService=e,this._commandService=t,this._formulaDataModel=n,p(this,`_compareFns`,[])}mergeCheck(e){var n;let{unitId:r,subUnitId:i,range:a}=e,o=(n=this._univerInstanceService.getUnit(r))==null?void 0:n.getSheetBySheetId(i);if(!o)return!1;let s=o.getMergeData().filter(e=>t.Rectangle.contains(a,e));return s.length===0||_(a,s)}emptyCheck(e){var t;let{unitId:n,subUnitId:r,range:i}=e,a=(t=this._univerInstanceService.getUnit(n))==null?void 0:t.getSheetBySheetId(r);if(!a)return!1;for(let e=i.startRow;e<=i.endRow;e++)for(let t=i.startColumn;t<=i.endColumn;t++)if(!l(a.getCellRaw(e,t)))return!0;return!1}singleCheck(e){return e.range.startRow!==e.range.endRow}formulaCheck(e){var n;let{unitId:r,subUnitId:i,range:a}=e,o=(n=this._formulaDataModel.getArrayFormulaRange())==null||(n=n[r])==null?void 0:n[i];for(let e in o){let n=o[Number(e)];for(let e in n){let r=n[Number(e)];if(r&&t.Rectangle.intersects(a,r))return!1}}return!0}registerCompareFn(e){this._compareFns.unshift(e)}getAllCompareFns(){return this._compareFns}applySort(e,t,r){let{unitId:i,subUnitId:a}=(0,n.getSheetCommandTarget)(this._univerInstanceService)||{};this._commandService.executeCommand(v.id,{orderRules:e.orderRules,range:e.range,hasTitle:e.hasTitle??!1,unitId:t||i,subUnitId:r||a})}};g=h([m(0,t.IUniverInstanceService),m(1,t.ICommandService),m(2,(0,t.Inject)(r.FormulaDataModel))],g);function _(e,t){let n=e.endRow-e.startRow+1,r=e.endColumn-e.startColumn+1,i=null,a=null,o=n*r,s=0;for(let n of t)if(n.startRow>=e.startRow&&n.endRow<=e.endRow&&n.startColumn>=e.startColumn&&n.endColumn<=e.endColumn){let e=n.endRow-n.startRow+1,t=n.endColumn-n.startColumn+1;if(i===null&&a===null)i=e,a=t;else if(e!==i||t!==a)return!1;s+=e*t}return s===o}let v={id:`sheet.command.sort-range`,type:t.CommandType.COMMAND,handler:(e,r)=>{let{range:i,orderRules:a,hasTitle:o,unitId:s,subUnitId:c}=r,l=e.get(g),u=e.get(t.IUniverInstanceService),{worksheet:d}=(0,n.getSheetCommandTarget)(u,r)||{};if(!d)return!1;let f=d.getMergeData().filter(e=>t.Rectangle.contains(i,e)),p=f.map(e=>e.startRow),{startRow:m,endRow:h}=i,_=o?m+1:m,v=[],S=[];for(let e=_;e<=h;e++)d.getRowFiltered(e)||d.getRowRawVisible(e)!==!1&&(!f.length||p.includes(e))&&(v.push({index:e,value:y(d,e,a)}),S.push(e));let C=l.getAllCompareFns();v.sort(x(a,b(C)));let w={};v.forEach(({index:e,value:t},n)=>{w[S[n]]=e});let T={id:n.ReorderRangeCommand.id,params:{unitId:s,subUnitId:c,range:i,order:w}},E=e.get(t.ICommandService);return(0,t.sequenceExecute)([T],E).result}};function y(e,t,n){let r=[];return n.forEach(({colIndex:n})=>{r.push(e.getCellRaw(t,n))}),r}function b(e){return(t,n,r)=>{for(let i=0;i<e.length;i++){let a=e[i](t,n,r);if(a!=null)return a}return 0}}function x(e,t){return function(n,r){let i=null;for(let a=0;a<e.length;a++){let o=n.value[a],s=r.value[a];if(i=t(e[a].type,o,s),i!==0&&i!=null)return i}return 0}}let S=class extends t.Disposable{constructor(e,t){super(),this._commandService=e,this._sortService=t,this._initCommands(),this._registerCompareFns()}_initCommands(){[v].forEach(e=>this.disposeWithMe(this._commandService.registerCommand(e)))}_registerCompareFns(){this._sortService.registerCompareFn((e,t,n)=>{let r=this._getCommonValue(t),i=this._getCommonValue(n),a=[o,c,s];for(let t=0;t<a.length;t++){let n=a[t](r,i,e);if(n!==null)return n}return null})}_getCommonValue(e){var n;return l(e)?null:(e==null||(n=e.p)==null||(n=n.body)==null?void 0:n.dataStream)||((e==null?void 0:e.t)===t.CellValueType.NUMBER?Number.parseFloat(`${e.v}`):(e==null?void 0:e.t)===t.CellValueType.STRING?typeof e.v==`number`?e.v:`${e.v}`:(e==null?void 0:e.t)===t.CellValueType.BOOLEAN?`${e.v}`:(e==null?void 0:e.t)===t.CellValueType.FORCE_STRING?Number.parseFloat(`${e.v}`):`${e==null?void 0:e.v}`)}};S=h([m(0,t.ICommandService),m(1,(0,t.Inject)(g))],S);var C=`@univerjs/sheets-sort`,w=`1.5.1`;let T={},E=class extends t.Plugin{constructor(e=T,n,r){super(),this._config=e,this._injector=n,this._configService=r;let{...i}=(0,t.merge)({},T,this._config);this._configService.setConfig(`sheets-sort.config`,i)}onStarting(){[[S],[g]].forEach(e=>this._injector.add(e))}onReady(){this._injector.get(S)}};p(E,`type`,t.UniverInstanceType.UNIVER_SHEET),p(E,`pluginName`,`SHEET_SORT_PLUGIN`),p(E,`packageName`,C),p(E,`version`,w),E=h([(0,t.DependentOn)(r.UniverFormulaEnginePlugin,n.UniverSheetsPlugin),m(1,(0,t.Inject)(t.Injector)),m(2,t.IConfigService)],E),Object.defineProperty(e,"SheetsSortController",{enumerable:!0,get:function(){return S}}),Object.defineProperty(e,"SheetsSortService",{enumerable:!0,get:function(){return g}}),e.SortRangeCommand=v,e.SortType=i,Object.defineProperty(e,"UniverSheetsSortPlugin",{enumerable:!0,get:function(){return E}})});
1
+ (function(e,t){typeof exports==`object`&&typeof module<`u`?t(exports,require("@univerjs/core"),require("@univerjs/sheets"),require("@univerjs/engine-formula")):typeof define==`function`&&define.amd?define([`exports`,`@univerjs/core`,`@univerjs/sheets`,`@univerjs/engine-formula`],t):(e=typeof globalThis<`u`?globalThis:e||self,t(e.UniverSheetsSort={},e.UniverCore,e.UniverSheets,e.UniverEngineFormula))})(this,function(e,t,n,r){Object.defineProperty(e,Symbol.toStringTag,{value:`Module`});let i=/* @__PURE__ */ function(e){return e.DESC=`desc`,e.ASC=`asc`,e}({}),a=e=>e.replace(/-/gi,``).replace(/'/gi,``),o=e=>!e||e.v==null&&e.p==null;function s(e){var n;if(!e)return 4;let r=e.v,i=e.p;if(r==null){var a;return i!=null&&(a=i.body)!=null&&a.dataStream?1:4}if(i!=null&&(n=i.body)!=null&&n.dataStream)return 1;switch(e.t){case t.CellValueType.NUMBER:return c(r);case t.CellValueType.STRING:return l(r);case t.CellValueType.BOOLEAN:return 2;case t.CellValueType.FORCE_STRING:return r===``?4:1;default:return u(r)}}function c(e){return typeof e==`number`?+!!Number.isNaN(e):+!!Number.isNaN(Number.parseFloat(`${e}`))}function l(e){return typeof e==`number`?0:e===``?4:typeof e==`string`&&e.charCodeAt(0)===35&&r.ERROR_TYPE_SET.has(e)?3:1}function u(e){return typeof e==`number`?+!!Number.isNaN(e):typeof e==`boolean`?2:e===``?4:1}function d(e){let t=e==null?void 0:e.v;return typeof t==`number`?t:Number.parseFloat(`${t}`)}function f(e){var t;let n=e==null||(t=e.p)==null||(t=t.body)==null?void 0:t.dataStream;if(n)return n;let r=e==null?void 0:e.v;return typeof r==`string`?r:`${r}`}function p(e){let r=e==null?void 0:e.v;return r===1||r===!0?1:r===0||r===!1?0:e?Number((0,n.getCellValue)(t.CellValueType.BOOLEAN,e)):0}function m(e,t){let n=a(e.toLocaleLowerCase()),r=a(t.toLocaleLowerCase());return n<r?-1:+(n>r)}function h(e,t,n){let r=s(t),i=s(n);if(r===4||i===4)return r===i?0:r===4?1:-1;let a=e===`asc`?1:-1;if(r!==i)return a*(r-i);switch(r){case 0:return a*Math.sign(d(t)-d(n));case 2:return a*Math.sign(p(t)-p(n));case 3:return 0;default:return a*m(f(t),f(n))}}function g(e){"@babel/helpers - typeof";return g=typeof Symbol==`function`&&typeof Symbol.iterator==`symbol`?function(e){return typeof e}:function(e){return e&&typeof Symbol==`function`&&e.constructor===Symbol&&e!==Symbol.prototype?`symbol`:typeof e},g(e)}function _(e,t){if(g(e)!=`object`||!e)return e;var n;if(typeof Symbol<`u`&&(n=e[Symbol.toPrimitive])!==void 0){var r=n.call(e,t||`default`);if(g(r)!=`object`)return r;throw TypeError(`@@toPrimitive must return a primitive value.`)}return(t===`string`?String:Number)(e)}function v(e){var t=_(e,`string`);return g(t)==`symbol`?t:t+``}function y(e,t,n){return(t=v(t))in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function b(e,t){return function(n,r){t(n,r,e)}}function x(e,t,n,r){var i=arguments.length,a=i<3?t:r===null?r=Object.getOwnPropertyDescriptor(t,n):r,o;if(typeof Reflect==`object`&&typeof Reflect.decorate==`function`)a=Reflect.decorate(e,t,n,r);else for(var s=e.length-1;s>=0;s--)(o=e[s])&&(a=(i<3?o(a):i>3?o(t,n,a):o(t,n))||a);return i>3&&a&&Object.defineProperty(t,n,a),a}let S=class extends t.Disposable{constructor(e,t,n){super(),this._univerInstanceService=e,this._commandService=t,this._formulaDataModel=n,y(this,`_compareFns`,[])}mergeCheck(e){var n;let{unitId:r,subUnitId:i,range:a}=e,o=(n=this._univerInstanceService.getUnit(r))==null?void 0:n.getSheetBySheetId(i);if(!o)return!1;let s=o.getMergeData().filter(e=>t.Rectangle.contains(a,e));return s.length===0||C(a,s)}emptyCheck(e){var t;let{unitId:n,subUnitId:r,range:i}=e,a=(t=this._univerInstanceService.getUnit(n))==null?void 0:t.getSheetBySheetId(r);if(!a)return!1;for(let e=i.startRow;e<=i.endRow;e++)for(let t=i.startColumn;t<=i.endColumn;t++)if(!o(a.getCellRaw(e,t)))return!0;return!1}singleCheck(e){return e.range.startRow!==e.range.endRow}formulaCheck(e){var n;let{unitId:r,subUnitId:i,range:a}=e,o=(n=this._formulaDataModel.getArrayFormulaRange())==null||(n=n[r])==null?void 0:n[i];for(let e in o){let n=o[Number(e)];for(let e in n){let r=n[Number(e)];if(r&&t.Rectangle.intersects(a,r))return!1}}return!0}registerCompareFn(e){this._compareFns.unshift(e)}getAllCompareFns(){return this._compareFns}applySort(e,t,r){let{unitId:i,subUnitId:a}=(0,n.getSheetCommandTarget)(this._univerInstanceService)||{};this._commandService.executeCommand(w.id,{orderRules:e.orderRules,range:e.range,hasTitle:e.hasTitle??!1,unitId:t||i,subUnitId:r||a})}};S=x([b(0,t.IUniverInstanceService),b(1,t.ICommandService),b(2,(0,t.Inject)(r.FormulaDataModel))],S);function C(e,t){let n=e.endRow-e.startRow+1,r=e.endColumn-e.startColumn+1,i=null,a=null,o=n*r,s=0;for(let n of t)if(n.startRow>=e.startRow&&n.endRow<=e.endRow&&n.startColumn>=e.startColumn&&n.endColumn<=e.endColumn){let e=n.endRow-n.startRow+1,t=n.endColumn-n.startColumn+1;if(i===null&&a===null)i=e,a=t;else if(e!==i||t!==a)return!1;s+=e*t}return s===o}let w={id:`sheet.command.sort-range`,type:t.CommandType.COMMAND,handler:(e,r)=>{let{range:i,orderRules:a,hasTitle:o,unitId:s,subUnitId:c}=r,l=e.get(S),u=e.get(t.IUniverInstanceService),{worksheet:d}=(0,n.getSheetCommandTarget)(u,r)||{};if(!d)return!1;let f=d.getMergeData().filter(e=>t.Rectangle.contains(i,e)),p=f.map(e=>e.startRow),{startRow:m,endRow:h}=i,g=o?m+1:m,_=[],v=[];for(let e=g;e<=h;e++)d.getRowFiltered(e)||d.getRowRawVisible(e)!==!1&&(!f.length||p.includes(e))&&(_.push({index:e,value:T(d,e,a)}),v.push(e));let y=l.getAllCompareFns();_.sort(D(a,E(y)));let b={};_.forEach(({index:e,value:t},n)=>{b[v[n]]=e});let x={id:n.ReorderRangeCommand.id,params:{unitId:s,subUnitId:c,range:i,order:b}},C=e.get(t.ICommandService);return(0,t.sequenceExecute)([x],C).result}};function T(e,t,n){let r=[];return n.forEach(({colIndex:n})=>{r.push(e.getCellRaw(t,n))}),r}function E(e){return(t,n,r)=>{for(let i=0;i<e.length;i++){let a=e[i](t,n,r);if(a!=null)return a}return 0}}function D(e,t){return function(n,r){let i=null;for(let a=0;a<e.length;a++){let o=n.value[a],s=r.value[a];if(i=t(e[a].type,o,s),i!==0&&i!=null)return i}return 0}}let O=class extends t.Disposable{constructor(e,t){super(),this._commandService=e,this._sortService=t,this._initCommands(),this._registerCompareFns()}_initCommands(){[w].forEach(e=>this.disposeWithMe(this._commandService.registerCommand(e)))}_registerCompareFns(){this._sortService.registerCompareFn(h)}};O=x([b(0,t.ICommandService),b(1,(0,t.Inject)(S))],O);var k=`@univerjs/sheets-sort`,A=`1.6.0`;let j={},M=class extends t.Plugin{constructor(e=j,n,r){super(),this._config=e,this._injector=n,this._configService=r;let{...i}=(0,t.merge)({},j,this._config);this._configService.setConfig(`sheets-sort.config`,i)}onStarting(){[[O],[S]].forEach(e=>this._injector.add(e))}onReady(){this._injector.get(O)}};y(M,`type`,t.UniverInstanceType.UNIVER_SHEET),y(M,`pluginName`,`SHEET_SORT_PLUGIN`),y(M,`packageName`,k),y(M,`version`,A),M=x([(0,t.DependentOn)(r.UniverFormulaEnginePlugin,n.UniverSheetsPlugin),b(1,(0,t.Inject)(t.Injector)),b(2,t.IConfigService)],M),Object.defineProperty(e,"SheetsSortController",{enumerable:!0,get:function(){return O}}),Object.defineProperty(e,"SheetsSortService",{enumerable:!0,get:function(){return S}}),e.SortRangeCommand=w,e.SortType=i,Object.defineProperty(e,"UniverSheetsSortPlugin",{enumerable:!0,get:function(){return M}})});
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@univerjs/sheets-sort",
3
- "version": "1.5.1",
3
+ "version": "1.6.0",
4
4
  "private": false,
5
5
  "description": "Sorting model, commands, and services for Univer Sheets.",
6
6
  "author": "DreamNum Co., Ltd. <developer@univer.ai>",
@@ -59,14 +59,14 @@
59
59
  "lib"
60
60
  ],
61
61
  "dependencies": {
62
- "@univerjs/core": "1.5.1",
63
- "@univerjs/engine-formula": "1.5.1",
64
- "@univerjs/sheets": "1.5.1"
62
+ "@univerjs/core": "1.6.0",
63
+ "@univerjs/engine-formula": "1.6.0",
64
+ "@univerjs/sheets": "1.6.0"
65
65
  },
66
66
  "devDependencies": {
67
- "@univerjs-infra/shared": "1.5.1",
68
- "tsrs": "0.23.0-ts7.1.0-beta",
69
- "vitest": "^5.1.0"
67
+ "@univerjs-infra/shared": "1.6.0",
68
+ "tsrs": "7.1.0-beta.tsrs2",
69
+ "vitest": "^5.1.1"
70
70
  },
71
71
  "scripts": {
72
72
  "test": "vitest run",