Sha256: f8105089869c162bd93a45e1e8c1472df8b8756f94bafb4e088c589e76e5c32d

Contents?: true

Size: 1.07 KB

Versions: 153

Compression:

Stored size: 1.07 KB

Contents

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

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

/**
 * Repeats the given string `n` times.
 *
 * @static
 * @memberOf _
 * @category String
 * @param {string} [string=''] The string to repeat.
 * @param {number} [n=0] The number of times to repeat the string.
 * @returns {string} Returns the repeated string.
 * @example
 *
 * _.repeat('*', 3);
 * // => '***'
 *
 * _.repeat('abc', 2);
 * // => 'abcabc'
 *
 * _.repeat('abc', 0);
 * // => ''
 */
function repeat(string, n) {
  var result = '';
  string = baseToString(string);
  n = +n;
  if (n < 1 || !string || !nativeIsFinite(n)) {
    return result;
  }
  // Leverage the exponentiation by squaring algorithm for a faster repeat.
  // See https://en.wikipedia.org/wiki/Exponentiation_by_squaring for more details.
  do {
    if (n % 2) {
      result += string;
    }
    n = nativeFloor(n / 2);
    string += string;
  } while (n);

  return result;
}

module.exports = repeat;

Version data entries

153 entries across 80 versions & 8 rubygems

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