@scx-js/scx-common 0.1.0 → 0.1.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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/util/ArrayUtils.js +51 -5
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@scx-js/scx-common",
3
- "version": "0.1.0",
3
+ "version": "0.1.2",
4
4
  "description": "SCX Common",
5
5
  "license": "MIT",
6
6
  "author": "scx567888",
@@ -47,7 +47,7 @@ function moveUpByIndex(list, index, loop = false, step = 1) {
47
47
  if (nextIndex < minIndex) {
48
48
  nextIndex = loop ? nextIndex % list.length + list.length : minIndex;
49
49
  }
50
- return moveItemByIndex(list, index, nextIndex);
50
+ moveItemByIndex(list, index, nextIndex);
51
51
  }
52
52
 
53
53
  /**
@@ -63,7 +63,7 @@ function moveDownByIndex(list, index, loop = false, step = 1) {
63
63
  if (nextIndex > maxIndex) {
64
64
  nextIndex = loop ? nextIndex % list.length : maxIndex;
65
65
  }
66
- return moveItemByIndex(list, index, nextIndex);
66
+ moveItemByIndex(list, index, nextIndex);
67
67
  }
68
68
 
69
69
  /**
@@ -72,7 +72,10 @@ function moveDownByIndex(list, index, loop = false, step = 1) {
72
72
  * @param item
73
73
  */
74
74
  function removeByItem(list, item) {
75
- return removeByIndex(list, list.indexOf(item));
75
+ let indexOf = list.indexOf(item);
76
+ if (indexOf !== -1) {
77
+ removeByIndex(list, indexOf);
78
+ }
76
79
  }
77
80
 
78
81
  /**
@@ -83,7 +86,10 @@ function removeByItem(list, item) {
83
86
  * @param step 步长
84
87
  */
85
88
  function moveUpByItem(list, item, loop = false, step = 1) {
86
- return moveUpByIndex(list, list.indexOf(item), loop, step);
89
+ let indexOf = list.indexOf(item);
90
+ if (indexOf !== -1) {
91
+ moveUpByIndex(list, indexOf, loop, step);
92
+ }
87
93
  }
88
94
 
89
95
  /**
@@ -94,7 +100,10 @@ function moveUpByItem(list, item, loop = false, step = 1) {
94
100
  * @param step 步长
95
101
  */
96
102
  function moveDownByItem(list, item, loop = false, step = 1) {
97
- return moveDownByIndex(list, list.indexOf(item), loop, step);
103
+ let indexOf = list.indexOf(item);
104
+ if (indexOf !== -1) {
105
+ moveDownByIndex(list, indexOf, loop, step);
106
+ }
98
107
  }
99
108
 
100
109
  /**
@@ -125,6 +134,40 @@ function arrayEquals(array1, array2) {
125
134
  return true;
126
135
  }
127
136
 
137
+ /**
138
+ * 根据 项 从数组中移除所有匹配的项
139
+ * @param list
140
+ * @param item
141
+ */
142
+ function removeAllByItem(list, item) {
143
+ removeIf(list, (c) => c === item);
144
+ }
145
+
146
+ /**
147
+ * 直接去重原数组 - 使用临时列表记录已出现过的元素
148
+ * @param list
149
+ */
150
+ function uniqueArray(list) {
151
+ const seen = new Set();
152
+ let j = 0;
153
+ for (let i = 0; i < list.length; i++) {
154
+ const item = list[i];
155
+ if (!seen.has(item)) {
156
+ seen.add(item);
157
+ list[j++] = item; // 将唯一元素移到数组前方
158
+ }
159
+ }
160
+ list.length = j; // 截断数组长度
161
+ }
162
+
163
+ function removeIf(list, filter) {
164
+ for (let i = list.length - 1; i >= 0; i--) { // 从后向前遍历,避免索引错位
165
+ if (filter(list[i])) {
166
+ removeByIndex(list, i);
167
+ }
168
+ }
169
+ }
170
+
128
171
  export {
129
172
  copyArray,
130
173
  removeByIndex,
@@ -135,4 +178,7 @@ export {
135
178
  moveDownByItem,
136
179
  insertItem,
137
180
  arrayEquals,
181
+ removeAllByItem,
182
+ uniqueArray,
183
+ removeIf,
138
184
  };