python-format-js icon indicating copy to clipboard operation
python-format-js copied to clipboard

Allowing to have a word inside brackets that will be treated as plain text, not a placeholder to be replaced

Open igorMSoares opened this issue 1 year ago • 0 comments

Is your feature request related to a problem? Please describe.

I'm using python-format-js through LlamaindexTS, and having problems when there's some string between { and } but the string is not a placeholder to be replaced by format() instead it's supposed to be interpreted as just plain text that happens to have a word inside of { }.

When LlamaindexTS invokes the format() function, the args_[0] is being passed as an object, so it's being explicit about which placeholders should be replaced by the value. Like so:

// args_[0]
{ query: 'some text', context: 'other text' }

If the text being formated is something like:

This is the query: {query} and this is the context: {context} but this {something} is just some plain text inside brackets

An exception is thrown by the check:

if (!ACTUAL_VALUE) throw new Error(`Replacement index ${INDEX_VAR} out of range for positional args tuple`);

Describe the solution you'd like

I wonder if it would be possible that, when args_0 is an object, any text between brackets that does not correspond to a key in args_0 could be just ignored and left as it is, without throwing an exception.

Describe alternatives you've considered

Maybe having an optional argument like { strict: false } that when passed would not throw an error in this scenario, but allowing the current behavior if set as { strict: true }

Additional context

I would like the possibility to have the following behavior:

"Your name is {name} and you have {age} years old! {something} <- this will not be replaced".format({
  name: "Jônatas",
  age: 21,
});

("My name is Jônatas and i have 21 years old! {something} <- this will not be replaced");

igorMSoares avatar Nov 05 '24 14:11 igorMSoares