Path: blob/main/extensions/copilot/test/pipeline/alternativeAction/util.ts
13394 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45const DEBUG = true;67export function log(...args: any[]) {8if (DEBUG) {9console.log(...args);10}11}1213export function binarySearch<T>(14array: readonly T[],15compare: (element: T) => number16): number {17let left = 0;18let right = array.length - 1;19let lastLess = -1;2021while (left <= right) {22const mid = Math.floor((left + right) / 2);23const cmp = compare(array[mid]);2425if (cmp === 0) {26return mid;27} else if (cmp < 0) {28lastLess = mid;29left = mid + 1;30} else {31right = mid - 1;32}33}3435return lastLess;36}3738//#region Either3940export type Either<L, R> = Left<L> | Right<R>;4142export namespace Either {43export function left<L>(value: L): Left<L> {44return new Left(value);45}4647export function right<R>(value: R): Right<R> {48return new Right(value);49}50}5152/**53* To instantiate a Left, use `Either.left(value)`.54* To instantiate a Right, use `Either.right(value)`.55*/56class Left<L> {57constructor(readonly value: L) { }5859isLeft(): this is Left<L> {60return true;61}6263isRight(): this is Right<never> {64return false;65}66}6768/**69* To instantiate a Left, use `Either.left(value)`.70* To instantiate a Right, use `Either.right(value)`.71*/72class Right<R> {73constructor(readonly value: R) { }7475isLeft(): this is Left<never> {76return false;77}7879isRight(): this is Right<R> {80return true;81}82}8384//#endregion858687