projen 0.65.49 → 0.65.50

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 (153) hide show
  1. package/.jsii +27 -10
  2. package/docs/api/API.md +8 -0
  3. package/lib/awscdk/auto-discover.js +5 -5
  4. package/lib/awscdk/awscdk-app-java.js +1 -1
  5. package/lib/awscdk/awscdk-app-py.js +1 -1
  6. package/lib/awscdk/awscdk-app-ts.js +1 -1
  7. package/lib/awscdk/awscdk-construct.js +2 -2
  8. package/lib/awscdk/awscdk-deps-java.js +1 -1
  9. package/lib/awscdk/awscdk-deps-js.js +1 -1
  10. package/lib/awscdk/awscdk-deps-py.js +1 -1
  11. package/lib/awscdk/awscdk-deps.js +1 -1
  12. package/lib/awscdk/cdk-config.js +1 -1
  13. package/lib/awscdk/cdk-tasks.js +1 -1
  14. package/lib/awscdk/integration-test.js +1 -1
  15. package/lib/awscdk/lambda-extension.js +1 -1
  16. package/lib/awscdk/lambda-function.js +2 -2
  17. package/lib/build/build-workflow.js +1 -1
  18. package/lib/cdk/auto-discover-base.js +2 -2
  19. package/lib/cdk/construct-lib.js +1 -1
  20. package/lib/cdk/integration-test-base.js +1 -1
  21. package/lib/cdk/jsii-docgen.js +1 -1
  22. package/lib/cdk/jsii-project.js +1 -1
  23. package/lib/cdk8s/auto-discover.js +2 -2
  24. package/lib/cdk8s/cdk8s-app-py.js +1 -1
  25. package/lib/cdk8s/cdk8s-app-ts.js +1 -1
  26. package/lib/cdk8s/cdk8s-construct.js +1 -1
  27. package/lib/cdk8s/cdk8s-deps-py.js +1 -1
  28. package/lib/cdk8s/cdk8s-deps.js +1 -1
  29. package/lib/cdk8s/integration-test.js +1 -1
  30. package/lib/cdktf/cdktf-construct.js +1 -1
  31. package/lib/circleci/circleci.js +1 -1
  32. package/lib/component.js +1 -1
  33. package/lib/dependencies.js +1 -1
  34. package/lib/dev-env.js +1 -1
  35. package/lib/docker-compose.js +2 -2
  36. package/lib/file.js +1 -1
  37. package/lib/gitattributes.js +1 -1
  38. package/lib/github/auto-approve.js +1 -1
  39. package/lib/github/auto-merge.js +1 -1
  40. package/lib/github/dependabot.js +1 -1
  41. package/lib/github/github-credentials.js +1 -1
  42. package/lib/github/github-project.js +1 -1
  43. package/lib/github/github.js +1 -1
  44. package/lib/github/mergify.js +1 -1
  45. package/lib/github/pr-template.js +1 -1
  46. package/lib/github/pull-request-lint.js +1 -1
  47. package/lib/github/stale.js +1 -1
  48. package/lib/github/task-workflow.js +1 -1
  49. package/lib/github/workflows.js +1 -1
  50. package/lib/gitlab/configuration.js +1 -1
  51. package/lib/gitlab/gitlab-configuration.js +1 -1
  52. package/lib/gitlab/nested-configuration.js +1 -1
  53. package/lib/gitpod.js +1 -1
  54. package/lib/ignore-file.js +1 -1
  55. package/lib/ini.js +1 -1
  56. package/lib/java/java-project.js +1 -1
  57. package/lib/java/junit.js +1 -1
  58. package/lib/java/maven-compile.js +1 -1
  59. package/lib/java/maven-packaging.js +1 -1
  60. package/lib/java/maven-sample.js +1 -1
  61. package/lib/java/pom.js +1 -1
  62. package/lib/java/projenrc.js +1 -1
  63. package/lib/javascript/bundler.js +1 -1
  64. package/lib/javascript/eslint.js +6 -4
  65. package/lib/javascript/jest.js +1 -1
  66. package/lib/javascript/node-package.js +1 -1
  67. package/lib/javascript/node-project.js +1 -1
  68. package/lib/javascript/npm-config.js +1 -1
  69. package/lib/javascript/prettier.js +1 -1
  70. package/lib/javascript/projenrc.js +1 -1
  71. package/lib/javascript/typescript-config.js +1 -1
  72. package/lib/javascript/upgrade-dependencies.js +2 -2
  73. package/lib/json-patch.js +1 -1
  74. package/lib/json.d.ts +1 -1
  75. package/lib/json.js +3 -3
  76. package/lib/license.js +1 -1
  77. package/lib/logger.js +1 -1
  78. package/lib/makefile.js +1 -1
  79. package/lib/object-file.js +1 -1
  80. package/lib/project-build.js +1 -1
  81. package/lib/project.js +1 -1
  82. package/lib/projects.js +1 -1
  83. package/lib/projenrc-json.js +1 -1
  84. package/lib/python/pip.js +1 -1
  85. package/lib/python/poetry.js +2 -2
  86. package/lib/python/projenrc.js +1 -1
  87. package/lib/python/pytest-sample.js +1 -1
  88. package/lib/python/pytest.js +1 -1
  89. package/lib/python/python-project.js +1 -1
  90. package/lib/python/python-sample.js +1 -1
  91. package/lib/python/requirements-file.js +1 -1
  92. package/lib/python/setuppy.js +1 -1
  93. package/lib/python/setuptools.js +1 -1
  94. package/lib/python/venv.js +1 -1
  95. package/lib/readme.js +1 -1
  96. package/lib/release/publisher.js +1 -1
  97. package/lib/release/release-trigger.js +1 -1
  98. package/lib/release/release.js +1 -1
  99. package/lib/renovatebot.js +1 -1
  100. package/lib/run-task.js +1845 -0
  101. package/lib/sample-file.js +2 -2
  102. package/lib/semver.js +1 -1
  103. package/lib/source-code.js +1 -1
  104. package/lib/task-runtime.js +1 -1
  105. package/lib/task.js +1 -1
  106. package/lib/tasks.js +1 -1
  107. package/lib/testing.js +1 -1
  108. package/lib/textfile.js +1 -1
  109. package/lib/toml.js +1 -1
  110. package/lib/typescript/projenrc.js +1 -1
  111. package/lib/typescript/typescript-typedoc.js +1 -1
  112. package/lib/typescript/typescript.js +3 -3
  113. package/lib/util/synth.d.ts +6 -0
  114. package/lib/util/synth.js +33 -5
  115. package/lib/version.js +1 -1
  116. package/lib/vscode/devcontainer.js +1 -1
  117. package/lib/vscode/extensions.js +1 -1
  118. package/lib/vscode/launch-config.js +1 -1
  119. package/lib/vscode/settings.js +1 -1
  120. package/lib/vscode/vscode.js +1 -1
  121. package/lib/web/next.js +3 -3
  122. package/lib/web/postcss.js +1 -1
  123. package/lib/web/react.js +4 -4
  124. package/lib/web/tailwind.js +1 -1
  125. package/lib/xmlfile.js +1 -1
  126. package/lib/yaml.js +1 -1
  127. package/node_modules/array-timsort/LICENSE +21 -0
  128. package/node_modules/array-timsort/README.md +288 -0
  129. package/node_modules/array-timsort/package.json +55 -0
  130. package/node_modules/array-timsort/src/index.js +1080 -0
  131. package/node_modules/comment-json/LICENSE +21 -0
  132. package/node_modules/comment-json/README.md +580 -0
  133. package/node_modules/comment-json/index.d.ts +97 -0
  134. package/node_modules/comment-json/package.json +68 -0
  135. package/node_modules/comment-json/src/array.js +288 -0
  136. package/node_modules/comment-json/src/common.js +181 -0
  137. package/node_modules/comment-json/src/index.js +13 -0
  138. package/node_modules/comment-json/src/parse.js +451 -0
  139. package/node_modules/comment-json/src/stringify.js +366 -0
  140. package/node_modules/core-util-is/LICENSE +19 -0
  141. package/node_modules/core-util-is/README.md +3 -0
  142. package/node_modules/core-util-is/lib/util.js +107 -0
  143. package/node_modules/core-util-is/package.json +38 -0
  144. package/node_modules/has-own-prop/index.d.ts +17 -0
  145. package/node_modules/has-own-prop/index.js +4 -0
  146. package/node_modules/has-own-prop/license +9 -0
  147. package/node_modules/has-own-prop/package.json +33 -0
  148. package/node_modules/has-own-prop/readme.md +30 -0
  149. package/node_modules/repeat-string/LICENSE +21 -0
  150. package/node_modules/repeat-string/README.md +136 -0
  151. package/node_modules/repeat-string/index.js +70 -0
  152. package/node_modules/repeat-string/package.json +77 -0
  153. package/package.json +3 -1
