kgar / foundry-vtt-more-handlebars-helpers

A Foundry VTT Module which registers a some additional handlebars helpers.
MIT License
2 stars 0 forks source link

Request: Table #2

Closed ObsidianTTRPGProject closed 11 months ago

ObsidianTTRPGProject commented 11 months ago

Table Lookup A new inline helper "{{table" is available. It is used to lookup a value in a static look-up table and replace it with another value.

The first parameter is the value to be translated into another value. value1 is the value to be compared to the lookup value. result1 is the result of the {{table}} helper if the lookup value is equal to value1 value2 result2 = second set of possible matches etc, as many pairs of value/result as you need. (any/all of the lookup value and value/result values can be fields or fixed strings) value can contain a javascript regular expression result can contain capturing groups (e.g. $1) to copy information from the matching string. {{!-- {{table "blue" "red" "angry" "blue" "sad" "yellow" "envious" "green" "happy"}} --}} {{!-- will be converted into the string 'sad' (taking "blue" and looking for the value/result pair that matches) --}} {{table lookup value1 result1 value2 result2 value3 result3}}

Helper

handlebars.registerHelper('table',     this.hb_table);
hb_table() {
        // HB:  {{ table string val1 result1 val2 result2 val3 result3 ... }}
        if (arguments.length < 4) return "";  // string val1 result1 options
        if (arguments[0] == undefined || arguments[0] == null) return arguments[0];
        let len     = arguments.length-1;
        let options = arguments[len];
        let value   = arguments[0].toString();
        for (let i=1; i<len; i+=2)
        {
            let result = value.match(RegExp(`^${arguments[i]}$`, 'u'));
            if (result)
            {
                value = arguments[i+1];
                // Replace all occurrences of $n with the corresponding match
                if (result.length>1) {
                    console.info(JSON.stringify(result.groups, null, 2));
                    value = value.replaceAll(/\$(\d+)/g, (match:string, p1:string) => {
                        let param = +p1;  // first parameter = 1
                        if (param < result.length)
                            return result[param];
                        else
                            return match;   // number is too high!
                    })
                }
            }
        }
        return hb_utils.value(value, this, options);
    }
kgar commented 11 months ago

Putting this here for reference while looking at how to implement: https://github.com/farling42/obsidian-import-json#new-handlebar-functions

kgar commented 11 months ago

Can you send me a solid test or example usage that I can plug into Foundry and test the implementation?

I'm not entirely sure what I'm looking for, but I've recreated the implementation in Martin's repo.

kgar commented 11 months ago

So, I ran Martin's test and got back the expected response:

Handlebars.compile('{{ more-handlebars-helpers-table "blue" "red" "angry" "blue" "sad" "yellow" "envious" "green" "happy" }}')()
// > 'sad'

I can use that for now. To make it more obvious, I will take the first "blue" and extract it to a JSON prop so that the user can see what is being evaluated.

ObsidianTTRPGProject commented 11 months ago

Here are some situations where we use it.

alignment: "{{table system.details.alignment.value "LG" "Lawful Good" "LN" "Lawful Neutral" "LE" "Lawful Evil" "NG" "Neutral Good" "N" "Neutral" "NE" "Neutral Evil" "CG" "Chaotic Good" "CN" "Chaotic Neutral" "CE" "Chaotic Evil"}}"
size: "{{table system.traits.size.value "ti" "Tiny" "sm" "Small" "med" "Medium" "lg" "Large" "hu" "Huge" "ga" "Gargantuan"}}"

Source Files: Note: These are created with the Foundry Markdown Exporter plugin. Blood Ooze.md Alchemical Drudge.md

kgar commented 11 months ago

Reopening. I meant to allow you to close these issues when you had a chance to test out the new helpers. v1.1.0 is ready.

ObsidianTTRPGProject commented 11 months ago

Thank you. This works perfectly!