Skip to content

Commit a717ad3

Browse files
Fixes #5690: Add trimStart and trimEnd
1 parent c666aa4 commit a717ad3

10 files changed

+106
-43
lines changed

lib/Runtime/Base/JnDirectFields.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,9 @@ ENTRY(toUpperCase)
389389
ENTRY(toUTCString)
390390
ENTRY(trim)
391391
ENTRY(trimLeft)
392+
ENTRY(trimStart)
392393
ENTRY(trimRight)
394+
ENTRY(trimEnd)
393395
ENTRY2(true_, _u("true")) // "true_" cannot be an identifier in C++ so using "true_" instead
394396
ENTRY(TypeError)
395397
ENTRY(undefined)

lib/Runtime/Library/JavascriptBuiltInFunctionList.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,9 @@ BUILTIN(JavascriptString, ToString, EntryToString, FunctionInfo::ErrorOnNew | Fu
246246
BUILTIN(JavascriptString, ToUpperCase, EntryToUpperCase, FunctionInfo::ErrorOnNew)
247247
BUILTIN(JavascriptString, Trim, EntryTrim, FunctionInfo::ErrorOnNew)
248248
BUILTIN(JavascriptString, TrimLeft, EntryTrimLeft, FunctionInfo::ErrorOnNew)
249+
BUILTIN(JavascriptString, TrimStart, EntryTrimLeft, FunctionInfo::ErrorOnNew)
249250
BUILTIN(JavascriptString, TrimRight, EntryTrimRight, FunctionInfo::ErrorOnNew)
251+
BUILTIN(JavascriptString, TrimEnd, EntryTrimRight, FunctionInfo::ErrorOnNew)
250252
BUILTIN(JavascriptString, Repeat, EntryRepeat, FunctionInfo::ErrorOnNew)
251253
BUILTIN(JavascriptString, StartsWith, EntryStartsWith, FunctionInfo::ErrorOnNew)
252254
BUILTIN(JavascriptString, EndsWith, EntryEndsWith, FunctionInfo::ErrorOnNew)

lib/Runtime/Library/JavascriptLibrary.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3405,9 +3405,15 @@ namespace Js
34053405
case PropertyIds::trimLeft:
34063406
return BuiltinFunction::JavascriptString_TrimLeft;
34073407

3408+
case PropertyIds::trimStart:
3409+
return BuiltinFunction::JavascriptString_TrimStart;
3410+
34083411
case PropertyIds::trimRight:
34093412
return BuiltinFunction::JavascriptString_TrimRight;
34103413

3414+
case PropertyIds::trimEnd:
3415+
return BuiltinFunction::JavascriptString_TrimEnd;
3416+
34113417
case PropertyIds::padStart:
34123418
return BuiltinFunction::JavascriptString_PadStart;
34133419

@@ -3994,9 +4000,15 @@ namespace Js
39944000
/* No inlining String_EndsWith */ library->AddFunctionToLibraryObject(stringPrototype, PropertyIds::endsWith, &JavascriptString::EntryInfo::EndsWith, 1);
39954001
/* No inlining String_Includes */ library->AddFunctionToLibraryObject(stringPrototype, PropertyIds::includes, &JavascriptString::EntryInfo::Includes, 1);
39964002
builtinFuncs[BuiltinFunction::JavascriptString_TrimLeft] = library->AddFunctionToLibraryObject(stringPrototype, PropertyIds::trimLeft, &JavascriptString::EntryInfo::TrimLeft, 0);
4003+
//builtinFuncs[BuiltinFunction::JavascriptString_TrimStart] = library->AddFunctionToLibraryObject(stringPrototype, PropertyIds::trimStart, &JavascriptString::EntryInfo::TrimStart, 0);
4004+
library->AddMember(stringPrototype, PropertyIds::trimStart, builtinFuncs[BuiltinFunction::JavascriptString_TrimLeft], PropertyBuiltInMethodDefaults);
39974005
builtinFuncs[BuiltinFunction::JavascriptString_TrimRight] = library->AddFunctionToLibraryObject(stringPrototype, PropertyIds::trimRight, &JavascriptString::EntryInfo::TrimRight, 0);
4006+
library->AddMember(stringPrototype, PropertyIds::trimEnd, builtinFuncs[BuiltinFunction::JavascriptString_TrimRight], PropertyBuiltInMethodDefaults);
39984007
}
39994008

4009+
4010+
4011+
40004012
library->AddFunctionToLibraryObjectWithName(stringPrototype, PropertyIds::_symbolIterator, PropertyIds::_RuntimeFunctionNameId_iterator, &JavascriptString::EntryInfo::SymbolIterator, 0);
40014013

40024014
builtinFuncs[BuiltinFunction::JavascriptString_PadStart] = library->AddFunctionToLibraryObject(stringPrototype, PropertyIds::padStart, &JavascriptString::EntryInfo::PadStart, 1);

lib/Runtime/Library/JavascriptString.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2405,7 +2405,6 @@ namespace Js
24052405
return TrimLeftRightHelper< true /*trimLeft*/, false /*trimRight*/>(pThis, scriptContext);
24062406
}
24072407

2408-
24092408
Var JavascriptString::EntryTrimRight(RecyclableObject* function, CallInfo callInfo, ...)
24102409
{
24112410
PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);

lib/Runtime/Library/JavascriptString.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,9 @@ namespace Js
244244
static FunctionInfo ToUpperCase;
245245
static FunctionInfo Trim;
246246
static FunctionInfo TrimLeft;
247+
static FunctionInfo TrimStart;
247248
static FunctionInfo TrimRight;
249+
static FunctionInfo TrimEnd;
248250
static FunctionInfo Repeat;
249251
static FunctionInfo StartsWith;
250252
static FunctionInfo EndsWith;

lib/Runtime/LibraryFunction.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ LIBRARY_FUNCTION(JavascriptString, ToLowerCase, 1, BIF_UseSrc0
4141
LIBRARY_FUNCTION(JavascriptString, ToUpperCase, 1, BIF_UseSrc0 | BIF_IgnoreDst , JavascriptString::EntryInfo::ToUpperCase)
4242
LIBRARY_FUNCTION(JavascriptString, Trim, 1, BIF_UseSrc0 | BIF_IgnoreDst , JavascriptString::EntryInfo::Trim)
4343
LIBRARY_FUNCTION(JavascriptString, TrimLeft, 1, BIF_UseSrc0 | BIF_IgnoreDst , JavascriptString::EntryInfo::TrimLeft)
44+
LIBRARY_FUNCTION(JavascriptString, TrimStart, 1, BIF_UseSrc0 | BIF_IgnoreDst , JavascriptString::EntryInfo::TrimStart)
4445
LIBRARY_FUNCTION(JavascriptString, TrimRight, 1, BIF_UseSrc0 | BIF_IgnoreDst , JavascriptString::EntryInfo::TrimRight)
46+
LIBRARY_FUNCTION(JavascriptString, TrimEnd, 1, BIF_UseSrc0 | BIF_IgnoreDst , JavascriptString::EntryInfo::TrimEnd)
4547
LIBRARY_FUNCTION(Math, Cos, 1, BIF_TypeSpecUnaryToFloat , Math::EntryInfo::Cos)
4648
LIBRARY_FUNCTION(Math, Exp, 1, BIF_TypeSpecUnaryToFloat , Math::EntryInfo::Exp)
4749
LIBRARY_FUNCTION(Math, Floor, 1, BIF_TypeSpecDstToInt | BIF_TypeSpecSrc1ToFloat , Math::EntryInfo::Floor)

test/DebuggerCommon/ES6_proto_invalidation.js.dbg.baseline

Lines changed: 52 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -226,11 +226,21 @@
226226
"length": "number 0",
227227
"name": "string trimLeft"
228228
},
229+
"trimStart": {
230+
"#__proto__": "function <large string>",
231+
"length": "number 0",
232+
"name": "string trimLeft"
233+
},
229234
"trimRight": {
230235
"#__proto__": "function <large string>",
231236
"length": "number 0",
232237
"name": "string trimRight"
233238
},
239+
"trimEnd": {
240+
"#__proto__": "function <large string>",
241+
"length": "number 0",
242+
"name": "string trimRight"
243+
},
234244
"Symbol.iterator": {
235245
"#__proto__": "function <large string>",
236246
"length": "number 0",
@@ -348,11 +358,6 @@
348358
"length": "number 1",
349359
"name": "string every"
350360
},
351-
"forEach": {
352-
"#__proto__": "function <large string>",
353-
"length": "number 1",
354-
"name": "string forEach"
355-
},
356361
"lastIndexOf": {
357362
"#__proto__": "function <large string>",
358363
"length": "number 1",
@@ -390,43 +395,48 @@
390395
},
391396
"keys": {
392397
"#__proto__": "function <large string>",
393-
"name": "string keys",
394-
"length": "number 0"
398+
"length": "number 0",
399+
"name": "string keys"
395400
},
396401
"values": {
397402
"#__proto__": "function <large string>",
398-
"name": "string values",
399-
"length": "number 0"
403+
"length": "number 0",
404+
"name": "string values"
400405
},
401406
"Symbol.iterator": {
402407
"#__proto__": "function <large string>",
403-
"name": "string values",
404-
"length": "number 0"
408+
"length": "number 0",
409+
"name": "string values"
405410
},
406411
"entries": {
407412
"#__proto__": "function <large string>",
408-
"name": "string entries",
409-
"length": "number 0"
413+
"length": "number 0",
414+
"name": "string entries"
410415
},
411416
"indexOf": {
412417
"#__proto__": "function <large string>",
413-
"name": "string indexOf",
414-
"length": "number 1"
418+
"length": "number 1",
419+
"name": "string indexOf"
415420
},
416421
"filter": {
417422
"#__proto__": "function <large string>",
418-
"name": "string filter",
419-
"length": "number 1"
423+
"length": "number 1",
424+
"name": "string filter"
420425
},
421426
"flat": {
422427
"#__proto__": "function <large string>",
423-
"name": "string flat",
424-
"length": "number 0"
428+
"length": "number 0",
429+
"name": "string flat"
425430
},
426431
"flatMap": {
427432
"#__proto__": "function <large string>",
428-
"name": "string flatMap",
429-
"length": "number 1"
433+
"length": "number 1",
434+
"name": "string flatMap"
435+
},
436+
"forEach": {
437+
"#__proto__": "function <large string>",
438+
"length": "number 1",
439+
"name": "string forEach"
430440
},
431441
"Symbol.unscopables": {
432442
"find": "boolean true",
@@ -575,11 +585,6 @@
575585
"length": "number 1",
576586
"name": "string every"
577587
},
578-
"forEach": {
579-
"#__proto__": "function <large string>",
580-
"length": "number 1",
581-
"name": "string forEach"
582-
},
583588
"lastIndexOf": {
584589
"#__proto__": "function <large string>",
585590
"length": "number 1",
@@ -617,43 +622,48 @@
617622
},
618623
"keys": {
619624
"#__proto__": "function <large string>",
620-
"name": "string keys",
621-
"length": "number 0"
625+
"length": "number 0",
626+
"name": "string keys"
622627
},
623628
"values": {
624629
"#__proto__": "function <large string>",
625-
"name": "string values",
626-
"length": "number 0"
630+
"length": "number 0",
631+
"name": "string values"
627632
},
628633
"Symbol.iterator": {
629634
"#__proto__": "function <large string>",
630-
"name": "string values",
631-
"length": "number 0"
635+
"length": "number 0",
636+
"name": "string values"
632637
},
633638
"entries": {
634639
"#__proto__": "function <large string>",
635-
"name": "string entries",
636-
"length": "number 0"
640+
"length": "number 0",
641+
"name": "string entries"
637642
},
638643
"indexOf": {
639644
"#__proto__": "function <large string>",
640-
"name": "string indexOf",
641-
"length": "number 1"
645+
"length": "number 1",
646+
"name": "string indexOf"
642647
},
643648
"filter": {
644649
"#__proto__": "function <large string>",
645-
"name": "string filter",
646-
"length": "number 1"
650+
"length": "number 1",
651+
"name": "string filter"
647652
},
648653
"flat": {
649654
"#__proto__": "function <large string>",
650-
"name": "string flat",
651-
"length": "number 0"
655+
"length": "number 0",
656+
"name": "string flat"
652657
},
653658
"flatMap": {
654659
"#__proto__": "function <large string>",
655-
"name": "string flatMap",
656-
"length": "number 1"
660+
"length": "number 1",
661+
"name": "string flatMap"
662+
},
663+
"forEach": {
664+
"#__proto__": "function <large string>",
665+
"length": "number 1",
666+
"name": "string forEach"
657667
},
658668
"Symbol.unscopables": {
659669
"find": "boolean true",

test/DebuggerCommon/TempStrExpr.js.dbg.baseline

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,9 @@
170170
"endsWith": "function <large string>",
171171
"includes": "function <large string>",
172172
"trimLeft": "function <large string>",
173+
"trimStart": "function <large string>",
173174
"trimRight": "function <large string>",
175+
"trimEnd": "function <large string>",
174176
"Symbol.iterator": "function <large string>",
175177
"padStart": "function <large string>",
176178
"padEnd": "function <large string>",

test/Strings/rlexe.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,12 @@
122122
<files>trim.js</files>
123123
<baseline>trim.baseline</baseline>
124124
</default>
125+
</test>
126+
<test>
127+
<default>
128+
<files>trimStart_trimEnd.js</files>
129+
<compile-flags>-args summary -endargs</compile-flags>
130+
</default>
125131
</test>
126132
<test>
127133
<default>

test/Strings/trimStart_trimEnd.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//-------------------------------------------------------------------------------------------------------
2+
// Copyright (C) Microsoft. All rights reserved.
3+
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
4+
//-------------------------------------------------------------------------------------------------------
5+
6+
WScript.LoadScriptFile("../UnitTestFramework/UnitTestFramework.js");
7+
8+
const tests = [
9+
{
10+
name: "trimLeft is same function as trimStart",
11+
body: function () {
12+
// NOTE: See comments in test/UnitTestFramework/UnitTestFramework.js for more info about what assertions you can use
13+
assert.areEqual(String.prototype.trimLeft, String.prototype.trimStart, "Both trimStart and trimLeft should point to the same function");
14+
}
15+
},
16+
{
17+
name: "trimRight is same function as trimEnd",
18+
body: function () {
19+
assert.areEqual(String.prototype.trimRight, String.prototype.trimEnd, "Both trimRight and trimEnd should point to the same function");
20+
}
21+
}
22+
];
23+
24+
// The test runner will pass "-args summary -endargs" to ch, so that "summary" appears as argument [0[] to the script,
25+
// and the following line will then ensure that the test will only display summary output (i.e. "PASS").
26+
testRunner.runTests(tests, { verbose: WScript.Arguments[0] != "summary" });

0 commit comments

Comments
 (0)