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

refactor: drop lodash.trim dependency #749

Merged
merged 8 commits into from
Nov 7, 2022
Merged

refactor: drop lodash.trim dependency #749

merged 8 commits into from
Nov 7, 2022

Conversation

XhmikosR
Copy link
Member

@XhmikosR XhmikosR commented Nov 2, 2022

Supersedes #742

Refs #598

strager and others added 3 commits November 2, 2022 08:00
Use JavaScript's built-in String.prototype.trim method instead of
lodash.trim, reducing dependency bloat.

In one case, we were calling lodash.trim with a Buffer instance.
lodash.trim calls toString on its argument, so we didn't notice this
issue before. Continue calling toString before calling
String.prototype.trim to preserve behavior.
@XhmikosR XhmikosR added the dependencies Pull requests that update a dependency file label Nov 2, 2022
@XhmikosR XhmikosR requested a review from Kreeg November 2, 2022 06:03
@XhmikosR XhmikosR marked this pull request as ready for review November 2, 2022 06:07
@XhmikosR
Copy link
Member Author

XhmikosR commented Nov 6, 2022

@Kreeg this seems to work fine from my tests. Does it LGTY?

@XhmikosR XhmikosR added the squash label Nov 6, 2022
svg = trim(svg);
name = trimStart(name.trim(), `${path.sep}.`) || path.basename(file);
// TODO: Avoid Buffer -> String -> Buffer conversion of svg.
svg = svg.toString().trim();
Copy link
Member Author

Choose a reason for hiding this comment

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

Just wondering, would it make more sense to check if svg is Buffer with Buffer.isBuffer() and if so only then call toString()?

Copy link
Member

Choose a reason for hiding this comment

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

This would seem a little overengineering for me.

@XhmikosR
Copy link
Member Author

XhmikosR commented Nov 7, 2022

After a closer look, this is not identical to what we had before. Previously, if you passed null or undefined, it would proceed silently and fail later on. Now we'll get a TypeError:

C:\Users\xmr\Desktop\svg-sprite\lib\svg-sprite.js:86
        file = file.trim();
                    ^

TypeError: Cannot read properties of undefined (reading 'trim')
    at SVGSpriter.add (C:\Users\xmr\Desktop\svg-sprite\lib\svg-sprite.js:86:21)
    at C:\Users\xmr\Desktop\svg-sprite\example.js:62:17
    at Array.forEach (<anonymous>)
    at addFixtureFiles (C:\Users\xmr\Desktop\svg-sprite\example.js:61:11)
    at Object.<anonymous> (C:\Users\xmr\Desktop\svg-sprite\example.js:71:1)
    at Module._compile (node:internal/modules/cjs/loader:1159:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1213:10)
    at Module.load (node:internal/modules/cjs/loader:1037:32)
    at Module._load (node:internal/modules/cjs/loader:878:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)

Node.js v18.12.1

name = trimStart(name.trim(), `${path.sep}.`) || path.basename(file);
// TODO: Avoid Buffer -> String -> Buffer conversion of svg.
if (Buffer.isBuffer(svg)) {
svg = svg.toString();
Copy link
Member

Choose a reason for hiding this comment

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

Nah, I think that's OK.

@XhmikosR XhmikosR merged commit 23958e8 into main Nov 7, 2022
@XhmikosR XhmikosR deleted the lodash-trim-dep branch November 7, 2022 15:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dependencies Pull requests that update a dependency file squash
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants