Skip to content

Commit 2c8e77b

Browse files
authored
[refactor] refactor trim utils and write tests (#6909)
1 parent 7ae1e6e commit 2c8e77b

File tree

3 files changed

+21
-9
lines changed

3 files changed

+21
-9
lines changed

src/compiler/utils/patterns.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
export const whitespace = /[ \t\r\n]/;
2+
export const start_whitespace = /^[ \t\r\n]*/;
3+
export const end_whitespace = /[ \t\r\n]*$/;
24

35
export const dimensions = /^(?:offset|client)(?:Width|Height)$/;

src/compiler/utils/trim.ts

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
1-
import { whitespace } from './patterns';
1+
import { start_whitespace, end_whitespace } from './patterns';
22

33
export function trim_start(str: string) {
4-
let i = 0;
5-
while (whitespace.test(str[i])) i += 1;
6-
7-
return str.slice(i);
4+
return str.replace(start_whitespace, '');
85
}
96

107
export function trim_end(str: string) {
11-
let i = str.length;
12-
while (whitespace.test(str[i - 1])) i -= 1;
13-
14-
return str.slice(0, i);
8+
return str.replace(end_whitespace, '');
159
}

test/utils/index.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import * as assert from 'assert';
2+
import { trim_start, trim_end } from '../../src/compiler/utils/trim';
3+
4+
describe('utils', () => {
5+
describe('trim', () => {
6+
it('trim_start', () => {
7+
const value = trim_start(' \r\n\t svelte content \r\n\t ');
8+
assert.equal(value, 'svelte content \r\n\t ');
9+
});
10+
11+
it('trim_end', () => {
12+
const value = trim_end(' \r\n\t svelte content \r\n\t ');
13+
assert.equal(value, ' \r\n\t svelte content');
14+
});
15+
});
16+
});

0 commit comments

Comments
 (0)