Skip to content

Commit

Permalink
Fix bug with filter function with no arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
kjellmorten committed Sep 21, 2017
1 parent 83dfc0b commit af4845f
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 1 deletion.
14 changes: 14 additions & 0 deletions lib/generate-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,20 @@ test('should generate with several filter functions', (t) => {
t.is(ret, expected)
})

test('should generate with filter function and no arguments', (t) => {
const lower = (value) => value.toLowerCase()
const compiled = [
'http://example.com/',
{param: 'section', filters: [{function: lower}]}
]
const params = {section: 'News'}
const expected = 'http://example.com/news'

const ret = generate(compiled, params)

t.is(ret, expected)
})

test('should skip filters without function', (t) => {
const append = (value, string) => value + string
const compiled = [
Expand Down
3 changes: 2 additions & 1 deletion lib/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ const expandParam = (params, useKeys = false, delimiter = ',') => ({
if (filters) {
value = filters.reduce((value, filter) => {
if (typeof filter.function === 'function') {
return filter.function(value, ...filter.args)
const args = filter.args || []
return filter.function(value, ...args)
}
return value
}, value)
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
},
"ava": {
"files": [
"tests/**/*-test.js",
"lib/**/*-test.js"
],
"source": [
"index.js",
"lib/**/!(*-test).js"
],
"babel": {
Expand Down
47 changes: 47 additions & 0 deletions tests/template-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import test from 'ava'

import {compile, generate} from '..'

test('should generate uri with replacement', (t) => {
const template = 'http://example.com/{section}{?first,max}'
const params = {section: 'news', first: 40, max: 20}
const expected = 'http://example.com/news?first=40&max=20'

const compiled = compile(template)
const uri = generate(compiled, params)

t.is(uri, expected)
})

test('should generate uri with optional parameters', (t) => {
const template = 'http://example.com/{section}{?first?,max?}'
const params = {section: 'news', max: 20}
const expected = 'http://example.com/news?max=20'

const compiled = compile(template)
const uri = generate(compiled, params)

t.is(uri, expected)
})

test('should generate uri with filter functions', (t) => {
const template = 'http://example.com/{section|append(_archive)}{?letter=name|max(1)|lower}'
const params = {section: 'news', name: 'John F.'}
const expected = 'http://example.com/news_archive?letter=j'

const compiled = compile(template)
const uri = generate(compiled, params)

t.is(uri, expected)
})

test('should generate empty string from empty string template', (t) => {
const template = ''
const params = {}
const expected = ''

const compiled = compile(template)
const uri = generate(compiled, params)

t.is(uri, expected)
})

0 comments on commit af4845f

Please sign in to comment.