import * as vscode from 'vscode' ; export function append < T > ( first , ... rest ) { if ( isThenable ( first ) ) { return appendAsync ( first , ... rest ) ; } else { return appendSyncAsync ( first , ... rest ) ; } } export function transform < T > ( obj , f ) { if ( isThenableStrict ( obj ) ) { return transformThenable ( obj , f ) ; } f ( obj ) ; return obj ; } export function transformPossiblyAsync < T > ( obj , f ) { if ( isThenableStrict ( obj ) ) { return transformPossiblyAsyncThenable ( obj , f ) ; } const transformer = f ( obj ) ; if ( transformer === true ) { return obj ; } else { return whenReady ( transformer , obj ) ; } } export function map < T , U > ( source , f ) { if ( isThenable ( source ) ) { return mapThenable ( source , f ) ; } if ( ! source ) { return source ; } return source . map ( f ) ; } function isThenable < T > ( r ) { return ! ! ( ( r as Thenable < T > ) . then ) ; } function isThenableStrict < T > ( r ) { return ! ! ( ( r as Thenable < T > ) . then ) ; } async function appendAsync < T > ( first , ... rest ) { return appendSyncAsync ( await first , ... rest ) ; } async function appendSyncAsync < T > ( first , ... rest ) { const f = first || [ ] ; const r = await Promise . all ( rest ) ; return f . concat ( ... r ) ; } async function transformThenable < T > ( obj , f ) { f ( await obj ) ; return obj ; } async function transformPossiblyAsyncThenable < T > ( obj , f ) { const transformer = f ( await obj ) ; if ( transformer === true ) { return obj ; } else { return whenReady ( transformer , obj ) ; } } async function mapThenable < T , U > ( obj , f ) { const sequence = await obj ; if ( ! sequence ) { return sequence ; } return sequence . map ( f ) ; } async function whenReady < T > ( w , obj ) { await w ; return obj ; }