_createPadding.js 1.1 KB

123456789101112131415161718192021222324252627282930313233
  1. import baseRepeat from './_baseRepeat.js';
  2. import baseToString from './_baseToString.js';
  3. import castSlice from './_castSlice.js';
  4. import hasUnicode from './_hasUnicode.js';
  5. import stringSize from './_stringSize.js';
  6. import stringToArray from './_stringToArray.js';
  7. /* Built-in method references for those with the same name as other `lodash` methods. */
  8. var nativeCeil = Math.ceil;
  9. /**
  10. * Creates the padding for `string` based on `length`. The `chars` string
  11. * is truncated if the number of characters exceeds `length`.
  12. *
  13. * @private
  14. * @param {number} length The padding length.
  15. * @param {string} [chars=' '] The string used as padding.
  16. * @returns {string} Returns the padding for `string`.
  17. */
  18. function createPadding(length, chars) {
  19. chars = chars === undefined ? ' ' : baseToString(chars);
  20. var charsLength = chars.length;
  21. if (charsLength < 2) {
  22. return charsLength ? baseRepeat(chars, length) : chars;
  23. }
  24. var result = baseRepeat(chars, nativeCeil(length / stringSize(chars)));
  25. return hasUnicode(chars)
  26. ? castSlice(stringToArray(result), 0, length).join('')
  27. : result.slice(0, length);
  28. }
  29. export default createPadding;