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

Updated the restoreCase logic to check for upper and lower case pluralization #136

Conversation

lappang-cheung
Copy link

I'd updated the logic for the restoreCase as it has missed some testcases as well and it shall pass for the new ones. And this is the solution for https://github.com/blakeembrey/pluralize/issues/131

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

      pluralize.addPluralRule(/vIvP$/i, 'vIvPs');

      expect(pluralize.plural('vIvP')).to.equal('vIvPs');
    });

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

      pluralize.addPluralRule(/IP$/i, 'IPs');

      expect(pluralize.plural('IP')).to.equal('IPs');
    });

    it('should allow new plural matching rules', function () {
      expect(pluralize.plural('Blank Application2')).to.equal('Blank Application2s');

      pluralize.addPluralRule(/tion2$/i, 'tion2S');

      expect(pluralize.plural('Blank Application2')).to.equal('Blank Application2S');
    });

Screen Shot 2019-11-01 at 10 44 06 AM

…s well added new testcases for checking them
@coveralls
Copy link

coveralls commented Nov 1, 2019

Coverage Status

Coverage remained the same at 100.0% when pulling 896313f on lappang-cheung:features/restoreCaseLogicUpdate into abb3991 on blakeembrey:master.

pluralize.js Outdated
@@ -49,6 +49,11 @@
// Tokens are an exact match.
if (word === token) return token;

// Starts with the same letters
if (token.toLowerCase().startsWith(word.toLowerCase())) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is a breaking change for supported environments of pluralize. We should change startsWith to something else. Can you also remove the token logic below? This probably overlaps in implementation.

Copy link
Author

Choose a reason for hiding this comment

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

Updated the code with:

if ((token.toLowerCase().substring(0, word.length)) === word.toLowerCase()) {
      return word + token.substring(word.length);
    }

These parts only check for special cases where we have weird combinations when calling the "pluralize" method and if this check fails then it goes to the base case. Such as "sTaRTREK" and wanting the result to be "sTaRTREKS".

.gitignore Outdated
@@ -3,3 +3,5 @@ node_modules
components
build
coverage
yarn.lock
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please don't change this.

@lappang-cheung
Copy link
Author

lappang-cheung commented Nov 1, 2019

Hey Blake,

I'd undid the changes to the ".gitignore" file and updated the "pluralize" file.

As for my logic:

// This is an unique case, where we try to preserved the words that aren't changed significantly.
    // E.g. "sTaRTREK" (plural form is sTaRTREKs).
    if ((token.toLowerCase().substring(0, word.length)) === word.toLowerCase()) {
      return word + token.substring(word.length);
    }

    // Lower cased words. E.g. "hello".
    if (word === word.toLowerCase()) return token.toLowerCase();

    // Upper cased words. E.g. "WHISKY".
    if (word === word.toUpperCase()) return token.toUpperCase();

This parts only check for special cases where we have weird combination when calling "pluralize" method and if this checks fails then it goes to the base case. Such as "sTaRTREK" and wanting the result to be "sTaRTREKS".

Screen Shot 2019-11-01 at 1 57 48 PM

@blakeembrey
Copy link
Collaborator

I’d like to see more tests and unnecessary changes reverted before I can merge it. Based on this PR, I’m pretty sure it’ll break anyone using upper cased words. At that point, it’s probably better to just always use lowercase and not have this logic at all.

@blakeembrey
Copy link
Collaborator

Actually, am I understanding this correctly that it'll only word if the entire token is matched? In that case, I think it'd be better that we find a more appropriate solution. It also doesn't solve #131 unless you intend to add manual overrides to every word, which seems redundant.

@blakeembrey
Copy link
Collaborator

If this is a real issue for people, I propose just removing the feature altogether and making the consumers of the library handle the casing they want. It'd make it slightly faster, make those overrides "expected", and making it to upper case is easier than doing all the other magic.

@blakeembrey
Copy link
Collaborator

Here's a test case to consider that fails with this change, if you're planning to add more tests:

> pluralize.plural('SCHEMA')
'SCHEMAta'

@Tobbe Tobbe mentioned this pull request Oct 8, 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

Successfully merging this pull request may close these issues.

None yet

3 participants