sprintf.js 763 B

1234567891011121314151617181920212223242526272829303132
  1. "use strict";
  2. /**
  3. * Copyright (c) 2013-present, Facebook, Inc.
  4. *
  5. * This source code is licensed under the MIT license found in the
  6. * LICENSE file in the root directory of this source tree.
  7. *
  8. * @typechecks
  9. */
  10. /**
  11. * Simple function for formatting strings.
  12. *
  13. * Replaces placeholders with values passed as extra arguments
  14. *
  15. * @param {string} format the base string
  16. * @param ...args the values to insert
  17. * @return {string} the replaced string
  18. */
  19. function sprintf(format) {
  20. for (var _len = arguments.length, args = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
  21. args[_key - 1] = arguments[_key];
  22. }
  23. var index = 0;
  24. return format.replace(/%s/g, function (match) {
  25. return args[index++];
  26. });
  27. }
  28. module.exports = sprintf;