import { ICommands , SearchFn , IOption , ICommandArgs } from './types' ; export const commandOptions = ( options ) : Array < IOption > => Object . keys ( options . commands ) . reduce ( ( acc , key ) => { if ( ! options . search || options . searchFn ( { source : key , search : options . search } ) ) { const { sliceStart , sliceEnd , inputValue } = options ; const newInputValueStart = inputValue && sliceStart !== undefined ? inputValue . slice ( 0 , sliceStart ) + key : key ; const newInputValue = newInputValueStart + ( inputValue && sliceEnd !== undefined ? inputValue . slice ( sliceEnd ) : '' ) ; acc . push ( { value : key , data : options . commands [ key ] , inputValue : newInputValue , cursorTarget : newInputValueStart . length , searchValue : options . search , } ) ; } return acc ; } , [ ] ) ; export const argsOptions = ( options ) : Array < IOption > => { const search = options . search ? options . search ?. replace ( /^-(-?)/ , '' ) : undefined ; return Object . keys ( options . args ) . reduce ( ( acc , key ) => { if ( options . exclude && options . exclude . includes ( key ) ) { return acc ; } if ( ! search || options . searchFn ( { source : key , search } ) ) { const value = `--${ key }` ; const { sliceStart , sliceEnd , inputValue } = options ; const newInputValueStart = inputValue && sliceStart !== undefined ? inputValue . slice ( 0 , sliceStart ) + value : value ; const newInputValue = newInputValueStart + ( inputValue && sliceEnd !== undefined ? inputValue . slice ( sliceEnd ) : '' ) ; acc . push ( { value , data : options . args [ key ] , inputValue : newInputValue , cursorTarget : newInputValueStart . length , searchValue : search , } ) ; } return acc ; } , [ ] ) ; } ; export const valueOptions = < V extends { value : string ; } > ( options ) : Array < IOption > => options . options . reduce ( ( acc , option ) => { const key = option . value ; if ( ! options . search || options . searchFn ( { source : key , search : options . search } ) ) { const { sliceStart , sliceEnd , inputValue } = options ; const newInputValueStart = inputValue && sliceStart !== undefined ? inputValue . slice ( 0 , sliceStart ) + key : key ; const newInputValue = newInputValueStart + ( inputValue && sliceEnd !== undefined ? inputValue . slice ( sliceEnd ) : '' ) ; acc . push ( { value : key , data : option , inputValue : newInputValue , cursorTarget : newInputValueStart . length , searchValue : options . search , } ) ; } return acc ; } , [ ] ) ;