mirror of
https://github.com/Kingsrook/qqq.git
synced 2025-07-18 13:10:44 +00:00
CE-936 - Add method inferNameFromBackendName
This commit is contained in:
@ -1030,6 +1030,50 @@ public class QInstanceEnricher
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Do a default mapping from an underscore_style field name to a camelCase name.
|
||||
**
|
||||
** Examples:
|
||||
** <ul>
|
||||
** <li>word_another_word_more_words -> wordAnotherWordMoreWords</li>
|
||||
** <li>l_ul_ul_ul -> lUlUlUl</li>
|
||||
** <li>tla_first -> tlaFirst</li>
|
||||
** <li>word_then_tla_in_middle -> wordThenTlaInMiddle</li>
|
||||
** <li>end_with_tla -> endWithTla</li>
|
||||
** <li>tla_and_another_tla -> tlaAndAnotherTla</li>
|
||||
** <li>ALL_CAPS -> allCaps</li>
|
||||
** </ul>
|
||||
*******************************************************************************/
|
||||
public static String inferNameFromBackendName(String backendName)
|
||||
{
|
||||
StringBuilder rs = new StringBuilder();
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////
|
||||
// build a list of words in the name, then join them with _ and lower-case the result //
|
||||
////////////////////////////////////////////////////////////////////////////////////////
|
||||
String[] words = backendName.toLowerCase(Locale.ROOT).split("_");
|
||||
for(int i = 0; i < words.length; i++)
|
||||
{
|
||||
String word = words[i];
|
||||
if(i == 0)
|
||||
{
|
||||
rs.append(word);
|
||||
}
|
||||
else
|
||||
{
|
||||
rs.append(word.substring(0, 1).toUpperCase());
|
||||
if(word.length() > 1)
|
||||
{
|
||||
rs.append(word.substring(1));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return (rs.toString());
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** If a app didn't have any sections, generate "sensible defaults"
|
||||
*******************************************************************************/
|
||||
|
@ -232,6 +232,23 @@ class QInstanceEnricherTest extends BaseTest
|
||||
}
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
@Test
|
||||
void testInferNameFromBackendName()
|
||||
{
|
||||
assertEquals("id", QInstanceEnricher.inferNameFromBackendName("id"));
|
||||
assertEquals("wordAnotherWordMoreWords", QInstanceEnricher.inferNameFromBackendName("word_another_word_more_words"));
|
||||
assertEquals("lUlUlUl", QInstanceEnricher.inferNameFromBackendName("l_ul_ul_ul"));
|
||||
assertEquals("tlaFirst", QInstanceEnricher.inferNameFromBackendName("tla_first"));
|
||||
assertEquals("wordThenTlaInMiddle", QInstanceEnricher.inferNameFromBackendName("word_then_tla_in_middle"));
|
||||
assertEquals("endWithTla", QInstanceEnricher.inferNameFromBackendName("end_with_tla"));
|
||||
assertEquals("tlaAndAnotherTla", QInstanceEnricher.inferNameFromBackendName("tla_and_another_tla"));
|
||||
assertEquals("allCaps", QInstanceEnricher.inferNameFromBackendName("ALL_CAPS"));
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
|
Reference in New Issue
Block a user