Skip to content

Commit 200a689

Browse files
jedirandySheng Ran
authored and
Sheng Ran
committed
Update README
- add documentation of APIs
1 parent 3a175b5 commit 200a689

9 files changed

+1173
-58
lines changed

.travis.yml

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
language: node_js
22
node_js:
33
- "5"
4+
- "6"
5+
- "7"
46
script:
57
- npm run lint
8+
- npm run test -- --single-run
69
- npm run flow
7-
- npm run build
10+
- npm run build

LICENSE.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Copyright 2017 Sheng Ran
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4+
5+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6+
7+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

+50-6
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,27 @@
1-
# Redux simple router
2-
[![Build Status](https://travis-ci.org/jedirandy/redux-simple-router.svg?branch=master)](https://travis-ci.org/jedirandy/redux-simple-router)
1+
# redux-url
32

4-
A redux middleware that does one simple job: matches a route and dispatch an action.
3+
[![Build Status](https://travis-ci.org/jedirandy/redux-url.svg?branch=master)](https://travis-ci.org/jedirandy/redux-url)
4+
[![npm module](https://badge.fury.io/js/redux-url.svg)](https://www.npmjs.org/package/redux-url)
5+
6+
A redux middleware that does one simple job: matches a url pattern and dispatch an action.
7+
8+
## Install
9+
10+
```
11+
npm install redux-url
12+
```
13+
14+
[`history`](https://github.com/ReactTraining/history) is needed for it to work correctly.
515

616
## Usage
717

818
```javascript
919
import createHistory from 'history/createBrowserHistory'; // choose a history implementation
1020
import { createStore, applyMiddleware } from 'redux';
11-
import { createRouter, navigate } from 'redux-simple-router';
21+
import { createRouter, navigate } from 'redux-url';
1222

1323
const routes = {
14-
'/': 'HOME', // when url is matched, will dispatch an action of type home, payload is the matched result
24+
'/': 'HOME', // when url is matched, will dispatch an action of type 'HOME', the payload is the matched result
1525
'/todos/:id': ({ id }) => ({ type: 'CHANGE_TODO', payload: id }), // you can also pass a function to custom the action, the matched result will be passed in
1626
'*': 'NOT_FOUND'
1727
};
@@ -24,5 +34,39 @@ const store = createStore(
2434

2535
store.dispatch(navigate(location.pathname, false)); // for state to be restored from URL when refreshed
2636

27-
store.dispatch(navigate('/todos/123')); // change route
37+
store.dispatch(navigate('/todos/123')); // navigate to '/todos/123'
2838
```
39+
40+
## API
41+
42+
* `createRouter(routes, history)`:
43+
44+
creates the middleware
45+
- arguments
46+
* routes (*object*) : URL patterns to be mapped, where values can be:
47+
* string: when matched, an action will be dispatched of which the is the given string, the payload will be the matched result
48+
49+
* function: a function that takes the matched result and returns a
50+
51+
* history: the history object created from lib [`history`](https://github.com/ReactTraining/history),
52+
such as `createBrowserHistory`
53+
54+
- returns
55+
56+
the middleware
57+
58+
* `navigate(path: string, replace: *boolean* = false)`:
59+
60+
creates an action for going to the path, `replace` indicates whether it should modify the current history entry rather than push a new one
61+
62+
* `goBack()`:
63+
64+
creates an action for going back
65+
66+
* `goForward()`:
67+
68+
creates an action for going forward
69+
70+
* `go(n)`:
71+
72+
creates an action for going n (can be negative) steps

karma.conf.js

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
module.exports = function(config) {
2+
config.set({
3+
basePath: '.',
4+
singleRun: false,
5+
browsers: ['PhantomJS'],
6+
frameworks: ['mocha', 'chai'],
7+
files: [
8+
'node_modules/babel-polyfill/dist/polyfill.js',
9+
{
10+
pattern: 'tests/*.spec.js',
11+
watched: false,
12+
served: true,
13+
included: true
14+
}
15+
],
16+
preprocessors: {
17+
'tests/*.js': ['webpack'],
18+
'src/*.js': ['webpack']
19+
},
20+
webpack: {
21+
module: {
22+
loaders: [
23+
{
24+
test: /\.js$/,
25+
loader: 'babel-loader',
26+
query: {
27+
presets: ['es2015'],
28+
plugins: [
29+
'transform-object-rest-spread',
30+
'transform-class-properties',
31+
'transform-flow-strip-types'
32+
]
33+
}
34+
}
35+
]
36+
},
37+
externals: {
38+
'react/addons': true,
39+
'react/lib/ExecutionEnvironment': true,
40+
'react/lib/ReactContext': true
41+
}
42+
},
43+
webpackMiddleware: {
44+
noInfo: false,
45+
stats: {
46+
chunks: false
47+
}
48+
},
49+
plugins: [
50+
'karma-webpack',
51+
'karma-mocha',
52+
'karma-chai',
53+
'karma-phantomjs-launcher'
54+
]
55+
});
56+
};

package.json

+30-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,27 @@
11
{
2-
"name": "redux-simple-router",
2+
"name": "redux-url",
33
"version": "1.0.0",
44
"main": "dist/index.js",
55
"repository": {},
66
"license": "MIT",
77
"scripts": {
88
"flow": "flow check src",
99
"lint": "eslint src",
10-
"build": "webpack"
10+
"build": "webpack",
11+
"test": "karma start"
12+
},
13+
"repository": {
14+
"type": "git",
15+
"url": "https://github.com/jedirandy/redux-url.git"
16+
},
17+
"keywords": [
18+
"redux",
19+
"redux-middleware",
20+
"middleware",
21+
"url"
22+
],
23+
"dependencies": {
24+
"url-pattern": "^1.0.3"
1125
},
1226
"devDependencies": {
1327
"babel-core": "^6.23.1",
@@ -16,11 +30,24 @@
1630
"babel-plugin-transform-class-properties": "^6.23.0",
1731
"babel-plugin-transform-flow-strip-types": "^6.22.0",
1832
"babel-plugin-transform-object-rest-spread": "^6.23.0",
33+
"babel-polyfill": "^6.23.0",
1934
"babel-preset-es2015": "^6.22.0",
35+
"chai": "^3.5.0",
2036
"eslint": "^3.16.0",
2137
"eslint-plugin-react": "^6.10.0",
2238
"flow-bin": "^0.39.0",
23-
"url-pattern": "^1.0.3",
39+
"history": "^4.5.1",
40+
"karma": "^1.5.0",
41+
"karma-chai": "^0.1.0",
42+
"karma-mocha": "^1.3.0",
43+
"karma-phantomjs-launcher": "^1.0.2",
44+
"karma-webpack": "^2.0.2",
45+
"mocha": "^3.2.0",
46+
"phantomjs-prebuilt": "^2.1.14",
47+
"redux": "^3.6.0",
2448
"webpack": "^2.2.1"
49+
},
50+
"peerDependencies": {
51+
"history": "^4.0.0"
2552
}
2653
}

src/index.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// @flow
22
import UrlPattern from 'url-pattern';
3+
34
import type {
45
Store,
56
Routes,
@@ -8,7 +9,7 @@ import type {
89
Action
910
} from './types';
1011

11-
const routerType = '@@redux-simple-router';
12+
const routerType = '@@redux-url';
1213

1314
const createRouter = (routes: Routes, history: any) => {
1415
const patterns = Object.keys(routes).reduce((result, route) => ({
@@ -76,5 +77,6 @@ export {
7677
navigate,
7778
goBack,
7879
go,
79-
goForward
80+
goForward,
81+
routerType
8082
};

tests/index.spec.js

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import { createRouter, navigate, goBack, goForward, go } from '../src/';
2+
import { createMemoryHistory } from 'history';
3+
import { createStore, applyMiddleware } from 'redux';
4+
5+
describe('tests', () => {
6+
const routes = {
7+
'/': 'HOME',
8+
'/todos/:id': ({ id }) => ({ type: 'CHANGE_TODO', payload: parseInt(id) }),
9+
'*': 'NOT_FOUND'
10+
}
11+
12+
const reducer = (state = {}, action) => {
13+
switch (action.type) {
14+
case 'HOME':
15+
return {
16+
page: 'HOME'
17+
};
18+
case 'CHANGE_TODO':
19+
return {
20+
page: 'TODO',
21+
id: action.payload
22+
};
23+
case 'NOT_FOUND':
24+
return {
25+
page: 'NOT_FOUND',
26+
url: action.payload._
27+
}
28+
default:
29+
return state;
30+
}
31+
}
32+
33+
let store;
34+
let history;
35+
beforeEach(() => {
36+
history = createMemoryHistory();
37+
const router = createRouter(routes, history);
38+
store = createStore(
39+
reducer,
40+
applyMiddleware(router)
41+
);
42+
});
43+
44+
it('navigate()', () => {
45+
store.dispatch(navigate('/todos/123'));
46+
expect(store.getState()).to.deep.equal({
47+
page: 'TODO',
48+
id: 123
49+
});
50+
});
51+
52+
it('navigate() with replace option on does not push a new state', () => {
53+
expect(history.entries).to.have.length(1);
54+
store.dispatch(navigate('/todos/123', true));
55+
expect(history.entries).to.have.length(1);
56+
});
57+
58+
it('goBack()', () => {
59+
history.push('/1');
60+
expect(history.index).to.equal(1);
61+
store.dispatch(goBack());
62+
expect(history.index).to.equal(0);
63+
});
64+
65+
it('goForward()', () => {
66+
history.push('/1');
67+
expect(history.index).to.equal(1);
68+
history.go(-1);
69+
store.dispatch(goForward());
70+
expect(history.index).to.equal(1);
71+
});
72+
73+
it('go()', () => {
74+
history.push('/1');
75+
history.push('/2');
76+
history.push('/3');
77+
store.dispatch(go(-3));
78+
expect(history.index).to.equal(0);
79+
store.dispatch(go(2));
80+
expect(history.index).to.equal(2);
81+
});
82+
});

webpack.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ module.exports = {
99
output: {
1010
path: path.resolve(__dirname, 'dist'),
1111
filename: '[name].js',
12-
library: 'ReduxSimpleRouter',
12+
library: 'ReduxURL',
1313
libraryTarget: 'umd'
1414
},
1515
module: {

0 commit comments

Comments
 (0)