padEnd.js 1017 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. var createPadding = require('./_createPadding'),
  2. stringSize = require('./_stringSize'),
  3. toInteger = require('./toInteger'),
  4. toString = require('./toString');
  5. /**
  6. * Pads `string` on the right side if it's shorter than `length`. Padding
  7. * characters are truncated if they exceed `length`.
  8. *
  9. * @static
  10. * @memberOf _
  11. * @since 4.0.0
  12. * @category String
  13. * @param {string} [string=''] The string to pad.
  14. * @param {number} [length=0] The padding length.
  15. * @param {string} [chars=' '] The string used as padding.
  16. * @returns {string} Returns the padded string.
  17. * @example
  18. *
  19. * _.padEnd('abc', 6);
  20. * // => 'abc '
  21. *
  22. * _.padEnd('abc', 6, '_-');
  23. * // => 'abc_-_'
  24. *
  25. * _.padEnd('abc', 3);
  26. * // => 'abc'
  27. */
  28. function padEnd(string, length, chars) {
  29. string = toString(string);
  30. length = toInteger(length);
  31. var strLength = length ? stringSize(string) : 0;
  32. return (length && strLength < length)
  33. ? (string + createPadding(length - strLength, chars))
  34. : string;
  35. }
  36. module.exports = padEnd;