import { isNil , isFunction } from '@terascope/utils' ; import { FieldTransformConfig , InitialFunctionContext , FunctionContext } from '../../../../function-configs/interfaces' ; import { DynamicFunctionAdapterContext , FunctionAdapterContext } from '../../interfaces' ; import { validateFunctionArgs } from '../../../argument-validator' ; export function wholeFieldTransformColumnExecution < T extends Record < string , any > = Record < string , unknown > > ( fnDef , configs ) { return function _wholeFieldTransformColumnExecution ( input ) : unknown [ ] { if ( ! Array . isArray ( input ) ) { throw new Error ( 'Invalid input, expected an array of values' ) ; } const results = [ ] ; if ( isFunction ( configs . args ) ) { const dynamicContext = { ... configs , parent : input , } as DynamicFunctionAdapterContext < T > ; input . forEach ( ( value , index ) => { const newArgs = dynamicContext . args ( index ) as T ; validateFunctionArgs ( fnDef , newArgs ) ; const fnConfig = { ... dynamicContext , args : newArgs as T , } ; const fn = fnDef . create ( fnConfig ) ; const newValue = fn ( value , index ) ; if ( ! isNil ( newValue ) ) { results . push ( newValue ) ; } else if ( configs . preserveNulls ) { results . push ( null ) ; } } ) ; return results ; } const functionContext = { ... configs , args : configs . args as T , parent : input } ; const fn = fnDef . create ( functionContext ) ; input . forEach ( ( value , index ) => { const newValue = fn ( value , index ) ; if ( ! isNil ( newValue ) ) { results . push ( newValue ) ; } else if ( configs . preserveNulls ) { results . push ( null ) ; } } ) ; return results ; } ; }