import memoize from './vendor/fastMemoize' ; import { getRules } from './rules' ; const defaultOptions = { prefix : '' , jit : true , ruleLookupCache : true , ignoreCssVariables : true , } ; export type Options = { prefix : string ; jit : boolean ; ruleLookupCache : boolean ; ignoreCssVariables : boolean ; } ; const isCssVariable = ( className ) => { return className . startsWith ( '--' ) ; } ; const findTailwindPropertiesRaw = ( className , options ) => { return getRules ( options ) . find ( ( rule ) => rule . regex . test ( className ) ) ?. properties ?. filter ( ( property ) => ! options . ignoreCssVariables || ! isCssVariable ( property ) ) ; } ; const findTailwindPropertiesMemoized = memoize ( findTailwindPropertiesRaw ) ; export const findTailwindProperties = ( className , options = defaultOptions ) => options . ruleLookupCache ? findTailwindPropertiesMemoized ( className , options ) : findTailwindPropertiesRaw ( className , options ) ; const tailWindPremableEndIndex = ( className ) => className . lastIndexOf ( ':' ) ; const tailWindPremable = ( className ) => { const index = tailWindPremableEndIndex ( className ) ; return index === - 1 ? '' : className . substring ( 0 , index ) ; } ; const stripTailwindPremable = ( className ) : string => { return className . substring ( tailWindPremableEndIndex ( className ) + 1 ) ; } ; const stripTailwindImportant = ( className ) : string => { return className . replace ( /^!/g , '' ) ; } ; const stripTailwindOpacity = ( className ) : string => { return className . replace ( /\/(\d|\.|\[|\])+$/g , '' ) ; } ; const stripeTailwindJit = ( className ) : string => { return stripTailwindOpacity ( stripTailwindImportant ( className ) ) ; } ; const getTailwindClassName = ( className , options ) : string => { const strippedClassName = stripTailwindPremable ( className ) ; return options . jit ? stripeTailwindJit ( strippedClassName ) : strippedClassName ; } ; const isTailwindClash = ( classA , classB , options ) => { const premableA = tailWindPremable ( classA ) ; const premableB = tailWindPremable ( classB ) ; const classAProperties = findTailwindProperties ( getTailwindClassName ( classA , options ) , options ) ; const classBProperties = findTailwindProperties ( getTailwindClassName ( classB , options ) , options ) ; return classAProperties && classBProperties && premableA === premableB && classAProperties . some ( ( property ) => classBProperties . includes ( property ) ) ; } ; export const overrideTailwindClasses = ( classNamesString , optionsArgs = defaultOptions ) => { const options = { ... defaultOptions , ... optionsArgs } ; const classNames = classNamesString . split ( /\s+/ ) ; return classNames . reduce ( ( resultSoFar , className ) => { const nonClashingClasses = resultSoFar . filter ( ( r ) => ! isTailwindClash ( className , r , options ) ) ; return [ ... nonClashingClasses , className ] ; } , [ ] ) . join ( ' ' ) ; } ;