Skip to content

Commit

Permalink
Update deps
Browse files Browse the repository at this point in the history
  • Loading branch information
kjellmorten committed Aug 30, 2019
1 parent 0e896ea commit 86669d7
Show file tree
Hide file tree
Showing 7 changed files with 4,444 additions and 6,392 deletions.
62 changes: 40 additions & 22 deletions lib/compile-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ test('should compile template with no parameters', (t) => {

test('should compile parameter replacement', (t) => {
const template = 'http://example.com/{type}:{id}'
const expected = ['http://example.com/', {param: 'type'}, ':', {param: 'id'}]
const expected = ['http://example.com/', { param: 'type' }, ':', { param: 'id' }]

const ret = compile(template)

Expand All @@ -32,7 +32,7 @@ test('should compile parameter replacement', (t) => {

test('should compile parameter in the middle of the url', (t) => {
const template = 'http://example.com/{section}/latest'
const expected = ['http://example.com/', {param: 'section'}, '/latest']
const expected = ['http://example.com/', { param: 'section' }, '/latest']

const ret = compile(template)

Expand All @@ -43,9 +43,9 @@ test('should compile escaped curly brackets as text', (t) => {
const template = 'http://example.com/{type}?brackets=\\{\\}&page={first}'
const expected = [
'http://example.com/',
{param: 'type'},
{ param: 'type' },
'?brackets={}&page=',
{param: 'first'}
{ param: 'first' }
]

const ret = compile(template)
Expand All @@ -55,7 +55,7 @@ test('should compile escaped curly brackets as text', (t) => {

test('should compile optional param', (t) => {
const template = 'http://example.com/{required}/{optional?}'
const expected = ['http://example.com/', {param: 'required'}, '/', {param: 'optional', optional: true}]
const expected = ['http://example.com/', { param: 'required' }, '/', { param: 'optional', optional: true }]

const ret = compile(template)

Expand All @@ -64,7 +64,7 @@ test('should compile optional param', (t) => {

test('should compile list of params', (t) => {
const template = 'http://example.com/{first,max}'
const expected = ['http://example.com/', [null, {param: 'first'}, {param: 'max'}]]
const expected = ['http://example.com/', [null, { param: 'first' }, { param: 'max' }]]

const ret = compile(template)

Expand All @@ -73,7 +73,7 @@ test('should compile list of params', (t) => {

test('should compile query component', (t) => {
const template = 'http://example.com/{?first,max}'
const expected = ['http://example.com/', ['?', {param: 'first'}, {param: 'max'}]]
const expected = ['http://example.com/', ['?', { param: 'first' }, { param: 'max' }]]

const ret = compile(template)

Expand All @@ -82,7 +82,7 @@ test('should compile query component', (t) => {

test('should compile with specified key', (t) => {
const template = 'http://example.com/{?page=first,max}'
const expected = ['http://example.com/', ['?', {param: 'first', key: 'page'}, {param: 'max'}]]
const expected = ['http://example.com/', ['?', { param: 'first', key: 'page' }, { param: 'max' }]]

const ret = compile(template)

Expand All @@ -91,7 +91,7 @@ test('should compile with specified key', (t) => {

test('should compile optional query component', (t) => {
const template = 'http://example.com/{?first?,max}'
const expected = ['http://example.com/', ['?', {param: 'first', optional: true}, {param: 'max'}]]
const expected = ['http://example.com/', ['?', { param: 'first', optional: true }, { param: 'max' }]]

const ret = compile(template)

Expand All @@ -102,8 +102,8 @@ test('should explode path and dot expansion', (t) => {
const template = 'http://example.com/{/folders}{.suffix}'
const expected = [
'http://example.com/',
['/', {param: 'folders', explode: true}],
['.', {param: 'suffix', explode: true}]
['/', { param: 'folders', explode: true }],
['.', { param: 'suffix', explode: true }]
]

const ret = compile(template)
Expand All @@ -115,7 +115,7 @@ test('should compile filter function', (t) => {
const template = 'http://example.com/{folder|append(_archive)}'
const expected = [
'http://example.com/',
{param: 'folder', filters: [{function: append, args: ['_archive']}]}
{ param: 'folder', filters: [{ function: append, args: ['_archive'] }] }
]

const ret = compile(template)
Expand All @@ -130,8 +130,8 @@ test('should compile several filter functions', (t) => {
{
param: 'folder',
filters: [
{function: append, args: ['_archive']},
{function: prepend, args: ['user_']}
{ function: append, args: ['_archive'] },
{ function: prepend, args: ['user_'] }
]
}
]
Expand All @@ -147,7 +147,7 @@ test('should compile filter function with several args', (t) => {
'http://example.com',
[
'?',
{param: 'ids', key: 'keys', filters: [{function: wrap, args: ['[', '"', '"', ']']}]}
{ param: 'ids', key: 'keys', filters: [{ function: wrap, args: ['[', '"', '"', ']'] }] }
]
]

Expand All @@ -162,7 +162,7 @@ test('should compile with escaped comma in args', (t) => {
'http://example.com',
[
'?',
{param: 'ids', key: 'keys', filters: [{function: wrap, args: ['[', '"', '"', ',"extra"]']}]}
{ param: 'ids', key: 'keys', filters: [{ function: wrap, args: ['[', '"', '"', ',"extra"]'] }] }
]
]

Expand All @@ -175,7 +175,7 @@ test('should compile filter function with no args', (t) => {
const template = 'http://example.com/{folder|upper()}'
const expected = [
'http://example.com/',
{param: 'folder', filters: [{function: upper}]}
{ param: 'folder', filters: [{ function: upper }] }
]

const ret = compile(template)
Expand All @@ -187,7 +187,7 @@ test('should compile filter function with no parens', (t) => {
const template = 'http://example.com/{folder|upper}'
const expected = [
'http://example.com/',
{param: 'folder', filters: [{function: upper}]}
{ param: 'folder', filters: [{ function: upper }] }
]

const ret = compile(template)
Expand All @@ -199,9 +199,9 @@ test('should compile prefix modifier', (t) => {
const template = 'http://example.com/{section:1}/{section}'
const expected = [
'http://example.com/',
{param: 'section', filters: [{function: max, args: ['1']}]},
{ param: 'section', filters: [{ function: max, args: ['1'] }] },
'/',
{param: 'section'}
{ param: 'section' }
]

const ret = compile(template)
Expand All @@ -213,7 +213,7 @@ test('should compile prefix modifier before filter functions', (t) => {
const template = 'http://example.com/{section:1|append(_archive)}'
const expected = [
'http://example.com/',
{param: 'section', filters: [{function: max, args: ['1']}, {function: append, args: ['_archive']}]}
{ param: 'section', filters: [{ function: max, args: ['1'] }, { function: append, args: ['_archive'] }] }
]

const ret = compile(template)
Expand All @@ -225,14 +225,32 @@ test('should compile filter function with equal sign in argument', (t) => {
const template = 'http://example.com/{type|map(entry=entries)}'
const expected = [
'http://example.com/',
{param: 'type', filters: [{function: map, args: ['entry=entries']}]}
{ param: 'type', filters: [{ function: map, args: ['entry=entries'] }] }
]

const ret = compile(template)

t.deepEqual(ret, expected)
})

test('should return empty array when no template', (t) => {
const template = null
const expected = []

const ret = compile(template)

t.deepEqual(ret, expected)
})

test('should return empty array when empty string template', (t) => {
const template = ''
const expected = []

const ret = compile(template)

t.deepEqual(ret, expected)
})

test('should throw for unclosed parameter', (t) => {
const template = 'http://example.com/{type'

Expand Down
8 changes: 4 additions & 4 deletions lib/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const fixEscapedCommas = (args, arg, index, array) => {
}

const setOptional = (obj) => {
const {length} = obj.param
const { length } = obj.param
if (obj.param.substr(length - 1) === '?') {
obj.param = obj.param.substr(0, length - 1)
obj.optional = true
Expand All @@ -25,7 +25,7 @@ const setFilterFunctions = (obj) => {
obj.filters = filterParts.slice(1)
.map((filter) => {
const match = /^(\w+)\(([^)]*)\)$/.exec(filter)
const obj = {function: filters[(match && match.length > 1) ? match[1] : filter]}
const obj = { function: filters[(match && match.length > 1) ? match[1] : filter] }
if (match && match[2]) {
obj.args = match[2].split(',')
.reduce(fixEscapedCommas, [])
Expand All @@ -40,7 +40,7 @@ const setPrefixFunction = (obj) => {
const index = obj.param.indexOf(':')
if (index > -1) {
const key = obj.param.substr(index + 1)
obj.filters = [{function: filters.max, args: [key]}]
obj.filters = [{ function: filters.max, args: [key] }]
.concat(obj.filters || [])
obj.param = obj.param.substr(0, index)
}
Expand All @@ -60,7 +60,7 @@ const compileParam = (explode = false) => (param, index) => {
return param
}

const obj = {param}
const obj = { param }

setOptional(obj)
setFilterFunctions(obj)
Expand Down
Loading

0 comments on commit 86669d7

Please sign in to comment.