es6-extend.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. // Object.assign Polyfill
  2. if (typeof Object.assign !== 'function') {
  3. Object.defineProperty(Object, 'assign', {
  4. value: function (target, ...sources) {
  5. if (target == null) {
  6. throw new TypeError("Cannot convert undefined or null to object");
  7. }
  8. for (let source of sources) {
  9. if (source != null) {
  10. for (let key in source) {
  11. if (Object.prototype.hasOwnProperty.call(source, key)) {
  12. target[key] = source[key];
  13. }
  14. }
  15. }
  16. }
  17. return target;
  18. },
  19. writable: true,
  20. configurable: true,
  21. enumerable: false
  22. });
  23. }
  24. // String.prototype.includes Polyfill
  25. if (!String.prototype.includes) {
  26. Object.defineProperty(String.prototype, 'includes', {
  27. value: function (search, start = 0) {
  28. if (typeof start !== 'number') start = 0;
  29. return this.indexOf(search, start) !== -1;
  30. },
  31. writable: true,
  32. configurable: true,
  33. enumerable: false
  34. });
  35. }
  36. // Array.prototype.includes Polyfill
  37. if (!Array.prototype.includes) {
  38. Object.defineProperty(Array.prototype, 'includes', {
  39. value: function (searchElement, fromIndex = 0) {
  40. if (this == null) {
  41. throw new TypeError('"this" is null or not defined');
  42. }
  43. let o = Object(this);
  44. let len = o.length >>> 0;
  45. if (len === 0) return false;
  46. let n = fromIndex | 0;
  47. let k = Math.max(n >= 0 ? n : len - Math.abs(n), 0);
  48. while (k < len) {
  49. if (o[k] === searchElement) return true;
  50. k++;
  51. }
  52. return false;
  53. },
  54. writable: true,
  55. configurable: true,
  56. enumerable: false
  57. });
  58. }
  59. // String.prototype.startsWith Polyfill
  60. if (typeof String.prototype.startsWith !== 'function') {
  61. Object.defineProperty(String.prototype, 'startsWith', {
  62. value: function (prefix) {
  63. return this.slice(0, prefix.length) === prefix;
  64. },
  65. writable: true,
  66. configurable: true,
  67. enumerable: false
  68. });
  69. }
  70. // String.prototype.endsWith Polyfill
  71. if (typeof String.prototype.endsWith !== 'function') {
  72. Object.defineProperty(String.prototype, 'endsWith', {
  73. value: function (suffix) {
  74. return this.indexOf(suffix, this.length - suffix.length) !== -1;
  75. },
  76. writable: true,
  77. configurable: true,
  78. enumerable: false
  79. });
  80. }
  81. // Object.values Polyfill
  82. if (typeof Object.values !== 'function') {
  83. Object.defineProperty(Object, 'values', {
  84. value: function (obj) {
  85. if (obj == null) {
  86. throw new TypeError("Cannot convert undefined or null to object");
  87. }
  88. let res = [];
  89. for (let key in obj) {
  90. if (Object.prototype.hasOwnProperty.call(obj, key)) {
  91. res.push(obj[key]);
  92. }
  93. }
  94. return res;
  95. },
  96. writable: true,
  97. configurable: true,
  98. enumerable: false
  99. });
  100. }
  101. // Array.prototype.join Polyfill (Custom)
  102. if (typeof Array.prototype.join !== 'function') {
  103. Object.defineProperty(Array.prototype, 'join', {
  104. value: function (separator = '') {
  105. if (!Array.isArray(this)) {
  106. throw new TypeError(`${this} is not an array`);
  107. }
  108. return this.reduce((str, item, index) => {
  109. return str + (index > 0 ? separator : '') + item;
  110. }, '');
  111. },
  112. writable: true,
  113. configurable: true,
  114. enumerable: false
  115. });
  116. }
  117. // Array.prototype.toReversed Polyfill
  118. if (typeof Array.prototype.toReversed !== 'function') {
  119. Object.defineProperty(Array.prototype, 'toReversed', {
  120. value: function () {
  121. return this.slice().reverse();
  122. },
  123. writable: true,
  124. configurable: true,
  125. enumerable: false
  126. });
  127. }
  128. // Array.prototype.append (Alias for push)
  129. Object.defineProperty(Array.prototype, 'append', {
  130. value: Array.prototype.push,
  131. writable: true,
  132. configurable: true,
  133. enumerable: false
  134. });
  135. // String.prototype.strip (Alias for trim)
  136. Object.defineProperty(String.prototype, 'strip', {
  137. value: String.prototype.trim,
  138. writable: true,
  139. configurable: true,
  140. enumerable: false
  141. });
  142. // String.prototype.rstrip Polyfill
  143. Object.defineProperty(String.prototype, 'rstrip', {
  144. value: function (chars) {
  145. if (!chars) {
  146. return this.replace(/\s+$/, '');
  147. }
  148. let regex = new RegExp(`${chars}+$`);
  149. return this.replace(regex, '');
  150. },
  151. writable: true,
  152. configurable: true,
  153. enumerable: false
  154. });
  155. Object.defineProperty(String.prototype, 'join', {
  156. value: function (arr) {
  157. if (!Array.isArray(arr)) {
  158. throw new TypeError('Argument must be an array');
  159. }
  160. return arr.join(this);
  161. },
  162. writable: true,
  163. configurable: true,
  164. enumerable: false
  165. });
  166. //正则matchAll
  167. function matchesAll(str, pattern, flatten) {
  168. if (!pattern.global) {
  169. pattern = new RegExp(pattern.source, "g" + (pattern.ignoreCase ? "i" : "") + (pattern.multiline ? "m" : ""));
  170. }
  171. var matches = [];
  172. var match;
  173. while ((match = pattern.exec(str)) !== null) {
  174. matches.push(match);
  175. }
  176. return flatten ? matches.flat() : matches;
  177. }
  178. //文本扩展
  179. Object.defineProperties(String.prototype, {
  180. replaceX: {
  181. value: function (regex, replacement) {
  182. let matches = matchesAll(this, regex, true);
  183. if (matches && matches.length > 1) {
  184. const hasCaptureGroup = /\$\d/.test(replacement);
  185. if (hasCaptureGroup) {
  186. return this.replace(regex, (m) => m.replace(regex, replacement));
  187. } else {
  188. return this.replace(regex, (m, p1) => m.replace(p1, replacement));
  189. }
  190. }
  191. return this.replace(regex, replacement);
  192. },
  193. writable: true,
  194. configurable: true,
  195. enumerable: false
  196. },
  197. parseX: {
  198. get: function () {
  199. try {
  200. return JSON.parse(this);
  201. } catch (e) {
  202. console.log(`parseX json错误:${e.message}`);
  203. return this.startsWith("[") ? [] : {};
  204. }
  205. },
  206. configurable: true,
  207. enumerable: false
  208. }
  209. });
  210. //正则裁切
  211. function cut(text, start, end, method, All) {
  212. let result = "";
  213. let c = (t, s, e) => {
  214. let result = "";
  215. let rs = [];
  216. let results = [];
  217. try {
  218. let lr = new RegExp(String.raw`${s}`.toString());
  219. let rr = new RegExp(String.raw`${e}`.toString());
  220. const segments = t.split(lr);
  221. if (segments.length < 2) return '';
  222. let cutSegments = segments.slice(1).map(segment => {
  223. let splitSegment = segment.split(rr);
  224. //log(splitSegment)
  225. return splitSegment.length < 2 ? undefined : splitSegment[0] + e;
  226. }).filter(f => f);
  227. //log(cutSegments.at(-1))
  228. if (All) {
  229. return `[${cutSegments.join(',')}]`;
  230. } else {
  231. return cutSegments[0];
  232. }
  233. } catch (e) {
  234. console.log("Error cutting text:", e);
  235. }
  236. return result;
  237. }
  238. result = c(text, start, end);
  239. if (method && typeof method === "function") {
  240. result = method(result);
  241. }
  242. //console.log(result);
  243. return result
  244. }
  245. globalThis.matchesAll = matchesAll;
  246. globalThis.cut = cut;