mirror of
https://github.com/classilla/tenfourfox.git
synced 2024-10-21 11:24:51 +00:00
#509: M1260509 implement String.padStart/padEnd (needed for testing)
This commit is contained in:
parent
c7075f4527
commit
8b2316f2db
@ -4,6 +4,55 @@
|
|||||||
|
|
||||||
/*global intl_Collator: false, */
|
/*global intl_Collator: false, */
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A helper function implementing the logic for both String.prototype.padStart
|
||||||
|
* and String.prototype.padEnd as described in ES7 Draft March 29, 2016
|
||||||
|
*/
|
||||||
|
function String_pad(maxLength, fillString, padEnd=false) {
|
||||||
|
|
||||||
|
// Steps 1-2.
|
||||||
|
RequireObjectCoercible(this);
|
||||||
|
let str = ToString(this);
|
||||||
|
|
||||||
|
// Steps 3-4.
|
||||||
|
let intMaxLength = ToLength(maxLength);
|
||||||
|
let strLen = str.length;
|
||||||
|
|
||||||
|
// Step 5.
|
||||||
|
if (intMaxLength <= strLen)
|
||||||
|
return str;
|
||||||
|
|
||||||
|
// Steps 6-7.
|
||||||
|
let filler = fillString === undefined ? " " : ToString(fillString);
|
||||||
|
|
||||||
|
// Step 8.
|
||||||
|
if (filler === "")
|
||||||
|
return str;
|
||||||
|
|
||||||
|
// Step 9.
|
||||||
|
let fillLen = intMaxLength - strLen;
|
||||||
|
|
||||||
|
// Step 10.
|
||||||
|
let truncatedStringFiller = callFunction(String_repeat, filler,
|
||||||
|
fillLen / filler.length);
|
||||||
|
|
||||||
|
truncatedStringFiller += callFunction(String_substr, filler, 0,
|
||||||
|
fillLen % filler.length);
|
||||||
|
|
||||||
|
// Step 11.
|
||||||
|
if (padEnd === true)
|
||||||
|
return str + truncatedStringFiller;
|
||||||
|
return truncatedStringFiller + str;
|
||||||
|
}
|
||||||
|
|
||||||
|
function String_pad_start(maxLength, fillString=" ") {
|
||||||
|
return callFunction(String_pad, this, maxLength, fillString, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
function String_pad_end(maxLength, fillString=" ") {
|
||||||
|
return callFunction(String_pad, this, maxLength, fillString, true);
|
||||||
|
}
|
||||||
|
|
||||||
/* ES6 Draft Oct 14, 2014 21.1.3.19 */
|
/* ES6 Draft Oct 14, 2014 21.1.3.19 */
|
||||||
function String_substring(start, end) {
|
function String_substring(start, end) {
|
||||||
// Steps 1-3.
|
// Steps 1-3.
|
||||||
|
@ -4204,6 +4204,8 @@ static const JSFunctionSpec string_methods[] = {
|
|||||||
JS_INLINABLE_FN("charAt", str_charAt, 1,JSFUN_GENERIC_NATIVE, StringCharAt),
|
JS_INLINABLE_FN("charAt", str_charAt, 1,JSFUN_GENERIC_NATIVE, StringCharAt),
|
||||||
JS_INLINABLE_FN("charCodeAt", str_charCodeAt, 1,JSFUN_GENERIC_NATIVE, StringCharCodeAt),
|
JS_INLINABLE_FN("charCodeAt", str_charCodeAt, 1,JSFUN_GENERIC_NATIVE, StringCharCodeAt),
|
||||||
JS_SELF_HOSTED_FN("substring", "String_substring", 2,0),
|
JS_SELF_HOSTED_FN("substring", "String_substring", 2,0),
|
||||||
|
JS_SELF_HOSTED_FN("padStart", "String_pad_start", 2,0),
|
||||||
|
JS_SELF_HOSTED_FN("padEnd", "String_pad_end", 2,0),
|
||||||
JS_SELF_HOSTED_FN("codePointAt", "String_codePointAt", 1,0),
|
JS_SELF_HOSTED_FN("codePointAt", "String_codePointAt", 1,0),
|
||||||
JS_FN("includes", str_includes, 1,JSFUN_GENERIC_NATIVE),
|
JS_FN("includes", str_includes, 1,JSFUN_GENERIC_NATIVE),
|
||||||
JS_FN("contains", str_contains, 1,JSFUN_GENERIC_NATIVE),
|
JS_FN("contains", str_contains, 1,JSFUN_GENERIC_NATIVE),
|
||||||
|
Loading…
Reference in New Issue
Block a user