- code improvement: add function 'getVariableValueAsInteger' in JavaScript style
diff --git a/src/prettify/parser/Prettify.java b/src/prettify/parser/Prettify.java index 2abcaff..622b2d8 100644 --- a/src/prettify/parser/Prettify.java +++ b/src/prettify/parser/Prettify.java
@@ -677,8 +677,8 @@ * @const */ // Javascript treat true as 1 - String regexExcls = (regexLiterals instanceof Boolean ? 1 : (Integer) regexLiterals) > 1 - ? "" // Multiline regex literals + String regexExcls = Util.getVariableValueAsInteger(regexLiterals) > 1 + ? "" // Multiline regex literals : "\n\r"; /** * @const
diff --git a/src/prettify/parser/Util.java b/src/prettify/parser/Util.java index 419d955..4613ef8 100644 --- a/src/prettify/parser/Util.java +++ b/src/prettify/parser/Util.java
@@ -30,6 +30,34 @@ protected Util() { } + + /** + * Treat a variable as an integer in JavaScript style. Note this function can + * only handle integer and boolean currently. + * + * @param var the variable to get value from + * @return the integer value + * @throws IllegalArgumentException the data type of {@code var} is neither + * integer nor boolean. + */ + public static Integer getVariableValueAsInteger(Object var) { + if (var == null) { + throw new NullPointerException("argument 'var' cannot be null"); + } + + Integer returnResult = -1; + + if (var instanceof Integer) { + returnResult = (Integer) var; + } else if (var instanceof Boolean) { + // Javascript treat true as 1 + returnResult = (Boolean) var ? 1 : 0; + } else { + throw new IllegalArgumentException("'var' is neither integer nor boolean"); + } + + return returnResult; + } /** * Get all the matches for {@code string} compiled by {@code pattern}. If