Skip to content

Commit

Permalink
added in substring and startswith string functions
Browse files Browse the repository at this point in the history
  • Loading branch information
synapticloop committed Feb 7, 2017
1 parent 5669a4f commit 71c5575
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 13 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -216,19 +216,19 @@ repositories {

```
dependencies {
runtime(group: 'synapticloop', name: 'templar', version: '1.4.1', ext: 'jar')
runtime(group: 'synapticloop', name: 'templar', version: '1.4.2', ext: 'jar')
compile(group: 'synapticloop', name: 'templar', version: '1.4.1', ext: 'jar')
compile(group: 'synapticloop', name: 'templar', version: '1.4.2', ext: 'jar')
}
```

or, more simply for versions of gradle greater than 2.1

```
dependencies {
runtime 'synapticloop:templar:1.4.1'
runtime 'synapticloop:templar:1.4.2'
compile 'synapticloop:templar:1.4.1'
compile 'synapticloop:templar:1.4.2'
}
```

Expand All @@ -238,7 +238,7 @@ dependencies {
<dependency>
<groupId>synapticloop</groupId>
<artifactId>templar</artifactId>
<version>1.4.1</version>
<version>1.4.2</version>
<type>jar</type>
</dependency>
```
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ plugins {
group = 'synapticloop'
archivesBaseName = 'templar'
description = """Templar templating engine"""
version = '1.4.1'
version = '1.4.2'

sourceCompatibility = 1.7
targetCompatibility = 1.7
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package synapticloop.templar.function.string;

import synapticloop.templar.exception.FunctionException;
import synapticloop.templar.function.Function;
import synapticloop.templar.helper.ObjectHelper;
import synapticloop.templar.utils.TemplarContext;

/*
* Copyright (c) 2017 Synapticloop.
*
* All rights reserved.
*
* This code may contain contributions from other parties which, where
* applicable, will be listed in the default build file for the project
* ~and/or~ in a file named CONTRIBUTORS.txt in the root of the project.
*
* This source code and any derived binaries are covered by the terms and
* conditions of the Licence agreement ("the Licence"). You may not use this
* source code or any derived binaries except in compliance with the Licence.
* A copy of the Licence is available in the file named LICENSE.txt shipped with
* this source code or binaries.
*/

public class FunctionStartsWith extends Function {

public FunctionStartsWith() {
super(2);
}

@Override
protected Object evaluateFunction(Object[] args, TemplarContext templarContext) throws FunctionException {
Object argZero = ObjectHelper.evaluateObjectToDefault(args[0], templarContext);
Object argOne = ObjectHelper.evaluateObjectToDefault(args[1], templarContext);
if(null == argZero || null == argOne) {
throw new FunctionException("Could not evaluate arguments to String, arguments were: " + args[0] + ", " + args[1] + ", values: " + argZero + ", " + argOne);
}

return(argOne.toString().startsWith(argZero.toString()));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,45 @@

import synapticloop.templar.exception.FunctionException;
import synapticloop.templar.function.Function;
import synapticloop.templar.helper.ObjectHelper;
import synapticloop.templar.utils.TemplarContext;

public class FunctionSubString extends Function {

public FunctionSubString() {
super(3);
super(2, 3);
}

@Override
protected Object evaluateFunction(Object[] args, TemplarContext templarContext) throws FunctionException {
// arguments can be either 2 or 3
// TODO Auto-generated method stub
return null;
Object argZero = ObjectHelper.evaluateObjectToDefault(args[0], templarContext);
Object argOne = ObjectHelper.evaluateObjectToDefault(args[1], templarContext);

int from = 0;
int to = 0;

try {
from = Integer.parseInt(argOne.toString());
} catch(NumberFormatException ex) {
throw new FunctionException("Could not evaluate arguments to Integer, arguments was: " + args[1] + ", value: " + argOne);
}

Object argTwo = null;
if(args.length == 3) {
argTwo = ObjectHelper.evaluateObjectToDefault(args[2], templarContext);
try {
to = Integer.parseInt(argTwo.toString());
} catch(NumberFormatException ex) {
throw new FunctionException("Could not evaluate arguments to Integer, arguments was: " + args[2] + ", value: " + argTwo);
}
}

// at this point we should be good to go
if(args.length == 3) {
return(argZero.toString().substring(from, to));
} else {
return(argZero.toString().substring(from));
}
}

}
9 changes: 6 additions & 3 deletions src/main/java/synapticloop/templar/utils/TemplarContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
import synapticloop.templar.function.math.FunctionPower;
import synapticloop.templar.function.math.FunctionSubtract;
import synapticloop.templar.function.string.FunctionIndexOf;
import synapticloop.templar.function.string.FunctionStartsWith;
import synapticloop.templar.function.string.FunctionSubString;
import synapticloop.templar.function.string.FunctionToJson;
import synapticloop.templar.function.util.FunctionInstanceOf;

Expand Down Expand Up @@ -104,9 +106,10 @@ public class TemplarContext {
// string
functionMap.put("indexOf", new FunctionIndexOf()); // get the index of strings
functionMap.put("toJson", new FunctionToJson()); // convert a string into a JSON object
// NIY
// functionMap.put("substring", new FunctionSubString()); // get the substring
}
functionMap.put("startsWith", new FunctionStartsWith()); // determine whether a string starts with another string
functionMap.put("substring", new FunctionSubString()); // return a substring of a string

}

private static Map<String, String> functionAliasMap = new HashMap<String, String>();
static {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package synapticloop.templar.function.string;
import static org.junit.Assert.*;

import org.junit.Before;
import org.junit.Test;

import synapticloop.templar.exception.FunctionException;

public class FunctionSubStringTest {
private FunctionSubString functionSubString = null;

@Before
public void setup() {
functionSubString = new FunctionSubString();
}

@Test
public void testSubstring() throws FunctionException {
Object evaluate = functionSubString.evaluate("substring", new Object[] { "\"hello\"", "\"1\"" }, null);
assertEquals("ello", evaluate);
}

@Test
public void testSubstringRange() throws FunctionException {
Object evaluate = functionSubString.evaluate("substring", new Object[] { "\"hello\"", "\"1\"", "\"3\"" }, null);
assertEquals("el", evaluate);
}
}

0 comments on commit 71c5575

Please sign in to comment.