You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

46 lines
1.3 KiB

  1. # extsprintf: extended POSIX-style sprintf
  2. Stripped down version of s[n]printf(3c). We make a best effort to throw an
  3. exception when given a format string we don't understand, rather than ignoring
  4. it, so that we won't break existing programs if/when we go implement the rest
  5. of this.
  6. This implementation currently supports specifying
  7. * field alignment ('-' flag),
  8. * zero-pad ('0' flag)
  9. * always show numeric sign ('+' flag),
  10. * field width
  11. * conversions for strings, decimal integers, and floats (numbers).
  12. * argument size specifiers. These are all accepted but ignored, since
  13. Javascript has no notion of the physical size of an argument.
  14. Everything else is currently unsupported, most notably: precision, unsigned
  15. numbers, non-decimal numbers, and characters.
  16. Besides the usual POSIX conversions, this implementation supports:
  17. * `%j`: pretty-print a JSON object (using node's "inspect")
  18. * `%r`: pretty-print an Error object
  19. # Example
  20. First, install it:
  21. # npm install extsprintf
  22. Now, use it:
  23. var mod_extsprintf = require('extsprintf');
  24. console.log(mod_extsprintf.sprintf('hello %25s', 'world'));
  25. outputs:
  26. hello world
  27. # Also supported
  28. **printf**: same args as sprintf, but prints the result to stdout
  29. **fprintf**: same args as sprintf, preceded by a Node stream. Prints the result
  30. to the given stream.