@zhongguo168a/yxeditor-common 0.0.99 → 0.0.101

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/dist/index.d.ts CHANGED
@@ -863,12 +863,13 @@ declare class List<T = any> {
863
863
  removeList(other: List<T>): void;
864
864
  sort(compareFn?: (a: T, b: T) => number): void;
865
865
  reverse(): void;
866
- clear(): void;
866
+ clear(useIter?: boolean): void;
867
867
  /**
868
868
  * 克隆自身所有项到【target】对象,并返回【target】对象
869
869
  * @param target
870
+ * @param useIter 使用遍历的方式可以触发onSet
870
871
  */
871
- clone(target: this): this;
872
+ clone(target: this, useIter?: boolean): this;
872
873
  get length(): number;
873
874
  insertAt(index: number, ...item: T[]): void;
874
875
  /**
@@ -900,6 +901,13 @@ declare class List<T = any> {
900
901
  * @param target
901
902
  */
902
903
  random(count: number, enableRepeat?: boolean, target?: List<T>): List<T>;
904
+ /**
905
+ * 随机获取【count】个项,并返回【target】对象
906
+ * @param count
907
+ * @param enableRepeat 允许重复
908
+ * @param target
909
+ */
910
+ randomOne(): T;
903
911
  protected callPush(value: any): void;
904
912
  protected callRemove(value: any): void;
905
913
  protected _data: any[];
package/dist/index.esm.js CHANGED
@@ -3332,17 +3332,36 @@ class List {
3332
3332
  reverse() {
3333
3333
  this._data.reverse();
3334
3334
  }
3335
- clear() {
3336
- this._data.length = 0;
3335
+ clear(useIter = false) {
3336
+ let data = this._data;
3337
+ if (useIter) {
3338
+ for (let i = 0; i < data.length; i++) {
3339
+ const item = data[i];
3340
+ this.remove(item);
3341
+ }
3342
+ }
3343
+ else {
3344
+ this._data.length = 0;
3345
+ }
3337
3346
  }
3338
3347
  /**
3339
3348
  * 克隆自身所有项到【target】对象,并返回【target】对象
3340
3349
  * @param target
3350
+ * @param useIter 使用遍历的方式可以触发onSet
3341
3351
  */
3342
- clone(target) {
3343
- let newdata = target.getData().concat(this._data);
3344
- target.resetData(newdata);
3345
- return target;
3352
+ clone(target, useIter = false) {
3353
+ let data = this._data;
3354
+ if (useIter) {
3355
+ for (let i = 0; i < data.length; i++) {
3356
+ const item = data[i];
3357
+ target.push(item);
3358
+ }
3359
+ }
3360
+ else {
3361
+ let newdata = target.getData().concat(data);
3362
+ target.resetData(newdata);
3363
+ return target;
3364
+ }
3346
3365
  }
3347
3366
  get length() {
3348
3367
  return this._data.length;
@@ -3430,6 +3449,10 @@ class List {
3430
3449
  if (!target) {
3431
3450
  target = new List();
3432
3451
  }
3452
+ let data = this._data;
3453
+ if (data.length <= 0) {
3454
+ return target;
3455
+ }
3433
3456
  const copyItems = [...this._data]; // 创建副本以避免修改原数组
3434
3457
  for (let i = 0; i < count && copyItems.length > 0; i++) {
3435
3458
  const randomIndex = Math.floor(Math.random() * copyItems.length);
@@ -3440,6 +3463,20 @@ class List {
3440
3463
  }
3441
3464
  return target;
3442
3465
  }
3466
+ /**
3467
+ * 随机获取【count】个项,并返回【target】对象
3468
+ * @param count
3469
+ * @param enableRepeat 允许重复
3470
+ * @param target
3471
+ */
3472
+ randomOne() {
3473
+ let data = this._data;
3474
+ if (data.length <= 0) {
3475
+ return null;
3476
+ }
3477
+ const randomIndex = Math.floor(Math.random() * data.length);
3478
+ return data[randomIndex];
3479
+ }
3443
3480
  callPush(value) {
3444
3481
  if (this._onSet !== null) {
3445
3482
  this._onSet.call(this._onSetCaller, value);