Skip to content

Commit

Permalink
Release 1.1.2
Browse files Browse the repository at this point in the history
* Fix typo in example code (#62)
* Removes parenthesis around replaced params (#64)
  • Loading branch information
kuflash authored Sep 29, 2017
1 parent dc9c3b8 commit 0aa95b1
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 2 deletions.
11 changes: 11 additions & 0 deletions lib/params-applier/rules-applier.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,17 @@ export default (path = '', rules = []) => {
rules
.map(rule => applyRule(path, rule))
.reduce((result, item) => result.concat(item), [])
.map(location => {
// for each remaining (optional) param group that hasn't been removed, the optional group is removed from the location
// /foo/bar(/:param) => /foo/bar
location = location.replace(/\((.*:.*)\)/g, '');

// remove other parenthesis that might be wrapping params that have been replaced
// /foo(/:bar) => /foo(/bar-value) => /foo/bar-value
location = location.replace(/(\(|\))/g, '');

return location;
})
);

};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-router-sitemap",
"version": "1.1.1",
"version": "1.1.2",
"description": "Module to generate a sitemap for react-router configuration",
"repository": {
"type": "git",
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export default (
<Route>
<Route path='/' />
<Route path='/about' />
<Route path='/projects'>
<Route path='/projects' />
<Route path='/contacts' />
<Route path='/auth' />
</Route>
Expand Down
32 changes: 32 additions & 0 deletions test/spec/params-applier/rules-applier.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,36 @@ describe('rules applier', () => {

});

it('replaces optional params in path by list rules', () => {

const path = '/path/:param-one(/:param-two)';
const rules = [
{ 'param-one': 1 },
{ 'param-one': 1, 'param-two': 2 },
];
const etalon = [
'/path/1',
'/path/1/2',
];

expect(applyRules(path, rules)).toHaveSameItems(etalon, true);

});

it('removes optional params in path by list rules whose value hasn\'t been provided', () => {

const path = '/path/:param-one(/:param-two)';
const rules = [
{},
{ 'param-one': 1 },
];
const etalon = [
'/path/:param-one',
'/path/1',
];

expect(applyRules(path, rules)).toHaveSameItems(etalon, true);

});

});

0 comments on commit 0aa95b1

Please sign in to comment.