import { IWorkspacesSection , IYarnWorkspacesConfig } from './types' ; param source export function uniq < T > ( source ) { const seen = [ ] ; for ( const s of source ) { if ( seen . includes ( s ) ) { continue ; } seen . push ( s ) ; } return seen ; } param source param iteratee export function filterMap < T , R > ( source , iteratee ) { const result = [ ] ; let index = 0 ; for ( const item of source ) { const mapped = iteratee ( item , index ++ ) ; if ( mapped === false ) continue ; result . push ( mapped ) ; } return result ; } param source export function shrinkObject < T extends object > ( source ) { const object = { } ; for ( const key in source ) { if ( typeof source [ key ] !== 'undefined' ) { object [ key ] = source [ key ] ; } } return object ; } param source export function mergeObjects < T > ( source ) { return source . reduce ( ( accum , next ) => ( { ... accum , ... next } ) , { } ) ; } param name export function typed ( name ) { return `@types/${ name }` ; } param name export function untyped ( name ) { const prefix = '@types/' ; if ( ! name . startsWith ( prefix ) ) { return name ; } name = name . substring ( prefix . length ) ; const splat = name . split ( '__' ) ; if ( splat . length === 2 ) { return `@${ splat [ 0 ] }/${ splat [ 1 ] }` ; } return name ; } param source export function orderObject < T > ( source , comparer ? ) { const keys = Object . keys ( source ) . sort ( comparer ) ; const result = { } ; for ( const key of keys ) { result [ key ] = ( source as any ) [ key ] ; } return result as T ; } param fn export function promisify ( fn ) { return function promisified ( ... args ) { return new Promise < any > ( ( resolve , reject ) => { fn ( ... args , function callback ( err , result ) { if ( arguments . length === 1 && typeof err === 'boolean' ) { return resolve ( err ) ; } return ! err ? resolve ( result ) : reject ( err ) ; } ) ; } ) ; } ; } param source export function flatten < T > ( source ) { const result = [ ] ; for ( const items of source ) { for ( const item of items ) { result . push ( item ) ; } } return result ; } param fn export function memoizeAsync < T , U extends any [ ] , V > ( fn ) { const cache = new Map < any , Promise < V > > ( ) ; async function run ( ... args ) { try { return await fn ( ... args ) ; } catch ( err ) { cache . delete ( args [ 0 ] ) ; throw err ; } } return async function ( ... args ) : Promise < V > { const key = args [ 0 ] ; if ( cache . has ( key ) ) { return cache . get ( key ) ! ; } const p = run ( ... args ) ; cache . set ( key , p ) ; return p ; } ; } param data export function ensureWorkspacesArray ( data ? ) { if ( ! data ) { return [ ] ; } if ( ! Array . isArray ( data ) ) { return ensureWorkspacesArray ( data . packages ) ; } if ( ! data . every ( ( s ) => typeof s === 'string' ) ) { return [ ] ; } return data ; }