Sha256: de022f8b7a91bd88e64f5112d5dfa3ed4f4288af2c83b19fa1f2eed6579442f0

Contents?: true

Size: 1.28 KB

Versions: 153

Compression:

Stored size: 1.28 KB

Contents

var baseToString = require('../internal/baseToString'),
    createPadding = require('../internal/createPadding');

/* Native method references for those with the same name as other `lodash` methods. */
var nativeCeil = Math.ceil,
    nativeFloor = Math.floor,
    nativeIsFinite = global.isFinite;

/**
 * Pads `string` on the left and right sides if it's shorter than `length`.
 * Padding characters are truncated if they can't be evenly divided by `length`.
 *
 * @static
 * @memberOf _
 * @category String
 * @param {string} [string=''] The string to pad.
 * @param {number} [length=0] The padding length.
 * @param {string} [chars=' '] The string used as padding.
 * @returns {string} Returns the padded string.
 * @example
 *
 * _.pad('abc', 8);
 * // => '  abc   '
 *
 * _.pad('abc', 8, '_-');
 * // => '_-abc_-_'
 *
 * _.pad('abc', 3);
 * // => 'abc'
 */
function pad(string, length, chars) {
  string = baseToString(string);
  length = +length;

  var strLength = string.length;
  if (strLength >= length || !nativeIsFinite(length)) {
    return string;
  }
  var mid = (length - strLength) / 2,
      leftLength = nativeFloor(mid),
      rightLength = nativeCeil(mid);

  chars = createPadding('', rightLength, chars);
  return chars.slice(0, leftLength) + string + chars;
}

module.exports = pad;

Version data entries

153 entries across 80 versions & 8 rubygems

Version Path
ilog-0.4.1 node_modules/contents/node_modules/lodash/string/pad.js
ilog-0.4.1 node_modules/babel-plugin-proto-to-assign/node_modules/lodash/string/pad.js
ilog-0.4.1 node_modules/babel/node_modules/lodash/string/pad.js
ilog-0.4.0 node_modules/babel/node_modules/lodash/string/pad.js
ilog-0.4.0 node_modules/contents/node_modules/lodash/string/pad.js
ilog-0.4.0 node_modules/babel-plugin-proto-to-assign/node_modules/lodash/string/pad.js
ilog-0.3.3 node_modules/babel/node_modules/lodash/string/pad.js
ilog-0.3.3 node_modules/contents/node_modules/lodash/string/pad.js
ilog-0.3.3 node_modules/babel-plugin-proto-to-assign/node_modules/lodash/string/pad.js
trusty-festivity-extension-2.6.3 node_modules/bower/node_modules/configstore/node_modules/js-yaml/node_modules/argparse/node_modules/lodash/string/pad.js
trusty-festivity-extension-2.6.3 node_modules/bower/node_modules/inquirer/node_modules/lodash/string/pad.js
trusty-festivity-extension-2.6.2 node_modules/bower/node_modules/configstore/node_modules/js-yaml/node_modules/argparse/node_modules/lodash/string/pad.js
trusty-festivity-extension-2.6.2 node_modules/bower/node_modules/inquirer/node_modules/lodash/string/pad.js
trusty-festivity-extension-2.6.1 node_modules/bower/node_modules/configstore/node_modules/js-yaml/node_modules/argparse/node_modules/lodash/string/pad.js
trusty-festivity-extension-2.6.1 node_modules/bower/node_modules/inquirer/node_modules/lodash/string/pad.js
xcodebuild-helper-1.2.5 externals/ios-sim-master/node_modules/lodash/string/pad.js
trusty-festivity-extension-2.6 node_modules/bower/node_modules/configstore/node_modules/js-yaml/node_modules/argparse/node_modules/lodash/string/pad.js
trusty-festivity-extension-2.6 node_modules/bower/node_modules/inquirer/node_modules/lodash/string/pad.js
trusty-festivity-extension-2.5.19 node_modules/bower/node_modules/configstore/node_modules/js-yaml/node_modules/argparse/node_modules/lodash/string/pad.js
trusty-festivity-extension-2.5.19 node_modules/bower/node_modules/inquirer/node_modules/lodash/string/pad.js