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

Conversation

aykutkardas
Copy link
Contributor

Before submitting the PR, please make sure you do the following

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • Prefix your PR title with [feat], [fix], [chore], or [docs].
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.

Tests

  • Run the tests with npm test and lint the project with npm run lint

I made the trim utils more readable and performant. I've also included their tests to make sure they're working correctly.

image

@aykutkardas aykutkardas changed the title [refactor] trim utils and write tests [refactor] refactor trim utils and write tests Nov 6, 2021
@baseballyama
Copy link
Member

It looks good to me.

Should we add a test that contains spaces between content like " content "?
And \r, \n and \t also?

@aykutkardas
Copy link
Contributor Author

It looks good to me.

Should we add a test that contains spaces between content like " content "? And \r, \n and \t also?

Would it be better if I wrote the tests this way?

const value = trim_start('    lorem ipsum ');
assert.equal(value, 'lorem ipsum ');

const value = trim_end(' lorem ipsum   ');
assert.equal(value, ' lorem ipsum');

@baseballyama
Copy link
Member

Yes. like that!
Specific IMO is : ' \r\n\t content \r\n\t content \r\n\t '.

NOTE: ' \r\n\t ' is built from the following characters.

  • space
  • \r
  • \n
  • \t
  • tab

@drunkwinter
Copy link

Hi, i'm pretty sure you have a good reason, but i'm just curious as to why you don't use String.prototype.trimStart() here?

Does it have limitations i'm not familiar with, in that case i'd like to know ;)

@aykutkardas
Copy link
Contributor Author

Hi, i'm pretty sure you have a good reason, but i'm just curious as to why you don't use String.prototype.trimStart() here?

Does it have limitations i'm not familiar with, in that case i'd like to know ;)

'trimStart' is available since Node v10. Also, older browsers do not support it. @drunkwinter

@aykutkardas
Copy link
Contributor Author

aykutkardas commented Nov 7, 2021

Yes. like that! Specific IMO is : ' \r\n\t content \r\n\t content \r\n\t '.

Tests updated. @baseballyama Thanks for your feedback


Trim methods pass the tests, but I don't understand what it is that doesn't pass the test.
Tests (8, macOS-latest)

@baseballyama
Copy link
Member

Thank you for the fix!

Trim methods pass the tests, but I don't understand what it is that doesn't pass the test.
Sometimes tests are unstable.
Maybe it will pass if a maintainer runs CI again.

@dummdidumm dummdidumm added compiler Changes relating to the compiler perf labels Nov 7, 2021
@drunkwinter
Copy link

Hi, i'm pretty sure you have a good reason, but i'm just curious as to why you don't use String.prototype.trimStart() here?
Does it have limitations i'm not familiar with, in that case i'd like to know ;)

'trimStart' is available since Node v10. Also, older browsers do not support it. @drunkwinter

Thanks for explaining!

There is a polyfill listed on MDN with a shorter regex /^\s+/, which seems to be about 7% faster.

@aykutkardas
Copy link
Contributor Author

Thanks for explaining!

There is a polyfill listed on MDN with a shorter regex /^\s+/, which seems to be about 7% faster.

\s is an expression for whitespace characters. It also includes characters such as spaces, tabs, and line breaks. But I wanted to stick to the existing RegEx here.

@Conduitry Conduitry merged commit 2c8e77b into sveltejs:master Nov 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler Changes relating to the compiler perf
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants