Ohm-Management - Projektarbeit B-ME
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.

pad.js 1.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. var createPadding = require('./_createPadding'),
  2. stringSize = require('./_stringSize'),
  3. toInteger = require('./toInteger'),
  4. toString = require('./toString');
  5. /* Built-in method references for those with the same name as other `lodash` methods. */
  6. var nativeCeil = Math.ceil,
  7. nativeFloor = Math.floor;
  8. /**
  9. * Pads `string` on the left and right sides if it's shorter than `length`.
  10. * Padding characters are truncated if they can't be evenly divided by `length`.
  11. *
  12. * @static
  13. * @memberOf _
  14. * @since 3.0.0
  15. * @category String
  16. * @param {string} [string=''] The string to pad.
  17. * @param {number} [length=0] The padding length.
  18. * @param {string} [chars=' '] The string used as padding.
  19. * @returns {string} Returns the padded string.
  20. * @example
  21. *
  22. * _.pad('abc', 8);
  23. * // => ' abc '
  24. *
  25. * _.pad('abc', 8, '_-');
  26. * // => '_-abc_-_'
  27. *
  28. * _.pad('abc', 3);
  29. * // => 'abc'
  30. */
  31. function pad(string, length, chars) {
  32. string = toString(string);
  33. length = toInteger(length);
  34. var strLength = length ? stringSize(string) : 0;
  35. if (!length || strLength >= length) {
  36. return string;
  37. }
  38. var mid = (length - strLength) / 2;
  39. return (
  40. createPadding(nativeFloor(mid), chars) +
  41. string +
  42. createPadding(nativeCeil(mid), chars)
  43. );
  44. }
  45. module.exports = pad;