-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
22fed1f
commit 5cdedc0
Showing
7 changed files
with
415 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
import test from 'ava' | ||
|
||
import splitComponent from './splitComponent' | ||
|
||
test('should exist', (t) => { | ||
t.is(typeof splitComponent, 'function') | ||
}) | ||
|
||
test('should return empty array when no component', (t) => { | ||
const component = null | ||
const expected = [] | ||
|
||
const ret = splitComponent(component) | ||
|
||
t.deepEqual(ret, expected) | ||
}) | ||
|
||
test('should return empty array when no parameters', (t) => { | ||
const component = '{}' | ||
const expected = [] | ||
|
||
const ret = splitComponent(component) | ||
|
||
t.deepEqual(ret, expected) | ||
}) | ||
|
||
test('should split component into modifier and params', (t) => { | ||
const component = '{id}' | ||
const expected = [null, 'id'] | ||
|
||
const ret = splitComponent(component) | ||
|
||
t.deepEqual(ret, expected) | ||
}) | ||
|
||
test('should split several params', (t) => { | ||
const component = '{first,max}' | ||
const expected = [null, 'first', 'max'] | ||
|
||
const ret = splitComponent(component) | ||
|
||
t.deepEqual(ret, expected) | ||
}) | ||
|
||
test('should split with query modifier', (t) => { | ||
const component = '{?first,max}' | ||
const expected = ['?', 'first', 'max'] | ||
|
||
const ret = splitComponent(component) | ||
|
||
t.deepEqual(ret, expected) | ||
}) | ||
|
||
test('should split with query continuation modifier', (t) => { | ||
const component = '{&first,max}' | ||
const expected = ['&', 'first', 'max'] | ||
|
||
const ret = splitComponent(component) | ||
|
||
t.deepEqual(ret, expected) | ||
}) | ||
|
||
test('should split with path modifier', (t) => { | ||
const component = '{/first,max}' | ||
const expected = ['/', 'first', 'max'] | ||
|
||
const ret = splitComponent(component) | ||
|
||
t.deepEqual(ret, expected) | ||
}) | ||
|
||
test('should split with fragment modifier', (t) => { | ||
const component = '{#first,max}' | ||
const expected = ['#', 'first', 'max'] | ||
|
||
const ret = splitComponent(component) | ||
|
||
t.deepEqual(ret, expected) | ||
}) | ||
|
||
test('should split with dot modifier', (t) => { | ||
const component = '{.first,max}' | ||
const expected = ['.', 'first', 'max'] | ||
|
||
const ret = splitComponent(component) | ||
|
||
t.deepEqual(ret, expected) | ||
}) | ||
|
||
test('should split with path-style modifier', (t) => { | ||
const component = '{;first,max}' | ||
const expected = [';', 'first', 'max'] | ||
|
||
const ret = splitComponent(component) | ||
|
||
t.deepEqual(ret, expected) | ||
}) | ||
|
||
test('should split with filter function', (t) => { | ||
const component = '{/section|max(3),user}' | ||
const expected = ['/', 'section|max(3)', 'user'] | ||
|
||
const ret = splitComponent(component) | ||
|
||
t.deepEqual(ret, expected) | ||
}) | ||
|
||
test('should split with filter function several args', (t) => { | ||
const component = '{/section,type|map(entry=entries,article=articles)}' | ||
const expected = ['/', 'section', 'type|map(entry=entries,article=articles)'] | ||
|
||
const ret = splitComponent(component) | ||
|
||
t.deepEqual(ret, expected) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
const split = (component) => { | ||
const array = [] | ||
const regex = /[,()]/g | ||
let paran = false | ||
let from = 0 | ||
let match | ||
|
||
while ((match = regex.exec(component)) !== null) { | ||
switch (match[0]) { | ||
case '(': | ||
paran = true | ||
break | ||
case ')': | ||
paran = false | ||
break | ||
case ',': | ||
if (!paran) { | ||
array.push(component.substring(from, regex.lastIndex - 1).trim()) | ||
from = regex.lastIndex | ||
} | ||
} | ||
} | ||
|
||
array.push(component.substring(from).trim()) | ||
|
||
return array | ||
} | ||
|
||
function splitComponent (component) { | ||
const match = /^\{([?&/#.;]?).+\}$/.exec(component) | ||
if (!match) { | ||
return [] | ||
} | ||
const modifier = match[1] || null | ||
component = component.substring((modifier) ? 2 : 1, component.length - 1) | ||
|
||
return [modifier, ...split(component)] | ||
} | ||
|
||
module.exports = splitComponent |
Oops, something went wrong.