File size: 943 Bytes
4e1096a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
export type Preprocessor = (text: string) => string;
export type TextSubstitutions = Record<string, string>;

const defaultSubstitutions: TextSubstitutions = {
  Cover: 'The Cover',
  Dedication: 'Dedication Page',
  Acknowledgements: 'The Acknowledgements',
};

export const basicSubstitute = (text: string, substitutions: TextSubstitutions): string => {
  const allSubstitutions = { ...defaultSubstitutions, ...substitutions };

  if (Object.keys(allSubstitutions).length === 0) {
    return text;
  }

  if (allSubstitutions[text]) {
    return allSubstitutions[text];
  }

  return text;
};

export function createPreprocessor(substitutions: TextSubstitutions = {}): Preprocessor {
  return (text: string) => basicSubstitute(text, substitutions);
}

export function preprocess(texts: string[], substitutions: TextSubstitutions = {}): string[] {
  const preprocessor = createPreprocessor(substitutions);
  return texts.map(preprocessor);
}