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

Preserve case #184

Closed
Tobbe opened this issue Oct 8, 2021 · 6 comments
Closed

Preserve case #184

Tobbe opened this issue Oct 8, 2021 · 6 comments

Comments

@Tobbe
Copy link

Tobbe commented Oct 8, 2021

Hi

We use this library to automatically provide plural and singular forms of data model names.

Typical input would be
User -> Users
Posts -> Post
Comment -> Comments
etc

Those all work great

Problem is with multi-word model names where we need to provide our own rules, like UserNews. "News" is the same in singular and plural. So in our use case we need to make up a plural form. So we do pluralize.addIrregularRule('UserNews', 'UserNewses'). However, now when we do pluralize('UserNews') we get 'Usernewses'. For us it's important to keep the casing of all letters as they are. Our models are case sensitive. So we need the plural in this case to be UserNewses.

I found a related issue at #131 and PR at #136 but nothing has unfortunately come out of them yet.

@blakeembrey I'm happy to work on a PR this weekend if you can provide some guidance on how you want this solved.

@Tobbe
Copy link
Author

Tobbe commented Oct 8, 2021

Same issue with for example addPluralRule. I modified one of the tests

it('should allow new plural matching rules', function () {
  expect(pluralize.plural('regex')).to.equal('regexes');

  pluralize.addPluralRule(/gex$/i, 'gexii');
  pluralize.addPluralRule(/^FooBar$/, 'FooBarii');

  expect(pluralize.plural('regex')).to.equal('regexii');
  expect(pluralize.plural('FooBar')).to.equal('FooBarii');
});

It fails because it gets "Foobarii"

@Tobbe
Copy link
Author

Tobbe commented Oct 8, 2021

Digging in to the code a bit more I've figured out a workaround that works for us.
addIrregularRule won't work, because it always does toLowerCase. But combining addPluralRule and addSingularRule with just the last word of the multi-word names works.

So instead of

pluralize.addIrregularRule('UserNews', 'UserNewses')

I can do

pluralize.addPluralRule(/News$/, 'Newses')
pluralize.addSingularlRule(/Newses$/, 'News')

@blakeembrey
Copy link
Collaborator

This doesn’t sound so much like you want to preserve case as you want this library to actually understand that UserNews is two words and apply the pluralization as if the suffix is “news”? This sounds out-of-scope for the library and pretty difficult to achieve. If you have a specific format, I’d suggest giving it the parts you want to pluralize and then formatting it back yourself.

@Tobbe
Copy link
Author

Tobbe commented Oct 9, 2021

I noticed you had tests for both PascalCase and camelCase, so I thought that was something you wanted to support

  ['camelCase', 'camelCases'],
  ['PascalCase', 'PascalCases'],

Regardless, I found another scenario where my workaround where I only look at the last word doesn't work. That's when the plural should be e.g. UserNewsList.

The way it works is I parse a file with singular names. When I find a word I don't know how to pluralize I ask the user. So:
"I don't know how to make UserNews plural. Please provide the plural form:" And the user inputs "UserNewsList". Internally I now just look at the last word, News, and ask your pluralize library for the plural and singular form of that. With this input, just looking at the last word I'd think the plural form of "News" is "List", so the final plural is "UserList" 🙁 But even if I realized to input "NewsList" as the plural it would come out as "Newslist" from pluralize and would still break my implementation.

I realize that making NewsList the plural of News is even more out of scope for pluralize.

So yeah, I will definitely have to write some more logic that's specific to our use case on top of pluralize.

@blakeembrey
Copy link
Collaborator

I noticed you had tests for both PascalCase and camelCase

IIRC that was more around keeping the casing and not supporting pluralization, but it's a good flag. I'll probably look at eliminating some of that logic since it causes a lot of confusion for people.

@Tobbe
Copy link
Author

Tobbe commented Oct 9, 2021

@blakeembrey Thanks for your input in this thread. I'll close this issue now as what I want to do is out of scope for pluralize and is much better handled by the consumer of your library (i.e. my own app)

Thanks for creating and maintaining this library!

@Tobbe Tobbe closed this as completed Oct 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants