import NormalisationRuleStore from './normalisation-rule-store' ; import { LoadedNormalisationRule } from './types/normalisation-rule' ; export default class TextProcessRuleApplier { constructor ( private readonly normalisationRuleStore ) { } applyTo ( text ) { const rules = this . normalisationRuleStore . activeRules ; return rules . length !== 0 ? this . applyRulesToText ( rules , text ) : text ; } private applyRulesToText ( rules , text ) { return rules . reduce ( ( newText , rule ) => this . applyRuleToText ( rule , newText ) , text ) ; } private applyRuleToText ( rule , text ) { const pattern = new RegExp ( rule . match , 'g' ) ; if ( typeof rule . replaceWith === 'string' ) { return text . replace ( pattern , rule . replaceWith ) ; } return text . replace ( pattern , matched => { if ( typeof rule . replaceWith === 'string' ) return matched ; switch ( rule . replaceWith . letterCase ) { case 'lower' : return matched . toLowerCase ( ) ; case 'upper' : return matched . toUpperCase ( ) ; default : return matched ; } } ) ; } }