Skip to content

Commit

Permalink
Added "last" filter
Browse files Browse the repository at this point in the history
  • Loading branch information
mbosecke committed Feb 10, 2015
1 parent 3de0cdb commit 7e9b23a
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
## v1.4.0
- Added ServletLoader which is the only built-in loader that works with JBoss/Wildfly
- Added "first" filter
- Added "last" filter
- Added "join" filter

## v1.3.1
- Fixed lexing issue on windows
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ public Map<String, Filter> getFilters() {
filters.put("default", new DefaultFilter());
filters.put("first", new FirstFilter());
filters.put("join", new JoinFilter());
filters.put("last", new LastFilter());
filters.put("lower", new LowerFilter());
filters.put("numberformat", new NumberFormatFilter());
filters.put("sort", new SortFilter());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*******************************************************************************
* This file is part of Pebble.
*
* Copyright (c) 2014 by Mitchell Bösecke
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
******************************************************************************/
package com.mitchellbosecke.pebble.extension.core;

import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.mitchellbosecke.pebble.extension.Filter;

/**
* Returns the last element of a collection
*
* @author mbosecke
*
*/
public class LastFilter implements Filter {

@Override
public List<String> getArgumentNames() {
return null;
}

@Override
public Object apply(Object input, Map<String, Object> args) {
if (input == null) {
return null;
}

if(input instanceof String){
String inputString = (String)input;
return inputString.charAt(inputString.length() - 1);
}

@SuppressWarnings("unchecked")
Collection<Object> inputCollection = (Collection<Object>) input;
Object result = null;
Iterator<Object> iterator = inputCollection.iterator();
while(iterator.hasNext()){
result = iterator.next();
}
return result;
}
}
50 changes: 50 additions & 0 deletions src/test/java/com/mitchellbosecke/pebble/CoreFiltersTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,56 @@ public void testJoinWithNullInput() throws PebbleException, IOException {
template.evaluate(writer);
assertEquals("", writer.toString());
}

@Test
public void testLast() throws PebbleException, IOException {
Loader loader = new StringLoader();
PebbleEngine pebble = new PebbleEngine(loader);

PebbleTemplate template = pebble
.getTemplate("{{ names | last }}");

List<String> names = new ArrayList<>();
names.add("Alex");
names.add("Joe");
names.add("Bob");

Map<String, Object> context = new HashMap<>();
context.put("names", names);

Writer writer = new StringWriter();
template.evaluate(writer, context);
assertEquals("Bob", writer.toString());
}

@Test
public void testLastWithNullInput() throws PebbleException, IOException {
Loader loader = new StringLoader();
PebbleEngine pebble = new PebbleEngine(loader);

PebbleTemplate template = pebble
.getTemplate("{{ null | last }}");

Writer writer = new StringWriter();
template.evaluate(writer);
assertEquals("", writer.toString());
}

@Test
public void testLastWithStringInput() throws PebbleException, IOException {
Loader loader = new StringLoader();
PebbleEngine pebble = new PebbleEngine(loader);

PebbleTemplate template = pebble
.getTemplate("{{ name | last }}");

Map<String, Object> context = new HashMap<>();
context.put("name", "Alex");

Writer writer = new StringWriter();
template.evaluate(writer, context);
assertEquals("x", writer.toString());
}

public class User {

Expand Down

0 comments on commit 7e9b23a

Please sign in to comment.