Test cases
Test #1 Title *
Async
Code * String .prototype .padLeft = function (value, length ) {
if (length < this .length ) return this ;
if (typeof (value) != "string" ) value = String (value);
if (value.length == 0 ) return this ;
var thisArr = (this .split ('' ) || []),
valueArr = (value.split ('' ) || []),
resultArr = new Array (length),
padLen = length - thisArr.length ,
i = 0 ,
j = 0 ;
while (i < padLen) {
if (j >= valueArr.length ) {
j = 0 ;
}
resultArr[i++] = valueArr[j++];
}
j = 0 ;
while (i < length) {
resultArr[i++] = thisArr[j++];
}
return resultArr.join ('' );
}
String .prototype .padRight = function (value, length ) {
if (length < this .length ) return this ;
if (typeof (value) != "string" ) value = String (value);
if (value.length == 0 ) return this ;
var thisArr = (this .split ('' ) || []),
valueArr = (value.split ('' ) || []),
resultArr = new Array (length),
padLen = thisArr.length ,
i = 0 ,
j = 0 ;
while (i < padLen) {
resultArr[i++] = thisArr[j++];
}
j = 0 ;
while (i < length) {
if (j >= valueArr.length ) {
j = 0 ;
}
resultArr[i++] = valueArr[j++];
}
return resultArr.join ('' );
}
String .prototype .padCenter = function (value, length ) {
if (length < this .length ) return this ;
if (typeof (value) != "string" ) value = String (value);
if (value.length == 0 ) return this ;
return this .padLeft (value, this .length + Math .round ((length - this .length ) / 2 )).padRight (value, length);
}
"mystring" .padCenter ("*" , 1000000 );
Test #2 Title *
Async
Code * String .prototype .padLeft2 = function (value, length ) {
if (length < this .length ) return this ;
if (typeof (value) != "string" ) value = String (value);
if (value.length == 0 ) return this ;
var result = value,
padLen = length - this .length ;
while (result.length < padLen) {
result += value;
}
result = result.substr (0 , padLen) + this ;
return result;
}
String .prototype .padRight2 = function (value, length ) {
if (length < this .length ) return this ;
if (typeof (value) != "string" ) value = String (value);
if (value.length == 0 ) return this ;
var result = value,
padLen = length - this .length ;
while (result.length < padLen) {
result += value;
}
result = this + result.substr (0 , padLen);
return result;
}
String .prototype .padCenter2 = function (value, length ) {
if (length < this .length ) return this ;
if (typeof (value) != "string" ) value = String (value);
if (value.length == 0 ) return this ;
return this .padLeft2 (value, this .length + Math .round ((length - this .length ) / 2 )).padRight2 (value, length);
}
"mystring" .padCenter2 ("*" , 1000000 );
Title *
Async
Code * String .prototype .padLeft3 = function (value, length ) {
if (length < this .length ) return this ;
if (typeof (value) != "string" ) value = String (value);
if (value.length == 0 ) return this ;
var chars = (value.split ('' ) || []),
charsLen = chars.length ,
padLen = length - this .length ,
result = Array (padLen + 1 ),
i = 0 ,
j = 0 ;
while (i < padLen) {
if (j >= charsLen) {
j = 0 ;
}
result[i++] = chars[j++];
}
result[i] = this ;
return result.join ('' );
}
String .prototype .padRight3 = function (value, length ) {
if (length < this .length ) return this ;
if (typeof (value) != "string" ) value = String (value);
if (value.length == 0 ) return this ;
var chars = (value.split ('' ) || []),
charsLen = chars.length ,
padLen = length - this .length ,
result = Array (padLen + 1 ),
i = 0 ,
j = 0 ;
result[i++] = this ;
while (i <= padLen) {
if (j >= charsLen) {
j = 0 ;
}
result[i++] = chars[j++];
}
return result.join ('' );
}
String .prototype .padCenter3 = function (value, length ) {
if (length < this .length ) return this ;
if (typeof (value) != "string" ) value = String (value);
if (value.length == 0 ) return this ;
return this .padLeft3 (value, this .length + Math .round ((length - this .length ) / 2 )).padRight3 (value, length);
}
"mystring" .padCenter3 ("*" , 1000000 );
Title *
Async
Code * String .prototype .pad = function (l, s, t ) {
return s || (s = " " ), (l -= this .length ) > 0 ? (s = new Array (Math .ceil (l / s.length ) + 1 ).join (s)).substr (0 , t = !t ? l : t == 1 ? 0 : Math .ceil (l / 2 )) + this + s.substr (0 , l - t) : this ;
};
"mystring" .pad (1000000 , "*" , 2 );
Title *
Async
Code *
Title *
Async
Code *