@@ -0,0 +1,1080 @@
1
+ /**
2
+ * Default minimum size of a run.
3
+ */
4
+ const DEFAULT_MIN_MERGE = 32
5
+
6
+ /**
7
+ * Minimum ordered subsequece required to do galloping.
8
+ */
9
+ const DEFAULT_MIN_GALLOPING = 7
10
+
11
+ /**
12
+ * Default tmp storage length. Can increase depending on the size of the
13
+ * smallest run to merge.
14
+ */
15
+ const DEFAULT_TMP_STORAGE_LENGTH = 256
16
+
17
+ /**
18
+ * Pre-computed powers of 10 for efficient lexicographic comparison of
19
+ * small integers.
20
+ */
21
+ const POWERS_OF_TEN = [1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9]
22
+
23
+ let results
24
+
25
+ /**
26
+ * Estimate the logarithm base 10 of a small integer.
27
+ *
28
+ * @param {number} x - The integer to estimate the logarithm of.
29
+ * @return {number} - The estimated logarithm of the integer.
30
+ */
31
+ const log10 = x => x < 1e5
32
+ ? x < 1e2
33
+ ? x < 1e1
34
+ ? 0
35
+ : 1
36
+ : x < 1e4
37
+ ? x < 1e3
38
+ ? 2
39
+ : 3
40
+ : 4
41
+ : x < 1e7
42
+ ? x < 1e6
43
+ ? 5
44
+ : 6
45
+ : x < 1e9
46
+ ? x < 1e8
47
+ ? 7
48
+ : 8
49
+ : 9
50
+
51
+ /**
52
+ * Default alphabetical comparison of items.
53
+ *
54
+ * @param {string|object|number} a - First element to compare.
55
+ * @param {string|object|number} b - Second element to compare.
56
+ * @return {number} - A positive number if a.toString() > b.toString(), a
57
+ * negative number if .toString() < b.toString(), 0 otherwise.
58
+ */
59
+ function alphabeticalCompare (a, b) {
60
+ if (a === b) {
61
+ return 0
62
+ }
63
+
64
+ if (~ ~ a === a && ~ ~ b === b) {
65
+ if (a === 0 || b === 0) {
66
+ return a < b ? - 1 : 1
67
+ }
68
+
69
+ if (a < 0 || b < 0) {
70
+ if (b >= 0) {
71
+ return - 1
72
+ }
73
+
74
+ if (a >= 0) {
75
+ return 1
76
+ }
77
+
78
+ a = - a
79
+ b = - b
80
+ }
81
+
82
+ const al = log10(a)
83
+ const bl = log10(b)
84
+
85
+ let t = 0
86
+
87
+ if (al < bl) {
88
+ a *= POWERS_OF_TEN[bl - al - 1]
89
+ b /= 10
90
+ t = - 1
91
+ } else if (al > bl) {
92
+ b *= POWERS_OF_TEN[al - bl - 1]
93
+ a /= 10
94
+ t = 1
95
+ }
96
+
97
+ if (a === b) {
98
+ return t
99
+ }
100
+
101
+ return a < b ? - 1 : 1
102
+ }
103
+
104
+ const aStr = String(a)
105
+ const bStr = String(b)
106
+
107
+ if (aStr === bStr) {
108
+ return 0
109
+ }
110
+
111
+ return aStr < bStr ? - 1 : 1
112
+ }
113
+
114
+ /**
115
+ * Compute minimum run length for TimSort
116
+ *
117
+ * @param {number} n - The size of the array to sort.
118
+ */
119
+ function minRunLength (n) {
120
+ let r = 0
121
+
122
+ while (n >= DEFAULT_MIN_MERGE) {
123
+ r |= (n & 1)
124
+ n >>= 1
125
+ }
126
+
127
+ return n + r
128
+ }
129
+
130
+ /**
131
+ * Counts the length of a monotonically ascending or strictly monotonically
132
+ * descending sequence (run) starting at array[lo] in the range [lo, hi). If
133
+ * the run is descending it is made ascending.
134
+ *
135
+ * @param {array} array - The array to reverse.
136
+ * @param {number} lo - First element in the range (inclusive).
137
+ * @param {number} hi - Last element in the range.
138
+ * @param {function} compare - Item comparison function.
139
+ * @return {number} - The length of the run.
140
+ */
141
+ function makeAscendingRun (array, lo, hi, compare) {
142
+ let runHi = lo + 1
143
+
144
+ if (runHi === hi) {
145
+ return 1
146
+ }
147
+
148
+ // Descending
149
+ if (compare(array[runHi ++], array[lo]) < 0) {
150
+ while (runHi < hi && compare(array[runHi], array[runHi - 1]) < 0) {
151
+ runHi ++
152
+ }
153
+
154
+ reverseRun(array, lo, runHi)
155
+ reverseRun(results, lo, runHi)
156
+ // Ascending
157
+ } else {
158
+ while (runHi < hi && compare(array[runHi], array[runHi - 1]) >= 0) {
159
+ runHi ++
160
+ }
161
+ }
162
+
163
+ return runHi - lo
164
+ }
165
+
166
+ /**
167
+ * Reverse an array in the range [lo, hi).
168
+ *
169
+ * @param {array} array - The array to reverse.
170
+ * @param {number} lo - First element in the range (inclusive).
171
+ * @param {number} hi - Last element in the range.
172
+ */
173
+ function reverseRun (array, lo, hi) {
174
+ hi --
175
+
176
+ while (lo < hi) {
177
+ const t = array[lo]
178
+ array[lo ++] = array[hi]
179
+ array[hi --] = t
180
+ }
181
+ }
182
+
183
+ /**
184
+ * Perform the binary sort of the array in the range [lo, hi) where start is
185
+ * the first element possibly out of order.
186
+ *
187
+ * @param {array} array - The array to sort.
188
+ * @param {number} lo - First element in the range (inclusive).
189
+ * @param {number} hi - Last element in the range.
190
+ * @param {number} start - First element possibly out of order.
191
+ * @param {function} compare - Item comparison function.
192
+ */
193
+ function binaryInsertionSort (array, lo, hi, start, compare) {
194
+ if (start === lo) {
195
+ start ++
196
+ }
197
+
198
+ for (; start < hi; start ++) {
199
+ const pivot = array[start]
200
+ const pivotIndex = results[start]
201
+
202
+ // Ranges of the array where pivot belongs
203
+ let left = lo
204
+ let right = start
205
+
206
+ /*
207
+ * pivot >= array[i] for i in [lo, left)
208
+ * pivot < array[i] for i in in [right, start)
209
+ */
210
+ while (left < right) {
211
+ const mid = (left + right) >>> 1
212
+
213
+ if (compare(pivot, array[mid]) < 0) {
214
+ right = mid
215
+ } else {
216
+ left = mid + 1
217
+ }
218
+ }
219
+
220
+ /*
221
+ * Move elements right to make room for the pivot. If there are elements
222
+ * equal to pivot, left points to the first slot after them: this is also
223
+ * a reason for which TimSort is stable
224
+ */
225
+ let n = start - left
226
+ // Switch is just an optimization for small arrays
227
+ switch (n) {
228
+ case 3:
229
+ array[left + 3] = array[left + 2]
230
+ results[left + 3] = results[left + 2]
231
+ /* falls through */
232
+ case 2:
233
+ array[left + 2] = array[left + 1]
234
+ results[left + 2] = results[left + 1]
235
+ /* falls through */
236
+ case 1:
237
+ array[left + 1] = array[left]
238
+ results[left + 1] = results[left]
239
+ break
240
+ default:
241
+ while (n > 0) {
242
+ array[left + n] = array[left + n - 1]
243
+ results[left + n] = results[left + n - 1]
244
+ n --
245
+ }
246
+ }
247
+
248
+ array[left] = pivot
249
+ results[left] = pivotIndex
250
+ }
251
+ }
252
+
253
+ /**
254
+ * Find the position at which to insert a value in a sorted range. If the range
255
+ * contains elements equal to the value the leftmost element index is returned
256
+ * (for stability).
257
+ *
258
+ * @param {number} value - Value to insert.
259
+ * @param {array} array - The array in which to insert value.
260
+ * @param {number} start - First element in the range.
261
+ * @param {number} length - Length of the range.
262
+ * @param {number} hint - The index at which to begin the search.
263
+ * @param {function} compare - Item comparison function.
264
+ * @return {number} - The index where to insert value.
265
+ */
266
+ function gallopLeft (value, array, start, length, hint, compare) {
267
+ let lastOffset = 0
268
+ let maxOffset = 0
269
+ let offset = 1
270
+
271
+ if (compare(value, array[start + hint]) > 0) {
272
+ maxOffset = length - hint
273
+
274
+ while (
275
+ offset < maxOffset
276
+ && compare(value, array[start + hint + offset]) > 0
277
+ ) {
278
+ lastOffset = offset
279
+ offset = (offset << 1) + 1
280
+
281
+ if (offset <= 0) {
282
+ offset = maxOffset
283
+ }
284
+ }
285
+
286
+ if (offset > maxOffset) {
287
+ offset = maxOffset
288
+ }
289
+
290
+ // Make offsets relative to start
291
+ lastOffset += hint
292
+ offset += hint
293
+
294
+ // value <= array[start + hint]
295
+ } else {
296
+ maxOffset = hint + 1
297
+ while (
298
+ offset < maxOffset
299
+ && compare(value, array[start + hint - offset]) <= 0
300
+ ) {
301
+ lastOffset = offset
302
+ offset = (offset << 1) + 1
303
+
304
+ if (offset <= 0) {
305
+ offset = maxOffset
306
+ }
307
+ }
308
+ if (offset > maxOffset) {
309
+ offset = maxOffset
310
+ }
311
+
312
+ // Make offsets relative to start
313
+ const tmp = lastOffset
314
+ lastOffset = hint - offset
315
+ offset = hint - tmp
316
+ }
317
+
318
+ /*
319
+ * Now array[start+lastOffset] < value <= array[start+offset], so value
320
+ * belongs somewhere in the range (start + lastOffset, start + offset]. Do a
321
+ * binary search, with invariant array[start + lastOffset - 1] < value <=
322
+ * array[start + offset].
323
+ */
324
+ lastOffset ++
325
+ while (lastOffset < offset) {
326
+ const m = lastOffset + ((offset - lastOffset) >>> 1)
327
+
328
+ if (compare(value, array[start + m]) > 0) {
329
+ lastOffset = m + 1
330
+ } else {
331
+ offset = m
332
+ }
333
+ }
334
+ return offset
335
+ }
336
+
337
+ /**
338
+ * Find the position at which to insert a value in a sorted range. If the range
339
+ * contains elements equal to the value the rightmost element index is returned
340
+ * (for stability).
341
+ *
342
+ * @param {number} value - Value to insert.
343
+ * @param {array} array - The array in which to insert value.
344
+ * @param {number} start - First element in the range.
345
+ * @param {number} length - Length of the range.
346
+ * @param {number} hint - The index at which to begin the search.
347
+ * @param {function} compare - Item comparison function.
348
+ * @return {number} - The index where to insert value.
349
+ */
350
+ function gallopRight (value, array, start, length, hint, compare) {
351
+ let lastOffset = 0
352
+ let maxOffset = 0
353
+ let offset = 1
354
+
355
+ if (compare(value, array[start + hint]) < 0) {
356
+ maxOffset = hint + 1
357
+
358
+ while (
359
+ offset < maxOffset
360
+ && compare(value, array[start + hint - offset]) < 0
361
+ ) {
362
+ lastOffset = offset
363
+ offset = (offset << 1) + 1
364
+
365
+ if (offset <= 0) {
366
+ offset = maxOffset
367
+ }
368
+ }
369
+
370
+ if (offset > maxOffset) {
371
+ offset = maxOffset
372
+ }
373
+
374
+ // Make offsets relative to start
375
+ const tmp = lastOffset
376
+ lastOffset = hint - offset
377
+ offset = hint - tmp
378
+
379
+ // value >= array[start + hint]
380
+ } else {
381
+ maxOffset = length - hint
382
+
383
+ while (
384
+ offset < maxOffset
385
+ && compare(value, array[start + hint + offset]) >= 0
386
+ ) {
387
+ lastOffset = offset
388
+ offset = (offset << 1) + 1
389
+
390
+ if (offset <= 0) {
391
+ offset = maxOffset
392
+ }
393
+ }
394
+
395
+ if (offset > maxOffset) {
396
+ offset = maxOffset
397
+ }
398
+
399
+ // Make offsets relative to start
400
+ lastOffset += hint
401
+ offset += hint
402
+ }
403
+
404
+ /*
405
+ * Now array[start+lastOffset] < value <= array[start+offset], so value
406
+ * belongs somewhere in the range (start + lastOffset, start + offset]. Do a
407
+ * binary search, with invariant array[start + lastOffset - 1] < value <=
408
+ * array[start + offset].
409
+ */
410
+ lastOffset ++
411
+
412
+ while (lastOffset < offset) {
413
+ const m = lastOffset + ((offset - lastOffset) >>> 1)
414
+
415
+ if (compare(value, array[start + m]) < 0) {
416
+ offset = m
417
+ } else {
418
+ lastOffset = m + 1
419
+ }
420
+ }
421
+
422
+ return offset
423
+ }
424
+
425
+ class TimSort {
426
+ constructor (array, compare) {
427
+ this.array = array
428
+ this.compare = compare
429
+
430
+ const {length} = array
431
+
432
+ this.length = length
433
+ this.minGallop = DEFAULT_MIN_GALLOPING
434
+
435
+ this.tmpStorageLength = length < 2 * DEFAULT_TMP_STORAGE_LENGTH
436
+ ? length >>> 1
437
+ : DEFAULT_TMP_STORAGE_LENGTH
438
+
439
+ this.tmp = new Array(this.tmpStorageLength)
440
+ this.tmpIndex = new Array(this.tmpStorageLength)
441
+
442
+ this.stackLength = length < 120
443
+ ? 5
444
+ : length < 1542
445
+ ? 10
446
+ : length < 119151
447
+ ? 19
448
+ : 40
449
+
450
+ this.runStart = new Array(this.stackLength)
451
+ this.runLength = new Array(this.stackLength)
452
+
453
+ this.stackSize = 0
454
+ }
455
+
456
+ /**
457
+ * Push a new run on TimSort's stack.
458
+ *
459
+ * @param {number} runStart - Start index of the run in the original array.
460
+ * @param {number} runLength - Length of the run;
461
+ */
462
+ pushRun (runStart, runLength) {
463
+ this.runStart[this.stackSize] = runStart
464
+ this.runLength[this.stackSize] = runLength
465
+ this.stackSize += 1
466
+ }
467
+
468
+ /**
469
+ * Merge runs on TimSort's stack so that the following holds for all i:
470
+ * 1) runLength[i - 3] > runLength[i - 2] + runLength[i - 1]
471
+ * 2) runLength[i - 2] > runLength[i - 1]
472
+ */
473
+ mergeRuns () {
474
+ while (this.stackSize > 1) {
475
+ let n = this.stackSize - 2
476
+
477
+ if (
478
+ (
479
+ n >= 1
480
+ && this.runLength[n - 1] <= this.runLength[n] + this.runLength[n + 1]
481
+ )
482
+ || (
483
+ n >= 2
484
+ && this.runLength[n - 2] <= this.runLength[n] + this.runLength[n - 1]
485
+ )
486
+ ) {
487
+ if (this.runLength[n - 1] < this.runLength[n + 1]) {
488
+ n --
489
+ }
490
+ } else if (this.runLength[n] > this.runLength[n + 1]) {
491
+ break
492
+ }
493
+ this.mergeAt(n)
494
+ }
495
+ }
496
+
497
+ /**
498
+ * Merge all runs on TimSort's stack until only one remains.
499
+ */
500
+ forceMergeRuns () {
501
+ while (this.stackSize > 1) {
502
+ let n = this.stackSize - 2
503
+
504
+ if (n > 0 && this.runLength[n - 1] < this.runLength[n + 1]) {
505
+ n --
506
+ }
507
+
508
+ this.mergeAt(n)
509
+ }
510
+ }
511
+
512
+ /**
513
+ * Merge the runs on the stack at positions i and i+1. Must be always be called
514
+ * with i=stackSize-2 or i=stackSize-3 (that is, we merge on top of the stack).
515
+ *
516
+ * @param {number} i - Index of the run to merge in TimSort's stack.
517
+ */
518
+ mergeAt (i) {
519
+ const {compare} = this
520
+ const {array} = this
521
+
522
+ let start1 = this.runStart[i]
523
+ let length1 = this.runLength[i]
524
+ const start2 = this.runStart[i + 1]
525
+ let length2 = this.runLength[i + 1]
526
+
527
+ this.runLength[i] = length1 + length2
528
+
529
+ if (i === this.stackSize - 3) {
530
+ this.runStart[i + 1] = this.runStart[i + 2]
531
+ this.runLength[i + 1] = this.runLength[i + 2]
532
+ }
533
+
534
+ this.stackSize --
535
+
536
+ /*
537
+ * Find where the first element in the second run goes in run1. Previous
538
+ * elements in run1 are already in place
539
+ */
540
+ const k = gallopRight(array[start2], array, start1, length1, 0, compare)
541
+ start1 += k
542
+ length1 -= k
543
+
544
+ if (length1 === 0) {
545
+ return
546
+ }
547
+
548
+ /*
549
+ * Find where the last element in the first run goes in run2. Next elements
550
+ * in run2 are already in place
551
+ */
552
+ length2 = gallopLeft(
553
+ array[start1 + length1 - 1],
554
+ array,
555
+ start2,
556
+ length2,
557
+ length2 - 1,
558
+ compare
559
+ )
560
+
561
+ if (length2 === 0) {
562
+ return
563
+ }
564
+
565
+ /*
566
+ * Merge remaining runs. A tmp array with length = min(length1, length2) is
567
+ * used
568
+ */
569
+ if (length1 <= length2) {
570
+ this.mergeLow(start1, length1, start2, length2)
571
+ } else {
572
+ this.mergeHigh(start1, length1, start2, length2)
573
+ }
574
+ }
575
+
576
+ /**
577
+ * Merge two adjacent runs in a stable way. The runs must be such that the
578
+ * first element of run1 is bigger than the first element in run2 and the
579
+ * last element of run1 is greater than all the elements in run2.
580
+ * The method should be called when run1.length <= run2.length as it uses
581
+ * TimSort temporary array to store run1. Use mergeHigh if run1.length >
582
+ * run2.length.
583
+ *
584
+ * @param {number} start1 - First element in run1.
585
+ * @param {number} length1 - Length of run1.
586
+ * @param {number} start2 - First element in run2.
587
+ * @param {number} length2 - Length of run2.
588
+ */
589
+ mergeLow (start1, length1, start2, length2) {
590
+ const {compare} = this
591
+ const {array} = this
592
+ const {tmp} = this
593
+ const {tmpIndex} = this
594
+ let i = 0
595
+
596
+ for (i = 0; i < length1; i ++) {
597
+ tmp[i] = array[start1 + i]
598
+ tmpIndex[i] = results[start1 + i]
599
+ }
600
+
601
+ let cursor1 = 0
602
+ let cursor2 = start2
603
+ let dest = start1
604
+
605
+ array[dest] = array[cursor2]
606
+ results[dest] = results[cursor2]
607
+
608
+ dest ++
609
+ cursor2 ++
610
+
611
+ if (-- length2 === 0) {
612
+ for (i = 0; i < length1; i ++) {
613
+ array[dest + i] = tmp[cursor1 + i]
614
+ results[dest + i] = tmpIndex[cursor1 + i]
615
+ }
616
+ return
617
+ }
618
+
619
+ if (length1 === 1) {
620
+ for (i = 0; i < length2; i ++) {
621
+ array[dest + i] = array[cursor2 + i]
622
+ results[dest + i] = results[cursor2 + i]
623
+ }
624
+ array[dest + length2] = tmp[cursor1]
625
+ results[dest + length2] = tmpIndex[cursor1]
626
+ return
627
+ }
628
+
629
+ let {minGallop} = this
630
+
631
+ while (true) {
632
+ let count1 = 0
633
+ let count2 = 0
634
+ let exit = false
635
+
636
+ do {
637
+ if (compare(array[cursor2], tmp[cursor1]) < 0) {
638
+ array[dest] = array[cursor2]
639
+ results[dest] = results[cursor2]
640
+ dest ++
641
+ cursor2 ++
642
+ count2 ++
643
+ count1 = 0
644
+
645
+ if (-- length2 === 0) {
646
+ exit = true
647
+ break
648
+ }
649
+ } else {
650
+ array[dest] = tmp[cursor1]
651
+ results[dest] = tmpIndex[cursor1]
652
+ dest ++
653
+ cursor1 ++
654
+ count1 ++
655
+ count2 = 0
656
+ if (-- length1 === 1) {
657
+ exit = true
658
+ break
659
+ }
660
+ }
661
+ } while ((count1 | count2) < minGallop)
662
+
663
+ if (exit) {
664
+ break
665
+ }
666
+
667
+ do {
668
+ count1 = gallopRight(array[cursor2], tmp, cursor1, length1, 0, compare)
669
+
670
+ if (count1 !== 0) {
671
+ for (i = 0; i < count1; i ++) {
672
+ array[dest + i] = tmp[cursor1 + i]
673
+ results[dest + i] = tmpIndex[cursor1 + i]
674
+ }
675
+
676
+ dest += count1
677
+ cursor1 += count1
678
+ length1 -= count1
679
+ if (length1 <= 1) {
680
+ exit = true
681
+ break
682
+ }
683
+ }
684
+
685
+ array[dest] = array[cursor2]
686
+ results[dest] = results[cursor2]
687
+
688
+ dest ++
689
+ cursor2 ++
690
+
691
+ if (-- length2 === 0) {
692
+ exit = true
693
+ break
694
+ }
695
+
696
+ count2 = gallopLeft(tmp[cursor1], array, cursor2, length2, 0, compare)
697
+
698
+ if (count2 !== 0) {
699
+ for (i = 0; i < count2; i ++) {
700
+ array[dest + i] = array[cursor2 + i]
701
+ results[dest + i] = results[cursor2 + i]
702
+ }
703
+
704
+ dest += count2
705
+ cursor2 += count2
706
+ length2 -= count2
707
+
708
+ if (length2 === 0) {
709
+ exit = true
710
+ break
711
+ }
712
+ }
713
+ array[dest] = tmp[cursor1]
714
+ results[dest] = tmpIndex[cursor1]
715
+ dest ++
716
+ cursor1 ++
717
+
718
+ if (-- length1 === 1) {
719
+ exit = true
720
+ break
721
+ }
722
+
723
+ minGallop --
724
+ } while (
725
+ count1 >= DEFAULT_MIN_GALLOPING
726
+ || count2 >= DEFAULT_MIN_GALLOPING
727
+ )
728
+
729
+ if (exit) {
730
+ break
731
+ }
732
+
733
+ if (minGallop < 0) {
734
+ minGallop = 0
735
+ }
736
+
737
+ minGallop += 2
738
+ }
739
+
740
+ this.minGallop = minGallop
741
+
742
+ if (minGallop < 1) {
743
+ this.minGallop = 1
744
+ }
745
+
746
+ if (length1 === 1) {
747
+ for (i = 0; i < length2; i ++) {
748
+ array[dest + i] = array[cursor2 + i]
749
+ results[dest + i] = results[cursor2 + i]
750
+ }
751
+ array[dest + length2] = tmp[cursor1]
752
+ results[dest + length2] = tmpIndex[cursor1]
753
+ } else if (length1 === 0) {
754
+ throw new Error('mergeLow preconditions were not respected')
755
+ } else {
756
+ for (i = 0; i < length1; i ++) {
757
+ array[dest + i] = tmp[cursor1 + i]
758
+ results[dest + i] = tmpIndex[cursor1 + i]
759
+ }
760
+ }
761
+ }
762
+
763
+ /**
764
+ * Merge two adjacent runs in a stable way. The runs must be such that the
765
+ * first element of run1 is bigger than the first element in run2 and the
766
+ * last element of run1 is greater than all the elements in run2.
767
+ * The method should be called when run1.length > run2.length as it uses
768
+ * TimSort temporary array to store run2. Use mergeLow if run1.length <=
769
+ * run2.length.
770
+ *
771
+ * @param {number} start1 - First element in run1.
772
+ * @param {number} length1 - Length of run1.
773
+ * @param {number} start2 - First element in run2.
774
+ * @param {number} length2 - Length of run2.
775
+ */
776
+ mergeHigh (start1, length1, start2, length2) {
777
+ const {compare} = this
778
+ const {array} = this
779
+ const {tmp} = this
780
+ const {tmpIndex} = this
781
+ let i = 0
782
+
783
+ for (i = 0; i < length2; i ++) {
784
+ tmp[i] = array[start2 + i]
785
+ tmpIndex[i] = results[start2 + i]
786
+ }
787
+
788
+ let cursor1 = start1 + length1 - 1
789
+ let cursor2 = length2 - 1
790
+ let dest = start2 + length2 - 1
791
+ let customCursor = 0
792
+ let customDest = 0
793
+
794
+ array[dest] = array[cursor1]
795
+ results[dest] = results[cursor1]
796
+
797
+ dest --
798
+ cursor1 --
799
+
800
+ if (-- length1 === 0) {
801
+ customCursor = dest - (length2 - 1)
802
+
803
+ for (i = 0; i < length2; i ++) {
804
+ array[customCursor + i] = tmp[i]
805
+ results[customCursor + i] = tmpIndex[i]
806
+ }
807
+
808
+ return
809
+ }
810
+
811
+ if (length2 === 1) {
812
+ dest -= length1
813
+ cursor1 -= length1
814
+ customDest = dest + 1
815
+ customCursor = cursor1 + 1
816
+
817
+ for (i = length1 - 1; i >= 0; i --) {
818
+ array[customDest + i] = array[customCursor + i]
819
+ results[customDest + i] = results[customCursor + i]
820
+ }
821
+
822
+ array[dest] = tmp[cursor2]
823
+ results[dest] = tmpIndex[cursor2]
824
+ return
825
+ }
826
+
827
+ let {minGallop} = this
828
+
829
+ while (true) {
830
+ let count1 = 0
831
+ let count2 = 0
832
+ let exit = false
833
+
834
+ do {
835
+ if (compare(tmp[cursor2], array[cursor1]) < 0) {
836
+ array[dest] = array[cursor1]
837
+ results[dest] = results[cursor1]
838
+ dest --
839
+ cursor1 --
840
+ count1 ++
841
+ count2 = 0
842
+ if (-- length1 === 0) {
843
+ exit = true
844
+ break
845
+ }
846
+ } else {
847
+ array[dest] = tmp[cursor2]
848
+ results[dest] = tmpIndex[cursor2]
849
+ dest --
850
+ cursor2 --
851
+ count2 ++
852
+ count1 = 0
853
+ if (-- length2 === 1) {
854
+ exit = true
855
+ break
856
+ }
857
+ }
858
+ } while ((count1 | count2) < minGallop)
859
+
860
+ if (exit) {
861
+ break
862
+ }
863
+
864
+ do {
865
+ count1 = length1 - gallopRight(
866
+ tmp[cursor2],
867
+ array,
868
+ start1,
869
+ length1,
870
+ length1 - 1,
871
+ compare
872
+ )
873
+
874
+ if (count1 !== 0) {
875
+ dest -= count1
876
+ cursor1 -= count1
877
+ length1 -= count1
878
+ customDest = dest + 1
879
+ customCursor = cursor1 + 1
880
+
881
+ for (i = count1 - 1; i >= 0; i --) {
882
+ array[customDest + i] = array[customCursor + i]
883
+ results[customDest + i] = results[customCursor + i]
884
+ }
885
+
886
+ if (length1 === 0) {
887
+ exit = true
888
+ break
889
+ }
890
+ }
891
+
892
+ array[dest] = tmp[cursor2]
893
+ results[dest] = tmpIndex[cursor2]
894
+ dest --
895
+ cursor2 --
896
+
897
+ if (-- length2 === 1) {
898
+ exit = true
899
+ break
900
+ }
901
+
902
+ count2 = length2 - gallopLeft(
903
+ array[cursor1],
904
+ tmp,
905
+ 0,
906
+ length2,
907
+ length2 - 1,
908
+ compare
909
+ )
910
+
911
+ if (count2 !== 0) {
912
+ dest -= count2
913
+ cursor2 -= count2
914
+ length2 -= count2
915
+ customDest = dest + 1
916
+ customCursor = cursor2 + 1
917
+
918
+ for (i = 0; i < count2; i ++) {
919
+ array[customDest + i] = tmp[customCursor + i]
920
+ results[customDest + i] = tmpIndex[customCursor + i]
921
+ }
922
+
923
+ if (length2 <= 1) {
924
+ exit = true
925
+ break
926
+ }
927
+ }
928
+
929
+ array[dest] = array[cursor1]
930
+ results[dest] = results[cursor1]
931
+ dest --
932
+ cursor1 --
933
+
934
+ if (-- length1 === 0) {
935
+ exit = true
936
+ break
937
+ }
938
+
939
+ minGallop --
940
+ } while (
941
+ count1 >= DEFAULT_MIN_GALLOPING
942
+ || count2 >= DEFAULT_MIN_GALLOPING
943
+ )
944
+
945
+ if (exit) {
946
+ break
947
+ }
948
+
949
+ if (minGallop < 0) {
950
+ minGallop = 0
951
+ }
952
+
953
+ minGallop += 2
954
+ }
955
+
956
+ this.minGallop = minGallop
957
+
958
+ if (minGallop < 1) {
959
+ this.minGallop = 1
960
+ }
961
+
962
+ if (length2 === 1) {
963
+ dest -= length1
964
+ cursor1 -= length1
965
+ customDest = dest + 1
966
+ customCursor = cursor1 + 1
967
+
968
+ for (i = length1 - 1; i >= 0; i --) {
969
+ array[customDest + i] = array[customCursor + i]
970
+ results[customDest + i] = results[customCursor + i]
971
+ }
972
+
973
+ array[dest] = tmp[cursor2]
974
+ results[dest] = tmpIndex[cursor2]
975
+ } else if (length2 === 0) {
976
+ throw new Error('mergeHigh preconditions were not respected')
977
+ } else {
978
+ customCursor = dest - (length2 - 1)
979
+ for (i = 0; i < length2; i ++) {
980
+ array[customCursor + i] = tmp[i]
981
+ results[customCursor + i] = tmpIndex[i]
982
+ }
983
+ }
984
+ }
985
+ }
986
+
987
+ /**
988
+ * Sort an array in the range [lo, hi) using TimSort.
989
+ *
990
+ * @param {array} array - The array to sort.
991
+ * @param {function=} compare - Item comparison function. Default is
992
+ * alphabetical
993
+ * @param {number} lo - First element in the range (inclusive).
994
+ * @param {number} hi - Last element in the range.
995
+ * comparator.
996
+ */
997
+ function sort (array, compare, lo, hi) {
998
+ if (!Array.isArray(array)) {
999
+ throw new TypeError(
1000
+ `The "array" argument must be an array. Received ${array}`
1001
+ )
1002
+ }
1003
+
1004
+ results = []
1005
+
1006
+ const {length} = array
1007
+
1008
+ let i = 0
1009
+
1010
+ while (i < length) {
1011
+ results[i] = i ++
1012
+ }
1013
+
1014
+ /*
1015
+ * Handle the case where a comparison function is not provided. We do
1016
+ * lexicographic sorting
1017
+ */
1018
+ if (!compare) {
1019
+ compare = alphabeticalCompare
1020
+ } else if (typeof compare !== 'function') {
1021
+ hi = lo
1022
+ lo = compare
1023
+ compare = alphabeticalCompare
1024
+ }
1025
+
1026
+ if (!lo) {
1027
+ lo = 0
1028
+ }
1029
+ if (!hi) {
1030
+ hi = length
1031
+ }
1032
+
1033
+ let remaining = hi - lo
1034
+
1035
+ // The array is already sorted
1036
+ if (remaining < 2) {
1037
+ return results
1038
+ }
1039
+
1040
+ let runLength = 0
1041
+ // On small arrays binary sort can be used directly
1042
+ if (remaining < DEFAULT_MIN_MERGE) {
1043
+ runLength = makeAscendingRun(array, lo, hi, compare)
1044
+ binaryInsertionSort(array, lo, hi, lo + runLength, compare)
1045
+ return results
1046
+ }
1047
+
1048
+ const ts = new TimSort(array, compare)
1049
+
1050
+ const minRun = minRunLength(remaining)
1051
+
1052
+ do {
1053
+ runLength = makeAscendingRun(array, lo, hi, compare)
1054
+ if (runLength < minRun) {
1055
+ let force = remaining
1056
+ if (force > minRun) {
1057
+ force = minRun
1058
+ }
1059
+
1060
+ binaryInsertionSort(array, lo, lo + force, lo + runLength, compare)
1061
+ runLength = force
1062
+ }
1063
+ // Push new run and merge if necessary
1064
+ ts.pushRun(lo, runLength)
1065
+ ts.mergeRuns()
1066
+
1067
+ // Go find next run
1068
+ remaining -= runLength
1069
+ lo += runLength
1070
+ } while (remaining !== 0)
1071
+
1072
+ // Force merging of remaining runs
1073
+ ts.forceMergeRuns()
1074
+
1075
+ return results
1076
+ }
1077
+
1078
+ module.exports = {
1079
+ sort
1080
+ }