You-Dont-Need-Lodash-Underscore icon indicating copy to clipboard operation
You-Dont-Need-Lodash-Underscore copied to clipboard

trimStart and trimEnd

Open gnapse opened this issue 6 years ago • 11 comments

I think .trimStart and .trimEnd are easily replaceable:

_.trimStart(str) === str.replace(/^\s+/, '')
_.trimEnd(str) === str.replace(/\s+$/, '')

Just wanted to make sure it's ok and that I'm not missing anything here, so I can add a PR to add this to the README.

Update: I realize that these two lodash functions have an extra argument with the chars to be replaced. I still think it's doable, but perhaps it over complicates things. Perhaps we could mention in the README that when not needing to pass the second argument, it's possible to simply use the alternatives I list below, and leave the usage of the lodash functions for when the second argument is needed.

gnapse avatar Apr 28 '18 15:04 gnapse

Thanks @gnapse PR welcome

stevemao avatar Apr 29 '18 05:04 stevemao

I disagree. I'd prefer to not have needless regex in my code.

stevenvachon avatar May 07 '18 01:05 stevenvachon

Isn't this repo about recommendations to users of lodash of when they could go by with a relatively simple native alternative? I understand that some users may still prefer lodash's way of doing some particular task, and that's fine. This repo is not forcing anyone to follow all the native alternatives presented.

gnapse avatar May 07 '18 11:05 gnapse

This repo is not forcing anyone to follow all the native alternatives presented.

I agree to this ( and PR welcome 😃 )

terrierscript avatar May 07 '18 11:05 terrierscript

I guess, but it's going to cause a detailed initial configuration or annoying future changes when the "all" preset is too restrictive. Perhaps we need a "recommended" preset.

stevenvachon avatar May 07 '18 12:05 stevenvachon

I guess, but it's going to cause a detailed initial configuration or annoying future changes when the "all" preset is too restrictive. Perhaps we need a "recommended" preset.

I think you are talking about the eslint config?

stevemao avatar May 07 '18 14:05 stevemao

I think you are talking about the eslint config?

Isn't that the function of this project? But, yes, and this specifically: https://github.com/you-dont-need/You-Dont-Need-Lodash-Underscore/blob/master/configuring.md#now-configure-your-plugin

stevenvachon avatar May 07 '18 14:05 stevenvachon

For me the real benefit of this project, including its eslint configuration, is to remove unnecessary code from my bundles when I can go by with a lighter alternative. If you take a look at trimEnd's implementation you'll see that it's not at all a one liner or anything like that. It even depends on a few other lodash internal stuff (the same goes for trimStart).

However, for the most common use case where the second parameter is not provided, because you want to trim whitespace, the alternative solution involves a lot less code, a simple one-liner using a native javascript method, and a relatively simple regex. I think it's worth to show users the simpler alternative in this case. I'd even go as far as to say that it should be recommended, if we ever have a recommended preset. For users that do not want to write the regex, as is your case, the rule can always be turned off in the eslintrc file.

gnapse avatar May 07 '18 15:05 gnapse

Isn't that the function of this project?

The eslint doesn't really include things like these. Only the simple ones like concat() or map() (the ones you can easily replace lodash with the corresponding native methods without thinking too much). However, the readme can go a bit future as per @gnapse mentioned above.

stevemao avatar May 08 '18 00:05 stevemao

I see the readme as a good support for the eslint config. The primary goal is to be told "you don't need this".

stevenvachon avatar May 08 '18 00:05 stevenvachon

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/trimStart

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/trimEnd

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/Trim

heygrady avatar Nov 07 '19 21:11 heygrady