Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Template Strings #256

Open
RayPS opened this issue Sep 3, 2018 · 7 comments
Open

Template Strings #256

RayPS opened this issue Sep 3, 2018 · 7 comments

Comments

@RayPS
Copy link

RayPS commented Sep 3, 2018

Can we use template strings like

"`firstName` `lastName` is `age` years old."

instead of using many quotes and concatenate operators like

firstName & " " & lastName & " is " & age & " years old."
@gregory
Copy link

gregory commented Oct 1, 2018

following the javascript string interpolation would make more sense:

`${firstname} ${lastname} is ${age} years old.`

@markmelville
Copy link
Contributor

Bumping. I'd love this feature. Backticks are used to escape property names that have irregular characters. It might be not be feasible to use them for templates.

@tmdoit-zz
Copy link

I did some SQL query generators and moved to template literals (later to mustache) because of hard to read and maintain the result.

@Nenzyz
Copy link

Nenzyz commented Mar 21, 2020

Very dirty hack ready with some small issues.

It converts expression `...` into string with concatenating expression(s) found, if regex /(\${[^}]+})+/ matches, meaning substitution pattern found ${...}. Directly the whole expression changed into string concatenation etc.

`some ${1 + 1} thing ${2 - 2}`

will be internally represented as

"some " & ( 1 + 1 ) & " thing " & ( 2 - 2 ) & ""`

Any jsonata expression can be placed as a placeholder ${...}.

Again this is very dirty.

In parser.js near comment // test for quoted names (backticks) after name = path.substring(position, end);

                    // TI parts - Special case for `template strings` of JavaScript
                    //   no nesting templated are allowed, an least for now
                    // TODO: this is a VERY dirty hack for syntactic sugar
                    // TODO: different quotes should be avoided used in between substitutions 
                    //    i.e. `some ‘asd’ ${…} "asdasd" ‘asdas’ ${…}` 
                    //    such expression will not be able to process since part between two substitutions is used
                    //    "some 'asd'" & ( ${…} ) & "asdasd" 'asdas' & ( ${…}` ) & ""
                    //    this part `"asdasd" 'asdas'` can't be quoted correctly 
                    if (name.match(/(\${[^}]+})+/) != undefined) {
                        var before_path = path.slice(0, position - 1);
                        var after_path = path.slice(end + 1);

                        var iterations = name.split(/(\${[^}]+})+/g);
                        var iter_out = [];
                        iterations.forEach(el => {
                            if (el.substring(0,2) == "${" && el.slice(-1) == "}") {
                                iter_out.push(`& ( ${el.slice(2, el.length - 1)} ) &`);
                            } else {
                                var qchar = 
                                    (el.indexOf('"') == -1) ||
                                    (el.indexOf('"') >= el.indexOf("'") && el.indexOf('"') != -1 && el.indexOf("'") != -1) || 
                                    (el.indexOf("'") != -1) 
                                    ? '"' : "'";
                                iter_out.push(`${qchar}${el}${qchar}`);
                            }
                        });
                        path = "".concat(before_path, `${iter_out.join("")}`, after_path);
                        length = path.length;
                        end = path.indexOf(iter_out[0][0], position); // looking for a quote that is first char in first element
                        name = path.substring(position, end);
                        position = end + 1;
                        return create('string', name);
                    }
                    position = end + 1;
                    return create('name', name);
                }

My fork of jsonata is not on GitHub :(

@natcl
Copy link

natcl commented Nov 6, 2020

Bump also, this would be awesome !

@iain-b
Copy link

iain-b commented Feb 23, 2021

FWIW, me too!

@kevinmata92
Copy link

Bump, agree templating would be nice!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants