Add firstOrDefault helper

Array accesses are not strict null checked. Added a type checked helper for when you just want to get the first element of an array of fallback to a default if the array is empty
This commit is contained in:
Matt Bierner
2019-09-17 16:38:27 -07:00
parent 766352d992
commit 36b66c90e9
5 changed files with 15 additions and 30 deletions

View File

@@ -19,7 +19,7 @@ import { ICompositeControl } from 'vs/workbench/common/composite';
import { ActionRunner, IAction } from 'vs/base/common/actions';
import { IFileService } from 'vs/platform/files/common/files';
import { IPathData } from 'vs/platform/windows/common/windows';
import { coalesce } from 'vs/base/common/arrays';
import { coalesce, firstOrDefault } from 'vs/base/common/arrays';
export const ActiveEditorContext = new RawContextKey<string | null>('activeEditor', null);
export const ActiveEditorIsSaveableContext = new RawContextKey<boolean>('activeEditorIsSaveable', false);
@@ -384,11 +384,7 @@ export abstract class EditorInput extends Disposable implements IEditorInput {
* for the input. This allows subclasses to decide late which editor to use for the input on a case by case basis.
*/
getPreferredEditorId(candidates: string[]): string | undefined {
if (candidates.length > 0) {
return candidates[0];
}
return undefined;
return firstOrDefault(candidates);
}
/**