Simplified the regex pattern that is used to convert field names to config keys.

Also added some documentation and examples.
master
blablubbabc 2020-11-25 22:21:39 +01:00
parent 685fd96586
commit 9c678b58c4
1 changed files with 28 additions and 1 deletions

View File

@ -19,11 +19,38 @@ import com.nisovin.shopkeepers.util.TextUtils;
public class ConfigHelper {
private static final Pattern CONFIG_KEY_PATTERN = Pattern.compile("([A-Z][a-z]+)");
private static final Pattern CONFIG_KEY_PATTERN = Pattern.compile("([A-Z])");
private ConfigHelper() {
}
/**
* Converts from lower camel case field names to lowercase config keys that separate words by hyphens (-).
* <p>
* Some notes on the conversion:
* <ul>
* <li>Each uppercase (ASCII) character is interpreted as the beginning of a new word and a hyphen is inserted.
* <li>The conversion does not deal with uppercase abbreviations. For example, 'someSQLField' is expected to be
* called 'someSqlField'.
* <li>The field name is expected to start with a lowercase character.
* <li>Digits are treated like any other lowercase characters and not separated into different words.
* <li>All characters are converted to lowercase.
* </ul>
* <p>
* Examples:
* <ul>
* <li>"ab" -> "ab"
* <li>"aBcDe" -> "a-bc-de"
* <li>"aBCDe" -> "a-b-c-de"
* <li>"textLine1" -> "text-line1"
* <li>"1apple" -> "1apple"
* <li>"aBc1De2" -> "a-bc1-de2"
* </ul>
*
* @param fieldName
* the field name
* @return the config key
*/
public static String toConfigKey(String fieldName) {
return CONFIG_KEY_PATTERN.matcher(fieldName).replaceAll("-$1").toLowerCase(Locale.ROOT);
}