wikiparser-node 1.6.2 → 1.7.0-b

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 (169) hide show
  1. package/README.en.md +1 -1
  2. package/README.md +1 -1
  3. package/bundle/bundle.min.js +38 -0
  4. package/config/default.json +5 -0
  5. package/config/enwiki.json +4 -814
  6. package/config/llwiki.json +5 -35
  7. package/config/minimum.json +3 -0
  8. package/config/moegirl.json +5 -44
  9. package/config/zhwiki.json +6 -466
  10. package/extensions/dist/base.js +64 -0
  11. package/extensions/dist/editor.js +159 -0
  12. package/extensions/dist/highlight.js +58 -0
  13. package/extensions/dist/lint.js +72 -0
  14. package/extensions/editor.css +64 -0
  15. package/extensions/ui.css +144 -0
  16. package/i18n/zh-hans.json +2 -1
  17. package/i18n/zh-hant.json +2 -1
  18. package/package.json +37 -21
  19. package/config/.schema.json +0 -172
  20. package/dist/addon/table.d.ts +0 -1
  21. package/dist/addon/table.js +0 -485
  22. package/dist/base.d.ts +0 -69
  23. package/dist/base.js +0 -38
  24. package/dist/index.d.ts +0 -31
  25. package/dist/index.js +0 -212
  26. package/dist/internal.d.ts +0 -44
  27. package/dist/lib/element.d.ts +0 -151
  28. package/dist/lib/element.js +0 -618
  29. package/dist/lib/node.d.ts +0 -146
  30. package/dist/lib/node.js +0 -426
  31. package/dist/lib/range.d.ts +0 -104
  32. package/dist/lib/range.js +0 -385
  33. package/dist/lib/ranges.d.ts +0 -28
  34. package/dist/lib/ranges.js +0 -126
  35. package/dist/lib/text.d.ts +0 -63
  36. package/dist/lib/text.js +0 -354
  37. package/dist/lib/title.d.ts +0 -40
  38. package/dist/lib/title.js +0 -179
  39. package/dist/mixin/attributesParent.d.ts +0 -50
  40. package/dist/mixin/attributesParent.js +0 -81
  41. package/dist/mixin/fixed.d.ts +0 -6
  42. package/dist/mixin/fixed.js +0 -30
  43. package/dist/mixin/flagsParent.d.ts +0 -72
  44. package/dist/mixin/flagsParent.js +0 -59
  45. package/dist/mixin/hidden.d.ts +0 -5
  46. package/dist/mixin/hidden.js +0 -24
  47. package/dist/mixin/magicLinkParent.d.ts +0 -40
  48. package/dist/mixin/magicLinkParent.js +0 -42
  49. package/dist/mixin/singleLine.d.ts +0 -6
  50. package/dist/mixin/singleLine.js +0 -26
  51. package/dist/mixin/sol.d.ts +0 -6
  52. package/dist/mixin/sol.js +0 -43
  53. package/dist/mixin/syntax.d.ts +0 -9
  54. package/dist/mixin/syntax.js +0 -50
  55. package/dist/parser/braces.js +0 -130
  56. package/dist/parser/commentAndExt.js +0 -89
  57. package/dist/parser/converter.js +0 -40
  58. package/dist/parser/externalLinks.js +0 -45
  59. package/dist/parser/hrAndDoubleUnderscore.js +0 -40
  60. package/dist/parser/html.js +0 -38
  61. package/dist/parser/links.js +0 -97
  62. package/dist/parser/list.js +0 -97
  63. package/dist/parser/magicLinks.js +0 -42
  64. package/dist/parser/quotes.js +0 -67
  65. package/dist/parser/selector.js +0 -190
  66. package/dist/parser/table.js +0 -123
  67. package/dist/src/arg.d.ts +0 -58
  68. package/dist/src/arg.js +0 -214
  69. package/dist/src/atom.d.ts +0 -12
  70. package/dist/src/atom.js +0 -27
  71. package/dist/src/attribute.d.ts +0 -71
  72. package/dist/src/attribute.js +0 -502
  73. package/dist/src/attributes.d.ts +0 -109
  74. package/dist/src/attributes.js +0 -356
  75. package/dist/src/converter.d.ts +0 -33
  76. package/dist/src/converter.js +0 -134
  77. package/dist/src/converterFlags.d.ts +0 -87
  78. package/dist/src/converterFlags.js +0 -242
  79. package/dist/src/converterRule.d.ts +0 -77
  80. package/dist/src/converterRule.js +0 -209
  81. package/dist/src/extLink.d.ts +0 -43
  82. package/dist/src/extLink.js +0 -191
  83. package/dist/src/gallery.d.ts +0 -54
  84. package/dist/src/gallery.js +0 -139
  85. package/dist/src/heading.d.ts +0 -47
  86. package/dist/src/heading.js +0 -190
  87. package/dist/src/hidden.d.ts +0 -7
  88. package/dist/src/hidden.js +0 -23
  89. package/dist/src/html.d.ts +0 -65
  90. package/dist/src/html.js +0 -358
  91. package/dist/src/imageParameter.d.ts +0 -65
  92. package/dist/src/imageParameter.js +0 -257
  93. package/dist/src/imagemap.d.ts +0 -54
  94. package/dist/src/imagemap.js +0 -144
  95. package/dist/src/imagemapLink.d.ts +0 -31
  96. package/dist/src/imagemapLink.js +0 -93
  97. package/dist/src/index.d.ts +0 -143
  98. package/dist/src/index.js +0 -851
  99. package/dist/src/link/base.d.ts +0 -52
  100. package/dist/src/link/base.js +0 -228
  101. package/dist/src/link/category.d.ts +0 -13
  102. package/dist/src/link/category.js +0 -28
  103. package/dist/src/link/file.d.ts +0 -98
  104. package/dist/src/link/file.js +0 -272
  105. package/dist/src/link/galleryImage.d.ts +0 -32
  106. package/dist/src/link/galleryImage.js +0 -158
  107. package/dist/src/link/index.d.ts +0 -56
  108. package/dist/src/link/index.js +0 -133
  109. package/dist/src/magicLink.d.ts +0 -59
  110. package/dist/src/magicLink.js +0 -223
  111. package/dist/src/nested.d.ts +0 -43
  112. package/dist/src/nested.js +0 -105
  113. package/dist/src/nowiki/base.d.ts +0 -31
  114. package/dist/src/nowiki/base.js +0 -93
  115. package/dist/src/nowiki/comment.d.ts +0 -25
  116. package/dist/src/nowiki/comment.js +0 -76
  117. package/dist/src/nowiki/dd.d.ts +0 -8
  118. package/dist/src/nowiki/dd.js +0 -24
  119. package/dist/src/nowiki/doubleUnderscore.d.ts +0 -18
  120. package/dist/src/nowiki/doubleUnderscore.js +0 -101
  121. package/dist/src/nowiki/hr.d.ts +0 -5
  122. package/dist/src/nowiki/hr.js +0 -63
  123. package/dist/src/nowiki/index.d.ts +0 -16
  124. package/dist/src/nowiki/index.js +0 -27
  125. package/dist/src/nowiki/list.d.ts +0 -16
  126. package/dist/src/nowiki/list.js +0 -109
  127. package/dist/src/nowiki/listBase.d.ts +0 -5
  128. package/dist/src/nowiki/listBase.js +0 -61
  129. package/dist/src/nowiki/noinclude.d.ts +0 -10
  130. package/dist/src/nowiki/noinclude.js +0 -23
  131. package/dist/src/nowiki/quote.d.ts +0 -14
  132. package/dist/src/nowiki/quote.js +0 -131
  133. package/dist/src/onlyinclude.d.ts +0 -16
  134. package/dist/src/onlyinclude.js +0 -57
  135. package/dist/src/paramTag/index.d.ts +0 -37
  136. package/dist/src/paramTag/index.js +0 -77
  137. package/dist/src/paramTag/inputbox.d.ts +0 -8
  138. package/dist/src/paramTag/inputbox.js +0 -22
  139. package/dist/src/parameter.d.ts +0 -70
  140. package/dist/src/parameter.js +0 -264
  141. package/dist/src/pre.d.ts +0 -31
  142. package/dist/src/pre.js +0 -59
  143. package/dist/src/syntax.d.ts +0 -20
  144. package/dist/src/syntax.js +0 -86
  145. package/dist/src/table/base.d.ts +0 -27
  146. package/dist/src/table/base.js +0 -81
  147. package/dist/src/table/index.d.ts +0 -242
  148. package/dist/src/table/index.js +0 -497
  149. package/dist/src/table/td.d.ts +0 -86
  150. package/dist/src/table/td.js +0 -355
  151. package/dist/src/table/tr.d.ts +0 -32
  152. package/dist/src/table/tr.js +0 -58
  153. package/dist/src/table/trBase.d.ts +0 -53
  154. package/dist/src/table/trBase.js +0 -156
  155. package/dist/src/tagPair/ext.d.ts +0 -32
  156. package/dist/src/tagPair/ext.js +0 -203
  157. package/dist/src/tagPair/include.d.ts +0 -35
  158. package/dist/src/tagPair/include.js +0 -74
  159. package/dist/src/tagPair/index.d.ts +0 -27
  160. package/dist/src/tagPair/index.js +0 -128
  161. package/dist/src/transclude.d.ts +0 -167
  162. package/dist/src/transclude.js +0 -717
  163. package/dist/util/constants.js +0 -27
  164. package/dist/util/debug.js +0 -81
  165. package/dist/util/diff.js +0 -83
  166. package/dist/util/lint.js +0 -31
  167. package/dist/util/string.js +0 -60
  168. package/errors/README +0 -1
  169. package/printed/README +0 -1
