import { getParenthesesRange } from './text' ; const gradientLength = 'gradient' . length ; const conicGradient = 'conic-' ; const conicGradientLength = conicGradient . length ; const radialGradient = 'radial-' ; const linearGradient = 'linear-' ; export interface parsedGradient { type : string ; content : string ; hasComma : boolean ; } export function parseGradient ( value ) { const result = [ ] ; let index = 0 ; let startIndex = conicGradient . length ; while ( ( index = value . indexOf ( 'gradient' , startIndex ) ) !== - 1 ) { let typeGradient ; [ linearGradient , radialGradient , conicGradient ] . find ( ( possibleType ) => { if ( index - possibleType . length >= 0 ) { const possibleGradient = value . substring ( index - possibleType . length , index ) ; if ( possibleGradient === possibleType ) { if ( index - possibleType . length - 1 >= 9 ) { if ( value [ index - possibleType . length - 1 ] === '-' ) { typeGradient = `repeating-${ possibleType }gradient` ; return true ; } } typeGradient = `${ possibleType }gradient` ; return true ; } } } ) ; if ( ! typeGradient ) { break ; } const { start , end } = getParenthesesRange ( value , index + gradientLength ) ; const content = value . substring ( start + 1 , end - 1 ) ; startIndex = end + 1 + conicGradientLength ; result . push ( { type : typeGradient , content , hasComma : true , } ) ; } if ( result . length ) { result [ result . length - 1 ] . hasComma = false ; } return result ; }