I’m torso deep in re-writing the Booked front end for the 3.0 release. One of the big improvements will be the incorporation of modern JavaScript tools and patterns to replace the clunky JavaScript that currently handles client side functionality and dynamic rendering.

Most of the admin tools allow for CSV imports for data. I’m now using PapaParse to handle client side parsing of this information. But I really dislike the API where you have to provide callbacks for complete and error methods. So I made it into a Promise. This example is using TypeScript, but is easily translated.

export async function parseCsv<T>(file: File): Promise<ParseResult<T>> {
  return new Promise((resolve, reject) => {
    Papa.parse(file, {
      header: true,
      skipEmptyLines: true,
      transform: (value: string): string => {
        return value.trim();
      },
      complete: (results: ParseResult<T>) => {
        return resolve(results);
      },
      error: (error: ParseError) => {
        return reject(error);
      },
    });
  });
}

Which means I can now just call

const parsedCSVFile = await parseCsv(file);