time-to-botec

Benchmark sampling in different programming languages
Log | Files | Refs | README

_createPadding.js (1153B)


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