Skip to content

Commit b975ddf

Browse files
authored
Add format() method support.
1 parent fbd9340 commit b975ddf

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

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

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public class $<T> {
4747
private static final java.util.concurrent.atomic.AtomicInteger UNIQUE_ID =
4848
new java.util.concurrent.atomic.AtomicInteger(0);
4949
private static final String ALL_SYMBOLS = "([\\s\\S]+?)";
50+
private static final java.util.regex.Pattern FORMAT_PATTERN = java.util.regex.Pattern.compile("\\{\\s*(\\d*)\\s*\\}");
5051
private final Iterable<T> iterable;
5152
private final Optional<String> string;
5253

@@ -1875,6 +1876,28 @@ public static <K, V> Template<Map<K, V>> template(final String template) {
18751876
return new TemplateImpl<K, V>(template);
18761877
}
18771878

1879+
public static String format(final String template, final Object ... params) {
1880+
final java.util.regex.Matcher matcher = FORMAT_PATTERN.matcher(template);
1881+
final StringBuffer buffer = new StringBuffer();
1882+
int index = 0;
1883+
while (matcher.find()) {
1884+
if (matcher.group(1).isEmpty()) {
1885+
matcher.appendReplacement(buffer, "<%" + index++ + "%>");
1886+
} else {
1887+
matcher.appendReplacement(buffer, "<%" + matcher.group(1) + "%>");
1888+
}
1889+
}
1890+
matcher.appendTail(buffer);
1891+
final String newTemplate = buffer.toString();
1892+
final Map<Integer, String> args = newLinkedHashMap();
1893+
index = 0;
1894+
for (Object param : params) {
1895+
args.put(index, param.toString());
1896+
index += 1;
1897+
}
1898+
return new TemplateImpl<Integer, String>(newTemplate).apply(args);
1899+
}
1900+
18781901
public static <T> Iterable<T> iterate(final T seed, final UnaryOperator<T> unaryOperator) {
18791902
return new MyIterable<T>(seed, unaryOperator);
18801903
}
@@ -2624,7 +2647,6 @@ protected static <T> Set<T> newLinkedHashSetWithExpectedSize(int size) {
26242647
return new LinkedHashSet<T>((int) Math.max(size * CAPACITY_COEFF_2, CAPACITY_SIZE_16));
26252648
}
26262649

2627-
@SuppressWarnings("unchecked")
26282650
protected static <K, E> Map<K, E> newLinkedHashMap() {
26292651
return new LinkedHashMap<K, E>();
26302652
}

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,22 @@ public void templateValue3() {
258258
put("name", "moe"); put("value", "<script>"); } }));
259259
}
260260

261+
/*
262+
var fortmatted = _.format("hello: {}", "moe");
263+
=> "hello: moe"
264+
*/
265+
@Test
266+
public void format() {
267+
String fortmatted = $.format("hello: {0}", "moe");
268+
assertEquals("hello: moe", fortmatted);
269+
String fortmatted2 = $.format("hello: {}", "moe");
270+
assertEquals("hello: moe", fortmatted2);
271+
String fortmatted3 = $.format("hello: {}, {}", "moe", 123);
272+
assertEquals("hello: moe, 123", fortmatted3);
273+
String fortmatted4 = $.format("hello: {1}, {0}", "moe", 123);
274+
assertEquals("hello: 123, moe", fortmatted4);
275+
}
276+
261277
/*
262278
var object = {cheese: 'crumpets', stuff: function(){ return 'nonsense'; }};
263279
_.result(object, 'cheese');

0 commit comments

Comments
 (0)