mirror of
https://github.com/showdownjs/showdown.git
synced 2024-03-22 13:30:55 +08:00
fix(polyfill) String.prototype.repeat
This commit is contained in:
parent
66798912de
commit
a64a18bef8
|
@ -357,6 +357,45 @@ showdown.helper.encodeEmailAddress = function (mail) {
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* String.prototype.repeat polyfill
|
||||||
|
*
|
||||||
|
* @param str
|
||||||
|
* @param count
|
||||||
|
* @returns {string}
|
||||||
|
*/
|
||||||
|
function repeat (str, count) {
|
||||||
|
'use strict';
|
||||||
|
str = '' + str;
|
||||||
|
if (count < 0) {
|
||||||
|
throw new RangeError('repeat count must be non-negative');
|
||||||
|
}
|
||||||
|
if (count === Infinity) {
|
||||||
|
throw new RangeError('repeat count must be less than infinity');
|
||||||
|
}
|
||||||
|
count = Math.floor(count);
|
||||||
|
if (str.length === 0 || count === 0) {
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
// Ensuring count is a 31-bit integer allows us to heavily optimize the
|
||||||
|
// main part. But anyway, most current (August 2014) browsers can't handle
|
||||||
|
// strings 1 << 28 chars or longer, so:
|
||||||
|
/*jshint bitwise: false*/
|
||||||
|
if (str.length * count >= 1 << 28) {
|
||||||
|
throw new RangeError('repeat count must not overflow maximum string size');
|
||||||
|
}
|
||||||
|
/*jshint bitwise: true*/
|
||||||
|
var maxCount = str.length * count;
|
||||||
|
count = Math.floor(Math.log(count) / Math.log(2));
|
||||||
|
while (count) {
|
||||||
|
str += str;
|
||||||
|
count--;
|
||||||
|
}
|
||||||
|
str += str.substring(0, maxCount - str.length);
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* String.prototype.padEnd polyfill
|
||||||
*
|
*
|
||||||
* @param str
|
* @param str
|
||||||
* @param targetLength
|
* @param targetLength
|
||||||
|
@ -375,7 +414,7 @@ showdown.helper.padEnd = function padEnd (str, targetLength, padString) {
|
||||||
} else {
|
} else {
|
||||||
targetLength = targetLength - str.length;
|
targetLength = targetLength - str.length;
|
||||||
if (targetLength > padString.length) {
|
if (targetLength > padString.length) {
|
||||||
padString += padString.repeat(targetLength / padString.length); //append to original to ensure we are longer than needed
|
padString += repeat(padString, targetLength / padString.length); //append to original to ensure we are longer than needed
|
||||||
}
|
}
|
||||||
return String(str) + padString.slice(0,targetLength);
|
return String(str) + padString.slice(0,targetLength);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user