Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.blibli.oss.template.impl.TemplatingImpl;
import com.blibli.oss.template.properties.TemplateProperties;
import com.github.jknack.handlebars.Handlebars;
import com.github.jknack.handlebars.Options;
import com.github.jknack.handlebars.cache.ConcurrentMapTemplateCache;
import com.github.jknack.handlebars.cache.TemplateCache;
import com.github.jknack.handlebars.helper.StringHelpers;
Expand All @@ -15,6 +16,9 @@
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;

/**
* @author Eko Kurniawan Khannedy
*/
Expand Down Expand Up @@ -43,11 +47,20 @@ public Handlebars handlebars(TemplateLoader templateLoader, TemplateCache templa
Handlebars handlebars = new Handlebars()
.with(templateLoader)
.with(templateCache);
handlebars.registerHelper("customSeparator", (context, options) -> customSeparator((Double) context, options));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not all user need this function

StringHelpers.register(handlebars);

return handlebars;
}

public String customSeparator(Double data, Options options) {
String separator = (String) options.hash.getOrDefault("separator", ".");
DecimalFormatSymbols decimalFormatSymbols = new DecimalFormatSymbols();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

create new DecimalFormatSymbols every time customSeparator method invoked?

decimalFormatSymbols.setGroupingSeparator(separator.charAt(0));
DecimalFormat decimalFormat = new DecimalFormat("###,###", decimalFormatSymbols);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hardcode format?

return decimalFormat.format(data);
}

@Bean
public Templating templating(Handlebars handlebars) {
return new TemplatingImpl(handlebars);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,20 @@

import com.blibli.oss.template.properties.TemplateProperties;
import com.github.jknack.handlebars.Handlebars;
import com.github.jknack.handlebars.Options;
import com.github.jknack.handlebars.cache.ConcurrentMapTemplateCache;
import com.github.jknack.handlebars.cache.TemplateCache;
import com.github.jknack.handlebars.io.ClassPathTemplateLoader;
import com.github.jknack.handlebars.io.TemplateLoader;
import com.github.jknack.handlebars.io.TemplateSource;
import org.junit.Before;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.MockitoAnnotations;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
Expand All @@ -19,6 +27,14 @@ public class TemplatingAutoConfigurationTest {

private TemplatingAutoConfiguration configuration = new TemplatingAutoConfiguration();

@InjectMocks
private TemplatingAutoConfiguration templatingAutoConfiguration;

@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
}

@Test
public void testTemplateLoader() throws Exception {
TemplateProperties templateProperties = new TemplateProperties();
Expand Down Expand Up @@ -47,4 +63,13 @@ public void testHandlebars() throws Exception {
assertEquals(templateLoader, handlebars.getLoader());
}

@Test
public void testCustomSeparator() throws Exception {
Map<String,Object> hash = new HashMap<>();
hash.put("separator",".");
Options options = new Options(null, null, null, null, null, null, null, hash, new ArrayList<>());
String result = templatingAutoConfiguration.customSeparator(10500.50d, options);
assertEquals("10.500",result);
}

}