case-converter icon indicating copy to clipboard operation
case-converter copied to clipboard

Issue with upper-case and numeric only

Open owenvoke opened this issue 10 months ago • 2 comments

We are hitting issues where we have only upper-case and numeric in a string (technical terms).

fromAuto() will detect these as not uppercase, and result in using the UppercaseSplitter.

Some examples include:

  • IR35 -> I_R35
  • ISO8601 -> I_S_O8601

Is it possible to use the UnderscoreSplitter if it only contains upper-alpha and numeric characters? Or should this be handled in-app by using ->fromMacro() (or similar to prevent the UppercaseSplitter being used). Such as below:

/** @internal */
class CaseConverter
{
    public static function toMacro(string $value): string
    {
        $converter = new Convert($value);

        if (preg_match('#^[\p{Lu}\d]+$#u', $value)) {
            $converter->fromMacro();
        }

        return $converter->toMacro();
    }
}

owenvoke avatar Jun 04 '25 15:06 owenvoke

Problems with numbers are not new, https://github.com/jawira/case-converter/issues/85, this is something is described in documentation https://jawira.github.io/case-converter/known-issues.html.

Using numbers is always tricky, the reason is that number are not uppercase nor lowercase, but case-converter considers them always as lowercase as a tie-breaking mecanism.

I cannot change the actual behavior since it will cause some BC breaks. Maybe I can add some extra parameter to ->fromAuto() or create another fromAuto to customize number handling. Are you working in an open source project? can you provide a link please? It will help me to better understand the problem to get an appropriate solution.

jawira avatar Jun 05 '25 10:06 jawira

The package that this is used in is open source, but the enums are used within closed source code. Basically, this class handles conversion of native enums to GraphQL enums.

owenvoke avatar Jun 05 '25 10:06 owenvoke

Hello, could you update to v3.6.0 ?

To recognize digits as Uppercase you have to use fromAuto like this:


$cc = new \Jawira\CaseConverter\CaseConverter();

$str1 = $cc->convert('CPU486')->fromAuto()->toMacro();
$str2 = $cc->convert('CPU486')->fromAuto(false)->toMacro();

echo $str1, PHP_EOL; // C_P_U486
echo $str2, PHP_EOL; // CPU486

This feature is not documented yet.
Please give me some feedback so I can close this issue.

ps In the example I use CaseConverter factory, but you can still instantiate Convert class.

jawira avatar Jun 13 '25 21:06 jawira

I will update and test it out on Monday. Thank you!

owenvoke avatar Jun 14 '25 07:06 owenvoke

@jawira, this works perfectly for our scenarios. Thank you so much! 🙌🏻 Happy to close this ticket.

owenvoke avatar Jun 16 '25 08:06 owenvoke