blakeembrey / change-case

Convert strings between camelCase, PascalCase, Capital Case, snake_case and more
MIT License
2.21k stars 92 forks source link

Case Transformations Remove Punctuation #324

Closed jahoseph closed 6 months ago

jahoseph commented 6 months ago

Description: The change-case package, as used in Raycast's Change Case extension (https://github.com/raycast/extensions/issues/9691), is removing punctuation when transforming text cases, including 'Sentence case' and others. The removal of punctuation — including commas, colons, periods, ampersands, and more — merges text into a single run-on sentence. This behaviour disrupts the text structure and readability.

Example of Current Behavior: Input: "Story: The Quick, Brown Cat & Slow Dog Are Friends. They live happily." Output after case conversion: "Story the quick brown cat slow dog are friends they live happily." This is problematic as it removes punctuation necessary for maintaining the semantic integrity of the text.

Steps To Reproduce: Enter text with capitalisation and punctuation into the conversion function. Apply any case transformation, such as 'Sentence case'. Observe the punctuation-less output, irrespective of the selected case transformation.

Expected Behaviour: I would expect that during any case transformation, the punctuation remains intact, preserving the original structure of the text. The ideal output for 'Sentence case' should be: "Story: the quick, brown cat & slow dog are friends. They live happily."

Request for Feature Implementation: Is it possible to implement a feature within the 'change-case' package that maintains punctuation during all case transformations? Adding this feature would greatly enhance the functionality of the package, not only for Raycast extension users but for any other software implementations leveraging this package for text processing.

Preserving punctuation would maintain the readability and proper sentence structure, which is critical for any text transformation tool.

blakeembrey commented 6 months ago

While I understand your concern and appreciate the issue, it's not really something I intend to write or maintain. This library isn't intended to transform entire sentences with understanding of punctuation. It's just meant to transform between programming cases, i.e. camelCase to Camel case, and not intended for text processing.

I'm going to close this as won't fix for now, but I wouldn't reject a PR if someone wanted to write this feature themselves into the package. It'd require a rewrite and probably a new major version because the current design can not be reused for this, you'd need something that parses out both words and characters that should be ignored, while currently I'm only extracting the words to transform according to the different transformations.

blakeembrey commented 6 months ago

Another option would be to add a flag input to the existing title-case library I maintain which seems closer to your expectations: title-case. Does that seem helpful?

The tricky thing with maintaining anything that works for a proper sentence is that there's a ton of edge cases that pop up - different sentence endings (!, ?, .), acronym support (NASA), even made up words (iPhone, iOS). In title-case I did it by only changing the case of words that are lower-case only.

jahoseph commented 6 months ago

Thank you for the prompt response and for considering the request. I understand that change-case is primarily intended for programming and not for complex text processing involving punctuation within sentences. Your suggestion about potentially adding a flag to the title-case library is interesting, but I'm afraid I don't have the skills or experience to contribute. In the meantime, I'll look for other methods to handle the punctuation-preserving transformations needed for my use case. Thank you for your time and for maintaining such a useful set of libraries.

blakeembrey commented 6 months ago

I did end up making an attempt in the existing title-case library and now have an option for it: https://github.com/blakeembrey/change-case/releases/tag/title-case%404.3.0.