提交 3d0558ef 编写于 作者: M Matt Bierner

Split coalesce into inplace and copy version

Using an overload makes the code more complex and prevents  patterns such as `.then(coalesce)` or `.map(coalesce)`. It also makes it clear which code paths are actually being most used in our code
上级 46d8bb80
......@@ -297,29 +297,30 @@ function topStep<T>(array: T[], compare: (a: T, b: T) => number, result: T[], i:
}
/**
* @returns a new array with all undefined or null values removed. The original array is not modified at all.
* @returns a new array with all falsy values removed. The original array IS NOT modified.
*/
export function coalesce<T>(array: (T | undefined | null)[]): T[];
export function coalesce<T>(array: (T | undefined | null)[], inplace: true): void;
export function coalesce<T>(array: (T | undefined | null)[], inplace?: true): void | T[] {
export function coalesce<T>(array: (T | undefined | null)[]): T[] {
if (!array) {
if (!inplace) {
return array;
}
return array;
}
if (!inplace) {
return <T[]>array.filter(e => !!e);
return <T[]>array.filter(e => !!e);
}
} else {
let to = 0;
for (let i = 0; i < array.length; i++) {
if (!!array[i]) {
array[to] = array[i];
to += 1;
}
/**
* Remove all falsey values from `array`. The original array IS modified.
*/
export function coalesceInPlace<T>(array: (T | undefined | null)[]): void {
if (!array) {
return;
}
let to = 0;
for (let i = 0; i < array.length; i++) {
if (!!array[i]) {
array[to] = array[i];
to += 1;
}
array.length = to;
}
array.length = to;
}
/**
......
......@@ -299,14 +299,14 @@ suite('Arrays', () => {
test('coalesce - inplace', function () {
let a = [null, 1, null, 2, 3];
arrays.coalesce(a, true);
arrays.coalesceInPlace(a);
assert.equal(a.length, 3);
assert.equal(a[0], 1);
assert.equal(a[1], 2);
assert.equal(a[2], 3);
a = [null, 1, null, void 0, undefined, 2, 3];
arrays.coalesce(a, true);
arrays.coalesceInPlace(a);
assert.equal(a.length, 3);
assert.equal(a[0], 1);
assert.equal(a[1], 2);
......@@ -316,7 +316,7 @@ suite('Arrays', () => {
b[10] = 1;
b[20] = 2;
b[30] = 3;
arrays.coalesce(b, true);
arrays.coalesceInPlace(b);
assert.equal(b.length, 3);
assert.equal(b[0], 1);
assert.equal(b[1], 2);
......@@ -331,7 +331,7 @@ suite('Arrays', () => {
assert.equal(sparse.length, 1002);
arrays.coalesce(sparse, true);
arrays.coalesceInPlace(sparse);
assert.equal(sparse.length, 5);
});
});
......
......@@ -3,7 +3,7 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { binarySearch, coalesce, isFalsyOrEmpty } from 'vs/base/common/arrays';
import { binarySearch, isFalsyOrEmpty, coalesceInPlace } from 'vs/base/common/arrays';
import { CancellationToken, CancellationTokenSource } from 'vs/base/common/cancellation';
import { first, forEach, size } from 'vs/base/common/collections';
import { onUnexpectedExternalError } from 'vs/base/common/errors';
......@@ -214,7 +214,7 @@ export class OutlineGroup extends TreeElement {
};
}
coalesce(markers, true);
coalesceInPlace(markers);
}
}
......
......@@ -30,7 +30,7 @@ function getDefinitions<T>(
});
return Promise.all(promises)
.then(flatten)
.then(references => coalesce(references));
.then(coalesce);
}
......
......@@ -24,7 +24,7 @@ export function getHover(model: ITextModel, position: Position, token: Cancellat
});
});
return Promise.all(promises).then(values => coalesce(values));
return Promise.all(promises).then(coalesce);
}
registerDefaultLanguageCommand('_executeHoverProvider', (model, position) => getHover(model, position, CancellationToken.None));
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册