diff --git a/src/compiler/utils/patterns.ts b/src/compiler/utils/patterns.ts index 317a7c199..0728035a4 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 start_whitespace = /^[ \t\r\n]*/; +export const end_whitespace = /[ \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 43f413bb1..05af79cc9 100644 --- a/src/compiler/utils/trim.ts +++ b/src/compiler/utils/trim.ts @@ -1,15 +1,9 @@ -import { whitespace } from './patterns'; +import { start_whitespace, end_whitespace } 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(start_whitespace, ''); } 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(end_whitespace, ''); } diff --git a/test/utils/index.ts b/test/utils/index.ts new file mode 100644 index 000000000..a8aed9387 --- /dev/null +++ b/test/utils/index.ts @@ -0,0 +1,16 @@ +import * as assert from 'assert'; +import { trim_start, trim_end } from '../../src/compiler/utils/trim'; + +describe('utils', () => { + describe('trim', () => { + it('trim_start', () => { + const value = trim_start(' \r\n\t svelte content \r\n\t '); + assert.equal(value, 'svelte content \r\n\t '); + }); + + it('trim_end', () => { + const value = trim_end(' \r\n\t svelte content \r\n\t '); + assert.equal(value, ' \r\n\t svelte content'); + }); + }); +});