forked from magento/magento2-phpstorm-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCamelCaseToHyphen.java
51 lines (46 loc) · 1.33 KB
/
CamelCaseToHyphen.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
package com.magento.idea.magento2plugin.util;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@SuppressWarnings({
"PMD.ClassNamingConventions",
"PMD.NonThreadSafeSingleton"
})
public class CamelCaseToHyphen {
private static CamelCaseToHyphen instance;
/**
* Get Singleton.
*
* @return CamelCaseToHyphen
*/
public static CamelCaseToHyphen getInstance() {
if (null == instance) {
instance = new CamelCaseToHyphen();
}
return instance;
}
/**
* Converts camel case to hyphen.
* e.g. "TestString" -> "test-string"
*
* @param string the origin string.
* @return string
*/
public String convert(final String string) {
final String regex = "(?=[A-Z][a-z])";
final String subst = "-";
final Pattern pattern = Pattern.compile(regex);
final Matcher matcher = pattern.matcher(string);
final String result =
matcher.replaceAll(subst).toLowerCase(new Locale("en","EN"));
final char hyphenSeparator = '-';
if (result.charAt(0) == hyphenSeparator) {
return result.substring(1);
}
return result;
}
}