Skip to content

Commit

Permalink
Move codes
Browse files Browse the repository at this point in the history
  • Loading branch information
kuu12 committed Aug 26, 2018
1 parent 1987516 commit 2f58b43
Show file tree
Hide file tree
Showing 13 changed files with 83 additions and 92 deletions.
10 changes: 5 additions & 5 deletions README.cn.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ ReactDOM.render(

## Props传递
*Less Router* 保留数个props
- **传给已包装的组件:** `path` `title` `parentPath` `autoCache` `NotFound`
- **传给已包装的组件:** `path` `title` `parentPath` `autoCache` `notFound`
- **注入到原始组件:** `router` `path` `pathname` 以及 [URL参数](#基本使用及url参数)

其他props会直接传给原始组件:
Expand Down Expand Up @@ -211,18 +211,18 @@ export default Routing(Child);
## 404页面
```javascript
<ComponentRoute
NotFound
notFound
title="未找到该路径"
/>
```
`NotFound`支持动态路由,可以使该组件只在某个路径下时才触发
`notFound`支持动态路由,可以使该组件只在某个路径下时才触发
```javascript
import Routing from 'less-router';
import ChildRoute from './child';
const Parent = ({ path }) => (
<div>
<ChildRoute
NotFound
notFound
title="未找到该路径"
parentPath={path}
/>
Expand Down Expand Up @@ -271,7 +271,7 @@ Wrapped Component settings.
- parentPath
- autoFocus
- basename
- NotFound
- notFound

### Props injected to Origin Component

Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ When using `props.router.push(pathname)` or `props.router.replace(pathname)`, ju

## Props
*Less Router* reserve serveral props:
- **Input to wrapped `ComponentRoute`:** `path` `title` `parentPath` `autoCache` `NotFound`
- **Input to wrapped `ComponentRoute`:** `path` `title` `parentPath` `autoCache` `notFound`
- **Inject to origin `Component`:** `router` `path` `pathname` and [URL parameters](#basic-and-url-parameters)

Other props will straightly pass into origin `Component`:
Expand Down Expand Up @@ -198,18 +198,18 @@ See previous section [Matching Rules](#matching-rules)
## Not Found
```javascript
<ComponentRoute
NotFound
notFound
title="Not Found"
/>
```
`NotFound` also supports dynamic routing.
`notFound` also supports dynamic routing.
```javascript
import Routing from 'less-router';
import ChildRoute from './child';
const Parent = ({ path }) => (
<div>
<ChildRoute
NotFound
notFound
title="Not Found"
parentPath={path}
/>
Expand Down Expand Up @@ -258,7 +258,7 @@ Wrapped Component settings.
- parentPath
- autoFocus
- basename
- NotFound
- notFound

### Props injected to Origin Component

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "less-router",
"version": "1.7.0",
"version": "1.7.1",
"description": "A very easy React router component but full functionally.",
"main": "src/index.jsx",
"scripts": {
Expand Down
13 changes: 0 additions & 13 deletions src/helper.js

This file was deleted.

17 changes: 7 additions & 10 deletions src/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import matching from './path/match';
import { ROOT } from './message';
import { paramsFrom as params } from './path/regex';

const Routing = arg => {
function Routing(arg) {
switch (typeof arg) {
case 'function':
return props => {
return function (props) {
let Container;
if (!proxy.router)
Container = Router;
Expand All @@ -28,15 +28,12 @@ const Routing = arg => {
return <Container Component={arg} {...props} />;
};

case 'object': {
if (!proxy.router) {
throw new Error(ROOT);
}
const { children } = arg;
return <OneOf>{children}</OneOf>;
}
case 'object':
if (!proxy.router) throw new Error(ROOT);

return <OneOf>{arg.children}</OneOf>;
}
};
}

export default Routing;
export {
Expand Down
46 changes: 46 additions & 0 deletions src/path/helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
export function cacheable(func) {
const cache = {};
return function (arg) {
if (!(arg in cache))
cache[arg] = func(arg);
return cache[arg];
};
}

const join = (...paths) => {
let fullPath = paths
.filter(Boolean)
.filter(path => '/' !== path)
.map(addHeadRemoveTail)
.join('');

const last = paths[paths.length - 1];
if (last && last.endsWith('/'))
fullPath += '/';

return fullPath || '/';
};

const addHeadRemoveTail = path => path
.replace(/\/+$/, '')
.replace(/^(?=[^/])/, '/');

const separate = origin => {
let pathname;
let search;
const index = origin.indexOf('?');
if (index === -1) {
pathname = origin;
search = '';
} else {
pathname = origin.slice(0, index);
search = origin.slice(index);
}
return { pathname, search };
};

export {
join,
addHeadRemoveTail,
separate,
};
2 changes: 1 addition & 1 deletion src/path/match.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import proxy from '../proxy';
import { join } from './path';
import { join } from './helper';
import { regexFrom } from './regex';

export default (parentPath, path, pathname) => {
Expand Down
32 changes: 0 additions & 32 deletions src/path/path.js

This file was deleted.

17 changes: 0 additions & 17 deletions src/path/query.js

This file was deleted.

3 changes: 1 addition & 2 deletions src/path/regex.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import proxy from '../proxy';
import { cacheable } from '../helper';
import { join } from './path';
import { cacheable, join } from './helper';

const PARAMS = /:[\w-~]+(?=\/|$)/g;
const PARAMS_0 = '[\\w-~]+';
Expand Down
18 changes: 15 additions & 3 deletions src/router.jsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import React from 'react';
import proxy from './proxy';
import { locationState, addHeadRemoveTail } from './path/path';
import { addHeadRemoveTail, separate } from './path/helper';
import { regexFrom } from './path/regex';
import { separate } from './path/query';
import { promiseAndCallback } from './helper';
import { PATH_START, PATH_NOT_FOUND } from './message';

class Router extends React.Component {
Expand Down Expand Up @@ -97,6 +95,20 @@ class Router extends React.Component {
}
}

const locationState = (basename) => ({
pathname: decodeURIComponent(
location.pathname.replace(
new RegExp(`^${basename}`), ''
) || '/'
),
search: location.search,
});

const promiseAndCallback = (exec, callback) =>
typeof window.Promise === 'function'
? new window.Promise(exec).then(callback)
: exec(callback);

const queue = [];
window.addEventListener('popstate', () => {
if (!proxy.router) return;
Expand Down
3 changes: 1 addition & 2 deletions test/cache.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ describe('route change', () => {
parent.style.display
).toBe('none');
expect(document.getElementById('aaa'))
.toBeInstanceOf(HTMLElement);
.toBeInstanceOf(HTMLElement);
expect(document.getElementById('bbb'))
.toBeInstanceOf(HTMLElement);
expect(count).toBe(1);
Expand Down Expand Up @@ -128,7 +128,6 @@ describe('clear cache', () => {
console.log(JSON.stringify(proxy.router.cache));
console.log(JSON.stringify(proxy.router.registeredRoutes));
console.log('\n');
console.log(proxy.router.props.Component);
expect(count).toBe(2);
});
});
2 changes: 1 addition & 1 deletion test/path.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { join } from '../src/path/path';
import { join } from '../src/path/helper';
import { regexFrom, paramsFrom } from '../src/path/regex';

describe('join', () => {
Expand Down

0 comments on commit 2f58b43

Please sign in to comment.