sprintf.js icon indicating copy to clipboard operation
sprintf.js copied to clipboard

Preserve unmatched placeholders

Open ArmorDarks opened this issue 10 years ago • 1 comments

As mentioned here https://github.com/alexei/sprintf.js/issues/8#issuecomment-152675119, it would be nice to have option to return unmatched placeholders as they are.

For example, when we have this:

sprintf('Test number %s')

instead of 'Test number undefined' return 'Test number %s'

And same for named placeholders:

sprintf('%(name)s number 1')

instead of error about non-existing placeholder return '%(name)s number 1'

This will make possible complex chaining, when you need to process strings by few modules.

So far can't tell for sure how should it be done.

I have few options:

  • Global option during sprintf invocation:

    sprintf = require('sprintf-js').sprintf({
      preservePlaceholders: true
    })
    
    sprintf('%(name)s number 1')` -> `'%(name)s number 1'`
    
  • Live switch in and switch off:

    sprintf.preservePlaceholders(true)
    
    sprintf('%(name)s number 1')` -> `'%(name)s number 1'`
    
    sprintf.preservePlaceholders(false)
    
  • Special sprintfp function:

    sprintfp('%(name)s number 1')` -> `'%(name)s number 1'`
    

ArmorDarks avatar Oct 31 '15 00:10 ArmorDarks

+1

jonyrock avatar Feb 10 '16 16:02 jonyrock