import { IFnDef , TArgDef } from '../../typings' ; import { createFullMatch } from '../const/regs' ; import { useEnum } from './use-enum' ; import { useReg } from './use-reg' ; const funcReg = createFullMatch ( '([^(]+)\\((.+)\\)' ) ; const checkArg = ( argDef , val ) => { if ( argDef . type === 'enum' ) { return useEnum ( argDef . value , val ) ; } else if ( argDef . type === 'reg' ) { return useReg ( argDef . value , val ) ; } else { return argDef . value === val ; } } ; export const useFunc = ( fnDef , val ) : boolean => { const fn = funcReg . exec ( val ) ; if ( ! fn ) return false ; if ( fn [ 1 ] !== fnDef . name ) return false ; const values = fn [ 2 ] . split ( ',' ) . map ( s => s . trim ( ) ) ; if ( values . length > fnDef . args . length ) { return false ; } for ( let i = 0 ; i < values . length ; i ++ ) { const argDef = fnDef . args [ i ] ; if ( Array . isArray ( argDef . def ) ) { if ( ! argDef . def . some ( adef => checkArg ( adef , values [ i ] ) ) ) { return false ; } } else { if ( ! checkArg ( argDef . def , values [ i ] ) ) { return false ; } } } if ( values . length < fnDef . args . length ) { const argDef = fnDef . args [ values . length ] ; return ! ! argDef . optional ; } return true ; } ;