Source

helpers/apply-transforms/apply-transforms.ts

import get from "lodash.get";
import { Config } from "../../types/config";

/**
 * transforms are functions provided optionally by the user that "transform" the amp html string generated by ampLib
 * opts.transforms is an array of functions, executed left to right
 * output of function at 0th index is given as input to the function at index 1 and so on
 */
export function applyTransforms({ config, ampHtml }: ApplyTransformTypes): string {
  const transforms: TransformTypes = get(config, ["opts", "transforms"], []);
  return transforms.reduce(reducer, ampHtml);
}

function reducer(accumulator: string, transformFunction: (str: string) => string): string {
  return transformFunction.call(null, accumulator);
}

type TransformTypes = ((str: string) => string)[];
interface ApplyTransformTypes {
  config: Config;
  ampHtml: string;
}