From 5815b2b5584d35d5f85a8a2e4d3223840bcead76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aykut=20Karda=C5=9F?= Date: Sat, 6 Nov 2021 11:47:28 +0300 Subject: [PATCH] Refactor and Optimize trim utils --- src/compiler/utils/patterns.ts | 2 ++ src/compiler/utils/trim.ts | 12 +++--------- 2 files changed, 5 insertions(+), 9 deletions(-) diff --git a/src/compiler/utils/patterns.ts b/src/compiler/utils/patterns.ts index 317a7c1990..06b2739259 100644 --- a/src/compiler/utils/patterns.ts +++ b/src/compiler/utils/patterns.ts @@ -1,3 +1,5 @@ export const whitespace = /[ \t\r\n]/; +export const startWhitespace = /^[ \t\r\n]*/; +export const endWhitespace = /[ \t\r\n]*$/; export const dimensions = /^(?:offset|client)(?:Width|Height)$/; diff --git a/src/compiler/utils/trim.ts b/src/compiler/utils/trim.ts index 43f413bb1d..f9166fb4df 100644 --- a/src/compiler/utils/trim.ts +++ b/src/compiler/utils/trim.ts @@ -1,15 +1,9 @@ -import { whitespace } from './patterns'; +import { startWhitespace, endWhitespace } from './patterns'; export function trim_start(str: string) { - let i = 0; - while (whitespace.test(str[i])) i += 1; - - return str.slice(i); + return str.replace(startWhitespace, ''); } export function trim_end(str: string) { - let i = str.length; - while (whitespace.test(str[i - 1])) i -= 1; - - return str.slice(0, i); + return str.replace(endWhitespace, ''); }