Skip to content

Commit da754b0

Browse files
Skezzowskijavadev
authored andcommitted
Add U.explode(string) and U.implode(strings) methods.
1 parent 07736ea commit da754b0

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

src/main/java/com/github/underscore/lodash/U.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1691,6 +1691,37 @@ public static FetchResponse fetch(final String url, final String method, final S
16911691
}
16921692
}
16931693

1694+
public static List<String> explode(final String input) {
1695+
List<String> result = newArrayList();
1696+
if (isNull(input)) {
1697+
return result;
1698+
}
1699+
for (char character : input.toCharArray()) {
1700+
result.add(String.valueOf(character));
1701+
}
1702+
return result;
1703+
}
1704+
1705+
public static String implode(final String[] input) {
1706+
StringBuilder builder = new StringBuilder();
1707+
for (String character : input) {
1708+
if (nonNull(character)) {
1709+
builder.append(character);
1710+
}
1711+
}
1712+
return builder.toString();
1713+
}
1714+
1715+
public static String implode(final Iterable<String> input) {
1716+
StringBuilder builder = new StringBuilder();
1717+
for (String character: input) {
1718+
if (nonNull(character)) {
1719+
builder.append(character);
1720+
}
1721+
}
1722+
return builder.toString();
1723+
}
1724+
16941725
public String camelCase() {
16951726
return camelCase(getString().get());
16961727
}

src/test/java/com/github/underscore/lodash/StringTest.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
*/
2424
package com.github.underscore.lodash;
2525

26+
import static java.util.Arrays.asList;
27+
2628
import com.github.underscore.BiFunction;
2729
import com.github.underscore.Consumer;
2830
import com.github.underscore.Function;
@@ -33,6 +35,7 @@
3335

3436
import java.util.*;
3537
import org.junit.Test;
38+
3639
import static org.junit.Assert.assertEquals;
3740
import static org.junit.Assert.assertFalse;
3841
import static org.junit.Assert.assertNull;
@@ -68,6 +71,28 @@ public void camelCase() {
6871
assertEquals("a", U.camelCase("\u00c0"));
6972
}
7073

74+
/*
75+
_.explode("abc")
76+
=> ["a", "b", "c"]
77+
*/
78+
@Test
79+
public void explode() {
80+
assertEquals(asList("a", "b", "c"), U.explode("abc"));
81+
assertEquals(U.newArrayList(), U.explode(null));
82+
}
83+
84+
/*
85+
_.implode(["a", "b", "c"]);
86+
=> "abc"
87+
*/
88+
@Test
89+
public void implode() {
90+
assertEquals("abc", U.implode(new String[] {"a", "b", "c"}));
91+
assertEquals("ac", U.implode(new String[] {"a", null, "c"}));
92+
assertEquals("abc", U.implode(asList("a", "b", "c")));
93+
assertEquals("ac", U.implode(asList("a", null, "c")));
94+
}
95+
7196
/*
7297
_.lowerFirst('Fred');
7398
// => 'fred'

0 commit comments

Comments
 (0)