score:0

Accepted answer
const tokens = input.split(/\s+/g);

const output = [["period end", "date", "full name", "hours", "tracking name", "totalhours", "task 1 hours", "task 2 hours", "task 3 hours", "task 4 hours", "task 5 hours", "task 6 hours", "task 7 hours", "task 8 hours"]];

for (let origin = 0, index = 0; index < tokens.length; index++) {
    // find date range
    if (tokens.slice(index, index + 3).join(" ").match(/\d{1,2}\/\d{1,2}\/\d{4} - \d{1,2}\/\d{1,2}\/\d{4}/g)) {
        if (origin !== 0) {
            output.push([tokens.slice(origin, origin + 3).join(" "), ...tokens.slice(origin + 3, index)]);
        }

        origin = index;

        index += 2;
    }
}

for (const row of output) {
    // i get to be lazy, you have to use a library.
    console.log(row.join(","));
}

edit: i realized that this isn't actually capturing the last line of data, but i didn't want to rely on a fixed token-width for rows, so i'll leave it as an exercise for the reader to determine when you want the last line to end.


Related Query