@opensumi/ide-utils 2.21.13 → 2.22.0
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/lib/arrays.d.ts +3 -3
- package/lib/arrays.d.ts.map +1 -1
- package/lib/arrays.js.map +1 -1
- package/lib/async.d.ts +3 -3
- package/lib/async.d.ts.map +1 -1
- package/lib/buffer.js +4 -4
- package/lib/buffer.js.map +1 -1
- package/lib/character-classifier.js +3 -3
- package/lib/character-classifier.js.map +1 -1
- package/lib/decorators.d.ts +1 -1
- package/lib/decorators.d.ts.map +1 -1
- package/lib/encoding.d.ts +7 -1
- package/lib/encoding.d.ts.map +1 -1
- package/lib/encoding.js +15 -2
- package/lib/encoding.js.map +1 -1
- package/lib/errors.d.ts +2 -2
- package/lib/errors.d.ts.map +1 -1
- package/lib/event.d.ts +2 -2
- package/lib/event.d.ts.map +1 -1
- package/lib/filters.d.ts +3 -3
- package/lib/filters.d.ts.map +1 -1
- package/lib/filters.js +25 -25
- package/lib/filters.js.map +1 -1
- package/lib/glob.d.ts +2 -2
- package/lib/glob.d.ts.map +1 -1
- package/lib/glob.js +1 -1
- package/lib/glob.js.map +1 -1
- package/lib/hash.js +10 -10
- package/lib/hash.js.map +1 -1
- package/lib/iterator.d.ts +1 -1
- package/lib/iterator.d.ts.map +1 -1
- package/lib/linked-text.d.ts +1 -1
- package/lib/linked-text.d.ts.map +1 -1
- package/lib/linked-text.js.map +1 -1
- package/lib/map.js +17 -17
- package/lib/map.js.map +1 -1
- package/lib/objects.d.ts +1 -0
- package/lib/objects.d.ts.map +1 -1
- package/lib/objects.js +38 -1
- package/lib/objects.js.map +1 -1
- package/lib/os.d.ts +1 -8
- package/lib/os.d.ts.map +1 -1
- package/lib/os.js +4 -8
- package/lib/os.js.map +1 -1
- package/lib/path.d.ts +1 -1
- package/lib/path.d.ts.map +1 -1
- package/lib/path.js +29 -29
- package/lib/path.js.map +1 -1
- package/lib/platform.js +8 -8
- package/lib/platform.js.map +1 -1
- package/lib/process.d.ts.map +1 -1
- package/lib/process.js +28 -13
- package/lib/process.js.map +1 -1
- package/lib/progress.js +3 -3
- package/lib/progress.js.map +1 -1
- package/lib/strings.js +9 -9
- package/lib/strings.js.map +1 -1
- package/lib/types.d.ts +5 -1
- package/lib/types.d.ts.map +1 -1
- package/lib/types.js +8 -1
- package/lib/types.js.map +1 -1
- package/lib/uint.js +4 -4
- package/lib/uint.js.map +1 -1
- package/lib/uri.js +8 -8
- package/lib/uri.js.map +1 -1
- package/lib/uuid.d.ts +1 -1
- package/lib/uuid.d.ts.map +1 -1
- package/lib/uuid.js +3 -2
- package/lib/uuid.js.map +1 -1
- package/package.json +8 -6
- package/src/ansi.ts +10 -0
- package/src/argv.ts +57 -0
- package/src/arrays.ts +328 -0
- package/src/async.ts +580 -0
- package/src/buffer.ts +290 -0
- package/src/cache.ts +37 -0
- package/src/cancellation.ts +136 -0
- package/src/charCode.ts +425 -0
- package/src/character-classifier.ts +82 -0
- package/src/const/encoding.ts +242 -0
- package/src/const/index.ts +1 -0
- package/src/date.ts +36 -0
- package/src/decorators.ts +166 -0
- package/src/disposable.ts +377 -0
- package/src/encoding.ts +278 -0
- package/src/errors.ts +192 -0
- package/src/event.ts +1024 -0
- package/src/file-uri.ts +41 -0
- package/src/filters.ts +887 -0
- package/src/functional.ts +43 -0
- package/src/glob.ts +752 -0
- package/src/hash.ts +336 -0
- package/src/iconLabels.ts +149 -0
- package/src/index.ts +40 -0
- package/src/iterator.ts +36 -0
- package/src/lifecycle.ts +48 -0
- package/src/linked-list.ts +144 -0
- package/src/linked-text.ts +55 -0
- package/src/lru-map.ts +133 -0
- package/src/map.ts +934 -0
- package/src/marshalling.ts +62 -0
- package/src/objects.ts +145 -0
- package/src/os.ts +76 -0
- package/src/path.ts +1885 -0
- package/src/platform.ts +194 -0
- package/src/process.ts +43 -0
- package/src/progress.ts +83 -0
- package/src/promises.ts +23 -0
- package/src/sequence.ts +28 -0
- package/src/strings.ts +1002 -0
- package/src/types.ts +214 -0
- package/src/uint.ts +59 -0
- package/src/uri.ts +298 -0
- package/src/uuid.ts +6 -0
package/src/arrays.ts
ADDED
|
@@ -0,0 +1,328 @@
|
|
|
1
|
+
/* ---------------------------------------------------------------------------------------------
|
|
2
|
+
* Copyright (c) Microsoft Corporation. All rights reserved.
|
|
3
|
+
* Licensed under the MIT License. See License.txt in the project root for license information.
|
|
4
|
+
*--------------------------------------------------------------------------------------------*/
|
|
5
|
+
// Some code copied and modified from https://github.com/microsoft/vscode/blob/1.44.0/src/vs/base/common/arrays.ts
|
|
6
|
+
|
|
7
|
+
import { IDisposable } from './disposable';
|
|
8
|
+
import { ISplice } from './sequence';
|
|
9
|
+
|
|
10
|
+
export function asStringArray(array: unknown, defaultValue: string[]): string[] {
|
|
11
|
+
if (!Array.isArray(array)) {
|
|
12
|
+
return defaultValue;
|
|
13
|
+
}
|
|
14
|
+
if (!array.every((e) => typeof e === 'string')) {
|
|
15
|
+
return defaultValue;
|
|
16
|
+
}
|
|
17
|
+
return array;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function isNonEmptyArray<T>(obj: ReadonlyArray<T> | undefined | null): obj is Array<T> {
|
|
21
|
+
return Array.isArray(obj) && obj.length > 0;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* 移除给定数组中的重复值
|
|
25
|
+
* keyFn函数支持指定校验逻辑
|
|
26
|
+
*/
|
|
27
|
+
export function distinct<T>(array: ReadonlyArray<T>, keyFn?: (t: T) => string): T[] {
|
|
28
|
+
if (!keyFn) {
|
|
29
|
+
return array.filter((element, position) => array.indexOf(element) === position);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const seen: { [key: string]: boolean } = Object.create(null);
|
|
33
|
+
return array.filter((elem) => {
|
|
34
|
+
const key = keyFn(elem);
|
|
35
|
+
if (seen[key]) {
|
|
36
|
+
return false;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
seen[key] = true;
|
|
40
|
+
|
|
41
|
+
return true;
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
interface IMutableSplice<T> extends ISplice<T> {
|
|
46
|
+
deleteCount: number;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Diffs two *sorted* arrays and computes the splices which apply the diff.
|
|
51
|
+
*/
|
|
52
|
+
export function sortedDiff<T>(
|
|
53
|
+
before: ReadonlyArray<T>,
|
|
54
|
+
after: ReadonlyArray<T>,
|
|
55
|
+
compare: (a: T, b: T) => number,
|
|
56
|
+
): ISplice<T>[] {
|
|
57
|
+
const result: IMutableSplice<T>[] = [];
|
|
58
|
+
|
|
59
|
+
function pushSplice(start: number, deleteCount: number, toInsert: T[]): void {
|
|
60
|
+
if (deleteCount === 0 && toInsert.length === 0) {
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const latest = result[result.length - 1];
|
|
65
|
+
|
|
66
|
+
if (latest && latest.start + latest.deleteCount === start) {
|
|
67
|
+
latest.deleteCount += deleteCount;
|
|
68
|
+
latest.toInsert.push(...toInsert);
|
|
69
|
+
} else {
|
|
70
|
+
result.push({ start, deleteCount, toInsert });
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
let beforeIdx = 0;
|
|
75
|
+
let afterIdx = 0;
|
|
76
|
+
|
|
77
|
+
while (true) {
|
|
78
|
+
if (beforeIdx === before.length) {
|
|
79
|
+
pushSplice(beforeIdx, 0, after.slice(afterIdx));
|
|
80
|
+
break;
|
|
81
|
+
}
|
|
82
|
+
if (afterIdx === after.length) {
|
|
83
|
+
pushSplice(beforeIdx, before.length - beforeIdx, []);
|
|
84
|
+
break;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const beforeElement = before[beforeIdx];
|
|
88
|
+
const afterElement = after[afterIdx];
|
|
89
|
+
const n = compare(beforeElement, afterElement);
|
|
90
|
+
if (n === 0) {
|
|
91
|
+
// equal
|
|
92
|
+
beforeIdx += 1;
|
|
93
|
+
afterIdx += 1;
|
|
94
|
+
} else if (n < 0) {
|
|
95
|
+
// beforeElement is smaller -> before element removed
|
|
96
|
+
pushSplice(beforeIdx, 1, []);
|
|
97
|
+
beforeIdx += 1;
|
|
98
|
+
} else if (n > 0) {
|
|
99
|
+
// beforeElement is greater -> after element added
|
|
100
|
+
pushSplice(beforeIdx, 0, [afterElement]);
|
|
101
|
+
afterIdx += 1;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
return result;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
export function equals<T>(
|
|
109
|
+
one: ReadonlyArray<T> | undefined,
|
|
110
|
+
other: ReadonlyArray<T> | undefined,
|
|
111
|
+
itemEquals: (a: T, b: T) => boolean = (a, b) => a === b,
|
|
112
|
+
): boolean {
|
|
113
|
+
if (one === other) {
|
|
114
|
+
return true;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
if (!one || !other) {
|
|
118
|
+
return false;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
if (one.length !== other.length) {
|
|
122
|
+
return false;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
for (let i = 0, len = one.length; i < len; i++) {
|
|
126
|
+
if (!itemEquals(one[i], other[i])) {
|
|
127
|
+
return false;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
return true;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
export function asArray<T>(x: T | T[]): T[];
|
|
135
|
+
export function asArray<T>(x: T | readonly T[]): readonly T[];
|
|
136
|
+
export function asArray<T>(x: T | T[]): T[] {
|
|
137
|
+
return Array.isArray(x) ? x : [x];
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* 获取非空数组
|
|
142
|
+
* @param array
|
|
143
|
+
*/
|
|
144
|
+
export function coalesce<T>(array: ReadonlyArray<T | undefined | null>): T[] {
|
|
145
|
+
return array.filter((e) => !!e) as T[];
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
export function addElement<T>(array: Array<T>, element: T, unshift = false): IDisposable {
|
|
149
|
+
if (unshift) {
|
|
150
|
+
array.unshift(element);
|
|
151
|
+
} else {
|
|
152
|
+
array.push(element);
|
|
153
|
+
}
|
|
154
|
+
return {
|
|
155
|
+
dispose: () => {
|
|
156
|
+
const index = array.indexOf(element);
|
|
157
|
+
if (index !== -1) {
|
|
158
|
+
array.splice(index, 1);
|
|
159
|
+
}
|
|
160
|
+
},
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
export function addMapElement<K, T>(map: Map<K, T>, key: K, element: T): IDisposable {
|
|
165
|
+
map.set(key, element);
|
|
166
|
+
return {
|
|
167
|
+
dispose: () => {
|
|
168
|
+
if (map.get(key) === element) {
|
|
169
|
+
map.delete(key);
|
|
170
|
+
}
|
|
171
|
+
},
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
type NonFunctional<T> = T extends Function ? never : T;
|
|
176
|
+
|
|
177
|
+
// 枚举 value 转数组值
|
|
178
|
+
export function enumValueToArray<T extends object>(enumeration: T): NonFunctional<T[keyof T]>[] {
|
|
179
|
+
return Object.keys(enumeration)
|
|
180
|
+
.filter((key) => isNaN(Number(key)))
|
|
181
|
+
.map((key) => enumeration[key])
|
|
182
|
+
.filter((val) => typeof val === 'number' || typeof val === 'string');
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* @returns false if the provided object is an array and not empty.
|
|
187
|
+
*/
|
|
188
|
+
export function isFalsyOrEmpty(obj: any): boolean {
|
|
189
|
+
return !Array.isArray(obj) || obj.length === 0;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
export function flatten<T>(arr: T[][]): T[] {
|
|
193
|
+
return ([] as T[]).concat(...arr);
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
export function range(to: number): number[];
|
|
197
|
+
export function range(arg: number, to?: number): number[] {
|
|
198
|
+
let from = typeof to === 'number' ? arg : 0;
|
|
199
|
+
|
|
200
|
+
if (typeof to === 'number') {
|
|
201
|
+
from = arg;
|
|
202
|
+
} else {
|
|
203
|
+
from = 0;
|
|
204
|
+
to = arg;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
const result: number[] = [];
|
|
208
|
+
|
|
209
|
+
if (from <= to) {
|
|
210
|
+
for (let i = from; i < to; i++) {
|
|
211
|
+
result.push(i);
|
|
212
|
+
}
|
|
213
|
+
} else {
|
|
214
|
+
for (let i = from; i > to; i--) {
|
|
215
|
+
result.push(i);
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
return result;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
export function fill<T>(num: number, value: T, arr: T[] = []): T[] {
|
|
223
|
+
for (let i = 0; i < num; i++) {
|
|
224
|
+
arr[i] = value;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
return arr;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* Takes a sorted array and a function p. The array is sorted in such a way that all elements where p(x) is false
|
|
232
|
+
* are located before all elements where p(x) is true.
|
|
233
|
+
* @returns the least x for which p(x) is true or array.length if no element fullfills the given function.
|
|
234
|
+
*/
|
|
235
|
+
export function findFirstInSorted<T>(array: ReadonlyArray<T>, p: (x: T) => boolean): number {
|
|
236
|
+
let low = 0;
|
|
237
|
+
let high = array.length;
|
|
238
|
+
if (high === 0) {
|
|
239
|
+
return 0; // no children
|
|
240
|
+
}
|
|
241
|
+
while (low < high) {
|
|
242
|
+
const mid = Math.floor((low + high) / 2);
|
|
243
|
+
if (p(array[mid])) {
|
|
244
|
+
high = mid;
|
|
245
|
+
} else {
|
|
246
|
+
low = mid + 1;
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
return low;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
type Compare<T> = (a: T, b: T) => number;
|
|
253
|
+
|
|
254
|
+
function _merge<T>(a: T[], compare: Compare<T>, lo: number, mid: number, hi: number, aux: T[]): void {
|
|
255
|
+
let leftIdx = lo;
|
|
256
|
+
let rightIdx = mid + 1;
|
|
257
|
+
for (let i = lo; i <= hi; i++) {
|
|
258
|
+
aux[i] = a[i];
|
|
259
|
+
}
|
|
260
|
+
for (let i = lo; i <= hi; i++) {
|
|
261
|
+
if (leftIdx > mid) {
|
|
262
|
+
// left side consumed
|
|
263
|
+
a[i] = aux[rightIdx++];
|
|
264
|
+
} else if (rightIdx > hi) {
|
|
265
|
+
// right side consumed
|
|
266
|
+
a[i] = aux[leftIdx++];
|
|
267
|
+
} else if (compare(aux[rightIdx], aux[leftIdx]) < 0) {
|
|
268
|
+
// right element is less -> comes first
|
|
269
|
+
a[i] = aux[rightIdx++];
|
|
270
|
+
} else {
|
|
271
|
+
// left element comes first (less or equal)
|
|
272
|
+
a[i] = aux[leftIdx++];
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
function _sort<T>(a: T[], compare: Compare<T>, lo: number, hi: number, aux: T[]) {
|
|
278
|
+
if (hi <= lo) {
|
|
279
|
+
return;
|
|
280
|
+
}
|
|
281
|
+
const mid = (lo + (hi - lo) / 2) | 0;
|
|
282
|
+
_sort(a, compare, lo, mid, aux);
|
|
283
|
+
_sort(a, compare, mid + 1, hi, aux);
|
|
284
|
+
if (compare(a[mid], a[mid + 1]) <= 0) {
|
|
285
|
+
// left and right are sorted and if the last-left element is less
|
|
286
|
+
// or equals than the first-right element there is nothing else
|
|
287
|
+
// to do
|
|
288
|
+
return;
|
|
289
|
+
}
|
|
290
|
+
_merge(a, compare, lo, mid, hi, aux);
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
/**
|
|
294
|
+
* Like `Array#sort` but always stable. Usually runs a little slower `than Array#sort`
|
|
295
|
+
* so only use this when actually needing stable sort.
|
|
296
|
+
*/
|
|
297
|
+
export function mergeSort<T>(data: T[], compare: Compare<T>): T[] {
|
|
298
|
+
_sort(data, compare, 0, data.length - 1, []);
|
|
299
|
+
return data;
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
/**
|
|
303
|
+
* Returns the first mapped value of the array which is not undefined.
|
|
304
|
+
*/
|
|
305
|
+
export function mapFind<T, R>(array: Iterable<T>, mapFn: (value: T) => R | undefined): R | undefined {
|
|
306
|
+
for (const value of array) {
|
|
307
|
+
const mapped = mapFn(value);
|
|
308
|
+
if (mapped !== undefined) {
|
|
309
|
+
return mapped;
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
return undefined;
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
export function groupBy<T>(data: ReadonlyArray<T>, compare: (a: T, b: T) => number): T[][] {
|
|
317
|
+
const result: T[][] = [];
|
|
318
|
+
let currentGroup: T[] | undefined;
|
|
319
|
+
for (const element of data.slice(0).sort(compare)) {
|
|
320
|
+
if (!currentGroup || compare(currentGroup[0], element) !== 0) {
|
|
321
|
+
currentGroup = [element];
|
|
322
|
+
result.push(currentGroup);
|
|
323
|
+
} else {
|
|
324
|
+
currentGroup.push(element);
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
return result;
|
|
328
|
+
}
|