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

Function proposal - format() #1

Open
glenveegee opened this issue May 16, 2020 · 0 comments
Open

Function proposal - format() #1

glenveegee opened this issue May 16, 2020 · 0 comments

Comments

@glenveegee
Copy link

glenveegee commented May 16, 2020

Given a template and either an array or object output a string interpolated with values:

For example in Javascript one might implement it as follows:

registerFunction(
  'format',
  ([template, templateStringsMap]) => {
    let newTemplate = template;
    for (const attr in templateStringsMap) {
      const rgx = new RegExp(`\\$\\{${attr}\\}`, 'g');
      newTemplate = newTemplate.replace(rgx, templateStringsMap[attr] ?? '');
    }
    return newTemplate;
  },
  [{ types: [TYPE_STRING] }, { types: [TYPE_OBJECT, TYPE_ARRAY] }],
);

Examples:

// With ARRAY input:
jmespath.search(
    [1,"2", null, ["a", 2, "foo"], {b: "z"}, true, false],
    "format4('${0} | ${1} | ${2} | ${3} | ${4} | ${5} | ${6} | ${7}', @)"
)
// OUTPUTS: '1 | 2 | null | a,2,foo | [object Object] | true | false | ${7}'
// With **OBJECT** input:
jmespath.search(
    {foo: 'FOO', bar: null},
    "format4('${foo} | ${bar} | ${baz}', @)"
);
// OUTPUTS: 'FOO | null | 12 | ${baz}'
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

1 participant