Skip to content

Commit 621ac1c

Browse files
authored
Remove each and print support from template.
1 parent 5ad8d7c commit 621ac1c

File tree

2 files changed

+16
-98
lines changed

2 files changed

+16
-98
lines changed

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

Lines changed: 15 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* The MIT License (MIT)
33
*
4-
* Copyright 2016 Valentyn Kolesnikov
4+
* Copyright 2015-2017 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
@@ -123,27 +123,6 @@ public String apply(Map<K, V> value) {
123123
"\\s*\\Q" + ((Map.Entry) element).getKey()
124124
+ "\\E\\s*")).matcher(result).replaceAll(escape(String.valueOf(((Map.Entry) element)
125125
.getValue())));
126-
java.util.regex.Matcher matcher = java.util.regex.Pattern.compile(
127-
evaluate.replace(ALL_SYMBOLS,
128-
"\\s*_\\.each\\((\\w+),\\s*function\\((\\w+)\\)\\s*\\{\\s*") + "(.*?)"
129-
+ evaluate.replace(ALL_SYMBOLS, "\\s*\\}\\);\\s*"))
130-
.matcher(result);
131-
if (matcher.find() && ((Map.Entry) element).getKey().equals(matcher.group(1))) {
132-
StringBuilder repeatResult = new StringBuilder();
133-
for (String item : (List<String>) ((Map.Entry) element).getValue()) {
134-
repeatResult.append(java.util.regex.Pattern.compile(
135-
interpolate.replace(ALL_SYMBOLS, "\\s*\\Q" + matcher.group(GROUP_INDEX_2)
136-
+ "\\E\\s*")).matcher(matcher.group(GROUP_INDEX_3)).replaceAll(item));
137-
}
138-
result = matcher.replaceFirst(repeatResult.toString());
139-
}
140-
java.util.regex.Matcher matcherPrint = java.util.regex.Pattern.compile(
141-
evaluate.replace(ALL_SYMBOLS,
142-
"\\s*print\\('([^']*)'\\s*\\+\\s*(\\w+)\\);\\s*")).matcher(result);
143-
if (matcherPrint.find() && ((Map.Entry) element).getKey().equals(matcherPrint.group(GROUP_INDEX_2))) {
144-
result = matcherPrint.replaceFirst(matcherPrint.group(1)
145-
+ ((Map.Entry) element).getValue());
146-
}
147126
}
148127
return result;
149128
}
@@ -409,15 +388,13 @@ public Class<?> apply(Object input) {
409388
try {
410389
final Method method = iterable.iterator().next().getClass().getMethod(methodName, argTypes.toArray(
411390
new Class[argTypes.size()]));
412-
each(iterable, new Block<E>() {
413-
public void apply(E arg) {
414-
try {
415-
result.add((E) method.invoke(arg, args.toArray(new Object[args.size()])));
416-
} catch (Exception e) {
417-
throw new IllegalArgumentException(e);
418-
}
391+
for (E arg : iterable) {
392+
try {
393+
result.add((E) method.invoke(arg, args.toArray(new Object[args.size()])));
394+
} catch (Exception e) {
395+
throw new IllegalArgumentException(e);
419396
}
420-
});
397+
}
421398
} catch (NoSuchMethodException e) {
422399
throw new IllegalArgumentException(e);
423400
}
@@ -1181,19 +1158,15 @@ public static <T> List<List<T>> zip(final List<T> ... lists) {
11811158
each(Arrays.asList(lists), new Block<List<T>>() {
11821159
@Override
11831160
public void apply(final List<T> list) {
1184-
$.each(list, new Block<T>() {
1185-
private int index;
1186-
1187-
@Override
1188-
public void apply(T elem) {
1189-
final List<T> nTuple = index >= zipped.size() ? $.<T>newArrayList() : zipped.get(index);
1190-
if (index >= zipped.size()) {
1191-
zipped.add(nTuple);
1192-
}
1193-
index += 1;
1194-
nTuple.add(elem);
1161+
int index = 0;
1162+
for (T elem : list) {
1163+
final List<T> nTuple = index >= zipped.size() ? $.<T>newArrayList() : zipped.get(index);
1164+
if (index >= zipped.size()) {
1165+
zipped.add(nTuple);
11951166
}
1196-
});
1167+
index += 1;
1168+
nTuple.add(elem);
1169+
}
11971170
}
11981171
});
11991172
return zipped;

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

Lines changed: 1 addition & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* The MIT License (MIT)
33
*
4-
* Copyright 2016 Valentyn Kolesnikov
4+
* Copyright 2015-2017 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
@@ -230,47 +230,6 @@ public void template4() {
230230
$.templateSettings(new HashMap<String, String>() { { put("interpolate", "<%=([\\s\\S]+?)%>"); } });
231231
}
232232

233-
/*
234-
var list = "<% _.each(people, function(name) { %> <li><%= name %></li> <% }); %>";
235-
_.template(list, {people: ['moe', 'curly', 'larry']});
236-
=> "<li>moe</li><li>curly</li><li>larry</li>"
237-
*/
238-
@Test
239-
public void templateEach() {
240-
String list = "<% _.each(people, function(name) { %> <li><%= name %></li> <% }); %>";
241-
Template<Map<String, Object>> compiled = $.template(list);
242-
assertEquals(" <li>moe</li> <li>curly</li> <li>larry</li> ",
243-
compiled.apply(new LinkedHashMap<String, Object>() { {
244-
put("people", asList("moe", "curly", "larry")); } }));
245-
String list2 = "<% _.each(people2, function(name) { %> <li><%= name %></li> <% }); %>";
246-
Template<Map<String, Object>> compiled2 = $.template(list2);
247-
assertEquals("<% _.each(people2, function(name) { %> <li><%= name %></li> <% }); %>",
248-
compiled2.apply(new LinkedHashMap<String, Object>() { {
249-
put("people", asList("moe", "curly", "larry")); } }));
250-
$.templateSettings(new HashMap<String, String>() { { put("interpolate", "\\{\\{=([\\s\\S]+?)\\}\\}");
251-
put("evaluate", "\\{\\{([\\s\\S]+?)\\}\\}"); } });
252-
String list3 = "{{ _.each(people, function(name) { }} <li>{{= name }}</li> {{ }); }}";
253-
Template<Map<String, Object>> compiled3 = $.template(list3);
254-
assertEquals(" <li>moe</li> <li>curly</li> <li>larry</li> ",
255-
compiled3.apply(new LinkedHashMap<String, Object>() { {
256-
put("people", asList("moe", "curly", "larry")); } }));
257-
$.templateSettings(new HashMap<String, String>() { { put("interpolate", "<%=([\\s\\S]+?)%>");
258-
put("evaluate", "<%([\\s\\S]+?)%>"); } });
259-
}
260-
261-
@Test
262-
public void templateEach2() {
263-
$.templateSettings(new HashMap<String, String>() { { put("interpolate", "\\$\\{([\\s\\S]+?)\\}");
264-
put("evaluate", "\\{\\{([\\s\\S]+?)\\}\\}"); } });
265-
String list = "{{ _.each(items, function(item) { }} <li>${ item }</li> {{ }); }}";
266-
Template<Map<String, Object>> compiled3 = $.template(list);
267-
assertEquals(" <li>moe</li> <li>curly</li> <li>larry</li> ",
268-
compiled3.apply(new LinkedHashMap<String, Object>() { {
269-
put("items", asList("moe", "curly", "larry")); } }));
270-
$.templateSettings(new HashMap<String, String>() { { put("interpolate", "<%=([\\s\\S]+?)%>");
271-
put("evaluate", "<%([\\s\\S]+?)%>"); } });
272-
}
273-
274233
/*
275234
var template = _.template("<b><%- value %></b>");
276235
template({value: '<script>'});
@@ -290,20 +249,6 @@ public void templateValue2() {
290249
template.apply(new LinkedHashMap<String, Object>() { {
291250
put("name", "moe"); put("value", "<script>"); } }));
292251
}
293-
/*
294-
var compiled = _.template("<% print('Hello ' + epithet); %>");
295-
compiled({epithet: "stooge"});
296-
=> "Hello stooge"
297-
*/
298-
@Test
299-
public void templatePrint() {
300-
Template<Map<String, Object>> compiled = $.template("<% print('Hello ' + epithet); %>");
301-
assertEquals("Hello stooge",
302-
compiled.apply(new LinkedHashMap<String, Object>() { { put("epithet", "stooge"); } }));
303-
Template<Map<String, Object>> compiled2 = $.template("<% print('Hello ' + epithet2); %>");
304-
assertEquals("<% print('Hello ' + epithet2); %>",
305-
compiled2.apply(new LinkedHashMap<String, Object>() { { put("epithet", "stooge"); } }));
306-
}
307252

308253
/*
309254
var object = {cheese: 'crumpets', stuff: function(){ return 'nonsense'; }};

0 commit comments

Comments
 (0)