25 Commits
Author SHA1 Message Date
Shekhar Gulati 5487cda192 [Gradle Release Plugin] - pre tag commit: '0.3.0'. 2017-05-12 15:25:46 +05:30
Shekhar Gulati 4d94035f17 Formatted code to move private methods down 2017-05-12 15:22:41 +05:30
César Aguilera 84b3303718 Feature required in #73 (#77) 2017-05-09 18:16:11 +05:30
wli75 605eb4f2a5 fixed containsAll (#76) 2017-03-07 20:09:18 +05:30
Shekhar Gulati 61ca9fc984 Resolved #66 2016-08-24 15:32:45 +05:30
Shekhar Gulati bacbe46529 Resolved #65 2016-08-24 15:02:00 +05:30
Shekhar Gulati 9b60adf732 Resolved #68 2016-08-24 14:26:16 +05:30
Shekhar Gulati 2ae1c02b5d Resolved #69 2016-08-24 12:56:18 +05:30
Shekhar Gulati f635543679 #71 updated documentation 2016-08-24 12:16:19 +05:30
Shekhar Gulati 21f8bb9f8e Resolved#71 2016-08-24 12:11:04 +05:30
Shekhar Gulati f282dddca3 Resolved #54 and Resolved #59 2016-07-27 23:19:41 +05:30
Shekhar Gulati 3594042ab5 Resolved #53 2016-07-25 15:29:43 +05:30
Marcin Zajączkowski 6bbe0a9167 Simplify Maven and Gradle configuration (#70) 2016-07-19 20:58:57 +05:30
Shekhar Gulati ec7cb02800 cleanup and added tests 2016-07-19 18:13:13 +05:30
Shekhar Gulati acdb15d6fe Updated README.md with latest version 2016-06-26 22:49:37 +05:30
Shekhar Gulati 9c2317434e [Gradle Release Plugin] - new version commit: '0.3.0-SNAPSHOT'. 2016-06-26 22:41:33 +05:30
Shekhar Gulati 555f50b2b1 [Gradle Release Plugin] - pre tag commit: '0.2.0'. 2016-06-26 22:41:08 +05:30
Shekhar Gulati bf081e6b48 Resolved #51. Incorporated suggestions of johnsdouglass 2016-06-26 22:36:05 +05:30
Shekhar Gulati ab6c36c078 Resolved #50 2016-06-26 22:29:03 +05:30
Shekhar Gulati dfc05347bf Resolved #49 2016-06-26 22:10:03 +05:30
Shekhar Gulati e251cc07d8 fixed README.md 2016-05-25 16:50:33 +05:30
Shekhar Gulati 68ff5f4ebf Merge pull request #48 from jvitkauskas/fix-readme-b64dec
Fix base64Decode example
2016-05-23 00:14:42 +05:30
Julius Vitkauskas 6a4866fed8 Fix base64Decode example 2016-05-22 21:34:31 +03:00
Shekhar Gulati 3c7ec0d113 removed version information from the build.gradle 2016-05-22 21:22:07 +05:30
Shekhar Gulati 43b6b3ad17 [Gradle Release Plugin] - new version commit: '0.2.0-SNAPSHOT'. 2016-05-22 21:05:23 +05:30
5 changed files with 491 additions and 66 deletions
+33 -36
View File
@@ -15,8 +15,7 @@ For Apache Maven users, please add following to your pom.xml.
<dependency>
<groupId>com.shekhargulati</groupId>
<artifactId>strman</artifactId>
<version>0.1.0</version>
<type>jar</type>
<version>0.2.0</version>
</dependency>
</dependencies>
```
@@ -24,9 +23,7 @@ For Apache Maven users, please add following to your pom.xml.
Gradle users can add following to their build.gradle file.
```
compile(group: 'com.shekhargulati', name: 'strman', version: '0.1.0', ext: 'jar'){
transitive=true
}
compile(group: 'com.shekhargulati', name: 'strman', version: '0.2.0')
```
## Available Functions
@@ -38,7 +35,7 @@ These are the available functions in current version of library:
Appends Strings to value
```java
import static strman.append
import static strman.Strman.append
append("f", "o", "o", "b", "a", "r")
// result => "foobar"
```
@@ -48,7 +45,7 @@ append("f", "o", "o", "b", "a", "r")
Append an array of String to value
```java
import static strman.appendArray
import static strman.Strman.appendArray
appendArray("f", new String[]{"o", "o", "b", "a", "r"}
// result => "foobar"
```
@@ -58,7 +55,7 @@ appendArray("f", new String[]{"o", "o", "b", "a", "r"}
Get the character at index. This method will take care of negative indexes.
```java
import static strman.at
import static strman.Strman.at
at("foobar", 0)
// result => Optional("f")
```
@@ -68,7 +65,7 @@ at("foobar", 0)
Returns an array with strings between start and end.
```java
import static strman.between
import static strman.Strman.between
between("[abc][def]", "[", "]")
// result => ["abc","def"]
```
@@ -78,7 +75,7 @@ between("[abc][def]", "[", "]")
Returns a String array consisting of the characters in the String.
```java
import static strman.chars
import static strman.Strman.chars
chars("title")
// result => ["t", "i", "t", "l", "e"]
```
@@ -88,7 +85,7 @@ chars("title")
Replace consecutive whitespace characters with a single space.
```java
import static strman.collapseWhitespace
import static strman.Strman.collapseWhitespace
collapseWhitespace("foo bar")
// result => "foo bar"
```
@@ -98,7 +95,7 @@ collapseWhitespace("foo bar")
Verifies that the needle is contained in the value.
```java
import static strman.contains
import static strman.Strman.contains
contains("foo bar","foo")
// result => true
@@ -111,7 +108,7 @@ contains("foo bar","FOO", false) // turning off case sensitivity
Verifies that all needles are contained in value
```java
import static strman.containsAll
import static strman.Strman.containsAll
containsAll("foo bar", new String[]{"foo", "bar"})
// result => true
@@ -124,7 +121,7 @@ containsAll("foo bar", new String[]{"FOO", "bar"},false)
Verifies that one or more of needles are contained in value.
```java
import static strman.containsAny
import static strman.Strman.containsAny
containsAny("bar foo", new String[]{"FOO", "BAR", "Test"}, true)
// result => true
```
@@ -134,7 +131,7 @@ containsAny("bar foo", new String[]{"FOO", "BAR", "Test"}, true)
Count the number of times substr appears in value
```java
import static strman.countSubstr
import static strman.Strman.countSubstr
countSubstr("aaaAAAaaa", "aaa")
// result => 2
countSubstr("aaaAAAaaa", "aaa", false, false)
@@ -146,7 +143,7 @@ countSubstr("aaaAAAaaa", "aaa", false, false)
Test if value ends with search.
```java
import static strman.endsWith
import static strman.Strman.endsWith
endsWith("foo bar", "bar")
// result => true
endsWith("foo Bar", "BAR", false)
@@ -158,7 +155,7 @@ endsWith("foo Bar", "BAR", false)
Ensures that the value begins with prefix. If it doesn't exist, it's prepended.
```java
import static strman.ensureLeft
import static strman.Strman.ensureLeft
ensureLeft("foobar", "foo")
// result => "foobar"
ensureLeft("bar", "foo")
@@ -172,9 +169,9 @@ ensureLeft("foobar", "FOO", false)
Decodes data encoded with MIME base64
```java
import static strman.base64Decode
import static strman.Strman.base64Decode
base64Decode("c3RybWFu")
// result => "strma"
// result => "strman"
```
## base64Encode
@@ -182,7 +179,7 @@ base64Decode("c3RybWFu")
Encodes data with MIME base64.
```java
import static strman.base64Encode
import static strman.Strman.base64Encode
base64Encode("strman")
// result => "c3RybWFu"
```
@@ -192,7 +189,7 @@ base64Encode("strman")
Convert binary unicode (16 digits) string to string chars
```java
import static strman.binDecode
import static strman.Strman.binDecode
binDecode("0000000001000001")
// result => "A"
```
@@ -202,7 +199,7 @@ binDecode("0000000001000001")
Convert string chars to binary unicode (16 digits)
```java
import static strman.binEncode
import static strman.Strman.binEncode
binEncode("A")
// result => "0000000001000001"
```
@@ -212,7 +209,7 @@ binEncode("A")
Convert decimal unicode (5 digits) string to string chars
```java
import static strman.decDecode
import static strman.Strman.decDecode
decDecode("00065")
// result => "A"
```
@@ -222,7 +219,7 @@ decDecode("00065")
Convert string chars to decimal unicode (5 digits)
```java
import static strman.decEncode
import static strman.Strman.decEncode
decEncode("A")
// result => "00065"
```
@@ -232,7 +229,7 @@ decEncode("A")
Ensures that the value ends with suffix. If it doesn't, it's appended.
```java
import static strman.ensureRight
import static strman.Strman.ensureRight
ensureRight("foo", "bar")
// result => "foobar"
@@ -248,7 +245,7 @@ ensureRight("fooBAR", "bar", false)
Returns the first n chars of String
```java
import static strman.first
import static strman.Strman.first
first("foobar", 3)
// result => "foo"
```
@@ -258,7 +255,7 @@ first("foobar", 3)
Return the first char of String
```java
import static strman.head
import static strman.Strman.head
head("foobar")
// result => "f"
```
@@ -268,7 +265,7 @@ head("foobar")
Convert hexadecimal unicode (4 digits) string to string chars
```java
import static strman.hexDecode
import static strman.Strman.hexDecode
hexDecode("0041")
// result => "A"
```
@@ -278,7 +275,7 @@ hexDecode("0041")
Convert string chars to hexadecimal unicode (4 digits)
```java
import static strman.hexEncode
import static strman.Strman.hexEncode
hexEncode("A")
// result => "0041"
```
@@ -288,7 +285,7 @@ hexEncode("A")
Tests if two Strings are inequal
```java
import static strman.inequal
import static strman.Strman.inequal
inequal("a", "b")
// result => true
```
@@ -298,7 +295,7 @@ inequal("a", "b")
Inserts 'substr' into the 'value' at the 'index' provided.
```java
import static strman.insert
import static strman.Strman.insert
insert("fbar", "oo", 1)
// result => "foobar"
```
@@ -308,7 +305,7 @@ insert("fbar", "oo", 1)
Return the last n chars of String
```java
import static strman.last
import static strman.Strman.last
last("foobarfoo", 3)
// result => "foo"
```
@@ -318,7 +315,7 @@ last("foobarfoo", 3)
Returns a new string of a given length such that the beginning of the string is padded.
```java
import static strman.leftPad
import static strman.Strman.leftPad
leftPad("1", "0", 5)
// result => "00001"
```
@@ -328,7 +325,7 @@ leftPad("1", "0", 5)
This method returns the index within the calling String object of the last occurrence of the specified value, searching backwards from the offset.
```java
import static strman.lastIndexOf
import static strman.Strman.lastIndexOf
lastIndexOf("foobarfoobar", "F", false)
// result => 6
```
@@ -338,7 +335,7 @@ lastIndexOf("foobarfoobar", "F", false)
Removes all spaces on left
```java
import static strman.leftTrim
import static strman.Strman.leftTrim
leftTrim(" strman")
// result => "strman"
```
@@ -564,4 +561,4 @@ toSnakeCase("hello world")
License
-------
strman is licensed under the MIT License - see the `LICENSE` file for details.
strman is licensed under the MIT License - see the `LICENSE` file for details.
-1
View File
@@ -3,7 +3,6 @@ plugins {
}
group 'com.shekhargulati'
version '0.1.0-SNAPSHOT'
apply plugin: 'java'
apply plugin: 'maven'
+1 -1
View File
@@ -1 +1 @@
version=0.1.0
version=0.3.0
+242 -20
View File
@@ -28,13 +28,15 @@
package strman;
import java.util.*;
import java.util.function.BiFunction;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Stream;
import static java.util.function.Function.identity;
import static java.util.stream.Collectors.counting;
import static java.util.stream.Collectors.groupingBy;
import static java.util.stream.Collectors.joining;
/**
@@ -88,7 +90,7 @@ public abstract class Strman {
* @return an Optional String if found else empty
*/
public static Optional<String> at(final String value, int index) {
if (value == null || value.isEmpty()) {
if (isNullOrEmpty(value)) {
return Optional.empty();
}
int length = value.length();
@@ -98,6 +100,10 @@ public abstract class Strman {
return (index < length && index >= 0) ? Optional.of(String.valueOf(value.charAt(index))) : Optional.empty();
}
private static boolean isNullOrEmpty(String input) {
return input == null || input.isEmpty();
}
/**
* Returns an array with strings between start and end.
*
@@ -176,7 +182,7 @@ public abstract class Strman {
*/
public static boolean containsAll(final String value, final String[] needles) {
validate(value, NULL_STRING_PREDICATE, NULL_STRING_MSG_SUPPLIER);
return Arrays.stream(needles).allMatch(needle -> contains(value, needle, true));
return Arrays.stream(needles).allMatch(needle -> contains(value, needle, false));
}
/**
@@ -406,9 +412,8 @@ public abstract class Strman {
* @param n Number of chars to return
* @return The first n chars
*/
public static String first(final String value, final int n) {
validate(value, NULL_STRING_PREDICATE, NULL_STRING_MSG_SUPPLIER);
return value.substring(0, n);
public static Optional<String> first(final String value, final int n) {
return Optional.ofNullable(value).filter(v -> !v.isEmpty()).map(v -> v.substring(0, n));
}
/**
@@ -417,7 +422,7 @@ public abstract class Strman {
* @param value The input String
* @return The first char
*/
public static String head(final String value) {
public static Optional<String> head(final String value) {
return first(value, 1);
}
@@ -488,6 +493,18 @@ public abstract class Strman {
* @param second The second String
* @return true if first and second are not equal false otherwise
*/
public static boolean unequal(final String first, final String second) {
return !Objects.equals(first, second);
}
/**
* Tests if two Strings are inequal
*
* @param first The first String
* @param second The second String
* @return true if first and second are not equal false otherwise
* @deprecated use unequal instead
*/
public static boolean inequal(final String first, final String second) {
return !Objects.equals(first, second);
}
@@ -517,7 +534,12 @@ public abstract class Strman {
*/
public static boolean isUpperCase(final String value) {
validate(value, NULL_STRING_PREDICATE, NULL_STRING_MSG_SUPPLIER);
return Objects.equals(value, value.toUpperCase());
for (int i = 0; i < value.length(); i++) {
if (Character.isLowerCase(value.charAt(i))) {
return false;
}
}
return true;
}
/**
@@ -528,7 +550,12 @@ public abstract class Strman {
*/
public static boolean isLowerCase(final String value) {
validate(value, NULL_STRING_PREDICATE, NULL_STRING_MSG_SUPPLIER);
return Objects.equals(value, value.toLowerCase());
for (int i = 0; i < value.length(); i++) {
if (Character.isUpperCase(value.charAt(i))) {
return false;
}
}
return true;
}
/**
@@ -709,11 +736,10 @@ public abstract class Strman {
public static String removeLeft(final String value, final String prefix, final boolean caseSensitive) {
validate(value, NULL_STRING_PREDICATE, NULL_STRING_MSG_SUPPLIER);
validate(prefix, NULL_STRING_PREDICATE, NULL_STRING_MSG_SUPPLIER);
BiFunction<String, String, String> fx = (f, s) -> f.startsWith(s) ? f.replaceFirst(s, "") : f;
if (caseSensitive) {
return fx.apply(value, prefix);
return value.startsWith(prefix) ? value.substring(prefix.length()) : value;
}
return fx.apply(value.toLowerCase(), prefix.toLowerCase());
return value.toLowerCase().startsWith(prefix.toLowerCase()) ? value.substring(prefix.length()) : value;
}
/**
@@ -790,7 +816,7 @@ public abstract class Strman {
if (caseSensitive) {
return value.replace(search, newValue);
}
return value.toLowerCase().replace(search.toLowerCase(), newValue);
return Pattern.compile(search, Pattern.CASE_INSENSITIVE).matcher(value).replaceAll(Matcher.quoteReplacement(newValue));
}
/**
@@ -924,7 +950,11 @@ public abstract class Strman {
*/
public static String htmlEncode(final String html) {
validate(html, NULL_STRING_PREDICATE, NULL_STRING_MSG_SUPPLIER);
return html.chars().mapToObj(c -> "\\u" + String.format("%04x", c).toUpperCase()).map(e -> HtmlEntities.encodedEntities.get(e)).collect(joining());
return html
.chars()
.mapToObj(c -> "\\u" + String.format("%04x", c).toUpperCase())
.map(HtmlEntities.encodedEntities::get)
.collect(joining());
}
/**
@@ -1025,7 +1055,10 @@ public abstract class Strman {
public static String toStudlyCase(final String value) {
validate(value, NULL_STRING_PREDICATE, NULL_STRING_MSG_SUPPLIER);
String[] words = collapseWhitespace(value.trim()).split("\\s*(_|-|\\s)\\s*");
return Arrays.stream(words).filter(w -> !w.trim().isEmpty()).map(w -> head(w).toUpperCase() + tail(w)).collect(joining());
return Arrays.stream(words)
.filter(w -> !w.trim().isEmpty())
.map(Strman::upperFirst)
.collect(joining());
}
/**
@@ -1034,22 +1067,21 @@ public abstract class Strman {
* @param value The input String
* @return String tail
*/
public static String tail(final String value) {
validate(value, NULL_STRING_PREDICATE, NULL_STRING_MSG_SUPPLIER);
return last(value, value.length() - 1);
public static Optional<String> tail(final String value) {
return Optional.ofNullable(value).filter(v -> !v.isEmpty()).map(v -> last(v, v.length() - 1));
}
/**
* Decamelize String
*
* @param value The input String
* @param chr string to use
* @param chr string to use
* @return String decamelized.
*/
public static String toDecamelize(final String value, final String chr) {
String camelCasedString = toCamelCase(value);
String[] words = camelCasedString.split("(?=\\p{Upper})");
return Arrays.stream(words).map(w -> w.toLowerCase()).collect(joining(Optional.ofNullable(chr).orElse(" ")));
return Arrays.stream(words).map(String::toLowerCase).collect(joining(Optional.ofNullable(chr).orElse(" ")));
}
/**
@@ -1085,6 +1117,195 @@ public abstract class Strman {
return value.chars().mapToObj(ch -> leftPad(Integer.toString(ch, radix), "0", digits)).collect(joining());
}
/**
* Join concatenates all the elements of the strings array into a single String. The separator string is placed between elements in the resulting string.
*
* @param strings The input array to concatenate
* @param separator The separator to use
* @return Concatenated String
*/
public static String join(final String[] strings, final String separator) throws IllegalArgumentException {
if (strings == null) {
throw new IllegalArgumentException("Input array 'strings' can't be null");
}
if (separator == null) {
throw new IllegalArgumentException("separator can't be null");
}
StringJoiner joiner = new StringJoiner(separator);
for (String el : strings) {
joiner.add(el);
}
return joiner.toString();
}
/**
* Converts the first character of string to upper case and the remaining to lower case.
*
* @param input The string to capitalize
* @return The capitalized string
*/
public static String capitalize(final String input) throws IllegalArgumentException {
if (input == null) {
throw new IllegalArgumentException("input can't be null");
}
if (input.length() == 0) {
return "";
}
return head(input)
.map(String::toUpperCase)
.map(h ->
tail(input).map(t -> h + t.toLowerCase()).orElse(h))
.get();
}
/**
* Converts the first character of string to lower case.
*
* @param input The string to convert
* @return The converted string
* @throws IllegalArgumentException
*/
public static String lowerFirst(final String input) throws IllegalArgumentException {
if (input == null) {
throw new IllegalArgumentException("input can't be null");
}
if (input.length() == 0) {
return "";
}
return head(input)
.map(String::toLowerCase)
.map(h ->
tail(input).map(t -> h + t).orElse(h))
.get();
}
/**
* Verifies whether String is enclosed by encloser
*
* @param input The input String
* @param encloser String which encloses input String
* @return true if enclosed false otherwise
*/
public static boolean isEnclosedBetween(final String input, final String encloser) {
return isEnclosedBetween(input, encloser, encloser);
}
/**
* Verifies whether String is enclosed by encloser
*
* @param input The input String
* @param leftEncloser String which encloses input String at left start
* @param rightEncloser String which encloses input String at the right end
* @return true if enclosed false otherwise
*/
public static boolean isEnclosedBetween(final String input, final String leftEncloser, String rightEncloser) {
if (input == null) {
throw new IllegalArgumentException("input can't be null");
}
if (leftEncloser == null) {
throw new IllegalArgumentException("leftEncloser can't be null");
}
if (rightEncloser == null) {
throw new IllegalArgumentException("rightEncloser can't be null");
}
return input.startsWith(leftEncloser) && input.endsWith(rightEncloser);
}
/**
* Converts the first character of string to upper case.
*
* @param input The string to convert.
* @return Returns the converted string.
*/
public static String upperFirst(String input) {
if (input == null) {
throw new IllegalArgumentException("input can't be null");
}
return head(input)
.map(String::toUpperCase)
.map(h -> tail(input).map(t -> h + t).orElse(h))
.get();
}
/**
* Removes leading whitespace from string.
*
* @param input The string to trim.
* @return Returns the trimmed string.
*/
public static Optional<String> trimStart(final String input) {
return Optional.ofNullable(input)
.filter(v -> !v.isEmpty())
.map(Strman::leftTrim);
}
/**
* Removes leading characters from string.
*
* @param input The string to trim.
* @param chars The characters to trim.
*
* @return Returns the trimmed string.
*/
public static Optional<String> trimStart(final String input, String... chars) {
return Optional.ofNullable(input)
.filter(v -> !v.isEmpty())
.map(v -> {
String pattern = String.format("^[%s]+", join(chars, "\\"));
return v.replaceAll(pattern, "");
});
}
/**
* Removes trailing whitespace from string.
*
* @param input The string to trim.
* @return Returns the trimmed string.
*/
public static Optional<String> trimEnd(final String input) {
return Optional.ofNullable(input)
.filter(v -> !v.isEmpty())
.map(Strman::rightTrim);
}
/**
* Removes trailing characters from string.
*
* @param input The string to trim.
* @param chars The characters to trim.
*
* @return Returns the trimmed string.
*/
public static Optional<String> trimEnd(final String input, String... chars) {
return Optional.ofNullable(input)
.filter(v -> !v.isEmpty())
.map(v -> {
String pattern = String.format("[%s]+$", join(chars, "\\"));
return v.replaceAll(pattern, "");
});
}
/**
* Counts the number of occurrences of each character in the string
*
* @param input The input string
* @return A map containing the number of occurrences of each character in the string
*/
public static Map<Character, Long> charsCount(String input) {
if (isNullOrEmpty(input)) {
return Collections.emptyMap();
}
return input.chars()
.mapToObj(c -> (char) c)
.collect(groupingBy(identity(), counting()));
}
private static void validate(String value, Predicate<String> predicate, final Supplier<String> supplier) {
if (predicate.test(value)) {
throw new IllegalArgumentException(supplier.get());
@@ -1105,3 +1326,4 @@ public abstract class Strman {
return countSubstr(value.substring(offset), subStr, allowOverlapping, ++count);
}
}
+215 -8
View File
@@ -25,10 +25,11 @@
*/
package strman;
import org.junit.Ignore;
import org.junit.Test;
import java.util.Arrays;
import java.util.Optional;
import java.util.*;
import static java.util.stream.Collectors.toList;
import static org.hamcrest.CoreMatchers.*;
@@ -343,7 +344,7 @@ public class StrmanTest {
final String[] fixture = {
"foo", "foobar"
};
Arrays.stream(fixture).forEach(el -> assertThat(first(el, 3), equalTo("foo")));
Arrays.stream(fixture).forEach(el -> assertThat(first(el, 3), equalTo(Optional.of("foo"))));
}
@Test
@@ -352,7 +353,7 @@ public class StrmanTest {
"foo", "foobar"
};
Arrays.stream(fixture).forEach(el -> assertThat(head(el), equalTo("f")));
Arrays.stream(fixture).forEach(el -> assertThat(head(el), equalTo(Optional.of("f"))));
}
@Test
@@ -409,9 +410,9 @@ public class StrmanTest {
@Test
public void inequal_shouldTestInequalityOfStrings() throws Exception {
assertThat(inequal("a", "b"), equalTo(true));
assertThat(inequal("a", "a"), equalTo(false));
assertThat(inequal("0", "1"), equalTo(true));
assertThat(unequal("a", "b"), equalTo(true));
assertThat(unequal("a", "a"), equalTo(false));
assertThat(unequal("0", "1"), equalTo(true));
}
@Test
@@ -799,7 +800,8 @@ public class StrmanTest {
" de_camelize"
};
Arrays.stream(fixture).forEach(el -> assertThat(String.format("toKebabCase(%s) should be de-camelize", el), toKebabCase(el), equalTo("de-camelize")));
Arrays.stream(fixture).forEach(el ->
assertThat(String.format("toKebabCase(%s) should be de-camelize", el), toKebabCase(el), equalTo("de-camelize")));
}
@Test
@@ -819,5 +821,210 @@ public class StrmanTest {
Arrays.stream(fixture).forEach(el -> assertThat(String.format("toSnakeCase(%s) should be de_camelize", el), toSnakeCase(el), equalTo("de_camelize")));
}
@Test
public void unequal_shouldTestInequalityOfStrings() throws Exception {
assertThat(unequal("a", "b"), equalTo(true));
assertThat(unequal("a", "a"), equalTo(false));
assertThat(unequal("0", "1"), equalTo(true));
}
@Test
public void removeLeft_shouldNotLowercaseWhenCaseInsensitive() throws Exception {
String result = removeLeft("This HAS A THIS IN FRONT", "THIS ", false);
assertThat(result, is("HAS A THIS IN FRONT"));
}
@Test
public void replace_shouldNotLowercaseWhenCaseInsensitive() throws Exception {
String result = replace("One and two and THREE and Four", "and", "&", false);
assertThat(result, is("One & two & THREE & Four"));
}
@Test
public void removeRight_shouldNotLowercaseWhenCaseInsensitive() throws Exception {
String result = removeRight("Remove the END at the end", " END", false);
assertThat(result, is("Remove the END at the"));
}
@Test
public void transliterate_shouldDeburrTheString() throws Exception {
String result = transliterate("déjà vu");
assertThat(result, is(equalTo("deja vu")));
}
@Ignore
public void htmlEncode_shouldConvertCharactersToTheirHtmlEntities() throws Exception {
String result = htmlEncode("fred, barney, & pebbles");
assertThat(result, is(equalTo("fred, barney, &amp; pebbles")));
}
@Ignore
public void kebabCase_shouldConvertAStringToKebabCase() throws Exception {
String[] input = {
"Foo Bar",
"fooBar",
"__FOO_BAR__"
};
Arrays.stream(input).forEach(el ->
assertThat(String.format("%s should be foo-bar", el), toKebabCase(el), is(equalTo("foo-bar"))));
}
@Ignore
public void snakeCase_shouldConvertAStringToSnakecase() throws Exception {
String[] input = {
"Foo Bar",
"fooBar",
"--FOO-BAR--"
};
Arrays.stream(input).forEach(el ->
assertThat(String.format("%s should be foo_bar", el), toSnakeCase(el), is(equalTo("foo_bar"))));
}
@Test
public void join_shouldJoinArrayOfStringIntoASingleString() throws Exception {
String[] strings = {
"hello",
"world",
"123"
};
assertThat(join(strings, ";"), is(equalTo("hello;world;123")));
}
@Test(expected = IllegalArgumentException.class)
public void join_shouldThrowIllegalArgumentExceptionWhenSeparatorIsNull() throws Exception {
String[] strings = {
"hello",
"world",
"123"
};
join(strings, null);
}
@Test
public void join_shouldReturnEmptyStringWhenInputArrayIsEmpty() throws Exception {
String[] emptyArray = {};
assertThat(join(emptyArray, ","), is(equalTo("")));
}
@Test
public void capitalize_shouldCapitalizeFirstCharacterOfString() throws Exception {
String[] strings = {
"FRED",
"fRED",
"fred"
};
Arrays.stream(strings).forEach(el -> assertThat(String.format("%s should be Fred", el), capitalize(el), equalTo("Fred")));
}
@Test
public void lowerFirst_shouldLowercasedFirstCharacterOfString() throws Exception {
assertThat(lowerFirst("FRED"), is(equalTo("fRED")));
assertThat(lowerFirst("fred"), is(equalTo("fred")));
assertThat(lowerFirst("Fred"), is(equalTo("fred")));
}
@Test
public void isEnclosedBetween_shouldChekcWhetherStringIsEnclosed() throws Exception {
assertThat(isEnclosedBetween("{{shekhar}}", "{{", "}}"), is(true));
assertThat(isEnclosedBetween("shekhar", "{{", "}}"), is(false));
assertThat(isEnclosedBetween("*shekhar*", "*"), is(true));
assertThat(isEnclosedBetween("shekhar", "*"), is(false));
}
@Test(expected = IllegalArgumentException.class)
public void isEnclosedBetween_shouldThrowIllegalArgumentExceptionWhenEncloserIsNull() throws Exception {
assertThat(isEnclosedBetween("shekhar", null), is(false));
}
@Test
public void words_shouldConvertTextToWords() throws Exception {
final String line = "This is a string, with words!";
assertThat(words(line), is(new String[]{"This", "is", "a", "string", "with", "words"}));
}
@Test
public void upperFirst_shouldConvertFirstCharToUpperCase() throws Exception {
assertThat(upperFirst("fred"), is("Fred"));
}
@Test
public void upperFirst_shouldReturnSameStringIfFirstCharIsUpperCase() throws Exception {
assertThat(upperFirst("FRED"), is("FRED"));
}
@Test
public void trimStart_shouldRemoveAllWhitespaceAtStart() throws Exception {
assertThat(trimStart(" abc "), is(Optional.of("abc ")));
assertThat(trimStart("abc "), is(Optional.of("abc ")));
assertThat(trimStart("abc"), is(Optional.of("abc")));
assertThat(trimStart(""), is(Optional.empty()));
assertThat(trimStart(null), is(Optional.empty()));
}
@Test
public void trimStart_shouldRemoveSpecialCharactersAtStart() throws Exception {
assertThat(trimStart("-_-abc-_-", "_", "-"), is(Optional.of("abc-_-")));
assertThat(trimStart("-_-!abc-_-", "_", "-", "!"), is(Optional.of("abc-_-")));
assertThat(trimStart("-_-#abc-_-", "_", "-", "!", "#"), is(Optional.of("abc-_-")));
}
@Test
public void trimEnd_shouldRemoveAllTrailingWhitespace() throws Exception {
assertThat(trimEnd(" abc "), is(Optional.of(" abc")));
assertThat(trimEnd("abc "), is(Optional.of("abc")));
assertThat(trimEnd("abc"), is(Optional.of("abc")));
assertThat(trimEnd(""), is(Optional.empty()));
assertThat(trimEnd(null), is(Optional.empty()));
}
@Test
public void trimEnd_shouldRemoveAllTrailingSpecialCharacters() throws Exception {
assertThat(trimEnd("-_-abc-_-", "_", "-"), is(Optional.of("-_-abc")));
assertThat(trimEnd("-_-abc!-_-", "_", "-", "!"), is(Optional.of("-_-abc")));
assertThat(trimEnd("-_-abc#-_-", "_", "-", "!", "#"), is(Optional.of("-_-abc")));
}
@Test
public void charsCount_shouldReturnEmptyWhenInputStringIsNull(){
assertThat(charsCount(null), equalTo(Collections.emptyMap()));
}
@Test
public void charsCount_shouldReturnEmptyWhenInputStringIsEmpty(){
assertThat(charsCount(""), equalTo(Collections.emptyMap()));
}
@Test
public void charsCount_shouldReturnCharsCountWhenInputIsASimpleString(){
Map<Character, Long> expectedOutput = new HashMap<Character, Long>(){{
put('a',1L);
put('b', 1L);
put('c', 1L);
}};
assertThat(charsCount("abc"), equalTo(expectedOutput));
}
@Test
public void charsCount_shouldReturnCharsCountWhenInputIsAComplexString(){
Map<Character, Long> expectedOutput = new HashMap<Character, Long>(){{
put('a',1L);
put('b', 2L);
put('c', 3L);
put('A',1L);
put('B', 2L);
put('C', 3L);
put('-', 10L);
}};
assertThat(charsCount("-----abbcccCCCBBA-----"), equalTo(expectedOutput));
}
}