pinojs / quick-format-unescaped

Solves a problem with util.format
MIT License
17 stars 13 forks source link

Fix behaviour when fmt param is null. #23

Closed hbacelar closed 5 years ago

hbacelar commented 5 years ago

This PR is related the following issue on pino: 595

When the fmt param is null, the code assumes that the first args element is the new fmt. This makes the module have the following behaviour:

Example:

format(null, ['foo %s', 'bar %s', 'baz']) 

Output:

'foo bar %s baz'

But a problem arises when the arg[0] is also null, throwing a TypeError.

format(null, [null, 'foo']) 
TypeError: Cannot read property 'length' of null
    at format (quick-format-unescaped/index.js:31:16)

This is inconsistent with this case that doesn't throw:

format(null, ['foo', null]) 

Output:

'foo null'

This commit tries to fix that. A call like format(null, [null, 'foo']) will return 'null foo'.

davidmarkclements commented 5 years ago

very nice thank you!