import { LangConfig } from './extension' ; export interface Options { shouldRemoveDuplicates : boolean ; shouldPrependCustomClasses : boolean ; customTailwindPrefix : string ; separator ? : RegExp ; replacement ? : string ; } param classString param sortOrder returns export const sortClassString = ( classString , sortOrder , options ) : string => { let classArray = classString . split ( options . separator || /\s+/g ) ; if ( options . shouldRemoveDuplicates ) { classArray = removeDuplicates ( classArray ) ; } const sortOrderClone = [ ... sortOrder ] ; if ( options . customTailwindPrefix . length > 0 ) { for ( var i = 0 ; i < sortOrderClone . length ; i ++ ) { sortOrderClone [ i ] = options . customTailwindPrefix + sortOrderClone [ i ] ; } } classArray = sortClassArray ( classArray , sortOrderClone , options . shouldPrependCustomClasses ) ; return classArray . join ( options . replacement || ' ' ) . trim ( ) ; } ; const sortClassArray = ( classArray , sortOrder , shouldPrependCustomClasses ) : string [ ] => [ ... classArray . filter ( ( el ) => shouldPrependCustomClasses && sortOrder . indexOf ( el ) === - 1 ) , ... classArray . filter ( ( el ) => sortOrder . indexOf ( el ) !== - 1 ) . sort ( ( a , b ) => sortOrder . indexOf ( a ) - sortOrder . indexOf ( b ) ) , ... classArray . filter ( ( el ) => ! shouldPrependCustomClasses && sortOrder . indexOf ( el ) === - 1 ) , ] ; const removeDuplicates = ( classArray ) : string [ ] => [ ... new Set ( classArray ) , ] ; function isArrayOfStrings ( value ) { return ( Array . isArray ( value ) && value . every ( ( item ) => typeof item === 'string' ) ) ; } export type Matcher = { regex : RegExp [ ] ; separator ? : RegExp ; replacement ? : string ; } ; function buildMatcher ( value ) { if ( typeof value === 'string' ) { return { regex : [ new RegExp ( value , 'gi' ) ] , } ; } else if ( isArrayOfStrings ( value ) ) { return { regex : value . map ( ( v ) => new RegExp ( v , 'gi' ) ) , } ; } else if ( value == undefined ) { return { regex : [ ] , } ; } else { return { regex : typeof value . regex === 'string' ? [ new RegExp ( value . regex , 'gi' ) ] : isArrayOfStrings ( value . regex ) ? value . regex . map ( ( v ) => new RegExp ( v , 'gi' ) ) : [ ] , separator : typeof value . separator === 'string' ? new RegExp ( value . separator , 'g' ) : undefined , replacement : value . replacement || value . separator , } ; } } export function buildMatchers ( value ) { if ( value == undefined ) { return [ ] ; } else if ( Array . isArray ( value ) ) { if ( ! value . length ) { return [ ] ; } else if ( ! isArrayOfStrings ( value ) ) { return value . map ( ( v ) => buildMatcher ( v ) ) ; } } return [ buildMatcher ( value ) ] ; } export function getTextMatch ( regexes , text , callback , startPosition = 0 ) { if ( regexes . length >= 1 ) { let wrapper ; while ( ( wrapper = regexes [ 0 ] . exec ( text ) ) !== null ) { const wrapperMatch = wrapper [ 0 ] ; const valueMatchIndex = wrapper . findIndex ( ( match , idx ) => idx !== 0 && match ) ; const valueMatch = wrapper [ valueMatchIndex ] ; const newStartPosition = startPosition + wrapper . index + wrapperMatch . lastIndexOf ( valueMatch ) ; if ( regexes . length === 1 ) { callback ( valueMatch , newStartPosition ) ; } else { getTextMatch ( regexes . slice ( 1 ) , valueMatch , callback , newStartPosition ) ; } } } }