From 7e9b23ab3a82fb2a8756dac0476d4dd548bf3efa Mon Sep 17 00:00:00 2001 From: Mitchell Bosecke Date: Mon, 9 Feb 2015 20:16:47 -0700 Subject: [PATCH] Added "last" filter --- CHANGELOG.md | 2 + .../pebble/extension/core/CoreExtension.java | 1 + .../pebble/extension/core/LastFilter.java | 51 +++++++++++++++++++ .../pebble/CoreFiltersTest.java | 50 ++++++++++++++++++ 4 files changed, 104 insertions(+) create mode 100644 src/main/java/com/mitchellbosecke/pebble/extension/core/LastFilter.java diff --git a/CHANGELOG.md b/CHANGELOG.md index b3357d903..db9ff2f02 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/main/java/com/mitchellbosecke/pebble/extension/core/CoreExtension.java b/src/main/java/com/mitchellbosecke/pebble/extension/core/CoreExtension.java index 716a5b66c..d0029e68d 100644 --- a/src/main/java/com/mitchellbosecke/pebble/extension/core/CoreExtension.java +++ b/src/main/java/com/mitchellbosecke/pebble/extension/core/CoreExtension.java @@ -119,6 +119,7 @@ public Map 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()); diff --git a/src/main/java/com/mitchellbosecke/pebble/extension/core/LastFilter.java b/src/main/java/com/mitchellbosecke/pebble/extension/core/LastFilter.java new file mode 100644 index 000000000..fc33505af --- /dev/null +++ b/src/main/java/com/mitchellbosecke/pebble/extension/core/LastFilter.java @@ -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 getArgumentNames() { + return null; + } + + @Override + public Object apply(Object input, Map args) { + if (input == null) { + return null; + } + + if(input instanceof String){ + String inputString = (String)input; + return inputString.charAt(inputString.length() - 1); + } + + @SuppressWarnings("unchecked") + Collection inputCollection = (Collection) input; + Object result = null; + Iterator iterator = inputCollection.iterator(); + while(iterator.hasNext()){ + result = iterator.next(); + } + return result; + } +} diff --git a/src/test/java/com/mitchellbosecke/pebble/CoreFiltersTest.java b/src/test/java/com/mitchellbosecke/pebble/CoreFiltersTest.java index 27dd44307..5cfa4a65c 100644 --- a/src/test/java/com/mitchellbosecke/pebble/CoreFiltersTest.java +++ b/src/test/java/com/mitchellbosecke/pebble/CoreFiltersTest.java @@ -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 names = new ArrayList<>(); + names.add("Alex"); + names.add("Joe"); + names.add("Bob"); + + Map 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 context = new HashMap<>(); + context.put("name", "Alex"); + + Writer writer = new StringWriter(); + template.evaluate(writer, context); + assertEquals("x", writer.toString()); + } public class User {