textParsing

<script>
  function electContractInfos(textToStudy, setValue) {
        if (!textToStudy || !textToStudy.length) {
            return;
        }
        //Regex for future use
        const globalRegex = /Votre\s+contrat(.+)/i;

        const powerRegex = /puissance\s*:\s*([0-9]+ kVA)/i;
        const baseRegex = /• (Base)/i;
        const heuresCreusesRegex = /• (Heures creuses)/i;
        const weekEndRegex = /• (Week-End)/i;
        const optionRegex = /• (base|Heures Creuses\s+\+\s+WE\s+\+\s+lundi|Heures Creuses\s+\+\s+WE\s+\+\s+mercredi|Heures Creuses\s+\+\s+WE\s+\+\s+vendredi|Heures Creuses\s+\+\s+WE|heures creuses|week-end|WE\s+\+\s+lundi|WE\s+\+\s+mercredi|WE\s+\+\s+vendredi|ejp)/i;

        // Elec
        const electricityContactTypes = [
            "Tarif Bleu",
            "Digiwatt",
            "Mes jours zen",
            "Mes jours zen plus",
            "Vert électrique auto",
            "Vert électrique",
            "Vert électrique régional",
            "Vert électrique week-end"
        ];
        const electContactTypeRegex = new RegExp(/Electricité\s+"?/.source + "(" + electricityContactTypes.join("|") + ")\"", "i");


        let electInfos = {
            type: null,
            heuresCreuses: null,
            option: null,
            weekend: null,
            power: null
        };

        const globalMatches = textToStudy.match(globalRegex);

        if (globalMatches) {
            let currentStudiedText = globalMatches[1];

            const electContactMatches = currentStudiedText.match(electContactTypeRegex);

            // GET CONTRACT TYPE
            electInfos.type = electContactMatches ? electContactMatches[1] : null;

            if (electInfos.type) {
                // GET THE POWER
                const powerMatches = currentStudiedText.match(powerRegex);
                electInfos.power = powerMatches ? powerMatches[1] : null

                // HEURES CREUSES OR NOT
                const heuresCreusesMatches = currentStudiedText.match(heuresCreusesRegex);
                const baseMatches = currentStudiedText.match(baseRegex);
                const weekendMatches = currentStudiedText.match(weekEndRegex);
                electInfos.heuresCreuses = heuresCreusesMatches ? true : (baseMatches ? false : null);
                electInfos.weekend = weekendMatches !== null;

                const optionMatches = currentStudiedText.match(optionRegex);
                if (optionMatches) {
                    electInfos.option = optionMatches[1].toLowerCase();
                }
            }

            if (electInfos.type && electInfos.power && (electInfos.heuresCreuses !== null || electInfos.weekend !== null)) {
                console.log("setting value")
                console.log(electInfos)
                setValue(electInfos);
            }
        } else {
            console.log('Could not find electricity contract infos');
        }
    }

    function pdlNumber(textToStudy, setValue){
        //console.log(textToStudy)
        let pdlRegex = /PDL\s*:\s*([0-9 ]+)/i;
        let pdlInfo = {
            number:null
        };
        const pdlMatches = textToStudy.match(pdlRegex);
        pdlInfo.number = pdlMatches ? pdlMatches[1] : null
        console.log("setting pdlNumber value")
        console.log(pdlInfo)
        setValue(pdlInfo);
    }

    const textFacts = [
        {
            name: "electricityContractInfos",
            method: electContractInfos,
            pageNumbers: [1]
        },
        {
            name: "pdlNumber",
            method: pdlNumber,
            pageNumbers: [9]
        }
    ];

    function register(facts) {
       facts.forEach(fact => {
            console.log("registering text fact"+fact.name)
            PDFZ.api.rules.registerTextFact(fact.name, fact.method, fact.pageNumbers);
            fact.pageNumbers.forEach(p=>{
                PDFZ.api.rules.executeOnPage(p)
            })
        });
    }

    (typeof PDFZ !== "undefined") && (async () => {
        await PDFZ.isReady;
        register(textFacts);
    })();

</script>