Skip to content

Commit 37435dc

Browse files
authored
Improve $.join(iterable, separator) method.
1 parent c432348 commit 37435dc

File tree

2 files changed

+9
-8
lines changed

2 files changed

+9
-8
lines changed

src/main/java/com/github/underscore/$.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2150,14 +2150,14 @@ public List<Comparable> sort() {
21502150

21512151
public static <T> String join(final Iterable<T> iterable, final String separator) {
21522152
final StringBuilder sb = new StringBuilder();
2153-
each(iterable, new Block<T>() {
2154-
public void apply(T item) {
2155-
if (!sb.toString().isEmpty()) {
2156-
sb.append(separator);
2157-
}
2158-
sb.append(item.toString());
2153+
int index = 0;
2154+
for (final T item : iterable) {
2155+
if (index > 0) {
2156+
sb.append(separator);
21592157
}
2160-
});
2158+
sb.append(item.toString());
2159+
index += 1;
2160+
}
21612161
return sb.toString();
21622162
}
21632163

src/test/java/com/github/underscore/UnderscoreTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* The MIT License (MIT)
33
*
4-
* Copyright 2015 Valentyn Kolesnikov
4+
* Copyright 2015, 2016 Valentyn Kolesnikov
55
*
66
* Permission is hereby granted, free of charge, to any person obtaining a copy
77
* of this software and associated documentation files (the "Software"), to deal
@@ -83,6 +83,7 @@ public void join() {
8383
assertEquals("some words example", new $(asList("some", "words", "example")).join());
8484
assertEquals("some words example", $.join(new String[] {"some", "words", "example"}));
8585
assertEquals("some words example", $.chain(asList("some", "words", "example")).join().item());
86+
assertEquals("--", $.join(asList("", "", ""), "-"));
8687
}
8788

8889
@Test

0 commit comments

Comments
 (0)