toss / es-toolkit

A modern JavaScript utility library that's 2-3 times faster and up to 97% smaller—a major upgrade to lodash.
https://es-toolkit.slash.page
Other
6.28k stars 269 forks source link

feat: add trim, ltrim and rtrim functions #411

Closed ankitjha420 closed 6 days ago

ankitjha420 commented 4 weeks ago

This addresses issue #384 by implementing and testing three string manipulation utility functions: trim, ltrim, and rtrim. These functions provide essential trimming capabilities, allowing for precise removal of specified characters from the start, end, or both ends of strings.

Implemented Functions:

trim(str: string, toTrim: string | string[]): string: Removes all specified characters from the string. ltrim(str: string, toTrim: string): string: Trims specified characters from the start of the string. rtrim(str: string, toTrim: string): string: Trims specified characters from the end of the string.

Tests & Benchmarks:

Added comprehensive unit tests for each function, covering various scenarios, including edge cases and handling of empty strings. (trim.spec.ts, ltrim.spec.ts, rtrim.spec.ts). Benchmark test file has been created

PS: This is my first contribution so I may have missed something obvious even though I read the contribution doc, please let me know.

vercel[bot] commented 4 weeks ago

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
es-toolkit ✅ Ready (Inspect) Visit Preview 💬 Add feedback Sep 13, 2024 6:33am
raon0211 commented 4 weeks ago

Let me review this during the weekend :)

nemorize commented 2 weeks ago

trim function you wrote is bit odd.

Normally, trim is a function that removes leading and trailing occurrences (ltrim(rtrim(target, toTrim), toTrim)), but the function you wrote removes all occurrences (target.replaceAll(toTrim, ''))

Lodash: https://lodash.com/docs/#trim PHP: https://www.php.net/manual/en/function.trim.php C#: https://learn.microsoft.com/en-us/dotnet/api/system.string.trim?view=net-8.0

ankitjha420 commented 2 weeks ago

Thank you @nemorize for the feedback, I corrected it. Please have a look again and sorry I misunderstood the issue.

codecov-commenter commented 6 days ago

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Project coverage is 99.54%. Comparing base (c5d0254) to head (2c797f6).

Additional details and impacted files [![Impacted file tree graph](https://app.codecov.io/gh/toss/es-toolkit/pull/411/graphs/tree.svg?width=650&height=150&src=pr&token=8N5S3AR3C7&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=toss)](https://app.codecov.io/gh/toss/es-toolkit/pull/411?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=toss) ```diff @@ Coverage Diff @@ ## main #411 +/- ## ========================================== - Coverage 99.60% 99.54% -0.07% ========================================== Files 199 202 +3 Lines 1514 1531 +17 Branches 400 403 +3 ========================================== + Hits 1508 1524 +16 - Misses 5 6 +1 Partials 1 1 ```