Skip to content
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

Replace _.trim with native JS #418

Closed
wants to merge 1 commit into from
Closed

Replace _.trim with native JS #418

wants to merge 1 commit into from

Conversation

XhmikosR
Copy link
Member

@XhmikosR XhmikosR commented Mar 1, 2021

Second attempt after #408.

name = _.trimStart(_.trim(name), path.sep + '.') || path.basename(file);
svg = _.trim(svg);
name = _.trimStart(name.trim(), path.sep + '.') || path.basename(file);
svg = typeof svg === 'string' ? svg.trim() : svg;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jkphl So, the question is do we need to trim here? svg can be a Buffer which resulted in failures without this check, but it makes me wonder.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@XhmikosR Hmmm … I'm a bit unsure here (and cannot test in this very moment). It's possible that _.trim() has the neat side effect to automatically serialize Buffers and convert non-strings to empty strings, so that svg reliably is a string in the end. Can you confirm this? If so, we should ensure that svg ends up as a string in the end. Apart from that, we can surely use the native method.

Copy link
Member Author

@XhmikosR XhmikosR Mar 7, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That I don't know TBH. It wasn't caught by the tests at all, I just hit it since I'm manually testing the master and lint branches on https://github.com/twbs/icons/blob/main/package.json#L28

Copy link
Member Author

@XhmikosR XhmikosR Mar 11, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jkphl yup, _.trim does cast the value to string before trimming.

So, the question is if we need to convert this to a string if it's not one? Or why even attempt to trim if it's a Buffer for instance. In my case svg is a Buffer and it's only used a few lines below for Vinyl contents. Which further makes me wonder if Buffer.from is needed there in case svg is already a Buffer?

Anyway, we can clean that up later, separately from this.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not absolutely sure about my original intentions here. I guess I wanted to allow both string and Buffer arguments (or possibly I didn't expect Buffer arguments at all in the beginning and they just happened to work?). Using _.trim() on the svg parameter makes sure we don't add invalid shapes consisting of, say, whitespace characters only. Not sure how easily one can ensure this for a Buffer as well. Casting a Buffer to a string and then re-creating as a Buffer definitely isn't necessary. But we would have to make sure that also string arguments still work as we don't know how people use the add() method. So maybe you're right and we should leave this as is for the time being and come back at a later point.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are also Buffer.isBuffer() or vinyl.isBuffer(). Either way, this is not currently well covered by the tests, so let's leave it for now and we'll revisit later.

@Kreeg
Copy link
Member

Kreeg commented Jun 23, 2022

@XhmikosR I think we can go back to this later.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants