Browse files

String.Trim with ability to specify trim characters + updated tests

  • Loading branch information...
1 parent febce32 commit cb6e0f3347f20cae47ede09b42dcd4d615b02443 @nikhilk committed Apr 20, 2013
View
16 src/Core/CoreLib/String.cs
@@ -379,20 +379,36 @@ public sealed class String {
return null;
}
+ [ScriptAlias("ss.trim")]
public string Trim() {
return null;
}
+ [ScriptAlias("ss.trim")]
+ public string Trim(char[] trimCharacters) {
+ return null;
+ }
+
[ScriptAlias("ss.trimEnd")]
public string TrimEnd() {
return null;
}
+ [ScriptAlias("ss.trimEnd")]
+ public string TrimEnd(char[] trimCharacters) {
+ return null;
+ }
+
[ScriptAlias("ss.trimStart")]
public string TrimStart() {
return null;
}
+ [ScriptAlias("ss.trimStart")]
+ public string TrimStart(char[] trimCharacters) {
+ return null;
+ }
+
/// <summary>
/// Decodes a string by replacing escaped parts with their equivalent textual representation.
/// </summary>
View
1 src/Core/Scripts/Runtime.js
@@ -71,6 +71,7 @@
endsWith: endsWith,
padLeft: padLeft,
padRight: padRight,
+ trim: trim,
trimStart: trimStart,
trimEnd: trimEnd,
insertString: insertString,
View
18 src/Core/Scripts/Runtime/String.js
@@ -58,11 +58,21 @@ function format(cultureOrFormat) {
});
}
-function trimStart(s) {
- return s.replace(/^\s*/, '');
+function trim(s, tc) {
+ if (tc || !String.prototype.trim) {
+ tc = tc ? tc.join('') : null;
+ var r = tc ? new RegExp('^[' + tc + ']+|[' + tc + ']+$', 'g') : /^\s+|\s+$/g;
+ return s.replace(r, '');
+ }
+ return s.trim();
+}
+function trimStart(s, tc) {
+ var r = tc ? new RegExp('^[' + tc.join('') + ']+') : /^\s+/;
+ return s.replace(r, '');
}
-function trimEnd(s) {
- return s.replace(/\s*$/, '');
+function trimEnd(s, tc) {
+ var r = tc ? new RegExp('[' + tc.join('') + ']+$') : /\s+$/;
+ return s.replace(r, '');
}
function startsWith(s, prefix) {
if (emptyString(prefix)) {
View
2 tests/TestCases/Basic/Metadata/Baseline.txt
@@ -1836,7 +1836,7 @@ Types:
Method: Trim
AssociatedType: String
Visibility: Public
- Generated Name: trim
+ Generated Name: ss.trim
Abstract: False
Method: TrimEnd
AssociatedType: String
View
2 tests/TestCases/Expression/Generics/Baseline.txt
@@ -14,7 +14,7 @@ define('test', ['ss', 'jquery'], function(ss, $) {
}, 0).toString(10);
var s4 = encodeURIComponent(this._func(10));
var f2 = this._func;
- f2(11).trim();
+ ss.trim(f2(11));
var d = {};
var s5 = $.extend(d, d)['abc'].toString(10);
var keys = ss.keyCount(d);
View
25 tests/TestSite/String.htm
@@ -22,11 +22,32 @@ <h2 id="qunit-userAgent"></h2>
test('trim', function() {
QUnit.equal(ss.trimStart('Hello'), 'Hello');
QUnit.equal(ss.trimStart(' Hello'), 'Hello');
- QUnit.equal(ss.trimEnd(' Hello'), ' Hello');
+ QUnit.equal(ss.trimStart(' Hello '), 'Hello ');
+
+ QUnit.equal(ss.trimEnd('Hello'), 'Hello');
QUnit.equal(ss.trimEnd('Hello '), 'Hello');
- QUnit.equal(ss.trimStart('Hello'), 'Hello');
+ QUnit.equal(ss.trimEnd(' Hello '), ' Hello');
+
QUnit.equal(ss.trimEnd(ss.trimStart(' Hello ')), 'Hello');
QUnit.equal(ss.trimEnd(ss.trimStart('\tHello ')), 'Hello');
+
+ QUnit.equal(ss.trim('Hello'), 'Hello');
+ QUnit.equal(ss.trim(' Hello '), 'Hello');
+ QUnit.equal(ss.trim('\tHello '), 'Hello');
+ QUnit.equal(ss.trim('\t Hello World '), 'Hello World');
+
+ QUnit.equal(ss.trimStart('00word00', ['0']), 'word00');
+ QUnit.equal(ss.trimStart('10word01', ['0', '1']), 'word01');
+ QUnit.equal(ss.trimStart('10 0word0 01', ['0', '1']), ' 0word0 01');
+
+ QUnit.equal(ss.trimEnd('00word00', ['0']), '00word');
+ QUnit.equal(ss.trimEnd('10word01', ['0', '1']), '10word');
+ QUnit.equal(ss.trimEnd('10 0word0 01', ['0', '1']), '10 0word0 ');
+
+ QUnit.equal(ss.trim('00word00', ['0']), 'word');
+ QUnit.equal(ss.trim('10word01', ['0', '1']), 'word');
+ QUnit.equal(ss.trim('10 0word0 01', ['0', '1']), ' 0word0 ');
+ QUnit.equal(ss.trim(' word ', []), 'word');
});
test('padLeft', function() {

0 comments on commit cb6e0f3

Please sign in to comment.