Skip to content

[refactor] refactor trim utils and write tests #6909

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Nov 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/compiler/utils/patterns.ts
Original file line number Diff line number Diff line change
@@ -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)$/;
12 changes: 3 additions & 9 deletions src/compiler/utils/trim.ts
Original file line number Diff line number Diff line change
@@ -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, '');
}
16 changes: 16 additions & 0 deletions test/utils/index.ts
Original file line number Diff line number Diff line change
@@ -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');
});
});
});