Node: How to access placeholder tokens?

Created on 11 Dec 2017  路  24Comments  路  Source: nodejs/node

Is there a simple way to return an array of tokens used as placeholder tokens (e.g. for util.format)?

https://nodejs.org/api/util.html#util_util_format_format_args

e.g. ['%s', '%d', '%i', ...]

question util

Most helpful comment

I don't see the point. Knowing what placeholders node.js knows about tells you nothing about how it interprets them. The only reasonable thing to do is call util.format.apply(util, args).

All 24 comments

I'm unaware of any way to do this programatically. What's the use case? If the use case is persuasive, then a request to add the feature would be likewise persuasive.

@Trott thank you for the quick reply. I've provided insight and references for the use case below.

I wrote my own log utility (see https://github.com/ladjs/logger) and want to support developers that might be using console.log in a way that uses placeholder tokens, e.g. console.log('hello world', { foo: 'bar' }, '%s', 'some text'); or console.log({ some: 'object' }, 'hello there %o', { hello: 'world' }) - in short design a drop-in replacement for util.format, console.log, and util.log.

I discovered the issue and realization that not everyone uses a logger the same way. Some people might use console.log and pass it a dozen arguments... while others might adhere to a strict two or three argument approach (some even with specific variable types for each argument).

In my logger package, the first argument should be a string, the second an object. At first I thought this was OK, but I want to support packages that might not adhere to this approach and not throw errors (which is bad design anyways - and I've removed it from the latest release of my package). See https://github.com/ladjs/logger/issues/6.

I've currently just created an array of tokens that I use (see https://github.com/ladjs/logger/blob/master/index.js#L39-L45)... and I was going to publish this as a package, e.g. placeholder-tokens. However I feel that this should be stored in the util package itself, and accessible through a property such as util.placeholderTokens or util.tokens for short (outputs the same Array basically).

Having it in in the core util package would be better as its core in itself, and I'd rather not have people with the same use case be using some random and possibly unmaintained npm package.

One argument for including in core is that we add placeholders from time to time (and could remove some too) so a userland package could provide the correct values for one version of Node.js but wrong values for another version.

It seems like this should be achievable with little or no performance overhead.

Deciding on a name might involve some bike-shedding, but that's OK. (In what is sure to be an unpopular suggestion, I was wondering if made sense to make it a property hung of util.format itself, so util.format.placeholders or something like that.)

@nodejs/collaborators: Thoughts on whether or not this feature should be implemented? And, if it gets implemented, thoughts on a name?

@niftylettuce If you want to submit a pull request adding this functionality, having actual code to review might speed things along ultimately. (Just be prepared to make changes because people will have suggestions.)

thoughts on a name?

format specifiers

util.format.specifiers? :-D




vote?

Honestly this doesn't look like it's too hard to keep track of in userland - and it's one more thing we have to maintain.

I'm +0 on this leaning to -1 but no strong feelings.

I'm +1. If we can keep these in sync easily, then we should definitely do it. If we have to duplicate code, then its better we do it here anyway.

I'm fine with it only if the array is Object.freeze()'d.

@TimothyGu huh? not objecting, but why?

@addaleax I don't want people to be able to modify the array in any way. People who override util.format will replace this array which is attached to the formatting function anyway.

I don't see the point. Knowing what placeholders node.js knows about tells you nothing about how it interprets them. The only reasonable thing to do is call util.format.apply(util, args).

I don't see the point. Knowing what placeholders node.js knows about tells you nothing about how it interprets them. The only reasonable thing to do is call util.format.apply(util, args).

I disagree. It is useful if you want to escape them.

s.replace(/%/g, '%%') ? If that's not sufficient, please elaborate.

That is not sufficient, as it will also escape '%' that are not placeholders.

Why does that matter? util.format('%q', 42) and util.format('%%q', 42) both return '%q 42'.

You are right.

This issue has been dormant for over a month so I'll go ahead and close it out.

@bnoordhuis can this be reopened? I'd be hesitant on closing any current issues that've been "dormant for over a month" as it's the start of the new year and a lot of people will have only just gotten back to work, etc.

I would have left it open if there was consensus that it's a good idea, but there isn't.

Apart from your comments everyone else seems to be okay with it so not sure what you mean by that.

@OmgImAlexis https://github.com/nodejs/node/issues/17601#issuecomment-351339618 has 3 馃憤 from 3 collaborators.

@OmgImAlexis We can and will reopen this issue if a valid and reasonable use case comes up which requires such a feature. Feel free to comment here (or open a new issue if you prefer) if you feel like such an API would have benefits.

I have published a package format-specifiers that provides this list and have set up an automated visual test to notify me of any changes so I can try to keep it up to date (source: https://github.com/niftylettuce/format-specifiers).

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jonathanong picture jonathanong  路  91Comments

jonathanong picture jonathanong  路  93Comments

substack picture substack  路  878Comments

speakeasypuncture picture speakeasypuncture  路  152Comments

yury-s picture yury-s  路  89Comments