Перейти к основному содержимому
← ⚡ JavaScript & the browser·Модуль C6 · Урок 10
ЗаданиеWrite function* parseCsv(text) yielding parsed rows as objects (headers from first line). Given const csv = 'name,age\nAnna,30\nBob,25\nClara,40', use for-of to sum the age column (parse as int). Log the sum.

Capstone: streaming CSV parser

275 XP15 min
Теория

Putting C6 together

A streaming CSV parser is the perfect generator capstone — it walks a string line by line, yielding one parsed row at a time. No need to materialize the whole table in memory.

The shape:

function* parseCsv(text) {
  const lines = text.trim().split("\n");
  const headers = lines[0].split(",");
  for (let i = 1; i < lines.length; i++) {
    const values = lines[i].split(",");
    const row = {};
    for (let j = 0; j < headers.length; j++) {
      row[headers[j]] = values[j];
    }
    yield row;
  }
}

You consume it with for-of. The capstone task takes a hard-coded CSV string, parses it, and sums a numeric column.

Why this matters

  • The same generator shape works on ReadableStream chunks from fetch — turns a 100MB CSV download into a row-by-row stream.
  • Async generators handle the streaming version (real fetch + TextDecoderStream).
  • You only need C6-04 + C6-09 to do it. Generators are not theoretical.
🔒

Зарегистрируйся чтобы кодить

Теория открыта всем. Интерактивный редактор, живой превью и проверка открываются с 7-дневным бесплатным trial — нужна карта, отмена в любое время.

Регистрация — бесплатный trial →

Первые 10 уроков в каждом треке — бесплатные. Карта не нужна.

← НазадСледующий урок →

Получай один совет по Python или веб-разработке — каждый день в почте

Коротко, без воды, без спама. Отписаться — в один клик.