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

Trim but with characters other than whitespace #415

Open
danielo515 opened this issue Jul 1, 2022 · 4 comments
Open

Trim but with characters other than whitespace #415

danielo515 opened this issue Jul 1, 2022 · 4 comments

Comments

@danielo515
Copy link

danielo515 commented Jul 1, 2022

Hello!

Today I found myself in a situation where I had to remove the numbers at the end of each string of one union type.
This is because I have my color tokens defined as an union of grey1 | grey2 | greyNN | red1 | red2 | redNN and I wanted just the name part for a function that was going to compose them, something like this:

function doSomething (color: string) {
return { bg: `${color}1`, text: `${color}11` } 
}

And I wanted to make sure that I only passed valid colors. But for that I need the list without the number at the end.
I was surprised of how easy was to encode this using type-fest:

import { Split } from 'type-fest';
type Digit = '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9';
type NumberAtEnd = `${string}${Digit}`;

export type RemoveNumbers<S extends string> = S extends `${NumberAtEnd}${Digit}`
  ? RemoveNumbers<Split<S, Digit>[0]>
  : Split<S, Digit>[0];

What do you think about adding this utility to the library? It can easily be extended to do a maximum of N digits. To be honest, I don't think anyone will need anything more than 2 or 3 or else you are doing something probably wrong

Regards

Upvote & Fund

  • We're using Polar.sh so you can upvote and help fund this issue.
  • The funding will be given to active contributors.
  • Thank you in advance for helping prioritize & fund our backlog.
Fund with Polar
@sindresorhus
Copy link
Owner

This feels a bit niche. Can you think of any other use-cases for this? Or maybe a way to make the utility a bit more general?

@danielo515
Copy link
Author

The only use case I think now is the one I had with color tokens (which I would say is a big one for FE people).
Maybe providing the type to remove from the tail could make it more general? Like if you want to remove a suffix, or a list of suffixes? But I don't know how more hard that may be.

@sindresorhus
Copy link
Owner

I think a RemovePrefix and RemoveSuffix type would be more generally useful.

@fregante fregante changed the title Proposal: Remove numbers at the end of string Trim but with characters other than whitespace Aug 17, 2024
@fregante
Copy link
Collaborator

fregante commented Aug 17, 2024

This is just TrimLeft/TrimRight but with '1234567890' as characters

This implementation might work: #134 (comment)

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

No branches or pull requests

3 participants