Skip to content

Commit a42a357

Browse files
authored
Release 1.2.0 (#84)
* Add support React Router v4 (#82) * Release 1.2.0
1 parent 66247e6 commit a42a357

File tree

8 files changed

+172
-33
lines changed

8 files changed

+172
-33
lines changed

api.md

Lines changed: 45 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,12 @@ Filter paths using the specified rules.
3636

3737
**Parameters**
3838

39-
- `filterConfig` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** Filter configuration
39+
- `filterConfig` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** Filter configuration
4040

4141
**Properties**
4242

43-
- `rules` **[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)<[RegExp](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp)>** List filter rules.
44-
- `isValid` **[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** Flag that defines a way to filter paths.
43+
- `rules` **[Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)<[RegExp](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/RegExp)>** List filter rules.
44+
- `isValid` **[Boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** Flag that defines a way to filter paths.
4545
If `true`, the path satisfying the rules will be included.
4646
If `false`, the path satisfying the rules will be excluded.
4747

@@ -65,7 +65,7 @@ Replace the dynamic parameters in paths using the given values.
6565

6666
**Parameters**
6767

68-
- `paramsConfig` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)<[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String), [Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)>** Configuration for replacing params.
68+
- `paramsConfig` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)<[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String), [Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)>** Configuration for replacing params.
6969

7070
**Examples**
7171

@@ -96,8 +96,8 @@ Convert array of paths to sitemap.
9696

9797
**Parameters**
9898

99-
- `hostname` **[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** The root name of your site.
100-
- `$1` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** (optional, default `{}`)
99+
- `hostname` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** The root name of your site.
100+
- `$1` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** (optional, default `{}`)
101101
- `$1.limitCountPaths` (optional, default `49999`)
102102

103103
## save
@@ -106,17 +106,17 @@ Save sitemaps and sitemap index in files.
106106

107107
**Parameters**
108108

109-
- `dist` **[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** The path and file name where the sitemap index is saved.
110-
- `publicPath` **\[[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)](default '/')** optional public path relative to hostname, default: '/'
109+
- `dist` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** The path and file name where the sitemap index is saved.
110+
- `publicPath` **\[[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)](default '/')** optional public path relative to hostname, default: '/'
111111

112112
# pathsSplitter
113113

114114
Module for splitting paths array in multiple arrays for support of large projects
115115

116116
**Parameters**
117117

118-
- `paths` **\[[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)]** Initial paths array (flattened) (optional, default `[]`)
119-
- `size` **\[[Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)]** (optional, default `49999`)
118+
- `paths` **\[[Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)]** Initial paths array (flattened) (optional, default `[]`)
119+
- `size` **\[[Number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)]** (optional, default `49999`)
120120

121121
**Examples**
122122

@@ -132,8 +132,8 @@ Module for applying params in dynamic paths.
132132

133133
**Parameters**
134134

135-
- `paths` **\[[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)<[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)>]** Array of paths
136-
- `paramsConfig` **\[[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)<[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String), [Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)>]** Configuration matching parameters and values
135+
- `paths` **\[[Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)<[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)>]** Array of paths
136+
- `paramsConfig` **\[[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)<[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String), [Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)>]** Configuration matching parameters and values
137137

138138
**Examples**
139139

@@ -167,17 +167,17 @@ const paths = applyParams(paths, config);
167167
// ['/path/a/1', '/path/b/2', '/path/b/3']
168168
```
169169

170-
Returns **[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)<[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)>** Array of paths
170+
Returns **[Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)<[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)>** Array of paths
171171

172172
# pathsFilter
173173

174174
Module for filtering an array of paths.
175175

176176
**Parameters**
177177

178-
- `paths` **\[[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)<[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)>]** Array of paths
179-
- `rules` **\[[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)<[RegExp](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp)>]** Filter rules
180-
- `isValidRules` **\[[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)]** Flag that defines a way to filter paths.
178+
- `paths` **\[[Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)<[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)>]** Array of paths
179+
- `rules` **\[[Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)<[RegExp](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/RegExp)>]** Filter rules
180+
- `isValidRules` **\[[Boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)]** Flag that defines a way to filter paths.
181181
If `true`, the path satisfying the rules will be included.
182182
If `false`, the path satisfying the rules will be excluded.
183183

@@ -205,48 +205,68 @@ const paths = filterPaths(paths, rules, isValidRules);
205205
// ['/auth']
206206
```
207207

208-
Returns **[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)<[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)>** Array of paths.
208+
Returns **[Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)<[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)>** Array of paths.
209+
210+
# createRoutes
211+
212+
Creates and returns an array of routes from the given object which
213+
may be a JSX route, a plain object route, or an array of either.
214+
215+
**Parameters**
216+
217+
- `routes` **Route** React Router configuration.
218+
219+
**Examples**
220+
221+
```javascript
222+
import { routesCreater as createRoutes } from 'react-router-sitemap';
223+
import { routesParser as parseRoutes } from 'react-router-sitemap';
224+
225+
const routes = createRoutes(<Route path='/home'>);
226+
const paths = parseRoutes(routes); // ['/home']
227+
```
228+
229+
Returns **[Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)&lt;[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)>**
209230

210231
# routesParser
211232

212233
Module for parsing the result of the `createRoutes(<Route>)` function.
213-
from [react-router](https://www.npmjs.com/package/react-router) package.
214234

215235
**Parameters**
216236

217-
- `routes` **\[([Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array) \| [Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object))]** Result of execute function
237+
- `routes` **\[([Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array) \| [Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object))]** Result of execute function
218238
`createRoutes(<Route>)` (optional, default `[]`)
219-
- `basePath` **\[[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)]** Prefix for all paths (optional, default `''`)
239+
- `basePath` **\[[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)]** Prefix for all paths (optional, default `''`)
220240

221241
**Examples**
222242

223243
```javascript
224-
import { createRoutes } from 'react-router';
244+
import { routesCreater as createRoutes } from 'react-router-sitemap';
225245
import { routesParser as parseRoutes } from 'react-router-sitemap';
226246

227247
const routes = createRoutes(<Route path='/home'>);
228248
const paths = parseRoutes(routes); // ['/home']
229249
```
230250

231251
```javascript
232-
import { createRoutes } from 'react-router';
252+
import { routesCreater as createRoutes } from 'react-router-sitemap';
233253
import { routesParser as parseRoutes } from 'react-router-sitemap';
234254

235255
const routes = createRoutes(<Route path='/home'>);
236256
const prefix = '/prefix';
237257
const paths = parseRoutes(routes, prefix); // ['/prefix/home']
238258
```
239259

240-
Returns **[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)&lt;[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)>** Array of paths
260+
Returns **[Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)&lt;[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)>** Array of paths
241261

242262
# sitemapBuilder
243263

244264
Module for building a sitemap using an array of paths. Uses the [sitemap](https://www.npmjs.com/package/sitemap) package.
245265

246266
**Parameters**
247267

248-
- `hostname` **\[[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)]** The root name of your site
249-
- `paths` **\[[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)&lt;[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)>]** Array of paths
268+
- `hostname` **\[[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)]** The root name of your site
269+
- `paths` **\[[Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)&lt;[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)>]** Array of paths
250270

251271
**Examples**
252272

lib/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import fs from 'fs';
22
import path from 'path';
33

44
import sm from 'sitemap';
5-
import { createRoutes } from 'react-router';
65

6+
import createRoutes from './routes-creater';
77
import parseRoutes from './routes-parser';
88
import filterPaths from './paths-filter';
99
import applyParams from './params-applier';
@@ -158,6 +158,7 @@ class Sitemap {
158158

159159
export default Sitemap;
160160

161+
export { default as routesCreater } from './routes-creater';
161162
export { default as routesParser } from './routes-parser';
162163
export { default as pathsFilter } from './paths-filter';
163164
export { default as paramsApplier } from './params-applier';

lib/routes-creater/index.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { isReactChildren, createRoutesFromReactChildren } from './routeUtils';
2+
3+
/**
4+
* @description Creates and returns an array of routes from the given object which
5+
* may be a JSX route, a plain object route, or an array of either.
6+
* @param {Route} routes - React Router configuration.
7+
* @return {Array<String>}
8+
*
9+
* @example
10+
* import { routesCreater as createRoutes } from 'react-router-sitemap';
11+
* import { routesParser as parseRoutes } from 'react-router-sitemap';
12+
*
13+
* const routes = createRoutes(<Route path='/home'>);
14+
* const paths = parseRoutes(routes); // ['/home']
15+
*/
16+
const createRoutes = routes => {
17+
if (isReactChildren(routes)) {
18+
routes = createRoutesFromReactChildren(routes);
19+
} else if (routes && !Array.isArray(routes)) {
20+
routes = [routes];
21+
}
22+
23+
return routes;
24+
};
25+
26+
export default createRoutes;

lib/routes-creater/routeUtils.js

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import React from 'react';
2+
3+
/**
4+
* @description Use React method to test if is a valid React Element.
5+
* @param {Object} object - Which to test if is valid React Element.
6+
* @return {Boolean}
7+
* @ignore
8+
*/
9+
const isValidChild = object => {
10+
return object === null || React.isValidElement(object);
11+
};
12+
13+
/**
14+
* @param {Object|array}
15+
* @return {Boolean}
16+
* @ignore
17+
*/
18+
const isReactChildren = object => {
19+
return isValidChild(object) || (Array.isArray(object) && object.every(isValidChild));
20+
};
21+
22+
/**
23+
* @description Creates and returns a routes object from the given ReactChildren. JSX
24+
* provides a convenient way to visualize how routes in the hierarchy are
25+
* nested.
26+
* @param {ReactChildren} children - ReactChildren in JSX
27+
* @return {Object} routes object
28+
* @ignore
29+
*/
30+
const createRoutesFromReactChildren = children => {
31+
const routes = [];
32+
33+
/**
34+
* @param {Object} element - ReactChild
35+
* @return {Object} route object
36+
* @ignore
37+
*/
38+
const createRouteFromReactElement = element => {
39+
const type = element.type;
40+
const route = Object.assign({}, type.defaultProps, element.props);
41+
42+
if (route.children) {
43+
const childRoutes = createRoutesFromReactChildren(route.children, route);
44+
45+
if (childRoutes.length) {
46+
route.childRoutes = childRoutes;
47+
}
48+
delete route.children;
49+
}
50+
51+
return route;
52+
};
53+
54+
React.Children.forEach(children, function (element) {
55+
if (React.isValidElement(element)) {
56+
// Component classes may have a static create* method.
57+
if (element.type.createRouteFromReactElement) {
58+
const route = element.type.createRouteFromReactElement(element);
59+
60+
if (route) {
61+
routes.push(route);
62+
}
63+
} else {
64+
routes.push(createRouteFromReactElement(element));
65+
}
66+
}
67+
});
68+
69+
return routes;
70+
};
71+
72+
export {
73+
isValidChild,
74+
isReactChildren,
75+
createRoutesFromReactChildren
76+
};

lib/routes-parser/index.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,21 @@ import buildPath from './path-builder';
22

33
/**
44
* Module for parsing the result of the `createRoutes(<Route>)` function.
5-
* from [react-router](https://www.npmjs.com/package/react-router) package.
65
* @module routesParser
76
* @param {Array|Object} [routes = []] Result of execute function
87
* `createRoutes(<Route>)`
98
* @param {String} [basePath = ''] Prefix for all paths
109
* @return {Array<String>} Array of paths
1110
*
1211
* @example
13-
* import { createRoutes } from 'react-router';
12+
* import { routesCreater as createRoutes } from 'react-router-sitemap';
1413
* import { routesParser as parseRoutes } from 'react-router-sitemap';
1514
*
1615
* const routes = createRoutes(<Route path='/home'>);
1716
* const paths = parseRoutes(routes); // ['/home']
1817
*
1918
* @example
20-
* import { createRoutes } from 'react-router';
19+
* import { routesCreater as createRoutes } from 'react-router-sitemap';
2120
* import { routesParser as parseRoutes } from 'react-router-sitemap';
2221
*
2322
* const routes = createRoutes(<Route path='/home'>);

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-router-sitemap",
3-
"version": "1.1.5",
3+
"version": "1.2.0",
44
"description": "Module to generate a sitemap for react-router configuration",
55
"repository": {
66
"type": "git",
@@ -19,7 +19,7 @@
1919
"test": "karma start ./config/karma.config.js",
2020
"build": "npm run lint && npm run test && webpack --config ./config/build.config.js",
2121
"example": "cd example && node ./sitemap-builder.js",
22-
"documentation": "documentation build ./lib ./lib/routes-parser ./lib/paths-filter ./lib/params-applier ./lib/sitemap-builder -f md > api.md",
22+
"documentation": "documentation build ./lib ./lib/routes-creater ./lib/routes-parser ./lib/paths-filter ./lib/params-applier ./lib/sitemap-builder -f md > api.md",
2323
"prepublish": "npm run build"
2424
},
2525
"author": "Igor Uvarov <kuflash@ya.ru> (http://kuflash.ru)",
@@ -58,7 +58,7 @@
5858
},
5959
"peerDependencies": {
6060
"react": "^15.1.0 || ^16.0.0",
61-
"react-router": "^2.3.0 || ^3.2.1"
61+
"react-router": "^2.3.0 || ^3.2.1 || ^4.3.0"
6262
},
6363
"dependencies": {
6464
"sitemap": "^1.6.0"

readme.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,23 @@ export default (
2727
</Route>
2828
);
2929
```
30+
If you are using v4 `react-router`, your `router.jsx` might be:
31+
```js
32+
import React from 'react';
33+
import { Switch, Route } from 'react-router';
34+
35+
export default (
36+
// Switch is added in v4 react-router
37+
<Switch>
38+
<Route path='/' />
39+
<Route path='/about' />
40+
<Route path='/projects' />
41+
<Route path='/contacts' />
42+
<Route path='/auth' />
43+
<Route /> // No-match case
44+
</Switch>
45+
);
46+
```
3047
And you need to create a script which will run from the command line or on the server.
3148

3249
_Please note that in this case you need a module 'babel-register' to work with the ES2105 syntax and `.jsx` format._

0 commit comments

Comments
 (0)