7e9ac6a6f2777606af4ead8ff1decb2a3a6ccde0.js 4.5 KB

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