@d-matrix/utils 1.27.1 → 1.28.1

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 (72) hide show
  1. package/dist/algorithm/binary.d.ts +8 -8
  2. package/dist/algorithm/binary.js +38 -38
  3. package/dist/algorithm/index.d.ts +2 -2
  4. package/dist/algorithm/index.js +2 -2
  5. package/dist/algorithm/tree.d.ts +34 -34
  6. package/dist/algorithm/tree.js +124 -124
  7. package/dist/array.d.ts +46 -5
  8. package/dist/array.js +123 -37
  9. package/dist/clipboard.d.ts +12 -12
  10. package/dist/clipboard.js +107 -107
  11. package/dist/color.d.ts +7 -7
  12. package/dist/color.js +41 -41
  13. package/dist/date.d.ts +34 -34
  14. package/dist/date.js +62 -62
  15. package/dist/decimal.d.ts +17 -17
  16. package/dist/decimal.js +23 -23
  17. package/dist/dom.d.ts +11 -11
  18. package/dist/dom.js +27 -27
  19. package/dist/echarts.d.ts +36 -36
  20. package/dist/echarts.js +112 -112
  21. package/dist/file.d.ts +49 -49
  22. package/dist/file.js +154 -154
  23. package/dist/i18n.d.ts +10 -10
  24. package/dist/i18n.js +12 -12
  25. package/dist/index.d.ts +16 -16
  26. package/dist/index.js +16 -16
  27. package/dist/number.d.ts +2 -2
  28. package/dist/number.js +4 -4
  29. package/dist/object.d.ts +13 -13
  30. package/dist/object.js +28 -28
  31. package/dist/operator.d.ts +6 -6
  32. package/dist/operator.js +6 -6
  33. package/dist/react/enhancedComponent.d.ts +12 -12
  34. package/dist/react/enhancedComponent.js +16 -16
  35. package/dist/react/index.d.ts +15 -12
  36. package/dist/react/index.js +15 -12
  37. package/dist/react/renderToString.d.ts +3 -3
  38. package/dist/react/renderToString.js +30 -30
  39. package/dist/react/types.d.ts +9 -9
  40. package/dist/react/types.js +1 -1
  41. package/dist/react/useCopyToClipboard.d.ts +21 -21
  42. package/dist/react/useCopyToClipboard.js +44 -44
  43. package/dist/react/useDeepCompareRef.d.ts +2 -2
  44. package/dist/react/useDeepCompareRef.js +11 -11
  45. package/dist/react/useDisableContextMenu.d.ts +7 -7
  46. package/dist/react/useDisableContextMenu.js +24 -24
  47. package/dist/react/useEventCallback.d.ts +7 -0
  48. package/dist/react/useEventCallback.js +19 -0
  49. package/dist/react/useForwardRef.d.ts +9 -9
  50. package/dist/react/useForwardRef.js +22 -22
  51. package/dist/react/useId.d.ts +1 -0
  52. package/dist/react/useId.js +12 -0
  53. package/dist/react/useIsFirstRender.d.ts +6 -6
  54. package/dist/react/useIsFirstRender.js +14 -14
  55. package/dist/react/useIsMounted.d.ts +2 -2
  56. package/dist/react/useIsMounted.js +13 -13
  57. package/dist/react/useIsomorphicLayoutEffect.d.ts +2 -2
  58. package/dist/react/useIsomorphicLayoutEffect.js +2 -2
  59. package/dist/react/useMediaQuery.d.ts +14 -14
  60. package/dist/react/useMediaQuery.js +42 -42
  61. package/dist/react/useSafeTimeout.d.ts +12 -0
  62. package/dist/react/useSafeTimeout.js +28 -0
  63. package/dist/react/useStateCallback.d.ts +2 -2
  64. package/dist/react/useStateCallback.js +17 -17
  65. package/dist/support.d.ts +12 -12
  66. package/dist/support.js +12 -12
  67. package/dist/timer.d.ts +5 -5
  68. package/dist/timer.js +5 -5
  69. package/dist/types.d.ts +128 -21
  70. package/dist/types.js +1 -1
  71. package/package.json +1 -1
  72. package/readme.md +5 -1
