jmespath / jmespath/jmespath.jep

Function proposal - format()

Open
#1 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
10
Forks
3
PR merge metrics
No merged PRs in 30d

Description

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:

```javascript
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:

```javascript
// 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}'
```
```javascript
// With **OBJECT** input:
jmespath.search(
{foo: 'FOO', bar: null},
"format4('${foo} | ${bar} | ${baz}', @)"
);
// OUTPUTS: 'FOO | null | 12 | ${baz}'
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.