提交 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,20 +297,22 @@ function topStep<T>(array: T[], compare: (a: T, b: T) => number, result: T[], i: ...@@ -297,20 +297,22 @@ 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)[]): T[] {
export function coalesce<T>(array: (T | undefined | null)[], inplace: true): void;
export function coalesce<T>(array: (T | undefined | null)[], inplace?: true): void | T[] {
if (!array) { if (!array) {
if (!inplace) {
return array; return array;
} }
}
if (!inplace) {
return <T[]>array.filter(e => !!e); return <T[]>array.filter(e => !!e);
}
} else { /**
* 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; let to = 0;
for (let i = 0; i < array.length; i++) { for (let i = 0; i < array.length; i++) {
if (!!array[i]) { if (!!array[i]) {
...@@ -319,7 +321,6 @@ export function coalesce<T>(array: (T | undefined | null)[], inplace?: true): vo ...@@ -319,7 +321,6 @@ export function coalesce<T>(array: (T | undefined | null)[], inplace?: true): vo
} }
} }
array.length = to; array.length = to;
}
} }
/** /**
......
...@@ -299,14 +299,14 @@ suite('Arrays', () => { ...@@ -299,14 +299,14 @@ suite('Arrays', () => {
test('coalesce - inplace', function () { test('coalesce - inplace', function () {
let a = [null, 1, null, 2, 3]; let a = [null, 1, null, 2, 3];
arrays.coalesce(a, true); arrays.coalesceInPlace(a);
assert.equal(a.length, 3); assert.equal(a.length, 3);
assert.equal(a[0], 1); assert.equal(a[0], 1);
assert.equal(a[1], 2); assert.equal(a[1], 2);
assert.equal(a[2], 3); assert.equal(a[2], 3);
a = [null, 1, null, void 0, undefined, 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.length, 3);
assert.equal(a[0], 1); assert.equal(a[0], 1);
assert.equal(a[1], 2); assert.equal(a[1], 2);
...@@ -316,7 +316,7 @@ suite('Arrays', () => { ...@@ -316,7 +316,7 @@ suite('Arrays', () => {
b[10] = 1; b[10] = 1;
b[20] = 2; b[20] = 2;
b[30] = 3; b[30] = 3;
arrays.coalesce(b, true); arrays.coalesceInPlace(b);
assert.equal(b.length, 3); assert.equal(b.length, 3);
assert.equal(b[0], 1); assert.equal(b[0], 1);
assert.equal(b[1], 2); assert.equal(b[1], 2);
...@@ -331,7 +331,7 @@ suite('Arrays', () => { ...@@ -331,7 +331,7 @@ suite('Arrays', () => {
assert.equal(sparse.length, 1002); assert.equal(sparse.length, 1002);
arrays.coalesce(sparse, true); arrays.coalesceInPlace(sparse);
assert.equal(sparse.length, 5); assert.equal(sparse.length, 5);
}); });
}); });
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
* Licensed under the MIT License. See License.txt in the project root for license information. * 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 { CancellationToken, CancellationTokenSource } from 'vs/base/common/cancellation';
import { first, forEach, size } from 'vs/base/common/collections'; import { first, forEach, size } from 'vs/base/common/collections';
import { onUnexpectedExternalError } from 'vs/base/common/errors'; import { onUnexpectedExternalError } from 'vs/base/common/errors';
...@@ -214,7 +214,7 @@ export class OutlineGroup extends TreeElement { ...@@ -214,7 +214,7 @@ export class OutlineGroup extends TreeElement {
}; };
} }
coalesce(markers, true); coalesceInPlace(markers);
} }
} }
......
...@@ -30,7 +30,7 @@ function getDefinitions<T>( ...@@ -30,7 +30,7 @@ function getDefinitions<T>(
}); });
return Promise.all(promises) return Promise.all(promises)
.then(flatten) .then(flatten)
.then(references => coalesce(references)); .then(coalesce);
} }
......
...@@ -24,7 +24,7 @@ export function getHover(model: ITextModel, position: Position, token: Cancellat ...@@ -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)); 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.
先完成此消息的编辑!
想要评论请 注册