@scx-js/scx-common 0.1.0 → 0.1.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 (2) hide show
  1. package/package.json +1 -1
  2. package/util/ArrayUtils.js +46 -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.1",
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,36 @@ 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
+ for (let i = list.length - 1; i >= 0; i--) {
144
+ if (list[i] === item) {
145
+ removeByIndex(list, i);
146
+ }
147
+ }
148
+ }
149
+
150
+ /**
151
+ * 直接去重原数组 - 使用临时列表记录已出现过的元素
152
+ * @param list
153
+ */
154
+ function uniqueArray(list) {
155
+ const seen = new Set();
156
+ let j = 0;
157
+ for (let i = 0; i < list.length; i++) {
158
+ const item = list[i];
159
+ if (!seen.has(item)) {
160
+ seen.add(item);
161
+ list[j++] = item; // 将唯一元素移到数组前方
162
+ }
163
+ }
164
+ list.length = j; // 截断数组长度
165
+ }
166
+
128
167
  export {
129
168
  copyArray,
130
169
  removeByIndex,
@@ -135,4 +174,6 @@ export {
135
174
  moveDownByItem,
136
175
  insertItem,
137
176
  arrayEquals,
177
+ removeAllByItem,
178
+ uniqueArray,
138
179
  };