@@ -1,8 +1,8 @@
1
- /**
2
- * 二分查找target 在arr中的索引
3
- * @param arr
4
- * @param target
5
- * @param forward true 向前取值,false
6
- * @param backward true 向后取值,false
7
- */
8
- export declare const binarySearchIndex: <T>(arr: T[], target: T, forward?: boolean, backward?: boolean) => number;
1
+ /**
2
+ * 二分查找target 在arr中的索引
3
+ * @param arr
4
+ * @param target
5
+ * @param forward true 向前取值,false
6
+ * @param backward true 向后取值,false
7
+ */
8
+ export declare const binarySearchIndex: <T>(arr: T[], target: T, forward?: boolean, backward?: boolean) => number;
@@ -1,38 +1,38 @@
1
- // 二分
2
- /**
3
- * 二分查找target 在arr中的索引
4
- * @param arr
5
- * @param target
6
- * @param forward true 向前取值,false
7
- * @param backward true 向后取值,false
8
- */
9
- export const binarySearchIndex = (arr, target, forward = false, backward = false) => {
10
- let low = 0;
11
- let high = arr.length - 1;
12
- let result = -1; // 如果找不到,返回-1
13
- while (low <= high) {
14
- const mid = Math.floor((low + high) / 2);
15
- if (arr[mid] === target) {
16
- return mid; // 找到目标值,返回索引
17
- }
18
- else if (arr[mid] < target) {
19
- low = mid + 1;
20
- }
21
- else {
22
- high = mid - 1;
23
- }
24
- if (forward) {
25
- // 向前取值
26
- if (arr[mid] < target) {
27
- result = mid;
28
- }
29
- }
30
- else if (backward) {
31
- // 向后取值
32
- if (arr[mid] > target) {
33
- result = mid;
34
- }
35
- }
36
- }
37
- return result;
38
- };
1
+ // 二分
2
+ /**
3
+ * 二分查找target 在arr中的索引
4
+ * @param arr
5
+ * @param target
6
+ * @param forward true 向前取值,false
7
+ * @param backward true 向后取值,false
8
+ */
9
+ export const binarySearchIndex = (arr, target, forward = false, backward = false) => {
10
+ let low = 0;
11
+ let high = arr.length - 1;
12
+ let result = -1; // 如果找不到,返回-1
13
+ while (low <= high) {
14
+ const mid = Math.floor((low + high) / 2);
15
+ if (arr[mid] === target) {
16
+ return mid; // 找到目标值,返回索引
17
+ }
18
+ else if (arr[mid] < target) {
19
+ low = mid + 1;
20
+ }
21
+ else {
22
+ high = mid - 1;
23
+ }
24
+ if (forward) {
25
+ // 向前取值
26
+ if (arr[mid] < target) {
27
+ result = mid;
28
+ }
29
+ }
30
+ else if (backward) {
31
+ // 向后取值
32
+ if (arr[mid] > target) {
33
+ result = mid;
34
+ }
35
+ }
36
+ }
37
+ return result;
38
+ };
@@ -1,2 +1,2 @@
1
- export * as tree from './tree';
2
- export * as binary from './binary';
1
+ export * as tree from './tree';
2
+ export * as binary from './binary';
@@ -1,2 +1,2 @@
1
- export * as tree from './tree';
2
- export * as binary from './binary';
1
+ export * as tree from './tree';
2
+ export * as binary from './binary';
@@ -1,34 +1,34 @@
1
- /**
2
- * 计算指定层级的节点数量
3
- * @param root tree data 对象
4
- * @param depth 需要计算节点数量的层级
5
- * @param childrenKey 子节点key
6
- * @returns 节点数量
7
- */
8
- export declare const nodeCountAtDepth: <T extends Record<string, any>>(root: T, depth: number, childrenKey?: string) => number;
9
- /**
10
- * 找到符合条件的节点
11
- * @param tree tree data 数组
12
- * @param predicate 对每个节点执行的函数, 如果返回true, 则返回该节点
13
- * @param childrenKey 子节点key
14
- * @returns 找到的节点
15
- */
16
- export declare const findNode: <T extends Record<string, any>>(tree: T[], predicate: (node: T) => boolean, childrenKey?: string) => T | null;
17
- /**
18
- * 根据子节点查找父节点
19
- * @param tree
20
- * @param child 子节点
21
- * @param indentityKey 节点唯一id字段名称
22
- * @param childrenKey 节点的子节点的字段名称
23
- * @returns 父节点
24
- */
25
- export declare const findParent: <T extends Record<string, any>>(tree: T | undefined, child: T | undefined, indentityKey?: string, childrenKey?: string) => T | null;
26
- /**
27
- * 查找节点路径
28
- * @param tree
29
- * @param func 符合条件的节点函数,返回 boolean
30
- * @param childrenKey 子节点key
31
- * @returns 节点路径
32
- */
33
- export declare function findPath<T extends Record<string, any>>(tree: T[], func: (node: T) => boolean, childrenKey?: string): T[] | null;
34
- export declare function flatten<T extends Record<string, any>>(tree: T[], childrenKey?: string): Record<string, any>[];
1
+ /**
2
+ * 计算指定层级的节点数量
3
+ * @param root tree data 对象
4
+ * @param depth 需要计算节点数量的层级
5
+ * @param childrenKey 子节点key
6
+ * @returns 节点数量
7
+ */
8
+ export declare const nodeCountAtDepth: <T extends Record<string, any>>(root: T, depth: number, childrenKey?: string) => number;
9
+ /**
10
+ * 找到符合条件的节点
11
+ * @param tree tree data 数组
12
+ * @param predicate 对每个节点执行的函数, 如果返回true, 则返回该节点
13
+ * @param childrenKey 子节点key
14
+ * @returns 找到的节点
15
+ */
16
+ export declare const findNode: <T extends Record<string, any>>(tree: T[], predicate: (node: T) => boolean, childrenKey?: string) => T | null;
17
+ /**
18
+ * 根据子节点查找父节点
19
+ * @param tree
20
+ * @param child 子节点
21
+ * @param indentityKey 节点唯一id字段名称
22
+ * @param childrenKey 节点的子节点的字段名称
23
+ * @returns 父节点
24
+ */
25
+ export declare const findParent: <T extends Record<string, any>>(tree: T | undefined, child: T | undefined, indentityKey?: string, childrenKey?: string) => T | null;
26
+ /**
27
+ * 查找节点路径
28
+ * @param tree
29
+ * @param func 符合条件的节点函数,返回 boolean
30
+ * @param childrenKey 子节点key
31
+ * @returns 节点路径
32
+ */
33
+ export declare function findPath<T extends Record<string, any>>(tree: T[], func: (node: T) => boolean, childrenKey?: string): T[] | null;
34
+ export declare function flatten<T extends Record<string, any>>(tree: T[], childrenKey?: string): Record<string, any>[];
@@ -1,124 +1,124 @@
1
- var __rest = (this && this.__rest) || function (s, e) {
2
- var t = {};
3
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
4
- t[p] = s[p];
5
- if (s != null && typeof Object.getOwnPropertySymbols === "function")
6
- for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
7
- if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
8
- t[p[i]] = s[p[i]];
9
- }
10
- return t;
11
- };
12
- /**
13
- * 计算指定层级的节点数量
14
- * @param root tree data 对象
15
- * @param depth 需要计算节点数量的层级
16
- * @param childrenKey 子节点key
17
- * @returns 节点数量
18
- */
19
- export const nodeCountAtDepth = (root, depth, childrenKey = 'children') => {
20
- const depths = { 0: 1 };
21
- (function recurTree(node, level) {
22
- var _a;
23
- level++;
24
- const children = (_a = node[childrenKey]) !== null && _a !== void 0 ? _a : [];
25
- if (depths[level] === undefined) {
26
- depths[level] = children.length;
27
- }
28
- else {
29
- depths[level] += children.length;
30
- }
31
- if (level + 1 > depth)
32
- return;
33
- for (let i = 0; i < children.length; i++) {
34
- recurTree(children[i], level);
35
- }
36
- })(root, 0);
37
- return depths[depth];
38
- };
39
- /**
40
- * 找到符合条件的节点
41
- * @param tree tree data 数组
42
- * @param predicate 对每个节点执行的函数, 如果返回true, 则返回该节点
43
- * @param childrenKey 子节点key
44
- * @returns 找到的节点
45
- */
46
- export const findNode = (tree, predicate, childrenKey = 'children') => {
47
- const list = [...tree];
48
- for (const node of list) {
49
- if (predicate(node))
50
- return node;
51
- Array.isArray(node[childrenKey]) && list.push(...node[childrenKey]);
52
- }
53
- return null;
54
- };
55
- /**
56
- * 根据子节点查找父节点
57
- * @param tree
58
- * @param child 子节点
59
- * @param indentityKey 节点唯一id字段名称
60
- * @param childrenKey 节点的子节点的字段名称
61
- * @returns 父节点
62
- */
63
- export const findParent = (tree, child, indentityKey = 'id', childrenKey = 'children') => {
64
- if (tree === undefined || child === undefined) {
65
- return null;
66
- }
67
- const children = tree[childrenKey];
68
- if (Array.isArray(children)) {
69
- const count = children.length;
70
- for (let i = 0; i < count; i++) {
71
- if (children[i][indentityKey] === child[indentityKey]) {
72
- return tree;
73
- }
74
- const parent = findParent(children[i], child, indentityKey, childrenKey);
75
- if (parent !== null) {
76
- return parent;
77
- }
78
- }
79
- }
80
- return null;
81
- };
82
- /**
83
- * 查找节点路径
84
- * @param tree
85
- * @param func 符合条件的节点函数,返回 boolean
86
- * @param childrenKey 子节点key
87
- * @returns 节点路径
88
- */
89
- export function findPath(tree, func, childrenKey = 'children') {
90
- const path = [];
91
- const list = [...tree];
92
- const visitedSet = new Set();
93
- while (list.length) {
94
- const node = list[0];
95
- if (visitedSet.has(node)) {
96
- path.pop();
97
- list.shift();
98
- }
99
- else {
100
- visitedSet.add(node);
101
- Array.isArray(node[childrenKey]) && list.unshift(...node[childrenKey]);
102
- path.push(node);
103
- if (func(node))
104
- return path;
105
- }
106
- }
107
- return null;
108
- }
109
- export function flatten(tree, childrenKey = 'children') {
110
- const result = [];
111
- function traverse(node) {
112
- // 将当前节点添加到结果数组
113
- const _a = node, _b = childrenKey, __ = _a[_b], nodePropsOmitChildren = __rest(_a, [typeof _b === "symbol" ? _b : _b + ""]);
114
- result.push(nodePropsOmitChildren);
115
- const children = node[childrenKey];
116
- // 如果节点有子节点,则递归处理每个子节点
117
- if (children && Array.isArray(children) && children.length > 0) {
118
- children.forEach((child) => traverse(child));
119
- }
120
- }
121
- // 遍历树的每个根节点
122
- tree.forEach((root) => traverse(root));
123
- return result;
124
- }
1
+ var __rest = (this && this.__rest) || function (s, e) {
2
+ var t = {};
3
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
4
+ t[p] = s[p];
5
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
6
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
7
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
8
+ t[p[i]] = s[p[i]];
9
+ }
10
+ return t;
11
+ };
12
+ /**
13
+ * 计算指定层级的节点数量
14
+ * @param root tree data 对象
15
+ * @param depth 需要计算节点数量的层级
16
+ * @param childrenKey 子节点key
17
+ * @returns 节点数量
18
+ */
19
+ export const nodeCountAtDepth = (root, depth, childrenKey = 'children') => {
20
+ const depths = { 0: 1 };
21
+ (function recurTree(node, level) {
22
+ var _a;
23
+ level++;
24
+ const children = (_a = node[childrenKey]) !== null && _a !== void 0 ? _a : [];
25
+ if (depths[level] === undefined) {
26
+ depths[level] = children.length;
27
+ }
28
+ else {
29
+ depths[level] += children.length;
30
+ }
31
+ if (level + 1 > depth)
32
+ return;
33
+ for (let i = 0; i < children.length; i++) {
34
+ recurTree(children[i], level);
35
+ }
36
+ })(root, 0);
37
+ return depths[depth];
38
+ };
39
+ /**
40
+ * 找到符合条件的节点
41
+ * @param tree tree data 数组
42
+ * @param predicate 对每个节点执行的函数, 如果返回true, 则返回该节点
43
+ * @param childrenKey 子节点key
44
+ * @returns 找到的节点
45
+ */
46
+ export const findNode = (tree, predicate, childrenKey = 'children') => {
47
+ const list = [...tree];
48
+ for (const node of list) {
49
+ if (predicate(node))
50
+ return node;
51
+ Array.isArray(node[childrenKey]) && list.push(...node[childrenKey]);
52
+ }
53
+ return null;
54
+ };
55
+ /**
56
+ * 根据子节点查找父节点
57
+ * @param tree
58
+ * @param child 子节点
59
+ * @param indentityKey 节点唯一id字段名称
60
+ * @param childrenKey 节点的子节点的字段名称
61
+ * @returns 父节点
62
+ */
63
+ export const findParent = (tree, child, indentityKey = 'id', childrenKey = 'children') => {
64
+ if (tree === undefined || child === undefined) {
65
+ return null;
66
+ }
67
+ const children = tree[childrenKey];
68
+ if (Array.isArray(children)) {
69
+ const count = children.length;
70
+ for (let i = 0; i < count; i++) {
71
+ if (children[i][indentityKey] === child[indentityKey]) {
72
+ return tree;
73
+ }
74
+ const parent = findParent(children[i], child, indentityKey, childrenKey);
75
+ if (parent !== null) {
76
+ return parent;
77
+ }
78
+ }
79
+ }
80
+ return null;
81
+ };
82
+ /**
83
+ * 查找节点路径
84
+ * @param tree
85
+ * @param func 符合条件的节点函数,返回 boolean
86
+ * @param childrenKey 子节点key
87
+ * @returns 节点路径
88
+ */
89
+ export function findPath(tree, func, childrenKey = 'children') {
90
+ const path = [];
91
+ const list = [...tree];
92
+ const visitedSet = new Set();
93
+ while (list.length) {
94
+ const node = list[0];
95
+ if (visitedSet.has(node)) {
96
+ path.pop();
97
+ list.shift();
98
+ }
99
+ else {
100
+ visitedSet.add(node);
101
+ Array.isArray(node[childrenKey]) && list.unshift(...node[childrenKey]);
102
+ path.push(node);
103
+ if (func(node))
104
+ return path;
105
+ }
106
+ }
107
+ return null;
108
+ }
109
+ export function flatten(tree, childrenKey = 'children') {
110
+ const result = [];
111
+ function traverse(node) {
112
+ // 将当前节点添加到结果数组
113
+ const _a = node, _b = childrenKey, __ = _a[_b], nodePropsOmitChildren = __rest(_a, [typeof _b === "symbol" ? _b : _b + ""]);
114
+ result.push(nodePropsOmitChildren);
115
+ const children = node[childrenKey];
116
+ // 如果节点有子节点,则递归处理每个子节点
117
+ if (children && Array.isArray(children) && children.length > 0) {
118
+ children.forEach((child) => traverse(child));
119
+ }
120
+ }
121
+ // 遍历树的每个根节点
122
+ tree.forEach((root) => traverse(root));
123
+ return result;
124
+ }
package/dist/array.d.ts CHANGED
@@ -1,5 +1,46 @@
1
- export declare function moveMutable<T>(array: T[], fromIndex: number, toIndex: number): void;
2
- export declare function moveImmutable<T>(array: T[], fromIndex: number, toIndex: number): T[];
3
- export declare function moveToStart<T>(array: T[], predicate: (item: T) => boolean): T[];
4
- export declare const moveMulti: <T extends unknown>(arr: T[], indexes: number[], start: number) => T[];
5
- export declare const getArrayOrUndefined: <T>(array?: T[] | null | undefined) => T[] | undefined;
1
+ /**
2
+ * 在数组中移动元素的位置(修改原数组)
3
+ *
4
+ * @param array - 要操作的数组
5
+ * @param fromIndex - 要移动元素的起始索引,支持负数(-1表示最后一个元素)
6
+ * @param toIndex - 目标位置的索引,支持负数(-1表示最后一个位置)
7
+ * @returns 无返回值,直接修改原数组
8
+ */
9
+ export declare function moveMutable<T>(array: T[], fromIndex: number, toIndex: number): void;
10
+ /**
11
+ * 创建一个新数组,将指定位置的元素移动到新位置,不修改原数组
12
+ * @param array 原始数组
13
+ * @param fromIndex 要移动元素的当前位置索引
14
+ * @param toIndex 元素要移动到的目标位置索引
15
+ * @returns 返回一个新的数组,其中指定元素已移动到新位置
16
+ */
17
+ export declare function moveImmutable<T>(array: T[], fromIndex: number, toIndex: number): T[];
18
+ /**
19
+ * 将数组中第一个满足条件的元素移动到数组开头
20
+ * @param array - 要操作的数组
21
+ * @param predicate - 判断元素是否满足条件的函数
22
+ * @returns 新的数组,其中第一个满足条件的元素被移动到开头位置
23
+ */
24
+ export declare function moveToStart<T>(array: T[], predicate: (item: T) => boolean): T[];
25
+ export declare const moveMulti: <T extends unknown>(arr: T[], indexes: number[], start: number) => T[];
26
+ /**
27
+ * 获取数组或undefined
28
+ *
29
+ * @param array - 可选的数组参数,可以是任意类型的数组、undefined或null
30
+ * @returns 如果传入的参数是有效的非空数组则返回该数组,否则返回undefined
31
+ */
32
+ export declare const getArrayOrUndefined: <T>(array?: T[] | null | undefined) => T[] | undefined;
33
+ /**
34
+ * 通用序号计算工具:从指定列表中提取「前缀+数字」格式的值,找到未被使用的最小正整数序号
35
+ * @param list 待检索的列表
36
+ * @param options 配置项(通用化核心)
37
+ * @param options.fieldName 要提取的字段名(比如 'groupName')
38
+ * @param options.prefix 匹配的前缀(比如 '组合')
39
+ * @param options.defaultNo 无可用序号时的默认值(默认1)
40
+ * @returns 未被使用的最小正整数序号
41
+ */
42
+ export declare function calcUnusedMinSerialNumber(list: any[], options: {
43
+ fieldName: string;
44
+ prefix: string;
45
+ defaultNo?: number;
46
+ }): number;
package/dist/array.js CHANGED
@@ -1,37 +1,123 @@
1
- export function moveMutable(array, fromIndex, toIndex) {
2
- const startIndex = fromIndex < 0 ? array.length + fromIndex : fromIndex;
3
- if (startIndex >= 0 && startIndex < array.length) {
4
- const endIndex = toIndex < 0 ? array.length + toIndex : toIndex;
5
- const [item] = array.splice(fromIndex, 1);
6
- array.splice(endIndex, 0, item);
7
- }
8
- }
9
- export function moveImmutable(array, fromIndex, toIndex) {
10
- const newArray = [...array];
11
- moveMutable(newArray, fromIndex, toIndex);
12
- return newArray;
13
- }
14
- export function moveToStart(array, predicate) {
15
- const index = array.findIndex(predicate);
16
- if (index < 0)
17
- return array;
18
- const newArray = [...array];
19
- moveMutable(newArray, index, 0);
20
- return newArray;
21
- }
22
- const removeSymbol = Symbol('Placeholder for removed element');
23
- export const moveMulti = (arr, indexes, start) => {
24
- const cloned = arr.slice();
25
- for (let i = 0; i < cloned.length; i++) {
26
- if (indexes.includes(i)) {
27
- cloned[i] = removeSymbol;
28
- }
29
- }
30
- const els = arr.filter((__, i) => indexes.includes(i));
31
- cloned.splice(start, 0, ...els);
32
- return cloned.filter((v) => v !== removeSymbol);
33
- };
34
- export const getArrayOrUndefined = (array) => {
35
- if (Array.isArray(array) && array.length > 0)
36
- return array;
37
- };
1
+ /**
2
+ * 在数组中移动元素的位置(修改原数组)
3
+ *
4
+ * @param array - 要操作的数组
5
+ * @param fromIndex - 要移动元素的起始索引,支持负数(-1表示最后一个元素)
6
+ * @param toIndex - 目标位置的索引,支持负数(-1表示最后一个位置)
7
+ * @returns 无返回值,直接修改原数组
8
+ */
9
+ export function moveMutable(array, fromIndex, toIndex) {
10
+ // 计算实际的起始索引,支持负数索引
11
+ const startIndex = fromIndex < 0 ? array.length + fromIndex : fromIndex;
12
+ // 检查起始索引是否有效
13
+ if (startIndex >= 0 && startIndex < array.length) {
14
+ // 计算实际的目标索引,支持负数索引
15
+ const endIndex = toIndex < 0 ? array.length + toIndex : toIndex;
16
+ // 执行元素移动:先删除元素,再在新位置插入
17
+ const [item] = array.splice(fromIndex, 1);
18
+ array.splice(endIndex, 0, item);
19
+ }
20
+ }
21
+ /**
22
+ * 创建一个新数组,将指定位置的元素移动到新位置,不修改原数组
23
+ * @param array 原始数组
24
+ * @param fromIndex 要移动元素的当前位置索引
25
+ * @param toIndex 元素要移动到的目标位置索引
26
+ * @returns 返回一个新的数组,其中指定元素已移动到新位置
27
+ */
28
+ export function moveImmutable(array, fromIndex, toIndex) {
29
+ // 创建原数组的浅拷贝
30
+ const newArray = [...array];
31
+ // 在新数组上执行移动操作
32
+ moveMutable(newArray, fromIndex, toIndex);
33
+ return newArray;
34
+ }
35
+ /**
36
+ * 将数组中第一个满足条件的元素移动到数组开头
37
+ * @param array - 要操作的数组
38
+ * @param predicate - 判断元素是否满足条件的函数
39
+ * @returns 新的数组,其中第一个满足条件的元素被移动到开头位置
40
+ */
41
+ export function moveToStart(array, predicate) {
42
+ // 查找第一个满足条件的元素索引
43
+ const index = array.findIndex(predicate);
44
+ if (index < 0)
45
+ return array;
46
+ // 创建新数组并移动元素到开头
47
+ const newArray = [...array];
48
+ moveMutable(newArray, index, 0);
49
+ return newArray;
50
+ }
51
+ /**
52
+ * 将数组中指定索引的元素移动到指定位置
53
+ * @param arr 原始数组
54
+ * @param indexes 需要移动的元素索引数组
55
+ * @param start 目标插入位置的起始索引
56
+ * @returns 移动元素后的新数组
57
+ */
58
+ const removeSymbol = Symbol('Placeholder for removed element');
59
+ export const moveMulti = (arr, indexes, start) => {
60
+ // 创建数组副本,用于标记需要移除的元素
61
+ const cloned = arr.slice();
62
+ for (let i = 0; i < cloned.length; i++) {
63
+ if (indexes.includes(i)) {
64
+ cloned[i] = removeSymbol;
65
+ }
66
+ }
67
+ // 提取需要移动的元素
68
+ const els = arr.filter((__, i) => indexes.includes(i));
69
+ // 在目标位置插入提取的元素,并过滤掉标记为移除的元素
70
+ cloned.splice(start, 0, ...els);
71
+ return cloned.filter((v) => v !== removeSymbol);
72
+ };
73
+ /**
74
+ * 获取数组或undefined
75
+ *
76
+ * @param array - 可选的数组参数,可以是任意类型的数组、undefined或null
77
+ * @returns 如果传入的参数是有效的非空数组则返回该数组,否则返回undefined
78
+ */
79
+ export const getArrayOrUndefined = (array) => {
80
+ // 检查参数是否为数组且长度大于0,如果是则返回原数组,否则返回undefined
81
+ if (Array.isArray(array) && array.length > 0)
82
+ return array;
83
+ return undefined;
84
+ };
85
+ /**
86
+ * 通用序号计算工具:从指定列表中提取「前缀+数字」格式的值,找到未被使用的最小正整数序号
87
+ * @param list 待检索的列表
88
+ * @param options 配置项(通用化核心)
89
+ * @param options.fieldName 要提取的字段名(比如 'groupName')
90
+ * @param options.prefix 匹配的前缀(比如 '组合')
91
+ * @param options.defaultNo 无可用序号时的默认值(默认1)
92
+ * @returns 未被使用的最小正整数序号
93
+ */
94
+ export function calcUnusedMinSerialNumber(list, options) {
95
+ const { fieldName, prefix, defaultNo = 1 } = options;
96
+ if (!(list === null || list === void 0 ? void 0 : list.length))
97
+ return defaultNo;
98
+ // 获取所有匹配前缀的项目并提取序号
99
+ const serialNumbers = list
100
+ .map((item) => item[fieldName])
101
+ .filter((value) => typeof value === 'string' && value.startsWith(prefix))
102
+ .map((value) => {
103
+ const numPart = value.substring(prefix.length);
104
+ // 如果前缀后没有数字,则认为是序号1(或者根据业务需求处理)
105
+ if (numPart === '')
106
+ return 1;
107
+ const num = parseInt(numPart, 10);
108
+ return isNaN(num) ? 0 : num;
109
+ })
110
+ .filter((num) => num > 0)
111
+ .sort((a, b) => a - b);
112
+ // 如果没有匹配项,返回默认值
113
+ if (serialNumbers.length === 0)
114
+ return defaultNo;
115
+ // 找到第一个缺失的正整数
116
+ for (let i = 0; i < serialNumbers.length; i++) {
117
+ if (serialNumbers[i] !== i + 1) {
118
+ return i + 1;
119
+ }
120
+ }
121
+ // 如果序列完整,返回下一个数字
122
+ return serialNumbers.length + 1;
123
+ }
@@ -1,12 +1,12 @@
1
- /**
2
- * 复制图片到剪贴板
3
- * @param element
4
- * @returns
5
- */
6
- export declare function writeImage(element: HTMLImageElement | null | string): Promise<unknown>;
7
- /**
8
- * 复制文本到剪贴板
9
- * @param text
10
- * @returns
11
- */
12
- export declare function writeText(text: string): Promise<void>;
1
+ /**
2
+ * 复制图片到剪贴板
3
+ * @param element
4
+ * @returns
5
+ */
6
+ export declare function writeImage(element: HTMLImageElement | null | string): Promise<unknown>;
7
+ /**
8
+ * 复制文本到剪贴板
9
+ * @param text
10
+ * @returns
11
+ */
12
+ export declare function writeText(text: string): Promise<void>;