@jupyterlab/galata 5.0.0-alpha.2 → 5.0.0-alpha.21
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.
- package/README.md +192 -31
- package/lib/benchmarkReporter.d.ts +1 -0
- package/lib/benchmarkReporter.js +34 -39
- package/lib/benchmarkReporter.js.map +1 -1
- package/lib/benchmarkVLTpl.js +19 -5
- package/lib/benchmarkVLTpl.js.map +1 -1
- package/lib/contents.d.ts +5 -5
- package/lib/contents.js +32 -36
- package/lib/contents.js.map +1 -1
- package/lib/extension/global.d.ts +197 -0
- package/lib/extension/global.js +601 -0
- package/lib/extension/global.js.map +1 -0
- package/lib/extension/index.d.ts +6 -0
- package/lib/extension/index.js +27 -0
- package/lib/extension/index.js.map +1 -0
- package/lib/extension/tokens.d.ts +232 -0
- package/lib/extension/tokens.js +13 -0
- package/lib/extension/tokens.js.map +1 -0
- package/lib/extension.d.ts +223 -0
- package/lib/{global.js → extension.js} +1 -2
- package/lib/extension.js.map +1 -0
- package/lib/fixtures.d.ts +32 -10
- package/lib/fixtures.js +64 -17
- package/lib/fixtures.js.map +1 -1
- package/lib/galata.d.ts +140 -19
- package/lib/galata.js +272 -87
- package/lib/galata.js.map +1 -1
- package/lib/helpers/activity.d.ts +6 -0
- package/lib/helpers/activity.js +19 -5
- package/lib/helpers/activity.js.map +1 -1
- package/lib/helpers/debuggerpanel.d.ts +4 -0
- package/lib/helpers/debuggerpanel.js +16 -0
- package/lib/helpers/debuggerpanel.js.map +1 -1
- package/lib/helpers/filebrowser.js +8 -2
- package/lib/helpers/filebrowser.js.map +1 -1
- package/lib/helpers/index.d.ts +1 -0
- package/lib/helpers/index.js +6 -1
- package/lib/helpers/index.js.map +1 -1
- package/lib/helpers/kernel.js +7 -7
- package/lib/helpers/kernel.js.map +1 -1
- package/lib/helpers/menu.d.ts +7 -0
- package/lib/helpers/menu.js +17 -1
- package/lib/helpers/menu.js.map +1 -1
- package/lib/helpers/notebook.d.ts +6 -4
- package/lib/helpers/notebook.js +127 -31
- package/lib/helpers/notebook.js.map +1 -1
- package/lib/helpers/sidebar.d.ts +8 -1
- package/lib/helpers/sidebar.js +33 -15
- package/lib/helpers/sidebar.js.map +1 -1
- package/lib/helpers/statusbar.js +1 -1
- package/lib/helpers/statusbar.js.map +1 -1
- package/lib/helpers/style.d.ts +42 -0
- package/lib/helpers/style.js +50 -0
- package/lib/helpers/style.js.map +1 -0
- package/lib/helpers/theme.js +1 -1
- package/lib/helpers/theme.js.map +1 -1
- package/lib/index.d.ts +5 -2
- package/lib/index.js +12 -3
- package/lib/index.js.map +1 -1
- package/lib/jupyterlabpage.d.ts +29 -4
- package/lib/jupyterlabpage.js +38 -22
- package/lib/jupyterlabpage.js.map +1 -1
- package/lib/playwright-config.js +5 -1
- package/lib/playwright-config.js.map +1 -1
- package/lib/utils.js +5 -1
- package/lib/utils.js.map +1 -1
- package/package.json +31 -47
- package/src/benchmarkReporter.ts +756 -0
- package/src/benchmarkVLTpl.ts +91 -0
- package/src/contents.ts +472 -0
- package/src/extension.ts +281 -0
- package/src/fixtures.ts +387 -0
- package/src/galata.ts +1035 -0
- package/src/helpers/activity.ts +115 -0
- package/src/helpers/debuggerpanel.ts +159 -0
- package/src/helpers/filebrowser.ts +228 -0
- package/src/helpers/index.ts +15 -0
- package/src/helpers/kernel.ts +39 -0
- package/src/helpers/logconsole.ts +32 -0
- package/src/helpers/menu.ts +228 -0
- package/src/helpers/notebook.ts +1217 -0
- package/src/helpers/performance.ts +57 -0
- package/src/helpers/sidebar.ts +289 -0
- package/src/helpers/statusbar.ts +56 -0
- package/src/helpers/style.ts +100 -0
- package/src/helpers/theme.ts +50 -0
- package/src/index.ts +19 -0
- package/src/jupyterlabpage.ts +704 -0
- package/src/playwright-config.ts +26 -0
- package/src/utils.ts +264 -0
- package/src/vega-statistics.d.ts +15 -0
- package/lib/global.d.ts +0 -23
- package/lib/global.js.map +0 -1
- package/lib/inpage/tokens.d.ts +0 -135
- package/lib/inpage/tokens.js +0 -9
- package/lib/inpage/tokens.js.map +0 -1
- package/lib/lib-inpage/inpage.js +0 -3957
- package/lib/lib-inpage/inpage.js.map +0 -1
- package/style/index.css +0 -10
- package/style/index.js +0 -10
package/lib/lib-inpage/inpage.js
DELETED
|
@@ -1,3957 +0,0 @@
|
|
|
1
|
-
/******/ (() => { // webpackBootstrap
|
|
2
|
-
/******/ "use strict";
|
|
3
|
-
/******/ var __webpack_modules__ = ({
|
|
4
|
-
|
|
5
|
-
/***/ 943:
|
|
6
|
-
/***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => {
|
|
7
|
-
|
|
8
|
-
__webpack_require__.r(__webpack_exports__);
|
|
9
|
-
/* harmony export */ __webpack_require__.d(__webpack_exports__, {
|
|
10
|
-
/* harmony export */ "ArrayExt": () => (/* binding */ ArrayExt),
|
|
11
|
-
/* harmony export */ "ArrayIterator": () => (/* binding */ ArrayIterator),
|
|
12
|
-
/* harmony export */ "ChainIterator": () => (/* binding */ ChainIterator),
|
|
13
|
-
/* harmony export */ "EmptyIterator": () => (/* binding */ EmptyIterator),
|
|
14
|
-
/* harmony export */ "EnumerateIterator": () => (/* binding */ EnumerateIterator),
|
|
15
|
-
/* harmony export */ "FilterIterator": () => (/* binding */ FilterIterator),
|
|
16
|
-
/* harmony export */ "FnIterator": () => (/* binding */ FnIterator),
|
|
17
|
-
/* harmony export */ "ItemIterator": () => (/* binding */ ItemIterator),
|
|
18
|
-
/* harmony export */ "KeyIterator": () => (/* binding */ KeyIterator),
|
|
19
|
-
/* harmony export */ "MapIterator": () => (/* binding */ MapIterator),
|
|
20
|
-
/* harmony export */ "RangeIterator": () => (/* binding */ RangeIterator),
|
|
21
|
-
/* harmony export */ "RepeatIterator": () => (/* binding */ RepeatIterator),
|
|
22
|
-
/* harmony export */ "RetroArrayIterator": () => (/* binding */ RetroArrayIterator),
|
|
23
|
-
/* harmony export */ "StrideIterator": () => (/* binding */ StrideIterator),
|
|
24
|
-
/* harmony export */ "StringExt": () => (/* binding */ StringExt),
|
|
25
|
-
/* harmony export */ "TakeIterator": () => (/* binding */ TakeIterator),
|
|
26
|
-
/* harmony export */ "ValueIterator": () => (/* binding */ ValueIterator),
|
|
27
|
-
/* harmony export */ "ZipIterator": () => (/* binding */ ZipIterator),
|
|
28
|
-
/* harmony export */ "chain": () => (/* binding */ chain),
|
|
29
|
-
/* harmony export */ "each": () => (/* binding */ each),
|
|
30
|
-
/* harmony export */ "empty": () => (/* binding */ empty),
|
|
31
|
-
/* harmony export */ "enumerate": () => (/* binding */ enumerate),
|
|
32
|
-
/* harmony export */ "every": () => (/* binding */ every),
|
|
33
|
-
/* harmony export */ "filter": () => (/* binding */ filter),
|
|
34
|
-
/* harmony export */ "find": () => (/* binding */ find),
|
|
35
|
-
/* harmony export */ "findIndex": () => (/* binding */ findIndex),
|
|
36
|
-
/* harmony export */ "iter": () => (/* binding */ iter),
|
|
37
|
-
/* harmony export */ "iterFn": () => (/* binding */ iterFn),
|
|
38
|
-
/* harmony export */ "iterItems": () => (/* binding */ iterItems),
|
|
39
|
-
/* harmony export */ "iterKeys": () => (/* binding */ iterKeys),
|
|
40
|
-
/* harmony export */ "iterValues": () => (/* binding */ iterValues),
|
|
41
|
-
/* harmony export */ "map": () => (/* binding */ map),
|
|
42
|
-
/* harmony export */ "max": () => (/* binding */ max),
|
|
43
|
-
/* harmony export */ "min": () => (/* binding */ min),
|
|
44
|
-
/* harmony export */ "minmax": () => (/* binding */ minmax),
|
|
45
|
-
/* harmony export */ "once": () => (/* binding */ once),
|
|
46
|
-
/* harmony export */ "range": () => (/* binding */ range),
|
|
47
|
-
/* harmony export */ "reduce": () => (/* binding */ reduce),
|
|
48
|
-
/* harmony export */ "repeat": () => (/* binding */ repeat),
|
|
49
|
-
/* harmony export */ "retro": () => (/* binding */ retro),
|
|
50
|
-
/* harmony export */ "some": () => (/* binding */ some),
|
|
51
|
-
/* harmony export */ "stride": () => (/* binding */ stride),
|
|
52
|
-
/* harmony export */ "take": () => (/* binding */ take),
|
|
53
|
-
/* harmony export */ "toArray": () => (/* binding */ toArray),
|
|
54
|
-
/* harmony export */ "toObject": () => (/* binding */ toObject),
|
|
55
|
-
/* harmony export */ "topologicSort": () => (/* binding */ topologicSort),
|
|
56
|
-
/* harmony export */ "zip": () => (/* binding */ zip)
|
|
57
|
-
/* harmony export */ });
|
|
58
|
-
// Copyright (c) Jupyter Development Team.
|
|
59
|
-
// Distributed under the terms of the Modified BSD License.
|
|
60
|
-
/*-----------------------------------------------------------------------------
|
|
61
|
-
| Copyright (c) 2014-2017, PhosphorJS Contributors
|
|
62
|
-
|
|
|
63
|
-
| Distributed under the terms of the BSD 3-Clause License.
|
|
64
|
-
|
|
|
65
|
-
| The full license is in the file LICENSE, distributed with this software.
|
|
66
|
-
|----------------------------------------------------------------------------*/
|
|
67
|
-
/**
|
|
68
|
-
* The namespace for array-specific algorithms.
|
|
69
|
-
*/
|
|
70
|
-
var ArrayExt;
|
|
71
|
-
(function (ArrayExt) {
|
|
72
|
-
/**
|
|
73
|
-
* Find the index of the first occurrence of a value in an array.
|
|
74
|
-
*
|
|
75
|
-
* @param array - The array-like object to search.
|
|
76
|
-
*
|
|
77
|
-
* @param value - The value to locate in the array. Values are
|
|
78
|
-
* compared using strict `===` equality.
|
|
79
|
-
*
|
|
80
|
-
* @param start - The index of the first element in the range to be
|
|
81
|
-
* searched, inclusive. The default value is `0`. Negative values
|
|
82
|
-
* are taken as an offset from the end of the array.
|
|
83
|
-
*
|
|
84
|
-
* @param stop - The index of the last element in the range to be
|
|
85
|
-
* searched, inclusive. The default value is `-1`. Negative values
|
|
86
|
-
* are taken as an offset from the end of the array.
|
|
87
|
-
*
|
|
88
|
-
* @returns The index of the first occurrence of the value, or `-1`
|
|
89
|
-
* if the value is not found.
|
|
90
|
-
*
|
|
91
|
-
* #### Notes
|
|
92
|
-
* If `stop < start` the search will wrap at the end of the array.
|
|
93
|
-
*
|
|
94
|
-
* #### Complexity
|
|
95
|
-
* Linear.
|
|
96
|
-
*
|
|
97
|
-
* #### Undefined Behavior
|
|
98
|
-
* A `start` or `stop` which is non-integral.
|
|
99
|
-
*
|
|
100
|
-
* #### Example
|
|
101
|
-
* ```typescript
|
|
102
|
-
* import { ArrayExt } from '@lumino/algorithm';
|
|
103
|
-
*
|
|
104
|
-
* let data = ['one', 'two', 'three', 'four', 'one'];
|
|
105
|
-
* ArrayExt.firstIndexOf(data, 'red'); // -1
|
|
106
|
-
* ArrayExt.firstIndexOf(data, 'one'); // 0
|
|
107
|
-
* ArrayExt.firstIndexOf(data, 'one', 1); // 4
|
|
108
|
-
* ArrayExt.firstIndexOf(data, 'two', 2); // -1
|
|
109
|
-
* ArrayExt.firstIndexOf(data, 'two', 2, 1); // 1
|
|
110
|
-
* ```
|
|
111
|
-
*/
|
|
112
|
-
function firstIndexOf(array, value, start, stop) {
|
|
113
|
-
if (start === void 0) { start = 0; }
|
|
114
|
-
if (stop === void 0) { stop = -1; }
|
|
115
|
-
var n = array.length;
|
|
116
|
-
if (n === 0) {
|
|
117
|
-
return -1;
|
|
118
|
-
}
|
|
119
|
-
if (start < 0) {
|
|
120
|
-
start = Math.max(0, start + n);
|
|
121
|
-
}
|
|
122
|
-
else {
|
|
123
|
-
start = Math.min(start, n - 1);
|
|
124
|
-
}
|
|
125
|
-
if (stop < 0) {
|
|
126
|
-
stop = Math.max(0, stop + n);
|
|
127
|
-
}
|
|
128
|
-
else {
|
|
129
|
-
stop = Math.min(stop, n - 1);
|
|
130
|
-
}
|
|
131
|
-
var span;
|
|
132
|
-
if (stop < start) {
|
|
133
|
-
span = stop + 1 + (n - start);
|
|
134
|
-
}
|
|
135
|
-
else {
|
|
136
|
-
span = stop - start + 1;
|
|
137
|
-
}
|
|
138
|
-
for (var i = 0; i < span; ++i) {
|
|
139
|
-
var j = (start + i) % n;
|
|
140
|
-
if (array[j] === value) {
|
|
141
|
-
return j;
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
return -1;
|
|
145
|
-
}
|
|
146
|
-
ArrayExt.firstIndexOf = firstIndexOf;
|
|
147
|
-
/**
|
|
148
|
-
* Find the index of the last occurrence of a value in an array.
|
|
149
|
-
*
|
|
150
|
-
* @param array - The array-like object to search.
|
|
151
|
-
*
|
|
152
|
-
* @param value - The value to locate in the array. Values are
|
|
153
|
-
* compared using strict `===` equality.
|
|
154
|
-
*
|
|
155
|
-
* @param start - The index of the first element in the range to be
|
|
156
|
-
* searched, inclusive. The default value is `-1`. Negative values
|
|
157
|
-
* are taken as an offset from the end of the array.
|
|
158
|
-
*
|
|
159
|
-
* @param stop - The index of the last element in the range to be
|
|
160
|
-
* searched, inclusive. The default value is `0`. Negative values
|
|
161
|
-
* are taken as an offset from the end of the array.
|
|
162
|
-
*
|
|
163
|
-
* @returns The index of the last occurrence of the value, or `-1`
|
|
164
|
-
* if the value is not found.
|
|
165
|
-
*
|
|
166
|
-
* #### Notes
|
|
167
|
-
* If `start < stop` the search will wrap at the front of the array.
|
|
168
|
-
*
|
|
169
|
-
* #### Complexity
|
|
170
|
-
* Linear.
|
|
171
|
-
*
|
|
172
|
-
* #### Undefined Behavior
|
|
173
|
-
* A `start` or `stop` which is non-integral.
|
|
174
|
-
*
|
|
175
|
-
* #### Example
|
|
176
|
-
* ```typescript
|
|
177
|
-
* import { ArrayExt } from '@lumino/algorithm';
|
|
178
|
-
*
|
|
179
|
-
* let data = ['one', 'two', 'three', 'four', 'one'];
|
|
180
|
-
* ArrayExt.lastIndexOf(data, 'red'); // -1
|
|
181
|
-
* ArrayExt.lastIndexOf(data, 'one'); // 4
|
|
182
|
-
* ArrayExt.lastIndexOf(data, 'one', 1); // 0
|
|
183
|
-
* ArrayExt.lastIndexOf(data, 'two', 0); // -1
|
|
184
|
-
* ArrayExt.lastIndexOf(data, 'two', 0, 1); // 1
|
|
185
|
-
* ```
|
|
186
|
-
*/
|
|
187
|
-
function lastIndexOf(array, value, start, stop) {
|
|
188
|
-
if (start === void 0) { start = -1; }
|
|
189
|
-
if (stop === void 0) { stop = 0; }
|
|
190
|
-
var n = array.length;
|
|
191
|
-
if (n === 0) {
|
|
192
|
-
return -1;
|
|
193
|
-
}
|
|
194
|
-
if (start < 0) {
|
|
195
|
-
start = Math.max(0, start + n);
|
|
196
|
-
}
|
|
197
|
-
else {
|
|
198
|
-
start = Math.min(start, n - 1);
|
|
199
|
-
}
|
|
200
|
-
if (stop < 0) {
|
|
201
|
-
stop = Math.max(0, stop + n);
|
|
202
|
-
}
|
|
203
|
-
else {
|
|
204
|
-
stop = Math.min(stop, n - 1);
|
|
205
|
-
}
|
|
206
|
-
var span;
|
|
207
|
-
if (start < stop) {
|
|
208
|
-
span = start + 1 + (n - stop);
|
|
209
|
-
}
|
|
210
|
-
else {
|
|
211
|
-
span = start - stop + 1;
|
|
212
|
-
}
|
|
213
|
-
for (var i = 0; i < span; ++i) {
|
|
214
|
-
var j = (start - i + n) % n;
|
|
215
|
-
if (array[j] === value) {
|
|
216
|
-
return j;
|
|
217
|
-
}
|
|
218
|
-
}
|
|
219
|
-
return -1;
|
|
220
|
-
}
|
|
221
|
-
ArrayExt.lastIndexOf = lastIndexOf;
|
|
222
|
-
/**
|
|
223
|
-
* Find the index of the first value which matches a predicate.
|
|
224
|
-
*
|
|
225
|
-
* @param array - The array-like object to search.
|
|
226
|
-
*
|
|
227
|
-
* @param fn - The predicate function to apply to the values.
|
|
228
|
-
*
|
|
229
|
-
* @param start - The index of the first element in the range to be
|
|
230
|
-
* searched, inclusive. The default value is `0`. Negative values
|
|
231
|
-
* are taken as an offset from the end of the array.
|
|
232
|
-
*
|
|
233
|
-
* @param stop - The index of the last element in the range to be
|
|
234
|
-
* searched, inclusive. The default value is `-1`. Negative values
|
|
235
|
-
* are taken as an offset from the end of the array.
|
|
236
|
-
*
|
|
237
|
-
* @returns The index of the first matching value, or `-1` if no
|
|
238
|
-
* matching value is found.
|
|
239
|
-
*
|
|
240
|
-
* #### Notes
|
|
241
|
-
* If `stop < start` the search will wrap at the end of the array.
|
|
242
|
-
*
|
|
243
|
-
* #### Complexity
|
|
244
|
-
* Linear.
|
|
245
|
-
*
|
|
246
|
-
* #### Undefined Behavior
|
|
247
|
-
* A `start` or `stop` which is non-integral.
|
|
248
|
-
*
|
|
249
|
-
* Modifying the length of the array while searching.
|
|
250
|
-
*
|
|
251
|
-
* #### Example
|
|
252
|
-
* ```typescript
|
|
253
|
-
* import { ArrayExt } from '@lumino/algorithm';
|
|
254
|
-
*
|
|
255
|
-
* function isEven(value: number): boolean {
|
|
256
|
-
* return value % 2 === 0;
|
|
257
|
-
* }
|
|
258
|
-
*
|
|
259
|
-
* let data = [1, 2, 3, 4, 3, 2, 1];
|
|
260
|
-
* ArrayExt.findFirstIndex(data, isEven); // 1
|
|
261
|
-
* ArrayExt.findFirstIndex(data, isEven, 4); // 5
|
|
262
|
-
* ArrayExt.findFirstIndex(data, isEven, 6); // -1
|
|
263
|
-
* ArrayExt.findFirstIndex(data, isEven, 6, 5); // 1
|
|
264
|
-
* ```
|
|
265
|
-
*/
|
|
266
|
-
function findFirstIndex(array, fn, start, stop) {
|
|
267
|
-
if (start === void 0) { start = 0; }
|
|
268
|
-
if (stop === void 0) { stop = -1; }
|
|
269
|
-
var n = array.length;
|
|
270
|
-
if (n === 0) {
|
|
271
|
-
return -1;
|
|
272
|
-
}
|
|
273
|
-
if (start < 0) {
|
|
274
|
-
start = Math.max(0, start + n);
|
|
275
|
-
}
|
|
276
|
-
else {
|
|
277
|
-
start = Math.min(start, n - 1);
|
|
278
|
-
}
|
|
279
|
-
if (stop < 0) {
|
|
280
|
-
stop = Math.max(0, stop + n);
|
|
281
|
-
}
|
|
282
|
-
else {
|
|
283
|
-
stop = Math.min(stop, n - 1);
|
|
284
|
-
}
|
|
285
|
-
var span;
|
|
286
|
-
if (stop < start) {
|
|
287
|
-
span = stop + 1 + (n - start);
|
|
288
|
-
}
|
|
289
|
-
else {
|
|
290
|
-
span = stop - start + 1;
|
|
291
|
-
}
|
|
292
|
-
for (var i = 0; i < span; ++i) {
|
|
293
|
-
var j = (start + i) % n;
|
|
294
|
-
if (fn(array[j], j)) {
|
|
295
|
-
return j;
|
|
296
|
-
}
|
|
297
|
-
}
|
|
298
|
-
return -1;
|
|
299
|
-
}
|
|
300
|
-
ArrayExt.findFirstIndex = findFirstIndex;
|
|
301
|
-
/**
|
|
302
|
-
* Find the index of the last value which matches a predicate.
|
|
303
|
-
*
|
|
304
|
-
* @param object - The array-like object to search.
|
|
305
|
-
*
|
|
306
|
-
* @param fn - The predicate function to apply to the values.
|
|
307
|
-
*
|
|
308
|
-
* @param start - The index of the first element in the range to be
|
|
309
|
-
* searched, inclusive. The default value is `-1`. Negative values
|
|
310
|
-
* are taken as an offset from the end of the array.
|
|
311
|
-
*
|
|
312
|
-
* @param stop - The index of the last element in the range to be
|
|
313
|
-
* searched, inclusive. The default value is `0`. Negative values
|
|
314
|
-
* are taken as an offset from the end of the array.
|
|
315
|
-
*
|
|
316
|
-
* @returns The index of the last matching value, or `-1` if no
|
|
317
|
-
* matching value is found.
|
|
318
|
-
*
|
|
319
|
-
* #### Notes
|
|
320
|
-
* If `start < stop` the search will wrap at the front of the array.
|
|
321
|
-
*
|
|
322
|
-
* #### Complexity
|
|
323
|
-
* Linear.
|
|
324
|
-
*
|
|
325
|
-
* #### Undefined Behavior
|
|
326
|
-
* A `start` or `stop` which is non-integral.
|
|
327
|
-
*
|
|
328
|
-
* Modifying the length of the array while searching.
|
|
329
|
-
*
|
|
330
|
-
* #### Example
|
|
331
|
-
* ```typescript
|
|
332
|
-
* import { ArrayExt } from '@lumino/algorithm';
|
|
333
|
-
*
|
|
334
|
-
* function isEven(value: number): boolean {
|
|
335
|
-
* return value % 2 === 0;
|
|
336
|
-
* }
|
|
337
|
-
*
|
|
338
|
-
* let data = [1, 2, 3, 4, 3, 2, 1];
|
|
339
|
-
* ArrayExt.findLastIndex(data, isEven); // 5
|
|
340
|
-
* ArrayExt.findLastIndex(data, isEven, 4); // 3
|
|
341
|
-
* ArrayExt.findLastIndex(data, isEven, 0); // -1
|
|
342
|
-
* ArrayExt.findLastIndex(data, isEven, 0, 1); // 5
|
|
343
|
-
* ```
|
|
344
|
-
*/
|
|
345
|
-
function findLastIndex(array, fn, start, stop) {
|
|
346
|
-
if (start === void 0) { start = -1; }
|
|
347
|
-
if (stop === void 0) { stop = 0; }
|
|
348
|
-
var n = array.length;
|
|
349
|
-
if (n === 0) {
|
|
350
|
-
return -1;
|
|
351
|
-
}
|
|
352
|
-
if (start < 0) {
|
|
353
|
-
start = Math.max(0, start + n);
|
|
354
|
-
}
|
|
355
|
-
else {
|
|
356
|
-
start = Math.min(start, n - 1);
|
|
357
|
-
}
|
|
358
|
-
if (stop < 0) {
|
|
359
|
-
stop = Math.max(0, stop + n);
|
|
360
|
-
}
|
|
361
|
-
else {
|
|
362
|
-
stop = Math.min(stop, n - 1);
|
|
363
|
-
}
|
|
364
|
-
var d;
|
|
365
|
-
if (start < stop) {
|
|
366
|
-
d = start + 1 + (n - stop);
|
|
367
|
-
}
|
|
368
|
-
else {
|
|
369
|
-
d = start - stop + 1;
|
|
370
|
-
}
|
|
371
|
-
for (var i = 0; i < d; ++i) {
|
|
372
|
-
var j = (start - i + n) % n;
|
|
373
|
-
if (fn(array[j], j)) {
|
|
374
|
-
return j;
|
|
375
|
-
}
|
|
376
|
-
}
|
|
377
|
-
return -1;
|
|
378
|
-
}
|
|
379
|
-
ArrayExt.findLastIndex = findLastIndex;
|
|
380
|
-
/**
|
|
381
|
-
* Find the first value which matches a predicate.
|
|
382
|
-
*
|
|
383
|
-
* @param array - The array-like object to search.
|
|
384
|
-
*
|
|
385
|
-
* @param fn - The predicate function to apply to the values.
|
|
386
|
-
*
|
|
387
|
-
* @param start - The index of the first element in the range to be
|
|
388
|
-
* searched, inclusive. The default value is `0`. Negative values
|
|
389
|
-
* are taken as an offset from the end of the array.
|
|
390
|
-
*
|
|
391
|
-
* @param stop - The index of the last element in the range to be
|
|
392
|
-
* searched, inclusive. The default value is `-1`. Negative values
|
|
393
|
-
* are taken as an offset from the end of the array.
|
|
394
|
-
*
|
|
395
|
-
* @returns The first matching value, or `undefined` if no matching
|
|
396
|
-
* value is found.
|
|
397
|
-
*
|
|
398
|
-
* #### Notes
|
|
399
|
-
* If `stop < start` the search will wrap at the end of the array.
|
|
400
|
-
*
|
|
401
|
-
* #### Complexity
|
|
402
|
-
* Linear.
|
|
403
|
-
*
|
|
404
|
-
* #### Undefined Behavior
|
|
405
|
-
* A `start` or `stop` which is non-integral.
|
|
406
|
-
*
|
|
407
|
-
* Modifying the length of the array while searching.
|
|
408
|
-
*
|
|
409
|
-
* #### Example
|
|
410
|
-
* ```typescript
|
|
411
|
-
* import { ArrayExt } from '@lumino/algorithm';
|
|
412
|
-
*
|
|
413
|
-
* function isEven(value: number): boolean {
|
|
414
|
-
* return value % 2 === 0;
|
|
415
|
-
* }
|
|
416
|
-
*
|
|
417
|
-
* let data = [1, 2, 3, 4, 3, 2, 1];
|
|
418
|
-
* ArrayExt.findFirstValue(data, isEven); // 2
|
|
419
|
-
* ArrayExt.findFirstValue(data, isEven, 2); // 4
|
|
420
|
-
* ArrayExt.findFirstValue(data, isEven, 6); // undefined
|
|
421
|
-
* ArrayExt.findFirstValue(data, isEven, 6, 5); // 2
|
|
422
|
-
* ```
|
|
423
|
-
*/
|
|
424
|
-
function findFirstValue(array, fn, start, stop) {
|
|
425
|
-
if (start === void 0) { start = 0; }
|
|
426
|
-
if (stop === void 0) { stop = -1; }
|
|
427
|
-
var index = findFirstIndex(array, fn, start, stop);
|
|
428
|
-
return index !== -1 ? array[index] : undefined;
|
|
429
|
-
}
|
|
430
|
-
ArrayExt.findFirstValue = findFirstValue;
|
|
431
|
-
/**
|
|
432
|
-
* Find the last value which matches a predicate.
|
|
433
|
-
*
|
|
434
|
-
* @param object - The array-like object to search.
|
|
435
|
-
*
|
|
436
|
-
* @param fn - The predicate function to apply to the values.
|
|
437
|
-
*
|
|
438
|
-
* @param start - The index of the first element in the range to be
|
|
439
|
-
* searched, inclusive. The default value is `-1`. Negative values
|
|
440
|
-
* are taken as an offset from the end of the array.
|
|
441
|
-
*
|
|
442
|
-
* @param stop - The index of the last element in the range to be
|
|
443
|
-
* searched, inclusive. The default value is `0`. Negative values
|
|
444
|
-
* are taken as an offset from the end of the array.
|
|
445
|
-
*
|
|
446
|
-
* @returns The last matching value, or `undefined` if no matching
|
|
447
|
-
* value is found.
|
|
448
|
-
*
|
|
449
|
-
* #### Notes
|
|
450
|
-
* If `start < stop` the search will wrap at the front of the array.
|
|
451
|
-
*
|
|
452
|
-
* #### Complexity
|
|
453
|
-
* Linear.
|
|
454
|
-
*
|
|
455
|
-
* #### Undefined Behavior
|
|
456
|
-
* A `start` or `stop` which is non-integral.
|
|
457
|
-
*
|
|
458
|
-
* Modifying the length of the array while searching.
|
|
459
|
-
*
|
|
460
|
-
* #### Example
|
|
461
|
-
* ```typescript
|
|
462
|
-
* import { ArrayExt } from '@lumino/algorithm';
|
|
463
|
-
*
|
|
464
|
-
* function isEven(value: number): boolean {
|
|
465
|
-
* return value % 2 === 0;
|
|
466
|
-
* }
|
|
467
|
-
*
|
|
468
|
-
* let data = [1, 2, 3, 4, 3, 2, 1];
|
|
469
|
-
* ArrayExt.findLastValue(data, isEven); // 2
|
|
470
|
-
* ArrayExt.findLastValue(data, isEven, 4); // 4
|
|
471
|
-
* ArrayExt.findLastValue(data, isEven, 0); // undefined
|
|
472
|
-
* ArrayExt.findLastValue(data, isEven, 0, 1); // 2
|
|
473
|
-
* ```
|
|
474
|
-
*/
|
|
475
|
-
function findLastValue(array, fn, start, stop) {
|
|
476
|
-
if (start === void 0) { start = -1; }
|
|
477
|
-
if (stop === void 0) { stop = 0; }
|
|
478
|
-
var index = findLastIndex(array, fn, start, stop);
|
|
479
|
-
return index !== -1 ? array[index] : undefined;
|
|
480
|
-
}
|
|
481
|
-
ArrayExt.findLastValue = findLastValue;
|
|
482
|
-
/**
|
|
483
|
-
* Find the index of the first element which compares `>=` to a value.
|
|
484
|
-
*
|
|
485
|
-
* @param array - The sorted array-like object to search.
|
|
486
|
-
*
|
|
487
|
-
* @param value - The value to locate in the array.
|
|
488
|
-
*
|
|
489
|
-
* @param fn - The 3-way comparison function to apply to the values.
|
|
490
|
-
* It should return `< 0` if an element is less than a value, `0` if
|
|
491
|
-
* an element is equal to a value, or `> 0` if an element is greater
|
|
492
|
-
* than a value.
|
|
493
|
-
*
|
|
494
|
-
* @param start - The index of the first element in the range to be
|
|
495
|
-
* searched, inclusive. The default value is `0`. Negative values
|
|
496
|
-
* are taken as an offset from the end of the array.
|
|
497
|
-
*
|
|
498
|
-
* @param stop - The index of the last element in the range to be
|
|
499
|
-
* searched, inclusive. The default value is `-1`. Negative values
|
|
500
|
-
* are taken as an offset from the end of the array.
|
|
501
|
-
*
|
|
502
|
-
* @returns The index of the first element which compares `>=` to the
|
|
503
|
-
* value, or `length` if there is no such element. If the computed
|
|
504
|
-
* index for `stop` is less than `start`, then the computed index
|
|
505
|
-
* for `start` is returned.
|
|
506
|
-
*
|
|
507
|
-
* #### Notes
|
|
508
|
-
* The array must already be sorted in ascending order according to
|
|
509
|
-
* the comparison function.
|
|
510
|
-
*
|
|
511
|
-
* #### Complexity
|
|
512
|
-
* Logarithmic.
|
|
513
|
-
*
|
|
514
|
-
* #### Undefined Behavior
|
|
515
|
-
* Searching a range which is not sorted in ascending order.
|
|
516
|
-
*
|
|
517
|
-
* A `start` or `stop` which is non-integral.
|
|
518
|
-
*
|
|
519
|
-
* Modifying the length of the array while searching.
|
|
520
|
-
*
|
|
521
|
-
* #### Example
|
|
522
|
-
* ```typescript
|
|
523
|
-
* import { ArrayExt } from '@lumino/algorithm';
|
|
524
|
-
*
|
|
525
|
-
* function numberCmp(a: number, b: number): number {
|
|
526
|
-
* return a - b;
|
|
527
|
-
* }
|
|
528
|
-
*
|
|
529
|
-
* let data = [0, 3, 4, 7, 7, 9];
|
|
530
|
-
* ArrayExt.lowerBound(data, 0, numberCmp); // 0
|
|
531
|
-
* ArrayExt.lowerBound(data, 6, numberCmp); // 3
|
|
532
|
-
* ArrayExt.lowerBound(data, 7, numberCmp); // 3
|
|
533
|
-
* ArrayExt.lowerBound(data, -1, numberCmp); // 0
|
|
534
|
-
* ArrayExt.lowerBound(data, 10, numberCmp); // 6
|
|
535
|
-
* ```
|
|
536
|
-
*/
|
|
537
|
-
function lowerBound(array, value, fn, start, stop) {
|
|
538
|
-
if (start === void 0) { start = 0; }
|
|
539
|
-
if (stop === void 0) { stop = -1; }
|
|
540
|
-
var n = array.length;
|
|
541
|
-
if (n === 0) {
|
|
542
|
-
return 0;
|
|
543
|
-
}
|
|
544
|
-
if (start < 0) {
|
|
545
|
-
start = Math.max(0, start + n);
|
|
546
|
-
}
|
|
547
|
-
else {
|
|
548
|
-
start = Math.min(start, n - 1);
|
|
549
|
-
}
|
|
550
|
-
if (stop < 0) {
|
|
551
|
-
stop = Math.max(0, stop + n);
|
|
552
|
-
}
|
|
553
|
-
else {
|
|
554
|
-
stop = Math.min(stop, n - 1);
|
|
555
|
-
}
|
|
556
|
-
var begin = start;
|
|
557
|
-
var span = stop - start + 1;
|
|
558
|
-
while (span > 0) {
|
|
559
|
-
var half = span >> 1;
|
|
560
|
-
var middle = begin + half;
|
|
561
|
-
if (fn(array[middle], value) < 0) {
|
|
562
|
-
begin = middle + 1;
|
|
563
|
-
span -= half + 1;
|
|
564
|
-
}
|
|
565
|
-
else {
|
|
566
|
-
span = half;
|
|
567
|
-
}
|
|
568
|
-
}
|
|
569
|
-
return begin;
|
|
570
|
-
}
|
|
571
|
-
ArrayExt.lowerBound = lowerBound;
|
|
572
|
-
/**
|
|
573
|
-
* Find the index of the first element which compares `>` than a value.
|
|
574
|
-
*
|
|
575
|
-
* @param array - The sorted array-like object to search.
|
|
576
|
-
*
|
|
577
|
-
* @param value - The value to locate in the array.
|
|
578
|
-
*
|
|
579
|
-
* @param fn - The 3-way comparison function to apply to the values.
|
|
580
|
-
* It should return `< 0` if an element is less than a value, `0` if
|
|
581
|
-
* an element is equal to a value, or `> 0` if an element is greater
|
|
582
|
-
* than a value.
|
|
583
|
-
*
|
|
584
|
-
* @param start - The index of the first element in the range to be
|
|
585
|
-
* searched, inclusive. The default value is `0`. Negative values
|
|
586
|
-
* are taken as an offset from the end of the array.
|
|
587
|
-
*
|
|
588
|
-
* @param stop - The index of the last element in the range to be
|
|
589
|
-
* searched, inclusive. The default value is `-1`. Negative values
|
|
590
|
-
* are taken as an offset from the end of the array.
|
|
591
|
-
*
|
|
592
|
-
* @returns The index of the first element which compares `>` than the
|
|
593
|
-
* value, or `length` if there is no such element. If the computed
|
|
594
|
-
* index for `stop` is less than `start`, then the computed index
|
|
595
|
-
* for `start` is returned.
|
|
596
|
-
*
|
|
597
|
-
* #### Notes
|
|
598
|
-
* The array must already be sorted in ascending order according to
|
|
599
|
-
* the comparison function.
|
|
600
|
-
*
|
|
601
|
-
* #### Complexity
|
|
602
|
-
* Logarithmic.
|
|
603
|
-
*
|
|
604
|
-
* #### Undefined Behavior
|
|
605
|
-
* Searching a range which is not sorted in ascending order.
|
|
606
|
-
*
|
|
607
|
-
* A `start` or `stop` which is non-integral.
|
|
608
|
-
*
|
|
609
|
-
* Modifying the length of the array while searching.
|
|
610
|
-
*
|
|
611
|
-
* #### Example
|
|
612
|
-
* ```typescript
|
|
613
|
-
* import { ArrayExt } from '@lumino/algorithm';
|
|
614
|
-
*
|
|
615
|
-
* function numberCmp(a: number, b: number): number {
|
|
616
|
-
* return a - b;
|
|
617
|
-
* }
|
|
618
|
-
*
|
|
619
|
-
* let data = [0, 3, 4, 7, 7, 9];
|
|
620
|
-
* ArrayExt.upperBound(data, 0, numberCmp); // 1
|
|
621
|
-
* ArrayExt.upperBound(data, 6, numberCmp); // 3
|
|
622
|
-
* ArrayExt.upperBound(data, 7, numberCmp); // 5
|
|
623
|
-
* ArrayExt.upperBound(data, -1, numberCmp); // 0
|
|
624
|
-
* ArrayExt.upperBound(data, 10, numberCmp); // 6
|
|
625
|
-
* ```
|
|
626
|
-
*/
|
|
627
|
-
function upperBound(array, value, fn, start, stop) {
|
|
628
|
-
if (start === void 0) { start = 0; }
|
|
629
|
-
if (stop === void 0) { stop = -1; }
|
|
630
|
-
var n = array.length;
|
|
631
|
-
if (n === 0) {
|
|
632
|
-
return 0;
|
|
633
|
-
}
|
|
634
|
-
if (start < 0) {
|
|
635
|
-
start = Math.max(0, start + n);
|
|
636
|
-
}
|
|
637
|
-
else {
|
|
638
|
-
start = Math.min(start, n - 1);
|
|
639
|
-
}
|
|
640
|
-
if (stop < 0) {
|
|
641
|
-
stop = Math.max(0, stop + n);
|
|
642
|
-
}
|
|
643
|
-
else {
|
|
644
|
-
stop = Math.min(stop, n - 1);
|
|
645
|
-
}
|
|
646
|
-
var begin = start;
|
|
647
|
-
var span = stop - start + 1;
|
|
648
|
-
while (span > 0) {
|
|
649
|
-
var half = span >> 1;
|
|
650
|
-
var middle = begin + half;
|
|
651
|
-
if (fn(array[middle], value) > 0) {
|
|
652
|
-
span = half;
|
|
653
|
-
}
|
|
654
|
-
else {
|
|
655
|
-
begin = middle + 1;
|
|
656
|
-
span -= half + 1;
|
|
657
|
-
}
|
|
658
|
-
}
|
|
659
|
-
return begin;
|
|
660
|
-
}
|
|
661
|
-
ArrayExt.upperBound = upperBound;
|
|
662
|
-
/**
|
|
663
|
-
* Test whether two arrays are shallowly equal.
|
|
664
|
-
*
|
|
665
|
-
* @param a - The first array-like object to compare.
|
|
666
|
-
*
|
|
667
|
-
* @param b - The second array-like object to compare.
|
|
668
|
-
*
|
|
669
|
-
* @param fn - The comparison function to apply to the elements. It
|
|
670
|
-
* should return `true` if the elements are "equal". The default
|
|
671
|
-
* compares elements using strict `===` equality.
|
|
672
|
-
*
|
|
673
|
-
* @returns Whether the two arrays are shallowly equal.
|
|
674
|
-
*
|
|
675
|
-
* #### Complexity
|
|
676
|
-
* Linear.
|
|
677
|
-
*
|
|
678
|
-
* #### Undefined Behavior
|
|
679
|
-
* Modifying the length of the arrays while comparing.
|
|
680
|
-
*
|
|
681
|
-
* #### Example
|
|
682
|
-
* ```typescript
|
|
683
|
-
* import { ArrayExt } from '@lumino/algorithm';
|
|
684
|
-
*
|
|
685
|
-
* let d1 = [0, 3, 4, 7, 7, 9];
|
|
686
|
-
* let d2 = [0, 3, 4, 7, 7, 9];
|
|
687
|
-
* let d3 = [42];
|
|
688
|
-
* ArrayExt.shallowEqual(d1, d2); // true
|
|
689
|
-
* ArrayExt.shallowEqual(d2, d3); // false
|
|
690
|
-
* ```
|
|
691
|
-
*/
|
|
692
|
-
function shallowEqual(a, b, fn) {
|
|
693
|
-
// Check for object identity first.
|
|
694
|
-
if (a === b) {
|
|
695
|
-
return true;
|
|
696
|
-
}
|
|
697
|
-
// Bail early if the lengths are different.
|
|
698
|
-
if (a.length !== b.length) {
|
|
699
|
-
return false;
|
|
700
|
-
}
|
|
701
|
-
// Compare each element for equality.
|
|
702
|
-
for (var i = 0, n = a.length; i < n; ++i) {
|
|
703
|
-
if (fn ? !fn(a[i], b[i]) : a[i] !== b[i]) {
|
|
704
|
-
return false;
|
|
705
|
-
}
|
|
706
|
-
}
|
|
707
|
-
// The array are shallowly equal.
|
|
708
|
-
return true;
|
|
709
|
-
}
|
|
710
|
-
ArrayExt.shallowEqual = shallowEqual;
|
|
711
|
-
/**
|
|
712
|
-
* Create a slice of an array subject to an optional step.
|
|
713
|
-
*
|
|
714
|
-
* @param array - The array-like object of interest.
|
|
715
|
-
*
|
|
716
|
-
* @param options - The options for configuring the slice.
|
|
717
|
-
*
|
|
718
|
-
* @returns A new array with the specified values.
|
|
719
|
-
*
|
|
720
|
-
* @throws An exception if the slice `step` is `0`.
|
|
721
|
-
*
|
|
722
|
-
* #### Complexity
|
|
723
|
-
* Linear.
|
|
724
|
-
*
|
|
725
|
-
* #### Undefined Behavior
|
|
726
|
-
* A `start`, `stop`, or `step` which is non-integral.
|
|
727
|
-
*
|
|
728
|
-
* #### Example
|
|
729
|
-
* ```typescript
|
|
730
|
-
* import { ArrayExt } from '@lumino/algorithm';
|
|
731
|
-
*
|
|
732
|
-
* let data = [0, 3, 4, 7, 7, 9];
|
|
733
|
-
* ArrayExt.slice(data); // [0, 3, 4, 7, 7, 9]
|
|
734
|
-
* ArrayExt.slice(data, { start: 2 }); // [4, 7, 7, 9]
|
|
735
|
-
* ArrayExt.slice(data, { start: 0, stop: 4 }); // [0, 3, 4, 7]
|
|
736
|
-
* ArrayExt.slice(data, { step: 2 }); // [0, 4, 7]
|
|
737
|
-
* ArrayExt.slice(data, { step: -1 }); // [9, 7, 7, 4, 3, 0]
|
|
738
|
-
* ```
|
|
739
|
-
*/
|
|
740
|
-
function slice(array, options) {
|
|
741
|
-
if (options === void 0) { options = {}; }
|
|
742
|
-
// Extract the options.
|
|
743
|
-
var start = options.start, stop = options.stop, step = options.step;
|
|
744
|
-
// Set up the `step` value.
|
|
745
|
-
if (step === undefined) {
|
|
746
|
-
step = 1;
|
|
747
|
-
}
|
|
748
|
-
// Validate the step size.
|
|
749
|
-
if (step === 0) {
|
|
750
|
-
throw new Error('Slice `step` cannot be zero.');
|
|
751
|
-
}
|
|
752
|
-
// Look up the length of the array.
|
|
753
|
-
var n = array.length;
|
|
754
|
-
// Set up the `start` value.
|
|
755
|
-
if (start === undefined) {
|
|
756
|
-
start = step < 0 ? n - 1 : 0;
|
|
757
|
-
}
|
|
758
|
-
else if (start < 0) {
|
|
759
|
-
start = Math.max(start + n, step < 0 ? -1 : 0);
|
|
760
|
-
}
|
|
761
|
-
else if (start >= n) {
|
|
762
|
-
start = step < 0 ? n - 1 : n;
|
|
763
|
-
}
|
|
764
|
-
// Set up the `stop` value.
|
|
765
|
-
if (stop === undefined) {
|
|
766
|
-
stop = step < 0 ? -1 : n;
|
|
767
|
-
}
|
|
768
|
-
else if (stop < 0) {
|
|
769
|
-
stop = Math.max(stop + n, step < 0 ? -1 : 0);
|
|
770
|
-
}
|
|
771
|
-
else if (stop >= n) {
|
|
772
|
-
stop = step < 0 ? n - 1 : n;
|
|
773
|
-
}
|
|
774
|
-
// Compute the slice length.
|
|
775
|
-
var length;
|
|
776
|
-
if ((step < 0 && stop >= start) || (step > 0 && start >= stop)) {
|
|
777
|
-
length = 0;
|
|
778
|
-
}
|
|
779
|
-
else if (step < 0) {
|
|
780
|
-
length = Math.floor((stop - start + 1) / step + 1);
|
|
781
|
-
}
|
|
782
|
-
else {
|
|
783
|
-
length = Math.floor((stop - start - 1) / step + 1);
|
|
784
|
-
}
|
|
785
|
-
// Compute the sliced result.
|
|
786
|
-
var result = [];
|
|
787
|
-
for (var i = 0; i < length; ++i) {
|
|
788
|
-
result[i] = array[start + i * step];
|
|
789
|
-
}
|
|
790
|
-
// Return the result.
|
|
791
|
-
return result;
|
|
792
|
-
}
|
|
793
|
-
ArrayExt.slice = slice;
|
|
794
|
-
/**
|
|
795
|
-
* Move an element in an array from one index to another.
|
|
796
|
-
*
|
|
797
|
-
* @param array - The mutable array-like object of interest.
|
|
798
|
-
*
|
|
799
|
-
* @param fromIndex - The index of the element to move. Negative
|
|
800
|
-
* values are taken as an offset from the end of the array.
|
|
801
|
-
*
|
|
802
|
-
* @param toIndex - The target index of the element. Negative
|
|
803
|
-
* values are taken as an offset from the end of the array.
|
|
804
|
-
*
|
|
805
|
-
* #### Complexity
|
|
806
|
-
* Linear.
|
|
807
|
-
*
|
|
808
|
-
* #### Undefined Behavior
|
|
809
|
-
* A `fromIndex` or `toIndex` which is non-integral.
|
|
810
|
-
*
|
|
811
|
-
* #### Example
|
|
812
|
-
* ```typescript
|
|
813
|
-
* import { ArrayExt } from from '@lumino/algorithm';
|
|
814
|
-
*
|
|
815
|
-
* let data = [0, 1, 2, 3, 4];
|
|
816
|
-
* ArrayExt.move(data, 1, 2); // [0, 2, 1, 3, 4]
|
|
817
|
-
* ArrayExt.move(data, 4, 2); // [0, 2, 4, 1, 3]
|
|
818
|
-
* ```
|
|
819
|
-
*/
|
|
820
|
-
function move(array, fromIndex, toIndex) {
|
|
821
|
-
var n = array.length;
|
|
822
|
-
if (n <= 1) {
|
|
823
|
-
return;
|
|
824
|
-
}
|
|
825
|
-
if (fromIndex < 0) {
|
|
826
|
-
fromIndex = Math.max(0, fromIndex + n);
|
|
827
|
-
}
|
|
828
|
-
else {
|
|
829
|
-
fromIndex = Math.min(fromIndex, n - 1);
|
|
830
|
-
}
|
|
831
|
-
if (toIndex < 0) {
|
|
832
|
-
toIndex = Math.max(0, toIndex + n);
|
|
833
|
-
}
|
|
834
|
-
else {
|
|
835
|
-
toIndex = Math.min(toIndex, n - 1);
|
|
836
|
-
}
|
|
837
|
-
if (fromIndex === toIndex) {
|
|
838
|
-
return;
|
|
839
|
-
}
|
|
840
|
-
var value = array[fromIndex];
|
|
841
|
-
var d = fromIndex < toIndex ? 1 : -1;
|
|
842
|
-
for (var i = fromIndex; i !== toIndex; i += d) {
|
|
843
|
-
array[i] = array[i + d];
|
|
844
|
-
}
|
|
845
|
-
array[toIndex] = value;
|
|
846
|
-
}
|
|
847
|
-
ArrayExt.move = move;
|
|
848
|
-
/**
|
|
849
|
-
* Reverse an array in-place.
|
|
850
|
-
*
|
|
851
|
-
* @param array - The mutable array-like object of interest.
|
|
852
|
-
*
|
|
853
|
-
* @param start - The index of the first element in the range to be
|
|
854
|
-
* reversed, inclusive. The default value is `0`. Negative values
|
|
855
|
-
* are taken as an offset from the end of the array.
|
|
856
|
-
*
|
|
857
|
-
* @param stop - The index of the last element in the range to be
|
|
858
|
-
* reversed, inclusive. The default value is `-1`. Negative values
|
|
859
|
-
* are taken as an offset from the end of the array.
|
|
860
|
-
*
|
|
861
|
-
* #### Complexity
|
|
862
|
-
* Linear.
|
|
863
|
-
*
|
|
864
|
-
* #### Undefined Behavior
|
|
865
|
-
* A `start` or `stop` index which is non-integral.
|
|
866
|
-
*
|
|
867
|
-
* #### Example
|
|
868
|
-
* ```typescript
|
|
869
|
-
* import { ArrayExt } from '@lumino/algorithm';
|
|
870
|
-
*
|
|
871
|
-
* let data = [0, 1, 2, 3, 4];
|
|
872
|
-
* ArrayExt.reverse(data, 1, 3); // [0, 3, 2, 1, 4]
|
|
873
|
-
* ArrayExt.reverse(data, 3); // [0, 3, 2, 4, 1]
|
|
874
|
-
* ArrayExt.reverse(data); // [1, 4, 2, 3, 0]
|
|
875
|
-
* ```
|
|
876
|
-
*/
|
|
877
|
-
function reverse(array, start, stop) {
|
|
878
|
-
if (start === void 0) { start = 0; }
|
|
879
|
-
if (stop === void 0) { stop = -1; }
|
|
880
|
-
var n = array.length;
|
|
881
|
-
if (n <= 1) {
|
|
882
|
-
return;
|
|
883
|
-
}
|
|
884
|
-
if (start < 0) {
|
|
885
|
-
start = Math.max(0, start + n);
|
|
886
|
-
}
|
|
887
|
-
else {
|
|
888
|
-
start = Math.min(start, n - 1);
|
|
889
|
-
}
|
|
890
|
-
if (stop < 0) {
|
|
891
|
-
stop = Math.max(0, stop + n);
|
|
892
|
-
}
|
|
893
|
-
else {
|
|
894
|
-
stop = Math.min(stop, n - 1);
|
|
895
|
-
}
|
|
896
|
-
while (start < stop) {
|
|
897
|
-
var a = array[start];
|
|
898
|
-
var b = array[stop];
|
|
899
|
-
array[start++] = b;
|
|
900
|
-
array[stop--] = a;
|
|
901
|
-
}
|
|
902
|
-
}
|
|
903
|
-
ArrayExt.reverse = reverse;
|
|
904
|
-
/**
|
|
905
|
-
* Rotate the elements of an array in-place.
|
|
906
|
-
*
|
|
907
|
-
* @param array - The mutable array-like object of interest.
|
|
908
|
-
*
|
|
909
|
-
* @param delta - The amount of rotation to apply to the elements. A
|
|
910
|
-
* positive value will rotate the elements to the left. A negative
|
|
911
|
-
* value will rotate the elements to the right.
|
|
912
|
-
*
|
|
913
|
-
* @param start - The index of the first element in the range to be
|
|
914
|
-
* rotated, inclusive. The default value is `0`. Negative values
|
|
915
|
-
* are taken as an offset from the end of the array.
|
|
916
|
-
*
|
|
917
|
-
* @param stop - The index of the last element in the range to be
|
|
918
|
-
* rotated, inclusive. The default value is `-1`. Negative values
|
|
919
|
-
* are taken as an offset from the end of the array.
|
|
920
|
-
*
|
|
921
|
-
* #### Complexity
|
|
922
|
-
* Linear.
|
|
923
|
-
*
|
|
924
|
-
* #### Undefined Behavior
|
|
925
|
-
* A `delta`, `start`, or `stop` which is non-integral.
|
|
926
|
-
*
|
|
927
|
-
* #### Example
|
|
928
|
-
* ```typescript
|
|
929
|
-
* import { ArrayExt } from '@lumino/algorithm';
|
|
930
|
-
*
|
|
931
|
-
* let data = [0, 1, 2, 3, 4];
|
|
932
|
-
* ArrayExt.rotate(data, 2); // [2, 3, 4, 0, 1]
|
|
933
|
-
* ArrayExt.rotate(data, -2); // [0, 1, 2, 3, 4]
|
|
934
|
-
* ArrayExt.rotate(data, 10); // [0, 1, 2, 3, 4]
|
|
935
|
-
* ArrayExt.rotate(data, 9); // [4, 0, 1, 2, 3]
|
|
936
|
-
* ArrayExt.rotate(data, 2, 1, 3); // [4, 2, 0, 1, 3]
|
|
937
|
-
* ```
|
|
938
|
-
*/
|
|
939
|
-
function rotate(array, delta, start, stop) {
|
|
940
|
-
if (start === void 0) { start = 0; }
|
|
941
|
-
if (stop === void 0) { stop = -1; }
|
|
942
|
-
var n = array.length;
|
|
943
|
-
if (n <= 1) {
|
|
944
|
-
return;
|
|
945
|
-
}
|
|
946
|
-
if (start < 0) {
|
|
947
|
-
start = Math.max(0, start + n);
|
|
948
|
-
}
|
|
949
|
-
else {
|
|
950
|
-
start = Math.min(start, n - 1);
|
|
951
|
-
}
|
|
952
|
-
if (stop < 0) {
|
|
953
|
-
stop = Math.max(0, stop + n);
|
|
954
|
-
}
|
|
955
|
-
else {
|
|
956
|
-
stop = Math.min(stop, n - 1);
|
|
957
|
-
}
|
|
958
|
-
if (start >= stop) {
|
|
959
|
-
return;
|
|
960
|
-
}
|
|
961
|
-
var length = stop - start + 1;
|
|
962
|
-
if (delta > 0) {
|
|
963
|
-
delta = delta % length;
|
|
964
|
-
}
|
|
965
|
-
else if (delta < 0) {
|
|
966
|
-
delta = ((delta % length) + length) % length;
|
|
967
|
-
}
|
|
968
|
-
if (delta === 0) {
|
|
969
|
-
return;
|
|
970
|
-
}
|
|
971
|
-
var pivot = start + delta;
|
|
972
|
-
reverse(array, start, pivot - 1);
|
|
973
|
-
reverse(array, pivot, stop);
|
|
974
|
-
reverse(array, start, stop);
|
|
975
|
-
}
|
|
976
|
-
ArrayExt.rotate = rotate;
|
|
977
|
-
/**
|
|
978
|
-
* Fill an array with a static value.
|
|
979
|
-
*
|
|
980
|
-
* @param array - The mutable array-like object to fill.
|
|
981
|
-
*
|
|
982
|
-
* @param value - The static value to use to fill the array.
|
|
983
|
-
*
|
|
984
|
-
* @param start - The index of the first element in the range to be
|
|
985
|
-
* filled, inclusive. The default value is `0`. Negative values
|
|
986
|
-
* are taken as an offset from the end of the array.
|
|
987
|
-
*
|
|
988
|
-
* @param stop - The index of the last element in the range to be
|
|
989
|
-
* filled, inclusive. The default value is `-1`. Negative values
|
|
990
|
-
* are taken as an offset from the end of the array.
|
|
991
|
-
*
|
|
992
|
-
* #### Notes
|
|
993
|
-
* If `stop < start` the fill will wrap at the end of the array.
|
|
994
|
-
*
|
|
995
|
-
* #### Complexity
|
|
996
|
-
* Linear.
|
|
997
|
-
*
|
|
998
|
-
* #### Undefined Behavior
|
|
999
|
-
* A `start` or `stop` which is non-integral.
|
|
1000
|
-
*
|
|
1001
|
-
* #### Example
|
|
1002
|
-
* ```typescript
|
|
1003
|
-
* import { ArrayExt } from '@lumino/algorithm';
|
|
1004
|
-
*
|
|
1005
|
-
* let data = ['one', 'two', 'three', 'four'];
|
|
1006
|
-
* ArrayExt.fill(data, 'r'); // ['r', 'r', 'r', 'r']
|
|
1007
|
-
* ArrayExt.fill(data, 'g', 1); // ['r', 'g', 'g', 'g']
|
|
1008
|
-
* ArrayExt.fill(data, 'b', 2, 3); // ['r', 'g', 'b', 'b']
|
|
1009
|
-
* ArrayExt.fill(data, 'z', 3, 1); // ['z', 'z', 'b', 'z']
|
|
1010
|
-
* ```
|
|
1011
|
-
*/
|
|
1012
|
-
function fill(array, value, start, stop) {
|
|
1013
|
-
if (start === void 0) { start = 0; }
|
|
1014
|
-
if (stop === void 0) { stop = -1; }
|
|
1015
|
-
var n = array.length;
|
|
1016
|
-
if (n === 0) {
|
|
1017
|
-
return;
|
|
1018
|
-
}
|
|
1019
|
-
if (start < 0) {
|
|
1020
|
-
start = Math.max(0, start + n);
|
|
1021
|
-
}
|
|
1022
|
-
else {
|
|
1023
|
-
start = Math.min(start, n - 1);
|
|
1024
|
-
}
|
|
1025
|
-
if (stop < 0) {
|
|
1026
|
-
stop = Math.max(0, stop + n);
|
|
1027
|
-
}
|
|
1028
|
-
else {
|
|
1029
|
-
stop = Math.min(stop, n - 1);
|
|
1030
|
-
}
|
|
1031
|
-
var span;
|
|
1032
|
-
if (stop < start) {
|
|
1033
|
-
span = stop + 1 + (n - start);
|
|
1034
|
-
}
|
|
1035
|
-
else {
|
|
1036
|
-
span = stop - start + 1;
|
|
1037
|
-
}
|
|
1038
|
-
for (var i = 0; i < span; ++i) {
|
|
1039
|
-
array[(start + i) % n] = value;
|
|
1040
|
-
}
|
|
1041
|
-
}
|
|
1042
|
-
ArrayExt.fill = fill;
|
|
1043
|
-
/**
|
|
1044
|
-
* Insert a value into an array at a specific index.
|
|
1045
|
-
*
|
|
1046
|
-
* @param array - The array of interest.
|
|
1047
|
-
*
|
|
1048
|
-
* @param index - The index at which to insert the value. Negative
|
|
1049
|
-
* values are taken as an offset from the end of the array.
|
|
1050
|
-
*
|
|
1051
|
-
* @param value - The value to set at the specified index.
|
|
1052
|
-
*
|
|
1053
|
-
* #### Complexity
|
|
1054
|
-
* Linear.
|
|
1055
|
-
*
|
|
1056
|
-
* #### Undefined Behavior
|
|
1057
|
-
* An `index` which is non-integral.
|
|
1058
|
-
*
|
|
1059
|
-
* #### Example
|
|
1060
|
-
* ```typescript
|
|
1061
|
-
* import { ArrayExt } from '@lumino/algorithm';
|
|
1062
|
-
*
|
|
1063
|
-
* let data = [0, 1, 2];
|
|
1064
|
-
* ArrayExt.insert(data, 0, -1); // [-1, 0, 1, 2]
|
|
1065
|
-
* ArrayExt.insert(data, 2, 12); // [-1, 0, 12, 1, 2]
|
|
1066
|
-
* ArrayExt.insert(data, -1, 7); // [-1, 0, 12, 1, 7, 2]
|
|
1067
|
-
* ArrayExt.insert(data, 6, 19); // [-1, 0, 12, 1, 7, 2, 19]
|
|
1068
|
-
* ```
|
|
1069
|
-
*/
|
|
1070
|
-
function insert(array, index, value) {
|
|
1071
|
-
var n = array.length;
|
|
1072
|
-
if (index < 0) {
|
|
1073
|
-
index = Math.max(0, index + n);
|
|
1074
|
-
}
|
|
1075
|
-
else {
|
|
1076
|
-
index = Math.min(index, n);
|
|
1077
|
-
}
|
|
1078
|
-
for (var i = n; i > index; --i) {
|
|
1079
|
-
array[i] = array[i - 1];
|
|
1080
|
-
}
|
|
1081
|
-
array[index] = value;
|
|
1082
|
-
}
|
|
1083
|
-
ArrayExt.insert = insert;
|
|
1084
|
-
/**
|
|
1085
|
-
* Remove and return a value at a specific index in an array.
|
|
1086
|
-
*
|
|
1087
|
-
* @param array - The array of interest.
|
|
1088
|
-
*
|
|
1089
|
-
* @param index - The index of the value to remove. Negative values
|
|
1090
|
-
* are taken as an offset from the end of the array.
|
|
1091
|
-
*
|
|
1092
|
-
* @returns The value at the specified index, or `undefined` if the
|
|
1093
|
-
* index is out of range.
|
|
1094
|
-
*
|
|
1095
|
-
* #### Complexity
|
|
1096
|
-
* Linear.
|
|
1097
|
-
*
|
|
1098
|
-
* #### Undefined Behavior
|
|
1099
|
-
* An `index` which is non-integral.
|
|
1100
|
-
*
|
|
1101
|
-
* #### Example
|
|
1102
|
-
* ```typescript
|
|
1103
|
-
* import { ArrayExt } from '@lumino/algorithm';
|
|
1104
|
-
*
|
|
1105
|
-
* let data = [0, 12, 23, 39, 14, 12, 75];
|
|
1106
|
-
* ArrayExt.removeAt(data, 2); // 23
|
|
1107
|
-
* ArrayExt.removeAt(data, -2); // 12
|
|
1108
|
-
* ArrayExt.removeAt(data, 10); // undefined;
|
|
1109
|
-
* ```
|
|
1110
|
-
*/
|
|
1111
|
-
function removeAt(array, index) {
|
|
1112
|
-
var n = array.length;
|
|
1113
|
-
if (index < 0) {
|
|
1114
|
-
index += n;
|
|
1115
|
-
}
|
|
1116
|
-
if (index < 0 || index >= n) {
|
|
1117
|
-
return undefined;
|
|
1118
|
-
}
|
|
1119
|
-
var value = array[index];
|
|
1120
|
-
for (var i = index + 1; i < n; ++i) {
|
|
1121
|
-
array[i - 1] = array[i];
|
|
1122
|
-
}
|
|
1123
|
-
array.length = n - 1;
|
|
1124
|
-
return value;
|
|
1125
|
-
}
|
|
1126
|
-
ArrayExt.removeAt = removeAt;
|
|
1127
|
-
/**
|
|
1128
|
-
* Remove the first occurrence of a value from an array.
|
|
1129
|
-
*
|
|
1130
|
-
* @param array - The array of interest.
|
|
1131
|
-
*
|
|
1132
|
-
* @param value - The value to remove from the array. Values are
|
|
1133
|
-
* compared using strict `===` equality.
|
|
1134
|
-
*
|
|
1135
|
-
* @param start - The index of the first element in the range to be
|
|
1136
|
-
* searched, inclusive. The default value is `0`. Negative values
|
|
1137
|
-
* are taken as an offset from the end of the array.
|
|
1138
|
-
*
|
|
1139
|
-
* @param stop - The index of the last element in the range to be
|
|
1140
|
-
* searched, inclusive. The default value is `-1`. Negative values
|
|
1141
|
-
* are taken as an offset from the end of the array.
|
|
1142
|
-
*
|
|
1143
|
-
* @returns The index of the removed value, or `-1` if the value
|
|
1144
|
-
* is not contained in the array.
|
|
1145
|
-
*
|
|
1146
|
-
* #### Notes
|
|
1147
|
-
* If `stop < start` the search will wrap at the end of the array.
|
|
1148
|
-
*
|
|
1149
|
-
* #### Complexity
|
|
1150
|
-
* Linear.
|
|
1151
|
-
*
|
|
1152
|
-
* #### Example
|
|
1153
|
-
* ```typescript
|
|
1154
|
-
* import { ArrayExt } from '@lumino/algorithm';
|
|
1155
|
-
*
|
|
1156
|
-
* let data = [0, 12, 23, 39, 14, 12, 75];
|
|
1157
|
-
* ArrayExt.removeFirstOf(data, 12); // 1
|
|
1158
|
-
* ArrayExt.removeFirstOf(data, 17); // -1
|
|
1159
|
-
* ArrayExt.removeFirstOf(data, 39, 3); // -1
|
|
1160
|
-
* ArrayExt.removeFirstOf(data, 39, 3, 2); // 2
|
|
1161
|
-
* ```
|
|
1162
|
-
*/
|
|
1163
|
-
function removeFirstOf(array, value, start, stop) {
|
|
1164
|
-
if (start === void 0) { start = 0; }
|
|
1165
|
-
if (stop === void 0) { stop = -1; }
|
|
1166
|
-
var index = firstIndexOf(array, value, start, stop);
|
|
1167
|
-
if (index !== -1) {
|
|
1168
|
-
removeAt(array, index);
|
|
1169
|
-
}
|
|
1170
|
-
return index;
|
|
1171
|
-
}
|
|
1172
|
-
ArrayExt.removeFirstOf = removeFirstOf;
|
|
1173
|
-
/**
|
|
1174
|
-
* Remove the last occurrence of a value from an array.
|
|
1175
|
-
*
|
|
1176
|
-
* @param array - The array of interest.
|
|
1177
|
-
*
|
|
1178
|
-
* @param value - The value to remove from the array. Values are
|
|
1179
|
-
* compared using strict `===` equality.
|
|
1180
|
-
*
|
|
1181
|
-
* @param start - The index of the first element in the range to be
|
|
1182
|
-
* searched, inclusive. The default value is `-1`. Negative values
|
|
1183
|
-
* are taken as an offset from the end of the array.
|
|
1184
|
-
*
|
|
1185
|
-
* @param stop - The index of the last element in the range to be
|
|
1186
|
-
* searched, inclusive. The default value is `0`. Negative values
|
|
1187
|
-
* are taken as an offset from the end of the array.
|
|
1188
|
-
*
|
|
1189
|
-
* @returns The index of the removed value, or `-1` if the value
|
|
1190
|
-
* is not contained in the array.
|
|
1191
|
-
*
|
|
1192
|
-
* #### Notes
|
|
1193
|
-
* If `start < stop` the search will wrap at the end of the array.
|
|
1194
|
-
*
|
|
1195
|
-
* #### Complexity
|
|
1196
|
-
* Linear.
|
|
1197
|
-
*
|
|
1198
|
-
* #### Example
|
|
1199
|
-
* ```typescript
|
|
1200
|
-
* import { ArrayExt } from '@lumino/algorithm';
|
|
1201
|
-
*
|
|
1202
|
-
* let data = [0, 12, 23, 39, 14, 12, 75];
|
|
1203
|
-
* ArrayExt.removeLastOf(data, 12); // 5
|
|
1204
|
-
* ArrayExt.removeLastOf(data, 17); // -1
|
|
1205
|
-
* ArrayExt.removeLastOf(data, 39, 2); // -1
|
|
1206
|
-
* ArrayExt.removeLastOf(data, 39, 2, 3); // 3
|
|
1207
|
-
* ```
|
|
1208
|
-
*/
|
|
1209
|
-
function removeLastOf(array, value, start, stop) {
|
|
1210
|
-
if (start === void 0) { start = -1; }
|
|
1211
|
-
if (stop === void 0) { stop = 0; }
|
|
1212
|
-
var index = lastIndexOf(array, value, start, stop);
|
|
1213
|
-
if (index !== -1) {
|
|
1214
|
-
removeAt(array, index);
|
|
1215
|
-
}
|
|
1216
|
-
return index;
|
|
1217
|
-
}
|
|
1218
|
-
ArrayExt.removeLastOf = removeLastOf;
|
|
1219
|
-
/**
|
|
1220
|
-
* Remove all occurrences of a value from an array.
|
|
1221
|
-
*
|
|
1222
|
-
* @param array - The array of interest.
|
|
1223
|
-
*
|
|
1224
|
-
* @param value - The value to remove from the array. Values are
|
|
1225
|
-
* compared using strict `===` equality.
|
|
1226
|
-
*
|
|
1227
|
-
* @param start - The index of the first element in the range to be
|
|
1228
|
-
* searched, inclusive. The default value is `0`. Negative values
|
|
1229
|
-
* are taken as an offset from the end of the array.
|
|
1230
|
-
*
|
|
1231
|
-
* @param stop - The index of the last element in the range to be
|
|
1232
|
-
* searched, inclusive. The default value is `-1`. Negative values
|
|
1233
|
-
* are taken as an offset from the end of the array.
|
|
1234
|
-
*
|
|
1235
|
-
* @returns The number of elements removed from the array.
|
|
1236
|
-
*
|
|
1237
|
-
* #### Notes
|
|
1238
|
-
* If `stop < start` the search will conceptually wrap at the end of
|
|
1239
|
-
* the array, however the array will be traversed front-to-back.
|
|
1240
|
-
*
|
|
1241
|
-
* #### Complexity
|
|
1242
|
-
* Linear.
|
|
1243
|
-
*
|
|
1244
|
-
* #### Example
|
|
1245
|
-
* ```typescript
|
|
1246
|
-
* import { ArrayExt } from '@lumino/algorithm';
|
|
1247
|
-
*
|
|
1248
|
-
* let data = [14, 12, 23, 39, 14, 12, 19, 14];
|
|
1249
|
-
* ArrayExt.removeAllOf(data, 12); // 2
|
|
1250
|
-
* ArrayExt.removeAllOf(data, 17); // 0
|
|
1251
|
-
* ArrayExt.removeAllOf(data, 14, 1, 4); // 1
|
|
1252
|
-
* ```
|
|
1253
|
-
*/
|
|
1254
|
-
function removeAllOf(array, value, start, stop) {
|
|
1255
|
-
if (start === void 0) { start = 0; }
|
|
1256
|
-
if (stop === void 0) { stop = -1; }
|
|
1257
|
-
var n = array.length;
|
|
1258
|
-
if (n === 0) {
|
|
1259
|
-
return 0;
|
|
1260
|
-
}
|
|
1261
|
-
if (start < 0) {
|
|
1262
|
-
start = Math.max(0, start + n);
|
|
1263
|
-
}
|
|
1264
|
-
else {
|
|
1265
|
-
start = Math.min(start, n - 1);
|
|
1266
|
-
}
|
|
1267
|
-
if (stop < 0) {
|
|
1268
|
-
stop = Math.max(0, stop + n);
|
|
1269
|
-
}
|
|
1270
|
-
else {
|
|
1271
|
-
stop = Math.min(stop, n - 1);
|
|
1272
|
-
}
|
|
1273
|
-
var count = 0;
|
|
1274
|
-
for (var i = 0; i < n; ++i) {
|
|
1275
|
-
if (start <= stop && i >= start && i <= stop && array[i] === value) {
|
|
1276
|
-
count++;
|
|
1277
|
-
}
|
|
1278
|
-
else if (stop < start &&
|
|
1279
|
-
(i <= stop || i >= start) &&
|
|
1280
|
-
array[i] === value) {
|
|
1281
|
-
count++;
|
|
1282
|
-
}
|
|
1283
|
-
else if (count > 0) {
|
|
1284
|
-
array[i - count] = array[i];
|
|
1285
|
-
}
|
|
1286
|
-
}
|
|
1287
|
-
if (count > 0) {
|
|
1288
|
-
array.length = n - count;
|
|
1289
|
-
}
|
|
1290
|
-
return count;
|
|
1291
|
-
}
|
|
1292
|
-
ArrayExt.removeAllOf = removeAllOf;
|
|
1293
|
-
/**
|
|
1294
|
-
* Remove the first occurrence of a value which matches a predicate.
|
|
1295
|
-
*
|
|
1296
|
-
* @param array - The array of interest.
|
|
1297
|
-
*
|
|
1298
|
-
* @param fn - The predicate function to apply to the values.
|
|
1299
|
-
*
|
|
1300
|
-
* @param start - The index of the first element in the range to be
|
|
1301
|
-
* searched, inclusive. The default value is `0`. Negative values
|
|
1302
|
-
* are taken as an offset from the end of the array.
|
|
1303
|
-
*
|
|
1304
|
-
* @param stop - The index of the last element in the range to be
|
|
1305
|
-
* searched, inclusive. The default value is `-1`. Negative values
|
|
1306
|
-
* are taken as an offset from the end of the array.
|
|
1307
|
-
*
|
|
1308
|
-
* @returns The removed `{ index, value }`, which will be `-1` and
|
|
1309
|
-
* `undefined` if the value is not contained in the array.
|
|
1310
|
-
*
|
|
1311
|
-
* #### Notes
|
|
1312
|
-
* If `stop < start` the search will wrap at the end of the array.
|
|
1313
|
-
*
|
|
1314
|
-
* #### Complexity
|
|
1315
|
-
* Linear.
|
|
1316
|
-
*
|
|
1317
|
-
* #### Example
|
|
1318
|
-
* ```typescript
|
|
1319
|
-
* import { ArrayExt } from '@lumino/algorithm';
|
|
1320
|
-
*
|
|
1321
|
-
* function isEven(value: number): boolean {
|
|
1322
|
-
* return value % 2 === 0;
|
|
1323
|
-
* }
|
|
1324
|
-
*
|
|
1325
|
-
* let data = [0, 12, 23, 39, 14, 12, 75];
|
|
1326
|
-
* ArrayExt.removeFirstWhere(data, isEven); // { index: 0, value: 0 }
|
|
1327
|
-
* ArrayExt.removeFirstWhere(data, isEven, 2); // { index: 3, value: 14 }
|
|
1328
|
-
* ArrayExt.removeFirstWhere(data, isEven, 4); // { index: -1, value: undefined }
|
|
1329
|
-
* ```
|
|
1330
|
-
*/
|
|
1331
|
-
function removeFirstWhere(array, fn, start, stop) {
|
|
1332
|
-
if (start === void 0) { start = 0; }
|
|
1333
|
-
if (stop === void 0) { stop = -1; }
|
|
1334
|
-
var value;
|
|
1335
|
-
var index = findFirstIndex(array, fn, start, stop);
|
|
1336
|
-
if (index !== -1) {
|
|
1337
|
-
value = removeAt(array, index);
|
|
1338
|
-
}
|
|
1339
|
-
return { index: index, value: value };
|
|
1340
|
-
}
|
|
1341
|
-
ArrayExt.removeFirstWhere = removeFirstWhere;
|
|
1342
|
-
/**
|
|
1343
|
-
* Remove the last occurrence of a value which matches a predicate.
|
|
1344
|
-
*
|
|
1345
|
-
* @param array - The array of interest.
|
|
1346
|
-
*
|
|
1347
|
-
* @param fn - The predicate function to apply to the values.
|
|
1348
|
-
*
|
|
1349
|
-
* @param start - The index of the first element in the range to be
|
|
1350
|
-
* searched, inclusive. The default value is `-1`. Negative values
|
|
1351
|
-
* are taken as an offset from the end of the array.
|
|
1352
|
-
*
|
|
1353
|
-
* @param stop - The index of the last element in the range to be
|
|
1354
|
-
* searched, inclusive. The default value is `0`. Negative values
|
|
1355
|
-
* are taken as an offset from the end of the array.
|
|
1356
|
-
*
|
|
1357
|
-
* @returns The removed `{ index, value }`, which will be `-1` and
|
|
1358
|
-
* `undefined` if the value is not contained in the array.
|
|
1359
|
-
*
|
|
1360
|
-
* #### Notes
|
|
1361
|
-
* If `start < stop` the search will wrap at the end of the array.
|
|
1362
|
-
*
|
|
1363
|
-
* #### Complexity
|
|
1364
|
-
* Linear.
|
|
1365
|
-
*
|
|
1366
|
-
* #### Example
|
|
1367
|
-
* ```typescript
|
|
1368
|
-
* import { ArrayExt } from '@lumino/algorithm';
|
|
1369
|
-
*
|
|
1370
|
-
* function isEven(value: number): boolean {
|
|
1371
|
-
* return value % 2 === 0;
|
|
1372
|
-
* }
|
|
1373
|
-
*
|
|
1374
|
-
* let data = [0, 12, 23, 39, 14, 12, 75];
|
|
1375
|
-
* ArrayExt.removeLastWhere(data, isEven); // { index: 5, value: 12 }
|
|
1376
|
-
* ArrayExt.removeLastWhere(data, isEven, 2); // { index: 1, value: 12 }
|
|
1377
|
-
* ArrayExt.removeLastWhere(data, isEven, 2, 1); // { index: -1, value: undefined }
|
|
1378
|
-
* ```
|
|
1379
|
-
*/
|
|
1380
|
-
function removeLastWhere(array, fn, start, stop) {
|
|
1381
|
-
if (start === void 0) { start = -1; }
|
|
1382
|
-
if (stop === void 0) { stop = 0; }
|
|
1383
|
-
var value;
|
|
1384
|
-
var index = findLastIndex(array, fn, start, stop);
|
|
1385
|
-
if (index !== -1) {
|
|
1386
|
-
value = removeAt(array, index);
|
|
1387
|
-
}
|
|
1388
|
-
return { index: index, value: value };
|
|
1389
|
-
}
|
|
1390
|
-
ArrayExt.removeLastWhere = removeLastWhere;
|
|
1391
|
-
/**
|
|
1392
|
-
* Remove all occurrences of values which match a predicate.
|
|
1393
|
-
*
|
|
1394
|
-
* @param array - The array of interest.
|
|
1395
|
-
*
|
|
1396
|
-
* @param fn - The predicate function to apply to the values.
|
|
1397
|
-
*
|
|
1398
|
-
* @param start - The index of the first element in the range to be
|
|
1399
|
-
* searched, inclusive. The default value is `0`. Negative values
|
|
1400
|
-
* are taken as an offset from the end of the array.
|
|
1401
|
-
*
|
|
1402
|
-
* @param stop - The index of the last element in the range to be
|
|
1403
|
-
* searched, inclusive. The default value is `-1`. Negative values
|
|
1404
|
-
* are taken as an offset from the end of the array.
|
|
1405
|
-
*
|
|
1406
|
-
* @returns The number of elements removed from the array.
|
|
1407
|
-
*
|
|
1408
|
-
* #### Notes
|
|
1409
|
-
* If `stop < start` the search will conceptually wrap at the end of
|
|
1410
|
-
* the array, however the array will be traversed front-to-back.
|
|
1411
|
-
*
|
|
1412
|
-
* #### Complexity
|
|
1413
|
-
* Linear.
|
|
1414
|
-
*
|
|
1415
|
-
* #### Example
|
|
1416
|
-
* ```typescript
|
|
1417
|
-
* import { ArrayExt } from '@lumino/algorithm';
|
|
1418
|
-
*
|
|
1419
|
-
* function isEven(value: number): boolean {
|
|
1420
|
-
* return value % 2 === 0;
|
|
1421
|
-
* }
|
|
1422
|
-
*
|
|
1423
|
-
* function isNegative(value: number): boolean {
|
|
1424
|
-
* return value < 0;
|
|
1425
|
-
* }
|
|
1426
|
-
*
|
|
1427
|
-
* let data = [0, 12, -13, -9, 23, 39, 14, -15, 12, 75];
|
|
1428
|
-
* ArrayExt.removeAllWhere(data, isEven); // 4
|
|
1429
|
-
* ArrayExt.removeAllWhere(data, isNegative, 0, 3); // 2
|
|
1430
|
-
* ```
|
|
1431
|
-
*/
|
|
1432
|
-
function removeAllWhere(array, fn, start, stop) {
|
|
1433
|
-
if (start === void 0) { start = 0; }
|
|
1434
|
-
if (stop === void 0) { stop = -1; }
|
|
1435
|
-
var n = array.length;
|
|
1436
|
-
if (n === 0) {
|
|
1437
|
-
return 0;
|
|
1438
|
-
}
|
|
1439
|
-
if (start < 0) {
|
|
1440
|
-
start = Math.max(0, start + n);
|
|
1441
|
-
}
|
|
1442
|
-
else {
|
|
1443
|
-
start = Math.min(start, n - 1);
|
|
1444
|
-
}
|
|
1445
|
-
if (stop < 0) {
|
|
1446
|
-
stop = Math.max(0, stop + n);
|
|
1447
|
-
}
|
|
1448
|
-
else {
|
|
1449
|
-
stop = Math.min(stop, n - 1);
|
|
1450
|
-
}
|
|
1451
|
-
var count = 0;
|
|
1452
|
-
for (var i = 0; i < n; ++i) {
|
|
1453
|
-
if (start <= stop && i >= start && i <= stop && fn(array[i], i)) {
|
|
1454
|
-
count++;
|
|
1455
|
-
}
|
|
1456
|
-
else if (stop < start && (i <= stop || i >= start) && fn(array[i], i)) {
|
|
1457
|
-
count++;
|
|
1458
|
-
}
|
|
1459
|
-
else if (count > 0) {
|
|
1460
|
-
array[i - count] = array[i];
|
|
1461
|
-
}
|
|
1462
|
-
}
|
|
1463
|
-
if (count > 0) {
|
|
1464
|
-
array.length = n - count;
|
|
1465
|
-
}
|
|
1466
|
-
return count;
|
|
1467
|
-
}
|
|
1468
|
-
ArrayExt.removeAllWhere = removeAllWhere;
|
|
1469
|
-
})(ArrayExt || (ArrayExt = {}));
|
|
1470
|
-
|
|
1471
|
-
// Copyright (c) Jupyter Development Team.
|
|
1472
|
-
// Distributed under the terms of the Modified BSD License.
|
|
1473
|
-
/*-----------------------------------------------------------------------------
|
|
1474
|
-
| Copyright (c) 2014-2017, PhosphorJS Contributors
|
|
1475
|
-
|
|
|
1476
|
-
| Distributed under the terms of the BSD 3-Clause License.
|
|
1477
|
-
|
|
|
1478
|
-
| The full license is in the file LICENSE, distributed with this software.
|
|
1479
|
-
|----------------------------------------------------------------------------*/
|
|
1480
|
-
/**
|
|
1481
|
-
* Create an iterator for an iterable object.
|
|
1482
|
-
*
|
|
1483
|
-
* @param object - The iterable or array-like object of interest.
|
|
1484
|
-
*
|
|
1485
|
-
* @returns A new iterator for the given object.
|
|
1486
|
-
*
|
|
1487
|
-
* #### Notes
|
|
1488
|
-
* This function allows iteration algorithms to operate on user-defined
|
|
1489
|
-
* iterable types and builtin array-like objects in a uniform fashion.
|
|
1490
|
-
*/
|
|
1491
|
-
function iter(object) {
|
|
1492
|
-
var it;
|
|
1493
|
-
if (typeof object.iter === 'function') {
|
|
1494
|
-
it = object.iter();
|
|
1495
|
-
}
|
|
1496
|
-
else {
|
|
1497
|
-
it = new ArrayIterator(object);
|
|
1498
|
-
}
|
|
1499
|
-
return it;
|
|
1500
|
-
}
|
|
1501
|
-
/**
|
|
1502
|
-
* Create an iterator for the keys in an object.
|
|
1503
|
-
*
|
|
1504
|
-
* @param object - The object of interest.
|
|
1505
|
-
*
|
|
1506
|
-
* @returns A new iterator for the keys in the given object.
|
|
1507
|
-
*
|
|
1508
|
-
* #### Complexity
|
|
1509
|
-
* Linear.
|
|
1510
|
-
*
|
|
1511
|
-
* #### Example
|
|
1512
|
-
* ```typescript
|
|
1513
|
-
* import { each, keys } from '@lumino/algorithm';
|
|
1514
|
-
*
|
|
1515
|
-
* let data = { one: 1, two: 2, three: 3 };
|
|
1516
|
-
*
|
|
1517
|
-
* each(keys(data), key => { console.log(key); }); // 'one', 'two', 'three'
|
|
1518
|
-
* ```
|
|
1519
|
-
*/
|
|
1520
|
-
function iterKeys(object) {
|
|
1521
|
-
return new KeyIterator(object);
|
|
1522
|
-
}
|
|
1523
|
-
/**
|
|
1524
|
-
* Create an iterator for the values in an object.
|
|
1525
|
-
*
|
|
1526
|
-
* @param object - The object of interest.
|
|
1527
|
-
*
|
|
1528
|
-
* @returns A new iterator for the values in the given object.
|
|
1529
|
-
*
|
|
1530
|
-
* #### Complexity
|
|
1531
|
-
* Linear.
|
|
1532
|
-
*
|
|
1533
|
-
* #### Example
|
|
1534
|
-
* ```typescript
|
|
1535
|
-
* import { each, values } from '@lumino/algorithm';
|
|
1536
|
-
*
|
|
1537
|
-
* let data = { one: 1, two: 2, three: 3 };
|
|
1538
|
-
*
|
|
1539
|
-
* each(values(data), value => { console.log(value); }); // 1, 2, 3
|
|
1540
|
-
* ```
|
|
1541
|
-
*/
|
|
1542
|
-
function iterValues(object) {
|
|
1543
|
-
return new ValueIterator(object);
|
|
1544
|
-
}
|
|
1545
|
-
/**
|
|
1546
|
-
* Create an iterator for the items in an object.
|
|
1547
|
-
*
|
|
1548
|
-
* @param object - The object of interest.
|
|
1549
|
-
*
|
|
1550
|
-
* @returns A new iterator for the items in the given object.
|
|
1551
|
-
*
|
|
1552
|
-
* #### Complexity
|
|
1553
|
-
* Linear.
|
|
1554
|
-
*
|
|
1555
|
-
* #### Example
|
|
1556
|
-
* ```typescript
|
|
1557
|
-
* import { each, items } from '@lumino/algorithm';
|
|
1558
|
-
*
|
|
1559
|
-
* let data = { one: 1, two: 2, three: 3 };
|
|
1560
|
-
*
|
|
1561
|
-
* each(items(data), value => { console.log(value); }); // ['one', 1], ['two', 2], ['three', 3]
|
|
1562
|
-
* ```
|
|
1563
|
-
*/
|
|
1564
|
-
function iterItems(object) {
|
|
1565
|
-
return new ItemIterator(object);
|
|
1566
|
-
}
|
|
1567
|
-
/**
|
|
1568
|
-
* Create an iterator for an iterator-like function.
|
|
1569
|
-
*
|
|
1570
|
-
* @param fn - A function which behaves like an iterator `next` method.
|
|
1571
|
-
*
|
|
1572
|
-
* @returns A new iterator for the given function.
|
|
1573
|
-
*
|
|
1574
|
-
* #### Notes
|
|
1575
|
-
* The returned iterator **cannot** be cloned.
|
|
1576
|
-
*
|
|
1577
|
-
* #### Example
|
|
1578
|
-
* ```typescript
|
|
1579
|
-
* import { each, iterFn } from '@lumino/algorithm';
|
|
1580
|
-
*
|
|
1581
|
-
* let it = iterFn((() => {
|
|
1582
|
-
* let i = 0;
|
|
1583
|
-
* return () => i > 3 ? undefined : i++;
|
|
1584
|
-
* })());
|
|
1585
|
-
*
|
|
1586
|
-
* each(it, v => { console.log(v); }); // 0, 1, 2, 3
|
|
1587
|
-
* ```
|
|
1588
|
-
*/
|
|
1589
|
-
function iterFn(fn) {
|
|
1590
|
-
return new FnIterator(fn);
|
|
1591
|
-
}
|
|
1592
|
-
/**
|
|
1593
|
-
* Invoke a function for each value in an iterable.
|
|
1594
|
-
*
|
|
1595
|
-
* @param object - The iterable or array-like object of interest.
|
|
1596
|
-
*
|
|
1597
|
-
* @param fn - The callback function to invoke for each value.
|
|
1598
|
-
*
|
|
1599
|
-
* #### Notes
|
|
1600
|
-
* Iteration can be terminated early by returning `false` from the
|
|
1601
|
-
* callback function.
|
|
1602
|
-
*
|
|
1603
|
-
* #### Complexity
|
|
1604
|
-
* Linear.
|
|
1605
|
-
*
|
|
1606
|
-
* #### Example
|
|
1607
|
-
* ```typescript
|
|
1608
|
-
* import { each } from '@lumino/algorithm';
|
|
1609
|
-
*
|
|
1610
|
-
* let data = [5, 7, 0, -2, 9];
|
|
1611
|
-
*
|
|
1612
|
-
* each(data, value => { console.log(value); });
|
|
1613
|
-
* ```
|
|
1614
|
-
*/
|
|
1615
|
-
function each(object, fn) {
|
|
1616
|
-
var index = 0;
|
|
1617
|
-
var it = iter(object);
|
|
1618
|
-
var value;
|
|
1619
|
-
while ((value = it.next()) !== undefined) {
|
|
1620
|
-
if (fn(value, index++) === false) {
|
|
1621
|
-
return;
|
|
1622
|
-
}
|
|
1623
|
-
}
|
|
1624
|
-
}
|
|
1625
|
-
/**
|
|
1626
|
-
* Test whether all values in an iterable satisfy a predicate.
|
|
1627
|
-
*
|
|
1628
|
-
* @param object - The iterable or array-like object of interest.
|
|
1629
|
-
*
|
|
1630
|
-
* @param fn - The predicate function to invoke for each value.
|
|
1631
|
-
*
|
|
1632
|
-
* @returns `true` if all values pass the test, `false` otherwise.
|
|
1633
|
-
*
|
|
1634
|
-
* #### Notes
|
|
1635
|
-
* Iteration terminates on the first `false` predicate result.
|
|
1636
|
-
*
|
|
1637
|
-
* #### Complexity
|
|
1638
|
-
* Linear.
|
|
1639
|
-
*
|
|
1640
|
-
* #### Example
|
|
1641
|
-
* ```typescript
|
|
1642
|
-
* import { every } from '@lumino/algorithm';
|
|
1643
|
-
*
|
|
1644
|
-
* let data = [5, 7, 1];
|
|
1645
|
-
*
|
|
1646
|
-
* every(data, value => value % 2 === 0); // false
|
|
1647
|
-
* every(data, value => value % 2 === 1); // true
|
|
1648
|
-
* ```
|
|
1649
|
-
*/
|
|
1650
|
-
function every(object, fn) {
|
|
1651
|
-
var index = 0;
|
|
1652
|
-
var it = iter(object);
|
|
1653
|
-
var value;
|
|
1654
|
-
while ((value = it.next()) !== undefined) {
|
|
1655
|
-
if (!fn(value, index++)) {
|
|
1656
|
-
return false;
|
|
1657
|
-
}
|
|
1658
|
-
}
|
|
1659
|
-
return true;
|
|
1660
|
-
}
|
|
1661
|
-
/**
|
|
1662
|
-
* Test whether any value in an iterable satisfies a predicate.
|
|
1663
|
-
*
|
|
1664
|
-
* @param object - The iterable or array-like object of interest.
|
|
1665
|
-
*
|
|
1666
|
-
* @param fn - The predicate function to invoke for each value.
|
|
1667
|
-
*
|
|
1668
|
-
* @returns `true` if any value passes the test, `false` otherwise.
|
|
1669
|
-
*
|
|
1670
|
-
* #### Notes
|
|
1671
|
-
* Iteration terminates on the first `true` predicate result.
|
|
1672
|
-
*
|
|
1673
|
-
* #### Complexity
|
|
1674
|
-
* Linear.
|
|
1675
|
-
*
|
|
1676
|
-
* #### Example
|
|
1677
|
-
* ```typescript
|
|
1678
|
-
* import { some } from '@lumino/algorithm';
|
|
1679
|
-
*
|
|
1680
|
-
* let data = [5, 7, 1];
|
|
1681
|
-
*
|
|
1682
|
-
* some(data, value => value === 7); // true
|
|
1683
|
-
* some(data, value => value === 3); // false
|
|
1684
|
-
* ```
|
|
1685
|
-
*/
|
|
1686
|
-
function some(object, fn) {
|
|
1687
|
-
var index = 0;
|
|
1688
|
-
var it = iter(object);
|
|
1689
|
-
var value;
|
|
1690
|
-
while ((value = it.next()) !== undefined) {
|
|
1691
|
-
if (fn(value, index++)) {
|
|
1692
|
-
return true;
|
|
1693
|
-
}
|
|
1694
|
-
}
|
|
1695
|
-
return false;
|
|
1696
|
-
}
|
|
1697
|
-
/**
|
|
1698
|
-
* Create an array from an iterable of values.
|
|
1699
|
-
*
|
|
1700
|
-
* @param object - The iterable or array-like object of interest.
|
|
1701
|
-
*
|
|
1702
|
-
* @returns A new array of values from the given object.
|
|
1703
|
-
*
|
|
1704
|
-
* #### Example
|
|
1705
|
-
* ```typescript
|
|
1706
|
-
* import { iter, toArray } from '@lumino/algorithm';
|
|
1707
|
-
*
|
|
1708
|
-
* let data = [1, 2, 3, 4, 5, 6];
|
|
1709
|
-
*
|
|
1710
|
-
* let stream = iter(data);
|
|
1711
|
-
*
|
|
1712
|
-
* toArray(stream); // [1, 2, 3, 4, 5, 6];
|
|
1713
|
-
* ```
|
|
1714
|
-
*/
|
|
1715
|
-
function toArray(object) {
|
|
1716
|
-
var index = 0;
|
|
1717
|
-
var result = [];
|
|
1718
|
-
var it = iter(object);
|
|
1719
|
-
var value;
|
|
1720
|
-
while ((value = it.next()) !== undefined) {
|
|
1721
|
-
result[index++] = value;
|
|
1722
|
-
}
|
|
1723
|
-
return result;
|
|
1724
|
-
}
|
|
1725
|
-
/**
|
|
1726
|
-
* Create an object from an iterable of key/value pairs.
|
|
1727
|
-
*
|
|
1728
|
-
* @param object - The iterable or array-like object of interest.
|
|
1729
|
-
*
|
|
1730
|
-
* @returns A new object mapping keys to values.
|
|
1731
|
-
*
|
|
1732
|
-
* #### Example
|
|
1733
|
-
* ```typescript
|
|
1734
|
-
* import { toObject } from '@lumino/algorithm';
|
|
1735
|
-
*
|
|
1736
|
-
* let data = [['one', 1], ['two', 2], ['three', 3]];
|
|
1737
|
-
*
|
|
1738
|
-
* toObject(data); // { one: 1, two: 2, three: 3 }
|
|
1739
|
-
* ```
|
|
1740
|
-
*/
|
|
1741
|
-
function toObject(object) {
|
|
1742
|
-
var it = iter(object);
|
|
1743
|
-
var pair;
|
|
1744
|
-
var result = {};
|
|
1745
|
-
while ((pair = it.next()) !== undefined) {
|
|
1746
|
-
result[pair[0]] = pair[1];
|
|
1747
|
-
}
|
|
1748
|
-
return result;
|
|
1749
|
-
}
|
|
1750
|
-
/**
|
|
1751
|
-
* An iterator for an array-like object.
|
|
1752
|
-
*
|
|
1753
|
-
* #### Notes
|
|
1754
|
-
* This iterator can be used for any builtin JS array-like object.
|
|
1755
|
-
*/
|
|
1756
|
-
var ArrayIterator = /** @class */ (function () {
|
|
1757
|
-
/**
|
|
1758
|
-
* Construct a new array iterator.
|
|
1759
|
-
*
|
|
1760
|
-
* @param source - The array-like object of interest.
|
|
1761
|
-
*/
|
|
1762
|
-
function ArrayIterator(source) {
|
|
1763
|
-
this._index = 0;
|
|
1764
|
-
this._source = source;
|
|
1765
|
-
}
|
|
1766
|
-
/**
|
|
1767
|
-
* Get an iterator over the object's values.
|
|
1768
|
-
*
|
|
1769
|
-
* @returns An iterator which yields the object's values.
|
|
1770
|
-
*/
|
|
1771
|
-
ArrayIterator.prototype.iter = function () {
|
|
1772
|
-
return this;
|
|
1773
|
-
};
|
|
1774
|
-
/**
|
|
1775
|
-
* Create an independent clone of the iterator.
|
|
1776
|
-
*
|
|
1777
|
-
* @returns A new independent clone of the iterator.
|
|
1778
|
-
*/
|
|
1779
|
-
ArrayIterator.prototype.clone = function () {
|
|
1780
|
-
var result = new ArrayIterator(this._source);
|
|
1781
|
-
result._index = this._index;
|
|
1782
|
-
return result;
|
|
1783
|
-
};
|
|
1784
|
-
/**
|
|
1785
|
-
* Get the next value from the iterator.
|
|
1786
|
-
*
|
|
1787
|
-
* @returns The next value from the iterator, or `undefined`.
|
|
1788
|
-
*/
|
|
1789
|
-
ArrayIterator.prototype.next = function () {
|
|
1790
|
-
if (this._index >= this._source.length) {
|
|
1791
|
-
return undefined;
|
|
1792
|
-
}
|
|
1793
|
-
return this._source[this._index++];
|
|
1794
|
-
};
|
|
1795
|
-
return ArrayIterator;
|
|
1796
|
-
}());
|
|
1797
|
-
/**
|
|
1798
|
-
* An iterator for the keys in an object.
|
|
1799
|
-
*
|
|
1800
|
-
* #### Notes
|
|
1801
|
-
* This iterator can be used for any JS object.
|
|
1802
|
-
*/
|
|
1803
|
-
var KeyIterator = /** @class */ (function () {
|
|
1804
|
-
/**
|
|
1805
|
-
* Construct a new key iterator.
|
|
1806
|
-
*
|
|
1807
|
-
* @param source - The object of interest.
|
|
1808
|
-
*
|
|
1809
|
-
* @param keys - The keys to iterate, if known.
|
|
1810
|
-
*/
|
|
1811
|
-
function KeyIterator(source, keys) {
|
|
1812
|
-
if (keys === void 0) { keys = Object.keys(source); }
|
|
1813
|
-
this._index = 0;
|
|
1814
|
-
this._source = source;
|
|
1815
|
-
this._keys = keys;
|
|
1816
|
-
}
|
|
1817
|
-
/**
|
|
1818
|
-
* Get an iterator over the object's values.
|
|
1819
|
-
*
|
|
1820
|
-
* @returns An iterator which yields the object's values.
|
|
1821
|
-
*/
|
|
1822
|
-
KeyIterator.prototype.iter = function () {
|
|
1823
|
-
return this;
|
|
1824
|
-
};
|
|
1825
|
-
/**
|
|
1826
|
-
* Create an independent clone of the iterator.
|
|
1827
|
-
*
|
|
1828
|
-
* @returns A new independent clone of the iterator.
|
|
1829
|
-
*/
|
|
1830
|
-
KeyIterator.prototype.clone = function () {
|
|
1831
|
-
var result = new KeyIterator(this._source, this._keys);
|
|
1832
|
-
result._index = this._index;
|
|
1833
|
-
return result;
|
|
1834
|
-
};
|
|
1835
|
-
/**
|
|
1836
|
-
* Get the next value from the iterator.
|
|
1837
|
-
*
|
|
1838
|
-
* @returns The next value from the iterator, or `undefined`.
|
|
1839
|
-
*/
|
|
1840
|
-
KeyIterator.prototype.next = function () {
|
|
1841
|
-
if (this._index >= this._keys.length) {
|
|
1842
|
-
return undefined;
|
|
1843
|
-
}
|
|
1844
|
-
var key = this._keys[this._index++];
|
|
1845
|
-
if (key in this._source) {
|
|
1846
|
-
return key;
|
|
1847
|
-
}
|
|
1848
|
-
return this.next();
|
|
1849
|
-
};
|
|
1850
|
-
return KeyIterator;
|
|
1851
|
-
}());
|
|
1852
|
-
/**
|
|
1853
|
-
* An iterator for the values in an object.
|
|
1854
|
-
*
|
|
1855
|
-
* #### Notes
|
|
1856
|
-
* This iterator can be used for any JS object.
|
|
1857
|
-
*/
|
|
1858
|
-
var ValueIterator = /** @class */ (function () {
|
|
1859
|
-
/**
|
|
1860
|
-
* Construct a new value iterator.
|
|
1861
|
-
*
|
|
1862
|
-
* @param source - The object of interest.
|
|
1863
|
-
*
|
|
1864
|
-
* @param keys - The keys to iterate, if known.
|
|
1865
|
-
*/
|
|
1866
|
-
function ValueIterator(source, keys) {
|
|
1867
|
-
if (keys === void 0) { keys = Object.keys(source); }
|
|
1868
|
-
this._index = 0;
|
|
1869
|
-
this._source = source;
|
|
1870
|
-
this._keys = keys;
|
|
1871
|
-
}
|
|
1872
|
-
/**
|
|
1873
|
-
* Get an iterator over the object's values.
|
|
1874
|
-
*
|
|
1875
|
-
* @returns An iterator which yields the object's values.
|
|
1876
|
-
*/
|
|
1877
|
-
ValueIterator.prototype.iter = function () {
|
|
1878
|
-
return this;
|
|
1879
|
-
};
|
|
1880
|
-
/**
|
|
1881
|
-
* Create an independent clone of the iterator.
|
|
1882
|
-
*
|
|
1883
|
-
* @returns A new independent clone of the iterator.
|
|
1884
|
-
*/
|
|
1885
|
-
ValueIterator.prototype.clone = function () {
|
|
1886
|
-
var result = new ValueIterator(this._source, this._keys);
|
|
1887
|
-
result._index = this._index;
|
|
1888
|
-
return result;
|
|
1889
|
-
};
|
|
1890
|
-
/**
|
|
1891
|
-
* Get the next value from the iterator.
|
|
1892
|
-
*
|
|
1893
|
-
* @returns The next value from the iterator, or `undefined`.
|
|
1894
|
-
*/
|
|
1895
|
-
ValueIterator.prototype.next = function () {
|
|
1896
|
-
if (this._index >= this._keys.length) {
|
|
1897
|
-
return undefined;
|
|
1898
|
-
}
|
|
1899
|
-
var key = this._keys[this._index++];
|
|
1900
|
-
if (key in this._source) {
|
|
1901
|
-
return this._source[key];
|
|
1902
|
-
}
|
|
1903
|
-
return this.next();
|
|
1904
|
-
};
|
|
1905
|
-
return ValueIterator;
|
|
1906
|
-
}());
|
|
1907
|
-
/**
|
|
1908
|
-
* An iterator for the items in an object.
|
|
1909
|
-
*
|
|
1910
|
-
* #### Notes
|
|
1911
|
-
* This iterator can be used for any JS object.
|
|
1912
|
-
*/
|
|
1913
|
-
var ItemIterator = /** @class */ (function () {
|
|
1914
|
-
/**
|
|
1915
|
-
* Construct a new item iterator.
|
|
1916
|
-
*
|
|
1917
|
-
* @param source - The object of interest.
|
|
1918
|
-
*
|
|
1919
|
-
* @param keys - The keys to iterate, if known.
|
|
1920
|
-
*/
|
|
1921
|
-
function ItemIterator(source, keys) {
|
|
1922
|
-
if (keys === void 0) { keys = Object.keys(source); }
|
|
1923
|
-
this._index = 0;
|
|
1924
|
-
this._source = source;
|
|
1925
|
-
this._keys = keys;
|
|
1926
|
-
}
|
|
1927
|
-
/**
|
|
1928
|
-
* Get an iterator over the object's values.
|
|
1929
|
-
*
|
|
1930
|
-
* @returns An iterator which yields the object's values.
|
|
1931
|
-
*/
|
|
1932
|
-
ItemIterator.prototype.iter = function () {
|
|
1933
|
-
return this;
|
|
1934
|
-
};
|
|
1935
|
-
/**
|
|
1936
|
-
* Create an independent clone of the iterator.
|
|
1937
|
-
*
|
|
1938
|
-
* @returns A new independent clone of the iterator.
|
|
1939
|
-
*/
|
|
1940
|
-
ItemIterator.prototype.clone = function () {
|
|
1941
|
-
var result = new ItemIterator(this._source, this._keys);
|
|
1942
|
-
result._index = this._index;
|
|
1943
|
-
return result;
|
|
1944
|
-
};
|
|
1945
|
-
/**
|
|
1946
|
-
* Get the next value from the iterator.
|
|
1947
|
-
*
|
|
1948
|
-
* @returns The next value from the iterator, or `undefined`.
|
|
1949
|
-
*/
|
|
1950
|
-
ItemIterator.prototype.next = function () {
|
|
1951
|
-
if (this._index >= this._keys.length) {
|
|
1952
|
-
return undefined;
|
|
1953
|
-
}
|
|
1954
|
-
var key = this._keys[this._index++];
|
|
1955
|
-
if (key in this._source) {
|
|
1956
|
-
return [key, this._source[key]];
|
|
1957
|
-
}
|
|
1958
|
-
return this.next();
|
|
1959
|
-
};
|
|
1960
|
-
return ItemIterator;
|
|
1961
|
-
}());
|
|
1962
|
-
/**
|
|
1963
|
-
* An iterator for an iterator-like function.
|
|
1964
|
-
*/
|
|
1965
|
-
var FnIterator = /** @class */ (function () {
|
|
1966
|
-
/**
|
|
1967
|
-
* Construct a new function iterator.
|
|
1968
|
-
*
|
|
1969
|
-
* @param fn - The iterator-like function of interest.
|
|
1970
|
-
*/
|
|
1971
|
-
function FnIterator(fn) {
|
|
1972
|
-
this._fn = fn;
|
|
1973
|
-
}
|
|
1974
|
-
/**
|
|
1975
|
-
* Get an iterator over the object's values.
|
|
1976
|
-
*
|
|
1977
|
-
* @returns An iterator which yields the object's values.
|
|
1978
|
-
*/
|
|
1979
|
-
FnIterator.prototype.iter = function () {
|
|
1980
|
-
return this;
|
|
1981
|
-
};
|
|
1982
|
-
/**
|
|
1983
|
-
* Create an independent clone of the iterator.
|
|
1984
|
-
*
|
|
1985
|
-
* @returns A new independent clone of the iterator.
|
|
1986
|
-
*/
|
|
1987
|
-
FnIterator.prototype.clone = function () {
|
|
1988
|
-
throw new Error('An `FnIterator` cannot be cloned.');
|
|
1989
|
-
};
|
|
1990
|
-
/**
|
|
1991
|
-
* Get the next value from the iterator.
|
|
1992
|
-
*
|
|
1993
|
-
* @returns The next value from the iterator, or `undefined`.
|
|
1994
|
-
*/
|
|
1995
|
-
FnIterator.prototype.next = function () {
|
|
1996
|
-
return this._fn.call(undefined);
|
|
1997
|
-
};
|
|
1998
|
-
return FnIterator;
|
|
1999
|
-
}());
|
|
2000
|
-
|
|
2001
|
-
// Copyright (c) Jupyter Development Team.
|
|
2002
|
-
/**
|
|
2003
|
-
* Chain together several iterables.
|
|
2004
|
-
*
|
|
2005
|
-
* @param objects - The iterable or array-like objects of interest.
|
|
2006
|
-
*
|
|
2007
|
-
* @returns An iterator which yields the values of the iterables
|
|
2008
|
-
* in the order in which they are supplied.
|
|
2009
|
-
*
|
|
2010
|
-
* #### Example
|
|
2011
|
-
* ```typescript
|
|
2012
|
-
* import { chain, toArray } from '@lumino/algorithm';
|
|
2013
|
-
*
|
|
2014
|
-
* let data1 = [1, 2, 3];
|
|
2015
|
-
* let data2 = [4, 5, 6];
|
|
2016
|
-
*
|
|
2017
|
-
* let stream = chain(data1, data2);
|
|
2018
|
-
*
|
|
2019
|
-
* toArray(stream); // [1, 2, 3, 4, 5, 6]
|
|
2020
|
-
* ```
|
|
2021
|
-
*/
|
|
2022
|
-
function chain() {
|
|
2023
|
-
var objects = [];
|
|
2024
|
-
for (var _i = 0; _i < arguments.length; _i++) {
|
|
2025
|
-
objects[_i] = arguments[_i];
|
|
2026
|
-
}
|
|
2027
|
-
return new ChainIterator(iter(objects.map(iter)));
|
|
2028
|
-
}
|
|
2029
|
-
/**
|
|
2030
|
-
* An iterator which chains together several iterators.
|
|
2031
|
-
*/
|
|
2032
|
-
var ChainIterator = /** @class */ (function () {
|
|
2033
|
-
/**
|
|
2034
|
-
* Construct a new chain iterator.
|
|
2035
|
-
*
|
|
2036
|
-
* @param source - The iterator of iterators of interest.
|
|
2037
|
-
*/
|
|
2038
|
-
function ChainIterator(source) {
|
|
2039
|
-
this._cloned = false;
|
|
2040
|
-
this._source = source;
|
|
2041
|
-
this._active = undefined;
|
|
2042
|
-
}
|
|
2043
|
-
/**
|
|
2044
|
-
* Get an iterator over the object's values.
|
|
2045
|
-
*
|
|
2046
|
-
* @returns An iterator which yields the object's values.
|
|
2047
|
-
*/
|
|
2048
|
-
ChainIterator.prototype.iter = function () {
|
|
2049
|
-
return this;
|
|
2050
|
-
};
|
|
2051
|
-
/**
|
|
2052
|
-
* Create an independent clone of the iterator.
|
|
2053
|
-
*
|
|
2054
|
-
* @returns A new independent clone of the iterator.
|
|
2055
|
-
*/
|
|
2056
|
-
ChainIterator.prototype.clone = function () {
|
|
2057
|
-
var result = new ChainIterator(this._source.clone());
|
|
2058
|
-
result._active = this._active && this._active.clone();
|
|
2059
|
-
result._cloned = true;
|
|
2060
|
-
this._cloned = true;
|
|
2061
|
-
return result;
|
|
2062
|
-
};
|
|
2063
|
-
/**
|
|
2064
|
-
* Get the next value from the iterator.
|
|
2065
|
-
*
|
|
2066
|
-
* @returns The next value from the iterator, or `undefined`.
|
|
2067
|
-
*/
|
|
2068
|
-
ChainIterator.prototype.next = function () {
|
|
2069
|
-
if (this._active === undefined) {
|
|
2070
|
-
var active = this._source.next();
|
|
2071
|
-
if (active === undefined) {
|
|
2072
|
-
return undefined;
|
|
2073
|
-
}
|
|
2074
|
-
this._active = this._cloned ? active.clone() : active;
|
|
2075
|
-
}
|
|
2076
|
-
var value = this._active.next();
|
|
2077
|
-
if (value !== undefined) {
|
|
2078
|
-
return value;
|
|
2079
|
-
}
|
|
2080
|
-
this._active = undefined;
|
|
2081
|
-
return this.next();
|
|
2082
|
-
};
|
|
2083
|
-
return ChainIterator;
|
|
2084
|
-
}());
|
|
2085
|
-
|
|
2086
|
-
/**
|
|
2087
|
-
* Create an empty iterator.
|
|
2088
|
-
*
|
|
2089
|
-
* @returns A new iterator which yields nothing.
|
|
2090
|
-
*
|
|
2091
|
-
* #### Example
|
|
2092
|
-
* ```typescript
|
|
2093
|
-
* import { empty, toArray } from '@lumino/algorithm';
|
|
2094
|
-
*
|
|
2095
|
-
* let stream = empty<number>();
|
|
2096
|
-
*
|
|
2097
|
-
* toArray(stream); // []
|
|
2098
|
-
* ```
|
|
2099
|
-
*/
|
|
2100
|
-
function empty() {
|
|
2101
|
-
return new EmptyIterator();
|
|
2102
|
-
}
|
|
2103
|
-
/**
|
|
2104
|
-
* An iterator which is always empty.
|
|
2105
|
-
*/
|
|
2106
|
-
var EmptyIterator = /** @class */ (function () {
|
|
2107
|
-
function EmptyIterator() {
|
|
2108
|
-
}
|
|
2109
|
-
/**
|
|
2110
|
-
* Get an iterator over the object's values.
|
|
2111
|
-
*
|
|
2112
|
-
* @returns An iterator which yields the object's values.
|
|
2113
|
-
*/
|
|
2114
|
-
EmptyIterator.prototype.iter = function () {
|
|
2115
|
-
return this;
|
|
2116
|
-
};
|
|
2117
|
-
/**
|
|
2118
|
-
* Create an independent clone of the iterator.
|
|
2119
|
-
*
|
|
2120
|
-
* @returns A new independent clone of the iterator.
|
|
2121
|
-
*/
|
|
2122
|
-
EmptyIterator.prototype.clone = function () {
|
|
2123
|
-
return new EmptyIterator();
|
|
2124
|
-
};
|
|
2125
|
-
/**
|
|
2126
|
-
* Get the next value from the iterator.
|
|
2127
|
-
*
|
|
2128
|
-
* @returns The next value from the iterator, or `undefined`.
|
|
2129
|
-
*/
|
|
2130
|
-
EmptyIterator.prototype.next = function () {
|
|
2131
|
-
return undefined;
|
|
2132
|
-
};
|
|
2133
|
-
return EmptyIterator;
|
|
2134
|
-
}());
|
|
2135
|
-
|
|
2136
|
-
// Copyright (c) Jupyter Development Team.
|
|
2137
|
-
/**
|
|
2138
|
-
* Enumerate an iterable object.
|
|
2139
|
-
*
|
|
2140
|
-
* @param object - The iterable or array-like object of interest.
|
|
2141
|
-
*
|
|
2142
|
-
* @param start - The starting enum value. The default is `0`.
|
|
2143
|
-
*
|
|
2144
|
-
* @returns An iterator which yields the enumerated values.
|
|
2145
|
-
*
|
|
2146
|
-
* #### Example
|
|
2147
|
-
* ```typescript
|
|
2148
|
-
* import { enumerate, toArray } from '@lumino/algorithm';
|
|
2149
|
-
*
|
|
2150
|
-
* let data = ['foo', 'bar', 'baz'];
|
|
2151
|
-
*
|
|
2152
|
-
* let stream = enumerate(data, 1);
|
|
2153
|
-
*
|
|
2154
|
-
* toArray(stream); // [[1, 'foo'], [2, 'bar'], [3, 'baz']]
|
|
2155
|
-
* ```
|
|
2156
|
-
*/
|
|
2157
|
-
function enumerate(object, start) {
|
|
2158
|
-
if (start === void 0) { start = 0; }
|
|
2159
|
-
return new EnumerateIterator(iter(object), start);
|
|
2160
|
-
}
|
|
2161
|
-
/**
|
|
2162
|
-
* An iterator which enumerates the source values.
|
|
2163
|
-
*/
|
|
2164
|
-
var EnumerateIterator = /** @class */ (function () {
|
|
2165
|
-
/**
|
|
2166
|
-
* Construct a new enumerate iterator.
|
|
2167
|
-
*
|
|
2168
|
-
* @param source - The iterator of values of interest.
|
|
2169
|
-
*
|
|
2170
|
-
* @param start - The starting enum value.
|
|
2171
|
-
*/
|
|
2172
|
-
function EnumerateIterator(source, start) {
|
|
2173
|
-
this._source = source;
|
|
2174
|
-
this._index = start;
|
|
2175
|
-
}
|
|
2176
|
-
/**
|
|
2177
|
-
* Get an iterator over the object's values.
|
|
2178
|
-
*
|
|
2179
|
-
* @returns An iterator which yields the object's values.
|
|
2180
|
-
*/
|
|
2181
|
-
EnumerateIterator.prototype.iter = function () {
|
|
2182
|
-
return this;
|
|
2183
|
-
};
|
|
2184
|
-
/**
|
|
2185
|
-
* Create an independent clone of the iterator.
|
|
2186
|
-
*
|
|
2187
|
-
* @returns A new independent clone of the iterator.
|
|
2188
|
-
*/
|
|
2189
|
-
EnumerateIterator.prototype.clone = function () {
|
|
2190
|
-
return new EnumerateIterator(this._source.clone(), this._index);
|
|
2191
|
-
};
|
|
2192
|
-
/**
|
|
2193
|
-
* Get the next value from the iterator.
|
|
2194
|
-
*
|
|
2195
|
-
* @returns The next value from the iterator, or `undefined`.
|
|
2196
|
-
*/
|
|
2197
|
-
EnumerateIterator.prototype.next = function () {
|
|
2198
|
-
var value = this._source.next();
|
|
2199
|
-
if (value === undefined) {
|
|
2200
|
-
return undefined;
|
|
2201
|
-
}
|
|
2202
|
-
return [this._index++, value];
|
|
2203
|
-
};
|
|
2204
|
-
return EnumerateIterator;
|
|
2205
|
-
}());
|
|
2206
|
-
|
|
2207
|
-
// Copyright (c) Jupyter Development Team.
|
|
2208
|
-
/**
|
|
2209
|
-
* Filter an iterable for values which pass a test.
|
|
2210
|
-
*
|
|
2211
|
-
* @param object - The iterable or array-like object of interest.
|
|
2212
|
-
*
|
|
2213
|
-
* @param fn - The predicate function to invoke for each value.
|
|
2214
|
-
*
|
|
2215
|
-
* @returns An iterator which yields the values which pass the test.
|
|
2216
|
-
*
|
|
2217
|
-
* #### Example
|
|
2218
|
-
* ```typescript
|
|
2219
|
-
* import { filter, toArray } from '@lumino/algorithm';
|
|
2220
|
-
*
|
|
2221
|
-
* let data = [1, 2, 3, 4, 5, 6];
|
|
2222
|
-
*
|
|
2223
|
-
* let stream = filter(data, value => value % 2 === 0);
|
|
2224
|
-
*
|
|
2225
|
-
* toArray(stream); // [2, 4, 6]
|
|
2226
|
-
* ```
|
|
2227
|
-
*/
|
|
2228
|
-
function filter(object, fn) {
|
|
2229
|
-
return new FilterIterator(iter(object), fn);
|
|
2230
|
-
}
|
|
2231
|
-
/**
|
|
2232
|
-
* An iterator which yields values which pass a test.
|
|
2233
|
-
*/
|
|
2234
|
-
var FilterIterator = /** @class */ (function () {
|
|
2235
|
-
/**
|
|
2236
|
-
* Construct a new filter iterator.
|
|
2237
|
-
*
|
|
2238
|
-
* @param source - The iterator of values of interest.
|
|
2239
|
-
*
|
|
2240
|
-
* @param fn - The predicate function to invoke for each value.
|
|
2241
|
-
*/
|
|
2242
|
-
function FilterIterator(source, fn) {
|
|
2243
|
-
this._index = 0;
|
|
2244
|
-
this._source = source;
|
|
2245
|
-
this._fn = fn;
|
|
2246
|
-
}
|
|
2247
|
-
/**
|
|
2248
|
-
* Get an iterator over the object's values.
|
|
2249
|
-
*
|
|
2250
|
-
* @returns An iterator which yields the object's values.
|
|
2251
|
-
*/
|
|
2252
|
-
FilterIterator.prototype.iter = function () {
|
|
2253
|
-
return this;
|
|
2254
|
-
};
|
|
2255
|
-
/**
|
|
2256
|
-
* Create an independent clone of the iterator.
|
|
2257
|
-
*
|
|
2258
|
-
* @returns A new independent clone of the iterator.
|
|
2259
|
-
*/
|
|
2260
|
-
FilterIterator.prototype.clone = function () {
|
|
2261
|
-
var result = new FilterIterator(this._source.clone(), this._fn);
|
|
2262
|
-
result._index = this._index;
|
|
2263
|
-
return result;
|
|
2264
|
-
};
|
|
2265
|
-
/**
|
|
2266
|
-
* Get the next value from the iterator.
|
|
2267
|
-
*
|
|
2268
|
-
* @returns The next value from the iterator, or `undefined`.
|
|
2269
|
-
*/
|
|
2270
|
-
FilterIterator.prototype.next = function () {
|
|
2271
|
-
var fn = this._fn;
|
|
2272
|
-
var it = this._source;
|
|
2273
|
-
var value;
|
|
2274
|
-
while ((value = it.next()) !== undefined) {
|
|
2275
|
-
if (fn(value, this._index++)) {
|
|
2276
|
-
return value;
|
|
2277
|
-
}
|
|
2278
|
-
}
|
|
2279
|
-
return undefined;
|
|
2280
|
-
};
|
|
2281
|
-
return FilterIterator;
|
|
2282
|
-
}());
|
|
2283
|
-
|
|
2284
|
-
// Copyright (c) Jupyter Development Team.
|
|
2285
|
-
/**
|
|
2286
|
-
* Find the first value in an iterable which matches a predicate.
|
|
2287
|
-
*
|
|
2288
|
-
* @param object - The iterable or array-like object to search.
|
|
2289
|
-
*
|
|
2290
|
-
* @param fn - The predicate function to apply to the values.
|
|
2291
|
-
*
|
|
2292
|
-
* @returns The first matching value, or `undefined` if no matching
|
|
2293
|
-
* value is found.
|
|
2294
|
-
*
|
|
2295
|
-
* #### Complexity
|
|
2296
|
-
* Linear.
|
|
2297
|
-
*
|
|
2298
|
-
* #### Example
|
|
2299
|
-
* ```typescript
|
|
2300
|
-
* import { find } from '@lumino/algorithm';
|
|
2301
|
-
*
|
|
2302
|
-
* interface IAnimal { species: string, name: string };
|
|
2303
|
-
*
|
|
2304
|
-
* function isCat(value: IAnimal): boolean {
|
|
2305
|
-
* return value.species === 'cat';
|
|
2306
|
-
* }
|
|
2307
|
-
*
|
|
2308
|
-
* let data: IAnimal[] = [
|
|
2309
|
-
* { species: 'dog', name: 'spot' },
|
|
2310
|
-
* { species: 'cat', name: 'fluffy' },
|
|
2311
|
-
* { species: 'alligator', name: 'pocho' }
|
|
2312
|
-
* ];
|
|
2313
|
-
*
|
|
2314
|
-
* find(data, isCat).name; // 'fluffy'
|
|
2315
|
-
* ```
|
|
2316
|
-
*/
|
|
2317
|
-
function find(object, fn) {
|
|
2318
|
-
var index = 0;
|
|
2319
|
-
var it = iter(object);
|
|
2320
|
-
var value;
|
|
2321
|
-
while ((value = it.next()) !== undefined) {
|
|
2322
|
-
if (fn(value, index++)) {
|
|
2323
|
-
return value;
|
|
2324
|
-
}
|
|
2325
|
-
}
|
|
2326
|
-
return undefined;
|
|
2327
|
-
}
|
|
2328
|
-
/**
|
|
2329
|
-
* Find the index of the first value which matches a predicate.
|
|
2330
|
-
*
|
|
2331
|
-
* @param object - The iterable or array-like object to search.
|
|
2332
|
-
*
|
|
2333
|
-
* @param fn - The predicate function to apply to the values.
|
|
2334
|
-
*
|
|
2335
|
-
* @returns The index of the first matching value, or `-1` if no
|
|
2336
|
-
* matching value is found.
|
|
2337
|
-
*
|
|
2338
|
-
* #### Complexity
|
|
2339
|
-
* Linear.
|
|
2340
|
-
*
|
|
2341
|
-
* #### Example
|
|
2342
|
-
* ```typescript
|
|
2343
|
-
* import { findIndex } from '@lumino/algorithm';
|
|
2344
|
-
*
|
|
2345
|
-
* interface IAnimal { species: string, name: string };
|
|
2346
|
-
*
|
|
2347
|
-
* function isCat(value: IAnimal): boolean {
|
|
2348
|
-
* return value.species === 'cat';
|
|
2349
|
-
* }
|
|
2350
|
-
*
|
|
2351
|
-
* let data: IAnimal[] = [
|
|
2352
|
-
* { species: 'dog', name: 'spot' },
|
|
2353
|
-
* { species: 'cat', name: 'fluffy' },
|
|
2354
|
-
* { species: 'alligator', name: 'pocho' }
|
|
2355
|
-
* ];
|
|
2356
|
-
*
|
|
2357
|
-
* findIndex(data, isCat); // 1
|
|
2358
|
-
* ```
|
|
2359
|
-
*/
|
|
2360
|
-
function findIndex(object, fn) {
|
|
2361
|
-
var index = 0;
|
|
2362
|
-
var it = iter(object);
|
|
2363
|
-
var value;
|
|
2364
|
-
while ((value = it.next()) !== undefined) {
|
|
2365
|
-
if (fn(value, index++)) {
|
|
2366
|
-
return index - 1;
|
|
2367
|
-
}
|
|
2368
|
-
}
|
|
2369
|
-
return -1;
|
|
2370
|
-
}
|
|
2371
|
-
/**
|
|
2372
|
-
* Find the minimum value in an iterable.
|
|
2373
|
-
*
|
|
2374
|
-
* @param object - The iterable or array-like object to search.
|
|
2375
|
-
*
|
|
2376
|
-
* @param fn - The 3-way comparison function to apply to the values.
|
|
2377
|
-
* It should return `< 0` if the first value is less than the second.
|
|
2378
|
-
* `0` if the values are equivalent, or `> 0` if the first value is
|
|
2379
|
-
* greater than the second.
|
|
2380
|
-
*
|
|
2381
|
-
* @returns The minimum value in the iterable. If multiple values are
|
|
2382
|
-
* equivalent to the minimum, the left-most value is returned. If
|
|
2383
|
-
* the iterable is empty, this returns `undefined`.
|
|
2384
|
-
*
|
|
2385
|
-
* #### Complexity
|
|
2386
|
-
* Linear.
|
|
2387
|
-
*
|
|
2388
|
-
* #### Example
|
|
2389
|
-
* ```typescript
|
|
2390
|
-
* import { min } from '@lumino/algorithm';
|
|
2391
|
-
*
|
|
2392
|
-
* function numberCmp(a: number, b: number): number {
|
|
2393
|
-
* return a - b;
|
|
2394
|
-
* }
|
|
2395
|
-
*
|
|
2396
|
-
* min([7, 4, 0, 3, 9, 4], numberCmp); // 0
|
|
2397
|
-
* ```
|
|
2398
|
-
*/
|
|
2399
|
-
function min(object, fn) {
|
|
2400
|
-
var it = iter(object);
|
|
2401
|
-
var value = it.next();
|
|
2402
|
-
if (value === undefined) {
|
|
2403
|
-
return undefined;
|
|
2404
|
-
}
|
|
2405
|
-
var result = value;
|
|
2406
|
-
while ((value = it.next()) !== undefined) {
|
|
2407
|
-
if (fn(value, result) < 0) {
|
|
2408
|
-
result = value;
|
|
2409
|
-
}
|
|
2410
|
-
}
|
|
2411
|
-
return result;
|
|
2412
|
-
}
|
|
2413
|
-
/**
|
|
2414
|
-
* Find the maximum value in an iterable.
|
|
2415
|
-
*
|
|
2416
|
-
* @param object - The iterable or array-like object to search.
|
|
2417
|
-
*
|
|
2418
|
-
* @param fn - The 3-way comparison function to apply to the values.
|
|
2419
|
-
* It should return `< 0` if the first value is less than the second.
|
|
2420
|
-
* `0` if the values are equivalent, or `> 0` if the first value is
|
|
2421
|
-
* greater than the second.
|
|
2422
|
-
*
|
|
2423
|
-
* @returns The maximum value in the iterable. If multiple values are
|
|
2424
|
-
* equivalent to the maximum, the left-most value is returned. If
|
|
2425
|
-
* the iterable is empty, this returns `undefined`.
|
|
2426
|
-
*
|
|
2427
|
-
* #### Complexity
|
|
2428
|
-
* Linear.
|
|
2429
|
-
*
|
|
2430
|
-
* #### Example
|
|
2431
|
-
* ```typescript
|
|
2432
|
-
* import { max } from '@lumino/algorithm';
|
|
2433
|
-
*
|
|
2434
|
-
* function numberCmp(a: number, b: number): number {
|
|
2435
|
-
* return a - b;
|
|
2436
|
-
* }
|
|
2437
|
-
*
|
|
2438
|
-
* max([7, 4, 0, 3, 9, 4], numberCmp); // 9
|
|
2439
|
-
* ```
|
|
2440
|
-
*/
|
|
2441
|
-
function max(object, fn) {
|
|
2442
|
-
var it = iter(object);
|
|
2443
|
-
var value = it.next();
|
|
2444
|
-
if (value === undefined) {
|
|
2445
|
-
return undefined;
|
|
2446
|
-
}
|
|
2447
|
-
var result = value;
|
|
2448
|
-
while ((value = it.next()) !== undefined) {
|
|
2449
|
-
if (fn(value, result) > 0) {
|
|
2450
|
-
result = value;
|
|
2451
|
-
}
|
|
2452
|
-
}
|
|
2453
|
-
return result;
|
|
2454
|
-
}
|
|
2455
|
-
/**
|
|
2456
|
-
* Find the minimum and maximum values in an iterable.
|
|
2457
|
-
*
|
|
2458
|
-
* @param object - The iterable or array-like object to search.
|
|
2459
|
-
*
|
|
2460
|
-
* @param fn - The 3-way comparison function to apply to the values.
|
|
2461
|
-
* It should return `< 0` if the first value is less than the second.
|
|
2462
|
-
* `0` if the values are equivalent, or `> 0` if the first value is
|
|
2463
|
-
* greater than the second.
|
|
2464
|
-
*
|
|
2465
|
-
* @returns A 2-tuple of the `[min, max]` values in the iterable. If
|
|
2466
|
-
* multiple values are equivalent, the left-most values are returned.
|
|
2467
|
-
* If the iterable is empty, this returns `undefined`.
|
|
2468
|
-
*
|
|
2469
|
-
* #### Complexity
|
|
2470
|
-
* Linear.
|
|
2471
|
-
*
|
|
2472
|
-
* #### Example
|
|
2473
|
-
* ```typescript
|
|
2474
|
-
* import { minmax } from '@lumino/algorithm';
|
|
2475
|
-
*
|
|
2476
|
-
* function numberCmp(a: number, b: number): number {
|
|
2477
|
-
* return a - b;
|
|
2478
|
-
* }
|
|
2479
|
-
*
|
|
2480
|
-
* minmax([7, 4, 0, 3, 9, 4], numberCmp); // [0, 9]
|
|
2481
|
-
* ```
|
|
2482
|
-
*/
|
|
2483
|
-
function minmax(object, fn) {
|
|
2484
|
-
var it = iter(object);
|
|
2485
|
-
var value = it.next();
|
|
2486
|
-
if (value === undefined) {
|
|
2487
|
-
return undefined;
|
|
2488
|
-
}
|
|
2489
|
-
var vmin = value;
|
|
2490
|
-
var vmax = value;
|
|
2491
|
-
while ((value = it.next()) !== undefined) {
|
|
2492
|
-
if (fn(value, vmin) < 0) {
|
|
2493
|
-
vmin = value;
|
|
2494
|
-
}
|
|
2495
|
-
else if (fn(value, vmax) > 0) {
|
|
2496
|
-
vmax = value;
|
|
2497
|
-
}
|
|
2498
|
-
}
|
|
2499
|
-
return [vmin, vmax];
|
|
2500
|
-
}
|
|
2501
|
-
|
|
2502
|
-
// Copyright (c) Jupyter Development Team.
|
|
2503
|
-
/**
|
|
2504
|
-
* Transform the values of an iterable with a mapping function.
|
|
2505
|
-
*
|
|
2506
|
-
* @param object - The iterable or array-like object of interest.
|
|
2507
|
-
*
|
|
2508
|
-
* @param fn - The mapping function to invoke for each value.
|
|
2509
|
-
*
|
|
2510
|
-
* @returns An iterator which yields the transformed values.
|
|
2511
|
-
*
|
|
2512
|
-
* #### Example
|
|
2513
|
-
* ```typescript
|
|
2514
|
-
* import { map, toArray } from '@lumino/algorithm';
|
|
2515
|
-
*
|
|
2516
|
-
* let data = [1, 2, 3];
|
|
2517
|
-
*
|
|
2518
|
-
* let stream = map(data, value => value * 2);
|
|
2519
|
-
*
|
|
2520
|
-
* toArray(stream); // [2, 4, 6]
|
|
2521
|
-
* ```
|
|
2522
|
-
*/
|
|
2523
|
-
function map(object, fn) {
|
|
2524
|
-
return new MapIterator(iter(object), fn);
|
|
2525
|
-
}
|
|
2526
|
-
/**
|
|
2527
|
-
* An iterator which transforms values using a mapping function.
|
|
2528
|
-
*/
|
|
2529
|
-
var MapIterator = /** @class */ (function () {
|
|
2530
|
-
/**
|
|
2531
|
-
* Construct a new map iterator.
|
|
2532
|
-
*
|
|
2533
|
-
* @param source - The iterator of values of interest.
|
|
2534
|
-
*
|
|
2535
|
-
* @param fn - The mapping function to invoke for each value.
|
|
2536
|
-
*/
|
|
2537
|
-
function MapIterator(source, fn) {
|
|
2538
|
-
this._index = 0;
|
|
2539
|
-
this._source = source;
|
|
2540
|
-
this._fn = fn;
|
|
2541
|
-
}
|
|
2542
|
-
/**
|
|
2543
|
-
* Get an iterator over the object's values.
|
|
2544
|
-
*
|
|
2545
|
-
* @returns An iterator which yields the object's values.
|
|
2546
|
-
*/
|
|
2547
|
-
MapIterator.prototype.iter = function () {
|
|
2548
|
-
return this;
|
|
2549
|
-
};
|
|
2550
|
-
/**
|
|
2551
|
-
* Create an independent clone of the iterator.
|
|
2552
|
-
*
|
|
2553
|
-
* @returns A new independent clone of the iterator.
|
|
2554
|
-
*/
|
|
2555
|
-
MapIterator.prototype.clone = function () {
|
|
2556
|
-
var result = new MapIterator(this._source.clone(), this._fn);
|
|
2557
|
-
result._index = this._index;
|
|
2558
|
-
return result;
|
|
2559
|
-
};
|
|
2560
|
-
/**
|
|
2561
|
-
* Get the next value from the iterator.
|
|
2562
|
-
*
|
|
2563
|
-
* @returns The next value from the iterator, or `undefined`.
|
|
2564
|
-
*/
|
|
2565
|
-
MapIterator.prototype.next = function () {
|
|
2566
|
-
var value = this._source.next();
|
|
2567
|
-
if (value === undefined) {
|
|
2568
|
-
return undefined;
|
|
2569
|
-
}
|
|
2570
|
-
return this._fn.call(undefined, value, this._index++);
|
|
2571
|
-
};
|
|
2572
|
-
return MapIterator;
|
|
2573
|
-
}());
|
|
2574
|
-
|
|
2575
|
-
/**
|
|
2576
|
-
* Create an iterator of evenly spaced values.
|
|
2577
|
-
*
|
|
2578
|
-
* @param start - The starting value for the range, inclusive.
|
|
2579
|
-
*
|
|
2580
|
-
* @param stop - The stopping value for the range, exclusive.
|
|
2581
|
-
*
|
|
2582
|
-
* @param step - The distance between each value.
|
|
2583
|
-
*
|
|
2584
|
-
* @returns An iterator which produces evenly spaced values.
|
|
2585
|
-
*
|
|
2586
|
-
* #### Notes
|
|
2587
|
-
* In the single argument form of `range(stop)`, `start` defaults to
|
|
2588
|
-
* `0` and `step` defaults to `1`.
|
|
2589
|
-
*
|
|
2590
|
-
* In the two argument form of `range(start, stop)`, `step` defaults
|
|
2591
|
-
* to `1`.
|
|
2592
|
-
*/
|
|
2593
|
-
function range(start, stop, step) {
|
|
2594
|
-
if (stop === undefined) {
|
|
2595
|
-
return new RangeIterator(0, start, 1);
|
|
2596
|
-
}
|
|
2597
|
-
if (step === undefined) {
|
|
2598
|
-
return new RangeIterator(start, stop, 1);
|
|
2599
|
-
}
|
|
2600
|
-
return new RangeIterator(start, stop, step);
|
|
2601
|
-
}
|
|
2602
|
-
/**
|
|
2603
|
-
* An iterator which produces a range of evenly spaced values.
|
|
2604
|
-
*/
|
|
2605
|
-
var RangeIterator = /** @class */ (function () {
|
|
2606
|
-
/**
|
|
2607
|
-
* Construct a new range iterator.
|
|
2608
|
-
*
|
|
2609
|
-
* @param start - The starting value for the range, inclusive.
|
|
2610
|
-
*
|
|
2611
|
-
* @param stop - The stopping value for the range, exclusive.
|
|
2612
|
-
*
|
|
2613
|
-
* @param step - The distance between each value.
|
|
2614
|
-
*/
|
|
2615
|
-
function RangeIterator(start, stop, step) {
|
|
2616
|
-
this._index = 0;
|
|
2617
|
-
this._start = start;
|
|
2618
|
-
this._stop = stop;
|
|
2619
|
-
this._step = step;
|
|
2620
|
-
this._length = Private.rangeLength(start, stop, step);
|
|
2621
|
-
}
|
|
2622
|
-
/**
|
|
2623
|
-
* Get an iterator over the object's values.
|
|
2624
|
-
*
|
|
2625
|
-
* @returns An iterator which yields the object's values.
|
|
2626
|
-
*/
|
|
2627
|
-
RangeIterator.prototype.iter = function () {
|
|
2628
|
-
return this;
|
|
2629
|
-
};
|
|
2630
|
-
/**
|
|
2631
|
-
* Create an independent clone of the iterator.
|
|
2632
|
-
*
|
|
2633
|
-
* @returns A new independent clone of the iterator.
|
|
2634
|
-
*/
|
|
2635
|
-
RangeIterator.prototype.clone = function () {
|
|
2636
|
-
var result = new RangeIterator(this._start, this._stop, this._step);
|
|
2637
|
-
result._index = this._index;
|
|
2638
|
-
return result;
|
|
2639
|
-
};
|
|
2640
|
-
/**
|
|
2641
|
-
* Get the next value from the iterator.
|
|
2642
|
-
*
|
|
2643
|
-
* @returns The next value from the iterator, or `undefined`.
|
|
2644
|
-
*/
|
|
2645
|
-
RangeIterator.prototype.next = function () {
|
|
2646
|
-
if (this._index >= this._length) {
|
|
2647
|
-
return undefined;
|
|
2648
|
-
}
|
|
2649
|
-
return this._start + this._step * this._index++;
|
|
2650
|
-
};
|
|
2651
|
-
return RangeIterator;
|
|
2652
|
-
}());
|
|
2653
|
-
/**
|
|
2654
|
-
* The namespace for the module implementation details.
|
|
2655
|
-
*/
|
|
2656
|
-
var Private;
|
|
2657
|
-
(function (Private) {
|
|
2658
|
-
/**
|
|
2659
|
-
* Compute the effective length of a range.
|
|
2660
|
-
*
|
|
2661
|
-
* @param start - The starting value for the range, inclusive.
|
|
2662
|
-
*
|
|
2663
|
-
* @param stop - The stopping value for the range, exclusive.
|
|
2664
|
-
*
|
|
2665
|
-
* @param step - The distance between each value.
|
|
2666
|
-
*
|
|
2667
|
-
* @returns The number of steps need to traverse the range.
|
|
2668
|
-
*/
|
|
2669
|
-
function rangeLength(start, stop, step) {
|
|
2670
|
-
if (step === 0) {
|
|
2671
|
-
return Infinity;
|
|
2672
|
-
}
|
|
2673
|
-
if (start > stop && step > 0) {
|
|
2674
|
-
return 0;
|
|
2675
|
-
}
|
|
2676
|
-
if (start < stop && step < 0) {
|
|
2677
|
-
return 0;
|
|
2678
|
-
}
|
|
2679
|
-
return Math.ceil((stop - start) / step);
|
|
2680
|
-
}
|
|
2681
|
-
Private.rangeLength = rangeLength;
|
|
2682
|
-
})(Private || (Private = {}));
|
|
2683
|
-
|
|
2684
|
-
// Copyright (c) Jupyter Development Team.
|
|
2685
|
-
function reduce(object, fn, initial) {
|
|
2686
|
-
// Setup the iterator and fetch the first value.
|
|
2687
|
-
var index = 0;
|
|
2688
|
-
var it = iter(object);
|
|
2689
|
-
var first = it.next();
|
|
2690
|
-
// An empty iterator and no initial value is an error.
|
|
2691
|
-
if (first === undefined && initial === undefined) {
|
|
2692
|
-
throw new TypeError('Reduce of empty iterable with no initial value.');
|
|
2693
|
-
}
|
|
2694
|
-
// If the iterator is empty, return the initial value.
|
|
2695
|
-
if (first === undefined) {
|
|
2696
|
-
return initial;
|
|
2697
|
-
}
|
|
2698
|
-
// If the iterator has a single item and no initial value, the
|
|
2699
|
-
// reducer is not invoked and the first item is the return value.
|
|
2700
|
-
var second = it.next();
|
|
2701
|
-
if (second === undefined && initial === undefined) {
|
|
2702
|
-
return first;
|
|
2703
|
-
}
|
|
2704
|
-
// If iterator has a single item and an initial value is provided,
|
|
2705
|
-
// the reducer is invoked and that result is the return value.
|
|
2706
|
-
if (second === undefined) {
|
|
2707
|
-
return fn(initial, first, index++);
|
|
2708
|
-
}
|
|
2709
|
-
// Setup the initial accumlated value.
|
|
2710
|
-
var accumulator;
|
|
2711
|
-
if (initial === undefined) {
|
|
2712
|
-
accumulator = fn(first, second, index++);
|
|
2713
|
-
}
|
|
2714
|
-
else {
|
|
2715
|
-
accumulator = fn(fn(initial, first, index++), second, index++);
|
|
2716
|
-
}
|
|
2717
|
-
// Iterate the rest of the values, updating the accumulator.
|
|
2718
|
-
var next;
|
|
2719
|
-
while ((next = it.next()) !== undefined) {
|
|
2720
|
-
accumulator = fn(accumulator, next, index++);
|
|
2721
|
-
}
|
|
2722
|
-
// Return the final accumulated value.
|
|
2723
|
-
return accumulator;
|
|
2724
|
-
}
|
|
2725
|
-
|
|
2726
|
-
/**
|
|
2727
|
-
* Create an iterator which repeats a value a number of times.
|
|
2728
|
-
*
|
|
2729
|
-
* @param value - The value to repeat.
|
|
2730
|
-
*
|
|
2731
|
-
* @param count - The number of times to repeat the value.
|
|
2732
|
-
*
|
|
2733
|
-
* @returns A new iterator which repeats the specified value.
|
|
2734
|
-
*
|
|
2735
|
-
* #### Example
|
|
2736
|
-
* ```typescript
|
|
2737
|
-
* import { repeat, toArray } from '@lumino/algorithm';
|
|
2738
|
-
*
|
|
2739
|
-
* let stream = repeat(7, 3);
|
|
2740
|
-
*
|
|
2741
|
-
* toArray(stream); // [7, 7, 7]
|
|
2742
|
-
* ```
|
|
2743
|
-
*/
|
|
2744
|
-
function repeat(value, count) {
|
|
2745
|
-
return new RepeatIterator(value, count);
|
|
2746
|
-
}
|
|
2747
|
-
/**
|
|
2748
|
-
* Create an iterator which yields a value a single time.
|
|
2749
|
-
*
|
|
2750
|
-
* @param value - The value to wrap in an iterator.
|
|
2751
|
-
*
|
|
2752
|
-
* @returns A new iterator which yields the value a single time.
|
|
2753
|
-
*
|
|
2754
|
-
* #### Example
|
|
2755
|
-
* ```typescript
|
|
2756
|
-
* import { once, toArray } from '@lumino/algorithm';
|
|
2757
|
-
*
|
|
2758
|
-
* let stream = once(7);
|
|
2759
|
-
*
|
|
2760
|
-
* toArray(stream); // [7]
|
|
2761
|
-
* ```
|
|
2762
|
-
*/
|
|
2763
|
-
function once(value) {
|
|
2764
|
-
return new RepeatIterator(value, 1);
|
|
2765
|
-
}
|
|
2766
|
-
/**
|
|
2767
|
-
* An iterator which repeats a value a specified number of times.
|
|
2768
|
-
*/
|
|
2769
|
-
var RepeatIterator = /** @class */ (function () {
|
|
2770
|
-
/**
|
|
2771
|
-
* Construct a new repeat iterator.
|
|
2772
|
-
*
|
|
2773
|
-
* @param value - The value to repeat.
|
|
2774
|
-
*
|
|
2775
|
-
* @param count - The number of times to repeat the value.
|
|
2776
|
-
*/
|
|
2777
|
-
function RepeatIterator(value, count) {
|
|
2778
|
-
this._value = value;
|
|
2779
|
-
this._count = count;
|
|
2780
|
-
}
|
|
2781
|
-
/**
|
|
2782
|
-
* Get an iterator over the object's values.
|
|
2783
|
-
*
|
|
2784
|
-
* @returns An iterator which yields the object's values.
|
|
2785
|
-
*/
|
|
2786
|
-
RepeatIterator.prototype.iter = function () {
|
|
2787
|
-
return this;
|
|
2788
|
-
};
|
|
2789
|
-
/**
|
|
2790
|
-
* Create an independent clone of the iterator.
|
|
2791
|
-
*
|
|
2792
|
-
* @returns A new independent clone of the iterator.
|
|
2793
|
-
*/
|
|
2794
|
-
RepeatIterator.prototype.clone = function () {
|
|
2795
|
-
return new RepeatIterator(this._value, this._count);
|
|
2796
|
-
};
|
|
2797
|
-
/**
|
|
2798
|
-
* Get the next value from the iterator.
|
|
2799
|
-
*
|
|
2800
|
-
* @returns The next value from the iterator, or `undefined`.
|
|
2801
|
-
*/
|
|
2802
|
-
RepeatIterator.prototype.next = function () {
|
|
2803
|
-
if (this._count <= 0) {
|
|
2804
|
-
return undefined;
|
|
2805
|
-
}
|
|
2806
|
-
this._count--;
|
|
2807
|
-
return this._value;
|
|
2808
|
-
};
|
|
2809
|
-
return RepeatIterator;
|
|
2810
|
-
}());
|
|
2811
|
-
|
|
2812
|
-
/**
|
|
2813
|
-
* Create an iterator for a retroable object.
|
|
2814
|
-
*
|
|
2815
|
-
* @param object - The retroable or array-like object of interest.
|
|
2816
|
-
*
|
|
2817
|
-
* @returns An iterator which traverses the object's values in reverse.
|
|
2818
|
-
*
|
|
2819
|
-
* #### Example
|
|
2820
|
-
* ```typescript
|
|
2821
|
-
* import { retro, toArray } from '@lumino/algorithm';
|
|
2822
|
-
*
|
|
2823
|
-
* let data = [1, 2, 3, 4, 5, 6];
|
|
2824
|
-
*
|
|
2825
|
-
* let stream = retro(data);
|
|
2826
|
-
*
|
|
2827
|
-
* toArray(stream); // [6, 5, 4, 3, 2, 1]
|
|
2828
|
-
* ```
|
|
2829
|
-
*/
|
|
2830
|
-
function retro(object) {
|
|
2831
|
-
var it;
|
|
2832
|
-
if (typeof object.retro === 'function') {
|
|
2833
|
-
it = object.retro();
|
|
2834
|
-
}
|
|
2835
|
-
else {
|
|
2836
|
-
it = new RetroArrayIterator(object);
|
|
2837
|
-
}
|
|
2838
|
-
return it;
|
|
2839
|
-
}
|
|
2840
|
-
/**
|
|
2841
|
-
* An iterator which traverses an array-like object in reverse.
|
|
2842
|
-
*
|
|
2843
|
-
* #### Notes
|
|
2844
|
-
* This iterator can be used for any builtin JS array-like object.
|
|
2845
|
-
*/
|
|
2846
|
-
var RetroArrayIterator = /** @class */ (function () {
|
|
2847
|
-
/**
|
|
2848
|
-
* Construct a new retro iterator.
|
|
2849
|
-
*
|
|
2850
|
-
* @param source - The array-like object of interest.
|
|
2851
|
-
*/
|
|
2852
|
-
function RetroArrayIterator(source) {
|
|
2853
|
-
this._source = source;
|
|
2854
|
-
this._index = source.length - 1;
|
|
2855
|
-
}
|
|
2856
|
-
/**
|
|
2857
|
-
* Get an iterator over the object's values.
|
|
2858
|
-
*
|
|
2859
|
-
* @returns An iterator which yields the object's values.
|
|
2860
|
-
*/
|
|
2861
|
-
RetroArrayIterator.prototype.iter = function () {
|
|
2862
|
-
return this;
|
|
2863
|
-
};
|
|
2864
|
-
/**
|
|
2865
|
-
* Create an independent clone of the iterator.
|
|
2866
|
-
*
|
|
2867
|
-
* @returns A new independent clone of the iterator.
|
|
2868
|
-
*/
|
|
2869
|
-
RetroArrayIterator.prototype.clone = function () {
|
|
2870
|
-
var result = new RetroArrayIterator(this._source);
|
|
2871
|
-
result._index = this._index;
|
|
2872
|
-
return result;
|
|
2873
|
-
};
|
|
2874
|
-
/**
|
|
2875
|
-
* Get the next value from the iterator.
|
|
2876
|
-
*
|
|
2877
|
-
* @returns The next value from the iterator, or `undefined`.
|
|
2878
|
-
*/
|
|
2879
|
-
RetroArrayIterator.prototype.next = function () {
|
|
2880
|
-
if (this._index < 0 || this._index >= this._source.length) {
|
|
2881
|
-
return undefined;
|
|
2882
|
-
}
|
|
2883
|
-
return this._source[this._index--];
|
|
2884
|
-
};
|
|
2885
|
-
return RetroArrayIterator;
|
|
2886
|
-
}());
|
|
2887
|
-
|
|
2888
|
-
// Copyright (c) Jupyter Development Team.
|
|
2889
|
-
/**
|
|
2890
|
-
* Topologically sort an iterable of edges.
|
|
2891
|
-
*
|
|
2892
|
-
* @param edges - The iterable or array-like object of edges to sort.
|
|
2893
|
-
* An edge is represented as a 2-tuple of `[fromNode, toNode]`.
|
|
2894
|
-
*
|
|
2895
|
-
* @returns The topologically sorted array of nodes.
|
|
2896
|
-
*
|
|
2897
|
-
* #### Notes
|
|
2898
|
-
* If a cycle is present in the graph, the cycle will be ignored and
|
|
2899
|
-
* the return value will be only approximately sorted.
|
|
2900
|
-
*
|
|
2901
|
-
* #### Example
|
|
2902
|
-
* ```typescript
|
|
2903
|
-
* import { topologicSort } from '@lumino/algorithm';
|
|
2904
|
-
*
|
|
2905
|
-
* let data = [
|
|
2906
|
-
* ['d', 'e'],
|
|
2907
|
-
* ['c', 'd'],
|
|
2908
|
-
* ['a', 'b'],
|
|
2909
|
-
* ['b', 'c']
|
|
2910
|
-
* ];
|
|
2911
|
-
*
|
|
2912
|
-
* topologicSort(data); // ['a', 'b', 'c', 'd', 'e']
|
|
2913
|
-
* ```
|
|
2914
|
-
*/
|
|
2915
|
-
function topologicSort(edges) {
|
|
2916
|
-
// Setup the shared sorting state.
|
|
2917
|
-
var sorted = [];
|
|
2918
|
-
var visited = new Set();
|
|
2919
|
-
var graph = new Map();
|
|
2920
|
-
// Add the edges to the graph.
|
|
2921
|
-
each(edges, addEdge);
|
|
2922
|
-
// Visit each node in the graph.
|
|
2923
|
-
graph.forEach(function (v, k) {
|
|
2924
|
-
visit(k);
|
|
2925
|
-
});
|
|
2926
|
-
// Return the sorted results.
|
|
2927
|
-
return sorted;
|
|
2928
|
-
// Add an edge to the graph.
|
|
2929
|
-
function addEdge(edge) {
|
|
2930
|
-
var fromNode = edge[0], toNode = edge[1];
|
|
2931
|
-
var children = graph.get(toNode);
|
|
2932
|
-
if (children) {
|
|
2933
|
-
children.push(fromNode);
|
|
2934
|
-
}
|
|
2935
|
-
else {
|
|
2936
|
-
graph.set(toNode, [fromNode]);
|
|
2937
|
-
}
|
|
2938
|
-
}
|
|
2939
|
-
// Recursively visit the node.
|
|
2940
|
-
function visit(node) {
|
|
2941
|
-
if (visited.has(node)) {
|
|
2942
|
-
return;
|
|
2943
|
-
}
|
|
2944
|
-
visited.add(node);
|
|
2945
|
-
var children = graph.get(node);
|
|
2946
|
-
if (children) {
|
|
2947
|
-
children.forEach(visit);
|
|
2948
|
-
}
|
|
2949
|
-
sorted.push(node);
|
|
2950
|
-
}
|
|
2951
|
-
}
|
|
2952
|
-
|
|
2953
|
-
// Copyright (c) Jupyter Development Team.
|
|
2954
|
-
/**
|
|
2955
|
-
* Iterate over an iterable using a stepped increment.
|
|
2956
|
-
*
|
|
2957
|
-
* @param object - The iterable or array-like object of interest.
|
|
2958
|
-
*
|
|
2959
|
-
* @param step - The distance to step on each iteration. A value
|
|
2960
|
-
* of less than `1` will behave the same as a value of `1`.
|
|
2961
|
-
*
|
|
2962
|
-
* @returns An iterator which traverses the iterable step-wise.
|
|
2963
|
-
*
|
|
2964
|
-
* #### Example
|
|
2965
|
-
* ```typescript
|
|
2966
|
-
* import { stride, toArray } from '@lumino/algorithm';
|
|
2967
|
-
*
|
|
2968
|
-
* let data = [1, 2, 3, 4, 5, 6];
|
|
2969
|
-
*
|
|
2970
|
-
* let stream = stride(data, 2);
|
|
2971
|
-
*
|
|
2972
|
-
* toArray(stream); // [1, 3, 5];
|
|
2973
|
-
* ```
|
|
2974
|
-
*/
|
|
2975
|
-
function stride(object, step) {
|
|
2976
|
-
return new StrideIterator(iter(object), step);
|
|
2977
|
-
}
|
|
2978
|
-
/**
|
|
2979
|
-
* An iterator which traverses a source iterator step-wise.
|
|
2980
|
-
*/
|
|
2981
|
-
var StrideIterator = /** @class */ (function () {
|
|
2982
|
-
/**
|
|
2983
|
-
* Construct a new stride iterator.
|
|
2984
|
-
*
|
|
2985
|
-
* @param source - The iterator of values of interest.
|
|
2986
|
-
*
|
|
2987
|
-
* @param step - The distance to step on each iteration. A value
|
|
2988
|
-
* of less than `1` will behave the same as a value of `1`.
|
|
2989
|
-
*/
|
|
2990
|
-
function StrideIterator(source, step) {
|
|
2991
|
-
this._source = source;
|
|
2992
|
-
this._step = step;
|
|
2993
|
-
}
|
|
2994
|
-
/**
|
|
2995
|
-
* Get an iterator over the object's values.
|
|
2996
|
-
*
|
|
2997
|
-
* @returns An iterator which yields the object's values.
|
|
2998
|
-
*/
|
|
2999
|
-
StrideIterator.prototype.iter = function () {
|
|
3000
|
-
return this;
|
|
3001
|
-
};
|
|
3002
|
-
/**
|
|
3003
|
-
* Create an independent clone of the iterator.
|
|
3004
|
-
*
|
|
3005
|
-
* @returns A new independent clone of the iterator.
|
|
3006
|
-
*/
|
|
3007
|
-
StrideIterator.prototype.clone = function () {
|
|
3008
|
-
return new StrideIterator(this._source.clone(), this._step);
|
|
3009
|
-
};
|
|
3010
|
-
/**
|
|
3011
|
-
* Get the next value from the iterator.
|
|
3012
|
-
*
|
|
3013
|
-
* @returns The next value from the iterator, or `undefined`.
|
|
3014
|
-
*/
|
|
3015
|
-
StrideIterator.prototype.next = function () {
|
|
3016
|
-
var value = this._source.next();
|
|
3017
|
-
for (var n = this._step - 1; n > 0; --n) {
|
|
3018
|
-
this._source.next();
|
|
3019
|
-
}
|
|
3020
|
-
return value;
|
|
3021
|
-
};
|
|
3022
|
-
return StrideIterator;
|
|
3023
|
-
}());
|
|
3024
|
-
|
|
3025
|
-
// Copyright (c) Jupyter Development Team.
|
|
3026
|
-
// Distributed under the terms of the Modified BSD License.
|
|
3027
|
-
/*-----------------------------------------------------------------------------
|
|
3028
|
-
| Copyright (c) 2014-2017, PhosphorJS Contributors
|
|
3029
|
-
|
|
|
3030
|
-
| Distributed under the terms of the BSD 3-Clause License.
|
|
3031
|
-
|
|
|
3032
|
-
| The full license is in the file LICENSE, distributed with this software.
|
|
3033
|
-
|----------------------------------------------------------------------------*/
|
|
3034
|
-
/**
|
|
3035
|
-
* The namespace for string-specific algorithms.
|
|
3036
|
-
*/
|
|
3037
|
-
var StringExt;
|
|
3038
|
-
(function (StringExt) {
|
|
3039
|
-
/**
|
|
3040
|
-
* Find the indices of characters in a source text.
|
|
3041
|
-
*
|
|
3042
|
-
* @param source - The source text which should be searched.
|
|
3043
|
-
*
|
|
3044
|
-
* @param query - The characters to locate in the source text.
|
|
3045
|
-
*
|
|
3046
|
-
* @param start - The index to start the search.
|
|
3047
|
-
*
|
|
3048
|
-
* @returns The matched indices, or `null` if there is no match.
|
|
3049
|
-
*
|
|
3050
|
-
* #### Complexity
|
|
3051
|
-
* Linear on `sourceText`.
|
|
3052
|
-
*
|
|
3053
|
-
* #### Notes
|
|
3054
|
-
* In order for there to be a match, all of the characters in `query`
|
|
3055
|
-
* **must** appear in `source` in the order given by `query`.
|
|
3056
|
-
*
|
|
3057
|
-
* Characters are matched using strict `===` equality.
|
|
3058
|
-
*/
|
|
3059
|
-
function findIndices(source, query, start) {
|
|
3060
|
-
if (start === void 0) { start = 0; }
|
|
3061
|
-
var indices = new Array(query.length);
|
|
3062
|
-
for (var i = 0, j = start, n = query.length; i < n; ++i, ++j) {
|
|
3063
|
-
j = source.indexOf(query[i], j);
|
|
3064
|
-
if (j === -1) {
|
|
3065
|
-
return null;
|
|
3066
|
-
}
|
|
3067
|
-
indices[i] = j;
|
|
3068
|
-
}
|
|
3069
|
-
return indices;
|
|
3070
|
-
}
|
|
3071
|
-
StringExt.findIndices = findIndices;
|
|
3072
|
-
/**
|
|
3073
|
-
* A string matcher which uses a sum-of-squares algorithm.
|
|
3074
|
-
*
|
|
3075
|
-
* @param source - The source text which should be searched.
|
|
3076
|
-
*
|
|
3077
|
-
* @param query - The characters to locate in the source text.
|
|
3078
|
-
*
|
|
3079
|
-
* @param start - The index to start the search.
|
|
3080
|
-
*
|
|
3081
|
-
* @returns The match result, or `null` if there is no match.
|
|
3082
|
-
* A lower `score` represents a stronger match.
|
|
3083
|
-
*
|
|
3084
|
-
* #### Complexity
|
|
3085
|
-
* Linear on `sourceText`.
|
|
3086
|
-
*
|
|
3087
|
-
* #### Notes
|
|
3088
|
-
* This scoring algorithm uses a sum-of-squares approach to determine
|
|
3089
|
-
* the score. In order for there to be a match, all of the characters
|
|
3090
|
-
* in `query` **must** appear in `source` in order. The index of each
|
|
3091
|
-
* matching character is squared and added to the score. This means
|
|
3092
|
-
* that early and consecutive character matches are preferred, while
|
|
3093
|
-
* late matches are heavily penalized.
|
|
3094
|
-
*/
|
|
3095
|
-
function matchSumOfSquares(source, query, start) {
|
|
3096
|
-
if (start === void 0) { start = 0; }
|
|
3097
|
-
var indices = findIndices(source, query, start);
|
|
3098
|
-
if (!indices) {
|
|
3099
|
-
return null;
|
|
3100
|
-
}
|
|
3101
|
-
var score = 0;
|
|
3102
|
-
for (var i = 0, n = indices.length; i < n; ++i) {
|
|
3103
|
-
var j = indices[i] - start;
|
|
3104
|
-
score += j * j;
|
|
3105
|
-
}
|
|
3106
|
-
return { score: score, indices: indices };
|
|
3107
|
-
}
|
|
3108
|
-
StringExt.matchSumOfSquares = matchSumOfSquares;
|
|
3109
|
-
/**
|
|
3110
|
-
* A string matcher which uses a sum-of-deltas algorithm.
|
|
3111
|
-
*
|
|
3112
|
-
* @param source - The source text which should be searched.
|
|
3113
|
-
*
|
|
3114
|
-
* @param query - The characters to locate in the source text.
|
|
3115
|
-
*
|
|
3116
|
-
* @param start - The index to start the search.
|
|
3117
|
-
*
|
|
3118
|
-
* @returns The match result, or `null` if there is no match.
|
|
3119
|
-
* A lower `score` represents a stronger match.
|
|
3120
|
-
*
|
|
3121
|
-
* #### Complexity
|
|
3122
|
-
* Linear on `sourceText`.
|
|
3123
|
-
*
|
|
3124
|
-
* #### Notes
|
|
3125
|
-
* This scoring algorithm uses a sum-of-deltas approach to determine
|
|
3126
|
-
* the score. In order for there to be a match, all of the characters
|
|
3127
|
-
* in `query` **must** appear in `source` in order. The delta between
|
|
3128
|
-
* the indices are summed to create the score. This means that groups
|
|
3129
|
-
* of matched characters are preferred, while fragmented matches are
|
|
3130
|
-
* penalized.
|
|
3131
|
-
*/
|
|
3132
|
-
function matchSumOfDeltas(source, query, start) {
|
|
3133
|
-
if (start === void 0) { start = 0; }
|
|
3134
|
-
var indices = findIndices(source, query, start);
|
|
3135
|
-
if (!indices) {
|
|
3136
|
-
return null;
|
|
3137
|
-
}
|
|
3138
|
-
var score = 0;
|
|
3139
|
-
var last = start - 1;
|
|
3140
|
-
for (var i = 0, n = indices.length; i < n; ++i) {
|
|
3141
|
-
var j = indices[i];
|
|
3142
|
-
score += j - last - 1;
|
|
3143
|
-
last = j;
|
|
3144
|
-
}
|
|
3145
|
-
return { score: score, indices: indices };
|
|
3146
|
-
}
|
|
3147
|
-
StringExt.matchSumOfDeltas = matchSumOfDeltas;
|
|
3148
|
-
/**
|
|
3149
|
-
* Highlight the matched characters of a source text.
|
|
3150
|
-
*
|
|
3151
|
-
* @param source - The text which should be highlighted.
|
|
3152
|
-
*
|
|
3153
|
-
* @param indices - The indices of the matched characters. They must
|
|
3154
|
-
* appear in increasing order and must be in bounds of the source.
|
|
3155
|
-
*
|
|
3156
|
-
* @param fn - The function to apply to the matched chunks.
|
|
3157
|
-
*
|
|
3158
|
-
* @returns An array of unmatched and highlighted chunks.
|
|
3159
|
-
*/
|
|
3160
|
-
function highlight(source, indices, fn) {
|
|
3161
|
-
// Set up the result array.
|
|
3162
|
-
var result = [];
|
|
3163
|
-
// Set up the counter variables.
|
|
3164
|
-
var k = 0;
|
|
3165
|
-
var last = 0;
|
|
3166
|
-
var n = indices.length;
|
|
3167
|
-
// Iterator over each index.
|
|
3168
|
-
while (k < n) {
|
|
3169
|
-
// Set up the chunk indices.
|
|
3170
|
-
var i = indices[k];
|
|
3171
|
-
var j = indices[k];
|
|
3172
|
-
// Advance the right chunk index until it's non-contiguous.
|
|
3173
|
-
while (++k < n && indices[k] === j + 1) {
|
|
3174
|
-
j++;
|
|
3175
|
-
}
|
|
3176
|
-
// Extract the unmatched text.
|
|
3177
|
-
if (last < i) {
|
|
3178
|
-
result.push(source.slice(last, i));
|
|
3179
|
-
}
|
|
3180
|
-
// Extract and highlight the matched text.
|
|
3181
|
-
if (i < j + 1) {
|
|
3182
|
-
result.push(fn(source.slice(i, j + 1)));
|
|
3183
|
-
}
|
|
3184
|
-
// Update the last visited index.
|
|
3185
|
-
last = j + 1;
|
|
3186
|
-
}
|
|
3187
|
-
// Extract any remaining unmatched text.
|
|
3188
|
-
if (last < source.length) {
|
|
3189
|
-
result.push(source.slice(last));
|
|
3190
|
-
}
|
|
3191
|
-
// Return the highlighted result.
|
|
3192
|
-
return result;
|
|
3193
|
-
}
|
|
3194
|
-
StringExt.highlight = highlight;
|
|
3195
|
-
/**
|
|
3196
|
-
* A 3-way string comparison function.
|
|
3197
|
-
*
|
|
3198
|
-
* @param a - The first string of interest.
|
|
3199
|
-
*
|
|
3200
|
-
* @param b - The second string of interest.
|
|
3201
|
-
*
|
|
3202
|
-
* @returns `-1` if `a < b`, else `1` if `a > b`, else `0`.
|
|
3203
|
-
*/
|
|
3204
|
-
function cmp(a, b) {
|
|
3205
|
-
return a < b ? -1 : a > b ? 1 : 0;
|
|
3206
|
-
}
|
|
3207
|
-
StringExt.cmp = cmp;
|
|
3208
|
-
})(StringExt || (StringExt = {}));
|
|
3209
|
-
|
|
3210
|
-
// Copyright (c) Jupyter Development Team.
|
|
3211
|
-
/**
|
|
3212
|
-
* Take a fixed number of items from an iterable.
|
|
3213
|
-
*
|
|
3214
|
-
* @param object - The iterable or array-like object of interest.
|
|
3215
|
-
*
|
|
3216
|
-
* @param count - The number of items to take from the iterable.
|
|
3217
|
-
*
|
|
3218
|
-
* @returns An iterator which yields the specified number of items
|
|
3219
|
-
* from the source iterable.
|
|
3220
|
-
*
|
|
3221
|
-
* #### Notes
|
|
3222
|
-
* The returned iterator will exhaust early if the source iterable
|
|
3223
|
-
* contains an insufficient number of items.
|
|
3224
|
-
*/
|
|
3225
|
-
function take(object, count) {
|
|
3226
|
-
return new TakeIterator(iter(object), count);
|
|
3227
|
-
}
|
|
3228
|
-
/**
|
|
3229
|
-
* An iterator which takes a fixed number of items from a source.
|
|
3230
|
-
*/
|
|
3231
|
-
var TakeIterator = /** @class */ (function () {
|
|
3232
|
-
/**
|
|
3233
|
-
* Construct a new take iterator.
|
|
3234
|
-
*
|
|
3235
|
-
* @param source - The iterator of interest.
|
|
3236
|
-
*
|
|
3237
|
-
* @param count - The number of items to take from the source.
|
|
3238
|
-
*/
|
|
3239
|
-
function TakeIterator(source, count) {
|
|
3240
|
-
this._source = source;
|
|
3241
|
-
this._count = count;
|
|
3242
|
-
}
|
|
3243
|
-
/**
|
|
3244
|
-
* Get an iterator over the object's values.
|
|
3245
|
-
*
|
|
3246
|
-
* @returns An iterator which yields the object's values.
|
|
3247
|
-
*/
|
|
3248
|
-
TakeIterator.prototype.iter = function () {
|
|
3249
|
-
return this;
|
|
3250
|
-
};
|
|
3251
|
-
/**
|
|
3252
|
-
* Create an independent clone of the iterator.
|
|
3253
|
-
*
|
|
3254
|
-
* @returns A new independent clone of the iterator.
|
|
3255
|
-
*/
|
|
3256
|
-
TakeIterator.prototype.clone = function () {
|
|
3257
|
-
return new TakeIterator(this._source.clone(), this._count);
|
|
3258
|
-
};
|
|
3259
|
-
/**
|
|
3260
|
-
* Get the next value from the iterator.
|
|
3261
|
-
*
|
|
3262
|
-
* @returns The next value from the iterator, or `undefined`.
|
|
3263
|
-
*/
|
|
3264
|
-
TakeIterator.prototype.next = function () {
|
|
3265
|
-
if (this._count <= 0) {
|
|
3266
|
-
return undefined;
|
|
3267
|
-
}
|
|
3268
|
-
var value = this._source.next();
|
|
3269
|
-
if (value === undefined) {
|
|
3270
|
-
return undefined;
|
|
3271
|
-
}
|
|
3272
|
-
this._count--;
|
|
3273
|
-
return value;
|
|
3274
|
-
};
|
|
3275
|
-
return TakeIterator;
|
|
3276
|
-
}());
|
|
3277
|
-
|
|
3278
|
-
// Copyright (c) Jupyter Development Team.
|
|
3279
|
-
/**
|
|
3280
|
-
* Iterate several iterables in lockstep.
|
|
3281
|
-
*
|
|
3282
|
-
* @param objects - The iterable or array-like objects of interest.
|
|
3283
|
-
*
|
|
3284
|
-
* @returns An iterator which yields successive tuples of values where
|
|
3285
|
-
* each value is taken in turn from the provided iterables. It will
|
|
3286
|
-
* be as long as the shortest provided iterable.
|
|
3287
|
-
*
|
|
3288
|
-
* #### Example
|
|
3289
|
-
* ```typescript
|
|
3290
|
-
* import { zip, toArray } from '@lumino/algorithm';
|
|
3291
|
-
*
|
|
3292
|
-
* let data1 = [1, 2, 3];
|
|
3293
|
-
* let data2 = [4, 5, 6];
|
|
3294
|
-
*
|
|
3295
|
-
* let stream = zip(data1, data2);
|
|
3296
|
-
*
|
|
3297
|
-
* toArray(stream); // [[1, 4], [2, 5], [3, 6]]
|
|
3298
|
-
* ```
|
|
3299
|
-
*/
|
|
3300
|
-
function zip() {
|
|
3301
|
-
var objects = [];
|
|
3302
|
-
for (var _i = 0; _i < arguments.length; _i++) {
|
|
3303
|
-
objects[_i] = arguments[_i];
|
|
3304
|
-
}
|
|
3305
|
-
return new ZipIterator(objects.map(iter));
|
|
3306
|
-
}
|
|
3307
|
-
/**
|
|
3308
|
-
* An iterator which iterates several sources in lockstep.
|
|
3309
|
-
*/
|
|
3310
|
-
var ZipIterator = /** @class */ (function () {
|
|
3311
|
-
/**
|
|
3312
|
-
* Construct a new zip iterator.
|
|
3313
|
-
*
|
|
3314
|
-
* @param source - The iterators of interest.
|
|
3315
|
-
*/
|
|
3316
|
-
function ZipIterator(source) {
|
|
3317
|
-
this._source = source;
|
|
3318
|
-
}
|
|
3319
|
-
/**
|
|
3320
|
-
* Get an iterator over the object's values.
|
|
3321
|
-
*
|
|
3322
|
-
* @returns An iterator which yields the object's values.
|
|
3323
|
-
*/
|
|
3324
|
-
ZipIterator.prototype.iter = function () {
|
|
3325
|
-
return this;
|
|
3326
|
-
};
|
|
3327
|
-
/**
|
|
3328
|
-
* Create an independent clone of the iterator.
|
|
3329
|
-
*
|
|
3330
|
-
* @returns A new independent clone of the iterator.
|
|
3331
|
-
*/
|
|
3332
|
-
ZipIterator.prototype.clone = function () {
|
|
3333
|
-
return new ZipIterator(this._source.map(function (it) { return it.clone(); }));
|
|
3334
|
-
};
|
|
3335
|
-
/**
|
|
3336
|
-
* Get the next value from the iterator.
|
|
3337
|
-
*
|
|
3338
|
-
* @returns The next value from the iterator, or `undefined`.
|
|
3339
|
-
*/
|
|
3340
|
-
ZipIterator.prototype.next = function () {
|
|
3341
|
-
var result = new Array(this._source.length);
|
|
3342
|
-
for (var i = 0, n = this._source.length; i < n; ++i) {
|
|
3343
|
-
var value = this._source[i].next();
|
|
3344
|
-
if (value === undefined) {
|
|
3345
|
-
return undefined;
|
|
3346
|
-
}
|
|
3347
|
-
result[i] = value;
|
|
3348
|
-
}
|
|
3349
|
-
return result;
|
|
3350
|
-
};
|
|
3351
|
-
return ZipIterator;
|
|
3352
|
-
}());
|
|
3353
|
-
|
|
3354
|
-
|
|
3355
|
-
|
|
3356
|
-
|
|
3357
|
-
/***/ }),
|
|
3358
|
-
|
|
3359
|
-
/***/ 531:
|
|
3360
|
-
/***/ ((__unused_webpack_module, exports) => {
|
|
3361
|
-
|
|
3362
|
-
|
|
3363
|
-
// Copyright (c) Bloomberg Finance LP.
|
|
3364
|
-
// Distributed under the terms of the Modified BSD License.
|
|
3365
|
-
Object.defineProperty(exports, "__esModule", ({ value: true }));
|
|
3366
|
-
exports.PLUGIN_ID_SETTINGS = exports.PLUGIN_ID_DOC_MANAGER = exports.PLUGIN_ID_ROUTER = void 0;
|
|
3367
|
-
exports.PLUGIN_ID_ROUTER = '@jupyterlab/application-extension:router';
|
|
3368
|
-
exports.PLUGIN_ID_DOC_MANAGER = '@jupyterlab/docmanager-extension:plugin';
|
|
3369
|
-
exports.PLUGIN_ID_SETTINGS = '@jupyterlab/apputils-extension:settings';
|
|
3370
|
-
|
|
3371
|
-
|
|
3372
|
-
/***/ })
|
|
3373
|
-
|
|
3374
|
-
/******/ });
|
|
3375
|
-
/************************************************************************/
|
|
3376
|
-
/******/ // The module cache
|
|
3377
|
-
/******/ var __webpack_module_cache__ = {};
|
|
3378
|
-
/******/
|
|
3379
|
-
/******/ // The require function
|
|
3380
|
-
/******/ function __webpack_require__(moduleId) {
|
|
3381
|
-
/******/ // Check if module is in cache
|
|
3382
|
-
/******/ var cachedModule = __webpack_module_cache__[moduleId];
|
|
3383
|
-
/******/ if (cachedModule !== undefined) {
|
|
3384
|
-
/******/ return cachedModule.exports;
|
|
3385
|
-
/******/ }
|
|
3386
|
-
/******/ // Create a new module (and put it into the cache)
|
|
3387
|
-
/******/ var module = __webpack_module_cache__[moduleId] = {
|
|
3388
|
-
/******/ // no module.id needed
|
|
3389
|
-
/******/ // no module.loaded needed
|
|
3390
|
-
/******/ exports: {}
|
|
3391
|
-
/******/ };
|
|
3392
|
-
/******/
|
|
3393
|
-
/******/ // Execute the module function
|
|
3394
|
-
/******/ __webpack_modules__[moduleId](module, module.exports, __webpack_require__);
|
|
3395
|
-
/******/
|
|
3396
|
-
/******/ // Return the exports of the module
|
|
3397
|
-
/******/ return module.exports;
|
|
3398
|
-
/******/ }
|
|
3399
|
-
/******/
|
|
3400
|
-
/************************************************************************/
|
|
3401
|
-
/******/ /* webpack/runtime/define property getters */
|
|
3402
|
-
/******/ (() => {
|
|
3403
|
-
/******/ // define getter functions for harmony exports
|
|
3404
|
-
/******/ __webpack_require__.d = (exports, definition) => {
|
|
3405
|
-
/******/ for(var key in definition) {
|
|
3406
|
-
/******/ if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {
|
|
3407
|
-
/******/ Object.defineProperty(exports, key, { enumerable: true, get: definition[key] });
|
|
3408
|
-
/******/ }
|
|
3409
|
-
/******/ }
|
|
3410
|
-
/******/ };
|
|
3411
|
-
/******/ })();
|
|
3412
|
-
/******/
|
|
3413
|
-
/******/ /* webpack/runtime/hasOwnProperty shorthand */
|
|
3414
|
-
/******/ (() => {
|
|
3415
|
-
/******/ __webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop))
|
|
3416
|
-
/******/ })();
|
|
3417
|
-
/******/
|
|
3418
|
-
/******/ /* webpack/runtime/make namespace object */
|
|
3419
|
-
/******/ (() => {
|
|
3420
|
-
/******/ // define __esModule on exports
|
|
3421
|
-
/******/ __webpack_require__.r = (exports) => {
|
|
3422
|
-
/******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
|
|
3423
|
-
/******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
3424
|
-
/******/ }
|
|
3425
|
-
/******/ Object.defineProperty(exports, '__esModule', { value: true });
|
|
3426
|
-
/******/ };
|
|
3427
|
-
/******/ })();
|
|
3428
|
-
/******/
|
|
3429
|
-
/************************************************************************/
|
|
3430
|
-
var __webpack_exports__ = {};
|
|
3431
|
-
// This entry need to be wrapped in an IIFE because it need to be isolated against other modules in the chunk.
|
|
3432
|
-
(() => {
|
|
3433
|
-
var exports = __webpack_exports__;
|
|
3434
|
-
var __webpack_unused_export__;
|
|
3435
|
-
|
|
3436
|
-
/* eslint-disable @typescript-eslint/ban-types */
|
|
3437
|
-
// Copyright (c) Bloomberg Finance LP.
|
|
3438
|
-
// Distributed under the terms of the Modified BSD License.
|
|
3439
|
-
__webpack_unused_export__ = ({ value: true });
|
|
3440
|
-
__webpack_unused_export__ = void 0;
|
|
3441
|
-
const algorithm_1 = __webpack_require__(943);
|
|
3442
|
-
const tokens_1 = __webpack_require__(531);
|
|
3443
|
-
/**
|
|
3444
|
-
* Factory for element containing a given class xpath
|
|
3445
|
-
*
|
|
3446
|
-
* @param className Class name
|
|
3447
|
-
* @returns The selector
|
|
3448
|
-
*/
|
|
3449
|
-
function xpContainsClass(className) {
|
|
3450
|
-
return `contains(concat(" ", normalize-space(@class), " "), " ${className} ")`;
|
|
3451
|
-
}
|
|
3452
|
-
/**
|
|
3453
|
-
* In-Page Galata helpers
|
|
3454
|
-
*/
|
|
3455
|
-
class GalataInpage {
|
|
3456
|
-
constructor() {
|
|
3457
|
-
var _a;
|
|
3458
|
-
this._app = (_a = window.jupyterlab) !== null && _a !== void 0 ? _a : window.jupyterapp;
|
|
3459
|
-
}
|
|
3460
|
-
/**
|
|
3461
|
-
* Get an application plugin
|
|
3462
|
-
*
|
|
3463
|
-
* @param pluginId Plugin ID
|
|
3464
|
-
* @returns Application plugin
|
|
3465
|
-
*/
|
|
3466
|
-
async getPlugin(pluginId) {
|
|
3467
|
-
return new Promise((resolve, reject) => {
|
|
3468
|
-
const app = this._app;
|
|
3469
|
-
const hasPlugin = app.hasPlugin(pluginId);
|
|
3470
|
-
if (hasPlugin) {
|
|
3471
|
-
try {
|
|
3472
|
-
const appAny = app;
|
|
3473
|
-
const plugin = appAny._pluginMap
|
|
3474
|
-
? appAny._pluginMap[pluginId]
|
|
3475
|
-
: undefined;
|
|
3476
|
-
if (plugin.activated) {
|
|
3477
|
-
resolve(plugin.service);
|
|
3478
|
-
}
|
|
3479
|
-
else {
|
|
3480
|
-
void app.activatePlugin(pluginId).then(response => {
|
|
3481
|
-
resolve(plugin.service);
|
|
3482
|
-
});
|
|
3483
|
-
}
|
|
3484
|
-
}
|
|
3485
|
-
catch (error) {
|
|
3486
|
-
console.error('Failed to get plugin', error);
|
|
3487
|
-
}
|
|
3488
|
-
}
|
|
3489
|
-
});
|
|
3490
|
-
}
|
|
3491
|
-
/**
|
|
3492
|
-
* Wait for a function to finish for max. timeout milliseconds
|
|
3493
|
-
*
|
|
3494
|
-
* @param fn Function
|
|
3495
|
-
* @param timeout Timeout
|
|
3496
|
-
*/
|
|
3497
|
-
async waitForFunction(fn, timeout) {
|
|
3498
|
-
return new Promise((resolve, reject) => {
|
|
3499
|
-
let checkTimer = null;
|
|
3500
|
-
let timeoutTimer = null;
|
|
3501
|
-
const check = async () => {
|
|
3502
|
-
checkTimer = null;
|
|
3503
|
-
if (await Promise.resolve(fn())) {
|
|
3504
|
-
if (timeoutTimer) {
|
|
3505
|
-
clearTimeout(timeoutTimer);
|
|
3506
|
-
}
|
|
3507
|
-
resolve();
|
|
3508
|
-
}
|
|
3509
|
-
else {
|
|
3510
|
-
checkTimer = window.setTimeout(check, 200);
|
|
3511
|
-
}
|
|
3512
|
-
};
|
|
3513
|
-
void check();
|
|
3514
|
-
if (timeout) {
|
|
3515
|
-
timeoutTimer = window.setTimeout(() => {
|
|
3516
|
-
timeoutTimer = null;
|
|
3517
|
-
if (checkTimer) {
|
|
3518
|
-
clearTimeout(checkTimer);
|
|
3519
|
-
}
|
|
3520
|
-
reject(new Error('Timed out waiting for condition to be fulfilled.'));
|
|
3521
|
-
}, timeout);
|
|
3522
|
-
}
|
|
3523
|
-
});
|
|
3524
|
-
}
|
|
3525
|
-
/**
|
|
3526
|
-
* Waits for the given `timeout` in milliseconds.
|
|
3527
|
-
*
|
|
3528
|
-
* @param timeout A timeout to wait for
|
|
3529
|
-
*/
|
|
3530
|
-
async waitForTimeout(timeout) {
|
|
3531
|
-
return new Promise(resolve => {
|
|
3532
|
-
setTimeout(() => {
|
|
3533
|
-
resolve();
|
|
3534
|
-
}, timeout);
|
|
3535
|
-
});
|
|
3536
|
-
}
|
|
3537
|
-
/**
|
|
3538
|
-
* Wait for a condition to fulfill or for a certain time
|
|
3539
|
-
*
|
|
3540
|
-
* @param condition Condition or timeout to wait for
|
|
3541
|
-
* @param timeout Timeout
|
|
3542
|
-
*/
|
|
3543
|
-
async waitFor(condition, timeout) {
|
|
3544
|
-
const conditionType = typeof condition;
|
|
3545
|
-
if (conditionType === 'function') {
|
|
3546
|
-
return this.waitForFunction(condition, timeout);
|
|
3547
|
-
}
|
|
3548
|
-
else if (conditionType === 'number') {
|
|
3549
|
-
return this.waitForTimeout(condition);
|
|
3550
|
-
}
|
|
3551
|
-
}
|
|
3552
|
-
/**
|
|
3553
|
-
* Wait for the route to be on path and close all documents
|
|
3554
|
-
*
|
|
3555
|
-
* @param path Path to monitor
|
|
3556
|
-
*/
|
|
3557
|
-
async waitForLaunch(path = '/lab') {
|
|
3558
|
-
let resolver;
|
|
3559
|
-
const delegate = new Promise(resolve => {
|
|
3560
|
-
resolver = resolve;
|
|
3561
|
-
});
|
|
3562
|
-
const router = await this.getPlugin(tokens_1.PLUGIN_ID_ROUTER);
|
|
3563
|
-
const docManager = await this.getPlugin(tokens_1.PLUGIN_ID_DOC_MANAGER);
|
|
3564
|
-
const listener = async (sender, args) => {
|
|
3565
|
-
if (args.path === path) {
|
|
3566
|
-
router === null || router === void 0 ? void 0 : router.routed.disconnect(listener);
|
|
3567
|
-
await (docManager === null || docManager === void 0 ? void 0 : docManager.closeAll());
|
|
3568
|
-
resolver();
|
|
3569
|
-
}
|
|
3570
|
-
};
|
|
3571
|
-
router === null || router === void 0 ? void 0 : router.routed.connect(listener);
|
|
3572
|
-
return delegate;
|
|
3573
|
-
}
|
|
3574
|
-
/**
|
|
3575
|
-
* Wait for an element to be found from a CSS selector
|
|
3576
|
-
*
|
|
3577
|
-
* @param selector CSS selector
|
|
3578
|
-
* @param node Element
|
|
3579
|
-
* @param options Options
|
|
3580
|
-
* @returns Selected element
|
|
3581
|
-
*/
|
|
3582
|
-
async waitForSelector(selector, node, options) {
|
|
3583
|
-
const waitForHidden = options && options.hidden;
|
|
3584
|
-
return new Promise((resolve, reject) => {
|
|
3585
|
-
const timer = setInterval(() => {
|
|
3586
|
-
const parent = node || document;
|
|
3587
|
-
const found = parent.querySelector(selector);
|
|
3588
|
-
if (waitForHidden) {
|
|
3589
|
-
if (!found) {
|
|
3590
|
-
clearInterval(timer);
|
|
3591
|
-
resolve(null);
|
|
3592
|
-
}
|
|
3593
|
-
}
|
|
3594
|
-
else if (found) {
|
|
3595
|
-
clearInterval(timer);
|
|
3596
|
-
resolve(found);
|
|
3597
|
-
}
|
|
3598
|
-
}, 200);
|
|
3599
|
-
});
|
|
3600
|
-
}
|
|
3601
|
-
/**
|
|
3602
|
-
* Wait for an element to be found from a XPath
|
|
3603
|
-
*
|
|
3604
|
-
* @param selector CSS selector
|
|
3605
|
-
* @param node Element
|
|
3606
|
-
* @param options Options
|
|
3607
|
-
* @returns Selected element
|
|
3608
|
-
*/
|
|
3609
|
-
async waitForXPath(selector, node, options) {
|
|
3610
|
-
const waitForHidden = options && options.hidden;
|
|
3611
|
-
return new Promise((resolve, reject) => {
|
|
3612
|
-
const timer = setInterval(() => {
|
|
3613
|
-
const parent = node || document;
|
|
3614
|
-
const iterator = document.evaluate(selector, parent, null, XPathResult.UNORDERED_NODE_ITERATOR_TYPE, null);
|
|
3615
|
-
const found = iterator && iterator.iterateNext();
|
|
3616
|
-
if (waitForHidden) {
|
|
3617
|
-
if (!found) {
|
|
3618
|
-
clearInterval(timer);
|
|
3619
|
-
resolve(null);
|
|
3620
|
-
}
|
|
3621
|
-
}
|
|
3622
|
-
else if (found) {
|
|
3623
|
-
clearInterval(timer);
|
|
3624
|
-
resolve(found);
|
|
3625
|
-
}
|
|
3626
|
-
}, 200);
|
|
3627
|
-
});
|
|
3628
|
-
}
|
|
3629
|
-
/**
|
|
3630
|
-
* Delete all cells of the active notebook
|
|
3631
|
-
*/
|
|
3632
|
-
async deleteNotebookCells() {
|
|
3633
|
-
const nbPanel = this._app.shell.currentWidget;
|
|
3634
|
-
const nb = nbPanel.content;
|
|
3635
|
-
this._app.commands.execute('notebook:delete-cell');
|
|
3636
|
-
nb.update();
|
|
3637
|
-
}
|
|
3638
|
-
/**
|
|
3639
|
-
* Add a cell to the active notebook
|
|
3640
|
-
*
|
|
3641
|
-
* @param cellType Cell type
|
|
3642
|
-
* @param source Cell input source
|
|
3643
|
-
* @returns Action success result
|
|
3644
|
-
*/
|
|
3645
|
-
addNotebookCell(cellType, source) {
|
|
3646
|
-
const nbPanel = this._app.shell.currentWidget;
|
|
3647
|
-
const nb = nbPanel.content;
|
|
3648
|
-
if (nb !== null) {
|
|
3649
|
-
this._app.commands.execute('notebook:insert-cell-below');
|
|
3650
|
-
const numCells = nb.widgets.length;
|
|
3651
|
-
if (nb.model) {
|
|
3652
|
-
nb.model.cells.beginCompoundOperation();
|
|
3653
|
-
nb.model.cells.set(numCells - 1, nb.model.contentFactory.createCell(cellType, {
|
|
3654
|
-
cell: {
|
|
3655
|
-
cell_type: cellType,
|
|
3656
|
-
source: source,
|
|
3657
|
-
metadata: {}
|
|
3658
|
-
}
|
|
3659
|
-
}));
|
|
3660
|
-
nb.model.cells.endCompoundOperation();
|
|
3661
|
-
}
|
|
3662
|
-
nb.update();
|
|
3663
|
-
}
|
|
3664
|
-
else {
|
|
3665
|
-
return false;
|
|
3666
|
-
}
|
|
3667
|
-
return true;
|
|
3668
|
-
}
|
|
3669
|
-
/**
|
|
3670
|
-
* Set the type and content of a cell in the active notebook
|
|
3671
|
-
*
|
|
3672
|
-
* @param cellIndex Cell index
|
|
3673
|
-
* @param cellType Cell type
|
|
3674
|
-
* @param source Cell input source
|
|
3675
|
-
* @returns Action success status
|
|
3676
|
-
*/
|
|
3677
|
-
setNotebookCell(cellIndex, cellType, source) {
|
|
3678
|
-
const nbPanel = this._app.shell.currentWidget;
|
|
3679
|
-
const nb = nbPanel.content;
|
|
3680
|
-
if (nb) {
|
|
3681
|
-
const numCells = nb.widgets.length;
|
|
3682
|
-
if (cellIndex < 0 || cellIndex >= numCells) {
|
|
3683
|
-
return false;
|
|
3684
|
-
}
|
|
3685
|
-
if (nb.model) {
|
|
3686
|
-
nb.model.cells.beginCompoundOperation();
|
|
3687
|
-
nb.model.cells.set(cellIndex, nb.model.contentFactory.createCell(cellType, {
|
|
3688
|
-
cell: {
|
|
3689
|
-
cell_type: cellType,
|
|
3690
|
-
source: source,
|
|
3691
|
-
metadata: {}
|
|
3692
|
-
}
|
|
3693
|
-
}));
|
|
3694
|
-
nb.model.cells.endCompoundOperation();
|
|
3695
|
-
}
|
|
3696
|
-
nb.update();
|
|
3697
|
-
}
|
|
3698
|
-
else {
|
|
3699
|
-
return false;
|
|
3700
|
-
}
|
|
3701
|
-
return true;
|
|
3702
|
-
}
|
|
3703
|
-
/**
|
|
3704
|
-
* Test if a cell is selected in the active notebook
|
|
3705
|
-
*
|
|
3706
|
-
* @param cellIndex Cell index
|
|
3707
|
-
* @returns Whether the cell is selected or not
|
|
3708
|
-
*/
|
|
3709
|
-
isNotebookCellSelected(cellIndex) {
|
|
3710
|
-
const nbPanel = this._app.shell.currentWidget;
|
|
3711
|
-
const nb = nbPanel.content;
|
|
3712
|
-
const numCells = nb.widgets.length;
|
|
3713
|
-
if (cellIndex < 0 || cellIndex >= numCells) {
|
|
3714
|
-
return false;
|
|
3715
|
-
}
|
|
3716
|
-
return nb.isSelected(nb.widgets[cellIndex]);
|
|
3717
|
-
}
|
|
3718
|
-
/**
|
|
3719
|
-
* Save the active notebook
|
|
3720
|
-
*/
|
|
3721
|
-
async saveActiveNotebook() {
|
|
3722
|
-
const nbPanel = this._app.shell.currentWidget;
|
|
3723
|
-
await nbPanel.context.save();
|
|
3724
|
-
}
|
|
3725
|
-
/**
|
|
3726
|
-
* Run the active notebook
|
|
3727
|
-
*/
|
|
3728
|
-
async runActiveNotebook() {
|
|
3729
|
-
await this._app.commands.execute('notebook:run-all-cells');
|
|
3730
|
-
}
|
|
3731
|
-
/**
|
|
3732
|
-
* Wait for the active notebook to be run
|
|
3733
|
-
*/
|
|
3734
|
-
async waitForNotebookRun() {
|
|
3735
|
-
const nbPanel = this._app.shell.currentWidget;
|
|
3736
|
-
const notebook = nbPanel.content;
|
|
3737
|
-
if (!notebook.widgets) {
|
|
3738
|
-
console.error('NOTEBOOK CELL PROBLEM', notebook);
|
|
3739
|
-
}
|
|
3740
|
-
const numCells = notebook.widgets.length;
|
|
3741
|
-
if (numCells === 0) {
|
|
3742
|
-
return;
|
|
3743
|
-
}
|
|
3744
|
-
const promises = [];
|
|
3745
|
-
for (let i = 0; i < numCells; ++i) {
|
|
3746
|
-
const cell = notebook.widgets[i];
|
|
3747
|
-
promises.push(this.waitForCellRun(cell));
|
|
3748
|
-
}
|
|
3749
|
-
await Promise.all(promises);
|
|
3750
|
-
}
|
|
3751
|
-
/**
|
|
3752
|
-
* Wait for a Markdown cell to be rendered
|
|
3753
|
-
*
|
|
3754
|
-
* @param cell Cell
|
|
3755
|
-
*/
|
|
3756
|
-
async waitForMarkdownCellRendered(cell) {
|
|
3757
|
-
await cell.ready;
|
|
3758
|
-
let resolver;
|
|
3759
|
-
const delegate = new Promise(resolve => {
|
|
3760
|
-
resolver = resolve;
|
|
3761
|
-
});
|
|
3762
|
-
let timer = window.setInterval(() => {
|
|
3763
|
-
if (cell.rendered) {
|
|
3764
|
-
if (timer) {
|
|
3765
|
-
clearInterval(timer);
|
|
3766
|
-
}
|
|
3767
|
-
timer = null;
|
|
3768
|
-
resolver();
|
|
3769
|
-
}
|
|
3770
|
-
}, 200);
|
|
3771
|
-
return delegate;
|
|
3772
|
-
}
|
|
3773
|
-
/**
|
|
3774
|
-
* Wait for a cell to be run and return its output element
|
|
3775
|
-
*
|
|
3776
|
-
* @param cell Cell
|
|
3777
|
-
* @param timeout Timeout
|
|
3778
|
-
* @returns Output element
|
|
3779
|
-
*/
|
|
3780
|
-
async waitForCellRun(cell, timeout = 2000) {
|
|
3781
|
-
const model = cell.model;
|
|
3782
|
-
const code = model.value.text;
|
|
3783
|
-
if (!code.trim()) {
|
|
3784
|
-
return null;
|
|
3785
|
-
}
|
|
3786
|
-
const emptyPrompt = '[ ]:';
|
|
3787
|
-
const runningPrompt = '[*]:';
|
|
3788
|
-
await this.waitForXPath(`.//div[${xpContainsClass('jp-InputArea-prompt')} and text()="${emptyPrompt}"]`, cell.node, { hidden: true });
|
|
3789
|
-
await this.waitForXPath(`.//div[${xpContainsClass('jp-InputArea-prompt')} and text()="${runningPrompt}"]`, cell.node, { hidden: true });
|
|
3790
|
-
const cellType = cell.model.type;
|
|
3791
|
-
if (cellType === 'markdown') {
|
|
3792
|
-
await this.waitForMarkdownCellRendered(cell);
|
|
3793
|
-
return null;
|
|
3794
|
-
}
|
|
3795
|
-
else if (cellType === 'raw') {
|
|
3796
|
-
return null;
|
|
3797
|
-
}
|
|
3798
|
-
else {
|
|
3799
|
-
// 'code'
|
|
3800
|
-
let resolver;
|
|
3801
|
-
const delegate = new Promise(resolve => {
|
|
3802
|
-
resolver = resolve;
|
|
3803
|
-
});
|
|
3804
|
-
let numTries = 0;
|
|
3805
|
-
let timer = null;
|
|
3806
|
-
let timeoutTimer = null;
|
|
3807
|
-
const clearAndResolve = (output) => {
|
|
3808
|
-
clearInterval(timer);
|
|
3809
|
-
timer = null;
|
|
3810
|
-
clearTimeout(timeoutTimer);
|
|
3811
|
-
timeoutTimer = null;
|
|
3812
|
-
resolver(output);
|
|
3813
|
-
};
|
|
3814
|
-
const startTimeout = () => {
|
|
3815
|
-
if (!timeoutTimer) {
|
|
3816
|
-
timeoutTimer = setTimeout(() => {
|
|
3817
|
-
clearAndResolve(null);
|
|
3818
|
-
}, timeout);
|
|
3819
|
-
}
|
|
3820
|
-
};
|
|
3821
|
-
const checkIfDone = () => {
|
|
3822
|
-
const output = cell.node.querySelector('.jp-Cell-outputArea .jp-OutputArea-output');
|
|
3823
|
-
if (output) {
|
|
3824
|
-
if (output.textContent === 'Loading widget...') {
|
|
3825
|
-
startTimeout();
|
|
3826
|
-
}
|
|
3827
|
-
else {
|
|
3828
|
-
clearAndResolve(output);
|
|
3829
|
-
}
|
|
3830
|
-
}
|
|
3831
|
-
else {
|
|
3832
|
-
if (numTries > 0) {
|
|
3833
|
-
clearAndResolve(null);
|
|
3834
|
-
}
|
|
3835
|
-
}
|
|
3836
|
-
numTries++;
|
|
3837
|
-
};
|
|
3838
|
-
checkIfDone();
|
|
3839
|
-
timer = setInterval(() => {
|
|
3840
|
-
checkIfDone();
|
|
3841
|
-
}, 200);
|
|
3842
|
-
return delegate;
|
|
3843
|
-
}
|
|
3844
|
-
}
|
|
3845
|
-
/**
|
|
3846
|
-
* Whether the given notebook will scroll or not
|
|
3847
|
-
*
|
|
3848
|
-
* @param notebook Notebook
|
|
3849
|
-
* @param position Position
|
|
3850
|
-
* @param threshold Threshold
|
|
3851
|
-
* @returns Test result
|
|
3852
|
-
*/
|
|
3853
|
-
notebookWillScroll(notebook, position, threshold = 25) {
|
|
3854
|
-
const node = notebook.node;
|
|
3855
|
-
const ar = node.getBoundingClientRect();
|
|
3856
|
-
const delta = position - ar.top - ar.height / 2;
|
|
3857
|
-
return Math.abs(delta) > (ar.height * threshold) / 100;
|
|
3858
|
-
}
|
|
3859
|
-
/**
|
|
3860
|
-
* Run the active notebook cell by cell
|
|
3861
|
-
* and execute the callback after each cell execution
|
|
3862
|
-
*
|
|
3863
|
-
* @param callback Callback
|
|
3864
|
-
*/
|
|
3865
|
-
async runActiveNotebookCellByCell(callback) {
|
|
3866
|
-
const nbPanel = this._app.shell.currentWidget;
|
|
3867
|
-
const notebook = nbPanel.content;
|
|
3868
|
-
if (!notebook.widgets) {
|
|
3869
|
-
console.error('NOTEBOOK CELL PROBLEM', notebook);
|
|
3870
|
-
}
|
|
3871
|
-
const numCells = notebook.widgets.length;
|
|
3872
|
-
if (numCells === 0) {
|
|
3873
|
-
return;
|
|
3874
|
-
}
|
|
3875
|
-
this._app.commands.execute('notebook:deselect-all');
|
|
3876
|
-
for (let i = 0; i < numCells; ++i) {
|
|
3877
|
-
const cell = notebook.widgets[i];
|
|
3878
|
-
notebook.activeCellIndex = i;
|
|
3879
|
-
notebook.select(cell);
|
|
3880
|
-
await this._app.commands.execute('notebook:run-cell');
|
|
3881
|
-
const output = await this.waitForCellRun(cell);
|
|
3882
|
-
if (callback && callback.onAfterCellRun) {
|
|
3883
|
-
await callback.onAfterCellRun(i);
|
|
3884
|
-
}
|
|
3885
|
-
const rectNode = output
|
|
3886
|
-
? cell.node.querySelector('.jp-Cell-outputArea')
|
|
3887
|
-
: cell.inputArea.node;
|
|
3888
|
-
const rect = rectNode === null || rectNode === void 0 ? void 0 : rectNode.getBoundingClientRect();
|
|
3889
|
-
const scrollThreshold = 45;
|
|
3890
|
-
let prevScroll = notebook.node.scrollTop;
|
|
3891
|
-
let willScroll = false;
|
|
3892
|
-
if (rect) {
|
|
3893
|
-
willScroll = this.notebookWillScroll(notebook, rect.bottom, scrollThreshold);
|
|
3894
|
-
if (willScroll && callback && callback.onBeforeScroll) {
|
|
3895
|
-
await callback.onBeforeScroll();
|
|
3896
|
-
}
|
|
3897
|
-
prevScroll = notebook.node.scrollTop;
|
|
3898
|
-
notebook.scrollToPosition(rect.bottom, scrollThreshold);
|
|
3899
|
-
}
|
|
3900
|
-
notebook.update();
|
|
3901
|
-
if (willScroll && callback && callback.onAfterScroll) {
|
|
3902
|
-
const newScroll = notebook.node.scrollTop;
|
|
3903
|
-
if (newScroll !== prevScroll) {
|
|
3904
|
-
console.error('Notebook scroll mispredicted!');
|
|
3905
|
-
}
|
|
3906
|
-
await callback.onAfterScroll();
|
|
3907
|
-
}
|
|
3908
|
-
}
|
|
3909
|
-
}
|
|
3910
|
-
/**
|
|
3911
|
-
* Get the index of a toolbar item
|
|
3912
|
-
*
|
|
3913
|
-
* @param itemName Item name
|
|
3914
|
-
* @returns Index
|
|
3915
|
-
*/
|
|
3916
|
-
getNotebookToolbarItemIndex(itemName) {
|
|
3917
|
-
const nbPanel = this._app.shell.currentWidget;
|
|
3918
|
-
const names = (0, algorithm_1.toArray)(nbPanel.toolbar.names());
|
|
3919
|
-
return names.indexOf(itemName);
|
|
3920
|
-
}
|
|
3921
|
-
/**
|
|
3922
|
-
* Test if a element is visible or not
|
|
3923
|
-
*
|
|
3924
|
-
* @param el Element
|
|
3925
|
-
* @returns Test result
|
|
3926
|
-
*/
|
|
3927
|
-
isElementVisible(el) {
|
|
3928
|
-
return !!(el.offsetWidth || el.offsetHeight || el.getClientRects().length);
|
|
3929
|
-
}
|
|
3930
|
-
/**
|
|
3931
|
-
* Set the application theme
|
|
3932
|
-
*
|
|
3933
|
-
* @param themeName Theme name
|
|
3934
|
-
*/
|
|
3935
|
-
async setTheme(themeName) {
|
|
3936
|
-
await this._app.commands.execute('apputils:change-theme', {
|
|
3937
|
-
theme: themeName
|
|
3938
|
-
});
|
|
3939
|
-
await this.waitFor(async () => {
|
|
3940
|
-
return document.body.dataset.jpThemeName === themeName;
|
|
3941
|
-
});
|
|
3942
|
-
}
|
|
3943
|
-
/**
|
|
3944
|
-
* Application object
|
|
3945
|
-
*/
|
|
3946
|
-
get app() {
|
|
3947
|
-
return this._app;
|
|
3948
|
-
}
|
|
3949
|
-
}
|
|
3950
|
-
__webpack_unused_export__ = GalataInpage;
|
|
3951
|
-
window.galataip = new GalataInpage();
|
|
3952
|
-
|
|
3953
|
-
})();
|
|
3954
|
-
|
|
3955
|
-
/******/ })()
|
|
3956
|
-
;
|
|
3957
|
-
//# sourceMappingURL=inpage.js.map
|