Skip to content

Commit 7c31d97

Browse files
authored
Move string trim methods from utilities to core (microsoft#44308)
1 parent 8721dd0 commit 7c31d97

File tree

2 files changed

+30
-31
lines changed

2 files changed

+30
-31
lines changed

src/compiler/core.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2311,4 +2311,34 @@ namespace ts {
23112311
}
23122312
return array.slice(0, index);
23132313
}
2314+
2315+
/**
2316+
* Removes the leading and trailing white space and line terminator characters from a string.
2317+
*/
2318+
export const trimString = !!String.prototype.trim ? ((s: string) => s.trim()) : (s: string) => trimStringEnd(trimStringStart(s));
2319+
2320+
/**
2321+
* Returns a copy with trailing whitespace removed.
2322+
*/
2323+
export const trimStringEnd = !!String.prototype.trimEnd ? ((s: string) => s.trimEnd()) : trimEndImpl;
2324+
2325+
/**
2326+
* Returns a copy with leading whitespace removed.
2327+
*/
2328+
export const trimStringStart = !!String.prototype.trimStart ? ((s: string) => s.trimStart()) : (s: string) => s.replace(/^\s+/g, "");
2329+
2330+
/**
2331+
* https://jsbench.me/gjkoxld4au/1
2332+
* The simple regex for this, /\s+$/g is O(n^2) in v8.
2333+
* The native .trimEnd method is by far best, but since that's technically ES2019,
2334+
* we provide a (still much faster than the simple regex) fallback.
2335+
*/
2336+
function trimEndImpl(s: string) {
2337+
let end = s.length - 1;
2338+
while (end >= 0) {
2339+
if (!isWhiteSpaceLike(s.charCodeAt(end))) break;
2340+
end--;
2341+
}
2342+
return s.slice(0, end + 1);
2343+
}
23142344
}

src/compiler/utilities.ts

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -531,37 +531,6 @@ namespace ts {
531531
return text;
532532
}
533533

534-
/**
535-
* Removes the leading and trailing white space and line terminator characters from a string.
536-
*/
537-
export const trimString = !!String.prototype.trim ? ((s: string) => s.trim()) : (s: string) => trimStringEnd(trimStringStart(s));
538-
539-
/**
540-
* Returns a copy with trailing whitespace removed.
541-
*/
542-
export const trimStringEnd = !!String.prototype.trimEnd ? ((s: string) => s.trimEnd()) : trimEndImpl;
543-
544-
545-
/**
546-
* Returns a copy with leading whitespace removed.
547-
*/
548-
export const trimStringStart = !!String.prototype.trimStart ? ((s: string) => s.trimStart()) : (s: string) => s.replace(/^\s+/g, "");
549-
550-
/**
551-
* https://jsbench.me/gjkoxld4au/1
552-
* The simple regex for this, /\s+$/g is O(n^2) in v8.
553-
* The native .trimEnd method is by far best, but since that's technically ES2019,
554-
* we provide a (still much faster than the simple regex) fallback.
555-
*/
556-
function trimEndImpl(s: string) {
557-
let end = s.length - 1;
558-
while (end >= 0) {
559-
if (!isWhiteSpaceLike(s.charCodeAt(end))) break;
560-
end--;
561-
}
562-
return s.slice(0, end + 1);
563-
}
564-
565534
export function getTextOfNode(node: Node, includeTrivia = false): string {
566535
return getSourceTextOfNodeFromSourceFile(getSourceFileOfNode(node), node, includeTrivia);
567536
}

0 commit comments

Comments
 (0)