'use strict';

const trim = (s) => s.match(/\W*(.+)\W*/)[1];

const productFromCSV = (productString) => {
  const [name, category, price] = productString.split(', ');
  return {
    name,
    category,
    price,
  };
};

const productsFromCSV = (csv) =>
  csv.split('\n').slice(1).map(trim).map(productFromCSV);

const productsCSV = `name, category, price
  Klingon Letter Opener, Office Warfare, 19.99
  Backpack of Holding, Travel, 29.99
  Tardis Alarmclock, Merchandise, 15.99`;

const products = productsFromCSV(productsCSV);
console.log(products);