package/dist/lib/range.js DELETED
@@ -1,385 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.AstRange = void 0;
4
- const constants_1 = require("../util/constants");
5
- /**
6
- * 计算绝对位置
7
- * @param referenceNode 容器
8
- * @param offset 相对位置
9
- */
10
- const getIndex = (referenceNode, offset) => referenceNode.getAbsoluteIndex() + referenceNode.getRelativeIndex(offset);
11
- /** 模拟Range对象 */
12
- class AstRange {
13
- #startContainer;
14
- #startOffset;
15
- #endContainer;
16
- #endOffset;
17
- /**
18
- * 未初始化时抛出错误
19
- * @param start 是否未初始化起点
20
- * @throws `Error` 未初始化
21
- */
22
- #notInit(start) {
23
- throw new Error(`请先指定${start ? '起始' : '终止'}位置!`);
24
- }
25
- /** 起点容器 */
26
- get startContainer() {
27
- return this.#startContainer ?? this.#notInit(true);
28
- }
29
- /** 起点位置 */
30
- get startOffset() {
31
- return this.#startOffset ?? this.#notInit(true);
32
- }
33
- /** 起点绝对位置 */
34
- get startIndex() {
35
- return getIndex(this.startContainer, this.startOffset);
36
- }
37
- /** 起点行列位置 */
38
- get startPos() {
39
- return this.startContainer.getRootNode().posFromIndex(this.startIndex);
40
- }
41
- /** 终点容器 */
42
- get endContainer() {
43
- return this.#endContainer ?? this.#notInit(false);
44
- }
45
- /** 终点位置 */
46
- get endOffset() {
47
- return this.#endOffset ?? this.#notInit(false);
48
- }
49
- /** 终点绝对位置 */
50
- get endIndex() {
51
- return getIndex(this.endContainer, this.endOffset);
52
- }
53
- /** 终点行列位置 */
54
- get endPos() {
55
- return this.endContainer.getRootNode().posFromIndex(this.endIndex);
56
- }
57
- /** 起始和终止位置是否重合 */
58
- get collapsed() {
59
- return this.startContainer === this.endContainer && this.startOffset === this.endOffset;
60
- }
61
- /** 最近的公共祖先 */
62
- get commonAncestorContainer() {
63
- const { startContainer, endContainer } = this;
64
- return startContainer.contains(endContainer) ? startContainer : startContainer.parentNode;
65
- }
66
- /**
67
- * 检查起点和终点的设置是否有效
68
- * @throws `RangeError` 起点和终点不是兄弟节点
69
- * @throws `RangeError` 起点位于终点之后
70
- */
71
- #check() {
72
- const { startContainer, startOffset, endContainer, endOffset } = this;
73
- if (startContainer === endContainer) {
74
- if (startOffset > endOffset) {
75
- throw new RangeError('起点不能位于终点之后!');
76
- }
77
- return;
78
- }
79
- const { type: startType, parentNode: startParent } = startContainer, { type: endType, parentNode: endParent } = endContainer;
80
- if (startType !== 'text') {
81
- if (endType !== 'text' || startContainer !== endParent) {
82
- throw new RangeError('起点和终点不是兄弟节点!');
83
- }
84
- else if (startOffset > endParent.childNodes.indexOf(endContainer)) {
85
- throw new RangeError('起点不能位于终点之后!');
86
- }
87
- }
88
- else if (endType === 'text') {
89
- if (!startParent || startParent !== endParent) {
90
- throw new RangeError('起点和终点不是兄弟节点!');
91
- }
92
- const { childNodes } = startParent;
93
- if (childNodes.indexOf(startContainer) > childNodes.indexOf(endContainer)) {
94
- throw new RangeError('起点不能位于终点之后!');
95
- }
96
- }
97
- else if (startParent !== endContainer) {
98
- throw new RangeError('起点和终点不是兄弟节点!');
99
- }
100
- else if (endOffset <= startParent.childNodes.indexOf(startContainer)) {
101
- throw new RangeError('起点不能位于终点之后!');
102
- }
103
- }
104
- /**
105
- * 设置起点
106
- * @param startNode 起点容器
107
- * @param startOffset 起点位置
108
- * @throws `RangeError` offset取值超出范围
109
- */
110
- setStart(startNode, startOffset) {
111
- const { length } = startNode;
112
- if (startOffset < 0 || startOffset > length) {
113
- throw new RangeError(`offset取值范围应为 0 ~ ${length}`);
114
- }
115
- this.#startContainer = startNode;
116
- this.#startOffset = startOffset;
117
- if (this.#endContainer) {
118
- try {
119
- this.#check();
120
- }
121
- catch (e) {
122
- this.#startContainer = undefined;
123
- this.#startOffset = undefined;
124
- throw e;
125
- }
126
- }
127
- }
128
- /**
129
- * 设置终点
130
- * @param endNode 终点容器
131
- * @param endOffset 终点位置
132
- * @throws `RangeError` offset取值超出范围
133
- */
134
- setEnd(endNode, endOffset) {
135
- const { length } = endNode;
136
- if (endOffset < 0 || endOffset > length) {
137
- throw new RangeError(`offset取值范围应为 0 ~ ${length}`);
138
- }
139
- this.#endContainer = endNode;
140
- this.#endOffset = endOffset;
141
- if (this.#startContainer) {
142
- try {
143
- this.#check();
144
- }
145
- catch (e) {
146
- this.#endContainer = undefined;
147
- this.#endOffset = undefined;
148
- throw e;
149
- }
150
- }
151
- }
152
- /**
153
- * 在节点后设置
154
- * @param method 方法名
155
- * @param referenceNode 节点
156
- * @throws `RangeError` 参考节点没有父节点
157
- */
158
- #setAfter(method, referenceNode) {
159
- const { parentNode } = referenceNode;
160
- if (!parentNode) {
161
- throw new RangeError('参考节点没有父节点!');
162
- }
163
- this[method](parentNode, parentNode.childNodes.indexOf(referenceNode) + 1);
164
- }
165
- /**
166
- * 在节点后设置起点
167
- * @param referenceNode 节点
168
- */
169
- setStartAfter(referenceNode) {
170
- this.#setAfter('setStart', referenceNode);
171
- }
172
- /**
173
- * 在节点后设置终点
174
- * @param referenceNode 节点
175
- */
176
- setEndAfter(referenceNode) {
177
- this.#setAfter('setEnd', referenceNode);
178
- }
179
- /**
180
- * 在节点前设置
181
- * @param method 方法名
182
- * @param referenceNode 节点
183
- * @throws `RangeError` 参考节点没有父节点
184
- */
185
- #setBefore(method, referenceNode) {
186
- const { parentNode } = referenceNode;
187
- if (!parentNode) {
188
- throw new RangeError('参考节点没有父节点!');
189
- }
190
- this[method](parentNode, parentNode.childNodes.indexOf(referenceNode));
191
- }
192
- /**
193
- * 在节点前设置起点
194
- * @param referenceNode 节点
195
- */
196
- setStartBefore(referenceNode) {
197
- this.#setBefore('setStart', referenceNode);
198
- }
199
- /**
200
- * 在节点前设置终点
201
- * @param referenceNode 节点
202
- */
203
- setEndBefore(referenceNode) {
204
- this.#setBefore('setEnd', referenceNode);
205
- }
206
- /**
207
- * 设置Range包含整个节点的内容
208
- * @param referenceNode 节点
209
- */
210
- selectNodeContents(referenceNode) {
211
- this.#startContainer = referenceNode;
212
- this.#startOffset = 0;
213
- this.#endContainer = referenceNode;
214
- this.#endOffset = referenceNode.length;
215
- }
216
- /**
217
- * 设置Range包含整个节点
218
- * @param referenceNode 节点
219
- * @throws `RangeError` 参考节点没有父节点
220
- */
221
- selectNode(referenceNode) {
222
- const { parentNode } = referenceNode;
223
- if (!parentNode) {
224
- throw new RangeError('参考节点没有父节点!');
225
- }
226
- const i = parentNode.childNodes.indexOf(referenceNode);
227
- this.#startContainer = parentNode;
228
- this.#startOffset = i;
229
- this.#endContainer = parentNode;
230
- this.#endOffset = i + 1;
231
- }
232
- /**
233
- * 使起始和终止位置重合
234
- * @param toStart 重合至起始位置
235
- */
236
- collapse(toStart) {
237
- if (toStart) {
238
- this.#endContainer = this.startContainer;
239
- this.#endOffset = this.startOffset;
240
- }
241
- else {
242
- this.#startContainer = this.endContainer;
243
- this.#startOffset = this.endOffset;
244
- }
245
- }
246
- /**
247
- * 比较端点和Range的位置
248
- * @param referenceNode 端点容器
249
- * @param offset 端点位置
250
- * @throws `RangeError` 不在同一个文档
251
- */
252
- comparePoint(referenceNode, offset) {
253
- const { startContainer, startIndex, endContainer, endIndex } = this;
254
- if (startContainer.getRootNode() !== referenceNode.getRootNode()) {
255
- throw new RangeError('待比较的端点不在同一个文档中!');
256
- }
257
- const index = getIndex(referenceNode, offset);
258
- if (index < startIndex || index === startIndex && !startContainer.contains(referenceNode)) {
259
- return -1;
260
- }
261
- return index < endIndex || index === endIndex && endContainer.contains(referenceNode) ? 0 : 1;
262
- }
263
- /**
264
- * 端点是否在Range中
265
- * @param referenceNode 端点容器
266
- * @param offset 端点位置
267
- */
268
- isPointInRange(referenceNode, offset) {
269
- return this.comparePoint(referenceNode, offset) === 0;
270
- }
271
- /** 复制AstRange对象 */
272
- cloneRange() {
273
- const range = new AstRange();
274
- range.setStart(this.startContainer, this.startOffset);
275
- range.setEnd(this.endContainer, this.endOffset);
276
- return range;
277
- }
278
- /** 删除Range中的内容 */
279
- deleteContents() {
280
- const { startContainer, endContainer, commonAncestorContainer } = this, { childNodes } = commonAncestorContainer;
281
- let { startOffset, endOffset } = this;
282
- if (commonAncestorContainer.type === 'text') {
283
- commonAncestorContainer.deleteData(startOffset, endOffset);
284
- return;
285
- }
286
- else if (startContainer.type === 'text') {
287
- startContainer.deleteData(startOffset, Infinity);
288
- startOffset = childNodes.indexOf(startContainer) + 1;
289
- }
290
- if (endContainer.type === 'text') {
291
- endContainer.deleteData(0, endOffset);
292
- endOffset = childNodes.indexOf(endContainer);
293
- }
294
- for (let i = endOffset - 1; i >= startOffset; i--) {
295
- commonAncestorContainer.removeAt(i);
296
- }
297
- this.#startContainer = commonAncestorContainer;
298
- this.#startOffset = startOffset;
299
- this.#endContainer = commonAncestorContainer;
300
- this.#endOffset = startOffset;
301
- }
302
- /** 获取行列位置和大小 */
303
- getBoundingClientRect() {
304
- const { startPos: { top, left }, endPos: { top: bottom, left: right } } = this;
305
- return { top, left, height: bottom - top + 1, width: bottom === top ? right - left : right };
306
- }
307
- /**
308
- * 在起始位置插入节点
309
- * @param newNode 插入的节点
310
- */
311
- insertNode(newNode) {
312
- const { startContainer, startOffset } = this, endContainer = this.#endContainer;
313
- if (startContainer.type === 'text') {
314
- if (startOffset) {
315
- startContainer.splitText(startOffset);
316
- this.#startContainer = startContainer.nextSibling;
317
- this.#startOffset = 0;
318
- startContainer.after(newNode);
319
- if (endContainer === startContainer) {
320
- this.#endContainer = this.#startContainer;
321
- this.#endOffset -= startOffset;
322
- }
323
- else if (endContainer && endContainer.type !== 'text') {
324
- this.#endOffset += 2;
325
- }
326
- }
327
- else {
328
- startContainer.before(newNode);
329
- if (endContainer && endContainer.type !== 'text') {
330
- this.#endOffset++;
331
- }
332
- }
333
- }
334
- else {
335
- startContainer.insertAt(newNode, startOffset);
336
- this.#startOffset++;
337
- if (endContainer === startContainer) {
338
- this.#endOffset++;
339
- }
340
- }
341
- }
342
- /** @private */
343
- toString() {
344
- const { startContainer, startIndex, endIndex } = this;
345
- return String(startContainer.getRootNode()).slice(startIndex, endIndex);
346
- }
347
- /** 获取范围内的全部节点 */
348
- extractContents() {
349
- const { startContainer, startOffset, endContainer, endOffset, commonAncestorContainer } = this, { childNodes } = commonAncestorContainer;
350
- if (commonAncestorContainer.type === 'text') {
351
- if (startOffset === endOffset) {
352
- return [];
353
- }
354
- commonAncestorContainer.splitText(endOffset);
355
- const nextSibling = commonAncestorContainer.splitText(startOffset);
356
- this.#startContainer = nextSibling;
357
- this.#startOffset = 0;
358
- this.#endContainer = nextSibling;
359
- this.#endOffset -= startOffset;
360
- return [nextSibling];
361
- }
362
- else if (endContainer.type === 'text') {
363
- if (endOffset && endOffset < endContainer.length) {
364
- endContainer.splitText(endOffset);
365
- }
366
- this.#endContainer = commonAncestorContainer;
367
- this.#endOffset = childNodes.indexOf(endContainer) + (endOffset && 1);
368
- }
369
- if (startContainer.type === 'text') {
370
- if (startOffset && startOffset < startContainer.length) {
371
- startContainer.splitText(startOffset);
372
- this.#endOffset++;
373
- }
374
- this.#startContainer = commonAncestorContainer;
375
- this.#startOffset = childNodes.indexOf(startContainer) + (startOffset && 1);
376
- }
377
- return commonAncestorContainer.childNodes.slice(this.#startOffset, this.#endOffset);
378
- }
379
- /** 在满足条件时拷贝范围内的全部节点 */
380
- cloneContents() {
381
- return this.extractContents().map(node => node.cloneNode());
382
- }
383
- }
384
- exports.AstRange = AstRange;
385
- constants_1.classes['AstRange'] = __filename;
@@ -1,28 +0,0 @@
1
- /** 模拟Python的Range对象。除`step`至少为`1`外,允许负数、小数或`end < start`的情形。 */
2
- export declare class Range {
3
- readonly start: number;
4
- readonly end: number;
5
- readonly step: number;
6
- /**
7
- * @param s 表达式
8
- * @throws `RangeError` 起点、终点和步长均应为整数
9
- * @throws `RangeError` n的系数不能为0
10
- * @throws `RangeError` 应使用CSS选择器或Python切片的格式
11
- */
12
- constructor(str: string);
13
- /**
14
- * 将Range转换为针对特定数组的下标集
15
- * @param arr 参考数组`[0, 1, 2, ...]`
16
- */
17
- applyTo(arr: readonly number[]): number[];
18
- }
19
- /** @extends {Array<number|Range>} */
20
- export declare class Ranges extends Array<number | Range> {
21
- /** @param a 表达式数组 */
22
- constructor(a?: number | string | Range | readonly (number | string | Range)[]);
23
- /**
24
- * 将Ranges转换为针对特定Array的下标集
25
- * @param arr 参考数组
26
- */
27
- applyTo(arr: number | readonly unknown[]): number[];
28
- }
@@ -1,126 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Ranges = exports.Range = void 0;
4
- const constants_1 = require("../util/constants");
5
- const debug_1 = require("../util/debug");
6
- const diff_1 = require("../util/diff");
7
- /** 模拟Python的Range对象。除`step`至少为`1`外,允许负数、小数或`end < start`的情形。 */
8
- class Range {
9
- start;
10
- end;
11
- step;
12
- /**
13
- * @param s 表达式
14
- * @throws `RangeError` 起点、终点和步长均应为整数
15
- * @throws `RangeError` n的系数不能为0
16
- * @throws `RangeError` 应使用CSS选择器或Python切片的格式
17
- */
18
- constructor(str) {
19
- str = str.trim();
20
- if (str === 'odd') {
21
- Object.assign(this, { start: 1, end: Infinity, step: 2 });
22
- }
23
- else if (str === 'even') {
24
- Object.assign(this, { start: 0, end: Infinity, step: 2 });
25
- }
26
- else if (str.includes(':')) {
27
- const [start, end, step = '1'] = str.split(':', 3);
28
- this.start = Number(start);
29
- this.end = Number(end?.trim() || Infinity);
30
- this.step = Math.max(Number(step), 1);
31
- if (!Number.isInteger(this.start)) {
32
- throw new RangeError(`The start of a range, \`${start}\`, should be an integer!`);
33
- }
34
- else if (this.end !== Infinity && !Number.isInteger(this.end)) {
35
- throw new RangeError(`The end of a range, \`${end}\`, should be an integer!`);
36
- }
37
- else if (!Number.isInteger(this.step)) {
38
- throw new RangeError(`The step of a range, \`${step}\`, should be an integer!`);
39
- }
40
- }
41
- else {
42
- const mt = /^([+-])?(\d+)?n(?:\s*([+-])\s*(\d+))?$/u
43
- .exec(str);
44
- if (mt) {
45
- const [, sgnA = '+', a = 1, sgnB = '+'] = mt, b = Number(mt[4] ?? 0);
46
- this.step = Number(a);
47
- if (this.step === 0) {
48
- throw new RangeError(`In the argument \`${str}\`, the coefficient of "n" must not be 0!`);
49
- }
50
- else if (sgnA === '+') { // `an+b` or `an-b`
51
- this.start = sgnB === '+' || b === 0 ? b : this.step - 1 - (b - 1) % this.step;
52
- this.end = Infinity;
53
- }
54
- else if (sgnB === '-') { // `-an-b`
55
- this.start = 0;
56
- this.end = b > 0 ? 0 : this.step;
57
- }
58
- else { // `-an+b`
59
- this.start = b % this.step;
60
- this.end = this.step + b;
61
- }
62
- }
63
- else {
64
- throw new RangeError(`The argument \`${str}\` should be either in the form of "an+b" as in CSS selectors or Python slices!`);
65
- }
66
- }
67
- }
68
- /**
69
- * 将Range转换为针对特定数组的下标集
70
- * @param arr 参考数组`[0, 1, 2, ...]`
71
- */
72
- applyTo(arr) {
73
- return arr.slice(this.start, this.end).filter((_, j) => j % this.step === 0);
74
- }
75
- }
76
- exports.Range = Range;
77
- /** @extends {Array<number|Range>} */
78
- class Ranges extends Array {
79
- /** @param a 表达式数组 */
80
- constructor(a) {
81
- super();
82
- if (a === undefined) {
83
- return;
84
- }
85
- for (const ele of (Array.isArray(a) ? a : [a])) {
86
- if (ele instanceof Range) {
87
- this.push(ele);
88
- continue;
89
- }
90
- else if (typeof ele === 'string' && !ele.trim()) {
91
- continue;
92
- }
93
- const number = Number(ele);
94
- if (Number.isInteger(number)) {
95
- this.push(number);
96
- }
97
- else if (typeof ele === 'string' && Number.isNaN(number)) {
98
- try {
99
- this.push(new Range(ele));
100
- }
101
- catch (e) {
102
- if (e instanceof RangeError) {
103
- (0, diff_1.error)(e.message);
104
- }
105
- }
106
- }
107
- }
108
- }
109
- /**
110
- * 将Ranges转换为针对特定Array的下标集
111
- * @param arr 参考数组
112
- */
113
- applyTo(arr) {
114
- const length = typeof arr === 'number' ? arr : arr.length, a = (0, debug_1.emptyArray)(length, i => i);
115
- return [
116
- ...new Set([...this].flatMap(ele => {
117
- if (typeof ele === 'number') {
118
- return ele < 0 ? ele + length : ele;
119
- }
120
- return ele.applyTo(a);
121
- })),
122
- ].filter(i => i >= 0 && i < length).sort();
123
- }
124
- }
125
- exports.Ranges = Ranges;
126
- constants_1.classes['Ranges'] = __filename;
@@ -1,63 +0,0 @@
1
- import { AstNode } from './node';
2
- import type { LintError } from '../base';
3
- /** 文本节点 */
4
- export declare class AstText extends AstNode {
5
- #private;
6
- readonly type = "text";
7
- readonly name: undefined;
8
- readonly data: string;
9
- /** 文本长度 */
10
- get length(): number;
11
- /** @param text 包含文本 */
12
- constructor(text: string);
13
- /** 可见部分 */
14
- text(): string;
15
- /**
16
- * @override
17
- * @param start
18
- * @param errorRegex 正则表达式
19
- * @throws `Error` 孤立文本节点
20
- */
21
- lint(start?: number, errorRegex?: RegExp): LintError[];
22
- /**
23
- * 替换字符串
24
- * @param text 替换的字符串
25
- */
26
- replaceData(text: string): void;
27
- /** @override */
28
- print(): string;
29
- /** 复制 */
30
- cloneNode(): AstText;
31
- /**
32
- * 在后方添加字符串
33
- * @param text 添加的字符串
34
- */
35
- appendData(text: string): void;
36
- /**
37
- * 删减字符串
38
- * @param offset 起始位置
39
- * @param count 删减字符数
40
- */
41
- deleteData(offset: number, count: number): void;
42
- /**
43
- * 插入字符串
44
- * @param offset 插入位置
45
- * @param text 待插入的字符串
46
- */
47
- insertData(offset: number, text: string): void;
48
- /**
49
- * 提取子串
50
- * @param offset 起始位置
51
- * @param count 字符数
52
- */
53
- substringData(offset: number, count: number): string;
54
- /**
55
- * 将文本子节点分裂为两部分
56
- * @param offset 分裂位置
57
- * @throws `RangeError` 错误的断开位置
58
- * @throws `Error` 没有父节点
59
- */
60
- splitText(offset: number): AstText;
61
- /** 转义 `=` */
62
- escape(): void;
63
- }