e728da7c6680c51b25b7eb57f6e998bf47314a83.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. System.register(["cc"], function (_export, _context) {
  2. "use strict";
  3. var _cclegacy, List, _crd;
  4. _export("List", void 0);
  5. return {
  6. setters: [function (_cc) {
  7. _cclegacy = _cc.cclegacy;
  8. }],
  9. execute: function () {
  10. _crd = true;
  11. _cclegacy._RF.push({}, "f4d36IVkDZEFYGJyOy6wiZw", "List", undefined);
  12. /** 列表 */
  13. _export("List", List = class List {
  14. constructor(only = true) {
  15. this.element = void 0;
  16. /** 是否保证元素的唯一性 */
  17. this.only = false;
  18. /** 元素数量(内部再增删时会修改这个参数,外部只做计算和绑定使用,切记不可做赋值操作) */
  19. this.count = 0;
  20. this.only = only;
  21. this.element = [];
  22. }
  23. /**
  24. * 添加到末尾(注意如果保证唯一性,那么重复时就直接返回)
  25. * @param value
  26. */
  27. push(value) {
  28. if (this.only) {
  29. let index = this.element.indexOf(value);
  30. if (index >= 0) {
  31. return false;
  32. }
  33. }
  34. this.element.push(value);
  35. this.count = this.element.length;
  36. return true;
  37. }
  38. /**
  39. * 添加到列表头部(注意如果保证唯一性,那么重复时就直接返回)
  40. * @param value
  41. * @returns
  42. */
  43. unshift(value) {
  44. if (this.only) {
  45. let index = this.element.indexOf(value);
  46. if (index >= 0) {
  47. return false;
  48. }
  49. }
  50. this.element.unshift(value);
  51. this.count = this.element.length;
  52. return true;
  53. }
  54. /**
  55. * 获取并删除最后一个元素
  56. * @returns
  57. */
  58. pop() {
  59. if (this.element.length > 0) {
  60. const result = this.element.pop();
  61. this.count = this.element.length;
  62. return result;
  63. }
  64. return null;
  65. }
  66. /**
  67. * 获取并删除第一个元素
  68. * @returns
  69. */
  70. shift() {
  71. if (this.element.length > 0) {
  72. const result = this.element.shift();
  73. this.count = this.element.length;
  74. return result;
  75. }
  76. return null;
  77. }
  78. /**
  79. * 删除指定索引的元素
  80. * @param index
  81. */
  82. removeAt(index) {
  83. if (index >= this.element.length) {
  84. throw new Error("删除索引超出范围!");
  85. }
  86. const result = this.element[index];
  87. this.element.splice(index, 1);
  88. this.count = this.element.length;
  89. return result;
  90. }
  91. /**
  92. * 删除元素
  93. * @param value
  94. */
  95. remove(value) {
  96. let index = this.element.indexOf(value);
  97. if (index < 0) {
  98. throw new Error("要删除的内容不在列表中!" + value);
  99. }
  100. const result = this.element[index];
  101. this.element.splice(index, 1);
  102. this.count = this.element.length;
  103. }
  104. /** 移除所有元素 */
  105. clear() {
  106. this.count = 0;
  107. this.element.length = 0;
  108. }
  109. /**
  110. * 判断是否包含
  111. * @param value
  112. * @returns
  113. */
  114. has(value) {
  115. return this.find(value) >= 0;
  116. }
  117. /**
  118. * 查找元素下标
  119. * @param value
  120. * @returns
  121. */
  122. find(value) {
  123. return this.element.indexOf(value);
  124. }
  125. /**
  126. * 查找元素下标
  127. * @param predicate
  128. * @returns
  129. */
  130. findIndex(predicate) {
  131. let index = this.element.findIndex(predicate);
  132. return index;
  133. }
  134. /**
  135. * 获取指定元素
  136. * @param index
  137. * @returns
  138. */
  139. get(index) {
  140. if (index >= this.element.length) {
  141. throw new Error("超出索引范围:" + index + "/" + this.element.length);
  142. }
  143. return this.element[index];
  144. }
  145. /**
  146. * 源列表数据(注意不要直接进行增删操作,而是通过List.push....等接口进行操作)
  147. */
  148. get elements() {
  149. return this.element;
  150. }
  151. });
  152. _cclegacy._RF.pop();
  153. _crd = false;
  154. }
  155. };
  156. });
  157. //# sourceMappingURL=e728da7c6680c51b25b7eb57f6e998bf47314a83.js.map