Javascript: Get data from positioned table

Date: 2021-09-20
(() => {
    function* getDataFromPositionedTable(tableSource) {
        let lines = String(tableSource).matchAll(/^.*$/gm); // split('\n');
        const headerLine = String(lines.next().value[0]);
        const columnsRegex = /([^\s]+[\s]+)/g;
        const columns = Array.from(headerLine.matchAll(columnsRegex)).map(m => ({ name: m[1].trim(), start: m.index, end: m.index + m[1].length }));

        for (const [line] of lines) {
            if (line.length < headerLine.length) continue;
            const item = {};
            for (const c of columns) {
                item[c.name] = line.substring(c.start, c.end).trim();
            }
            yield item;
        }
    }

    const text = document.getElementById("t1").value;
    const data2 = getDataFromPositionedTable(text);
    console.log(Array.from(data2));
})();

To get example data from Windows:

wmic printer list > printers.txt
:: OR
wmic startup list > list.txt

Example data:

Caption                   Command                                                                                                                                                       Description               Location                                                                                        SettingID  
LibreOffice 6.4           LibreOffice 6.4.lnk                                                                                                                                           LibreOffice 6.4           Startup                                                                                                    
Locate32 Autorun          Locate32 Autorun.lnk                                                                                                                                          Locate32 Autorun          Startup                                                                                                    
Verzenden naar OneNote    Verzenden naar OneNote.lnk                                                                                                                                    Verzenden naar OneNote    Startup                                                                                                    

Example result

[
    {
        "Caption": "LibreOffice 6.4",
        "Command": "LibreOffice 6.4.lnk",
        "Description": "LibreOffice 6.4",
        "Location": "Startup",
        "SettingID": ""
    },
    {
        "Caption": "Locate32 Autorun",
        "Command": "Locate32 Autorun.lnk",
        "Description": "Locate32 Autorun",
        "Location": "Startup",
        "SettingID": ""
    },
    {
        "Caption": "Verzenden naar OneNote",
        "Command": "Verzenden naar OneNote.lnk",
        "Description": "Verzenden naar OneNote",
        "Location": "Startup",
        "SettingID": ""
    }
]
52910cookie-checkJavascript: Get data from positioned table