diff --git a/README.cn.md b/README.cn.md index 298fef7..59ca077 100644 --- a/README.cn.md +++ b/README.cn.md @@ -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会直接传给原始组件: @@ -211,18 +211,18 @@ export default Routing(Child); ## 404页面 ```javascript ``` -`NotFound`支持动态路由,可以使该组件只在某个路径下时才触发 +`notFound`支持动态路由,可以使该组件只在某个路径下时才触发 ```javascript import Routing from 'less-router'; import ChildRoute from './child'; const Parent = ({ path }) => (
@@ -271,7 +271,7 @@ Wrapped Component settings. - parentPath - autoFocus - basename -- NotFound +- notFound ### Props injected to Origin Component diff --git a/README.md b/README.md index c279746..d0ec7ce 100644 --- a/README.md +++ b/README.md @@ -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`: @@ -198,18 +198,18 @@ See previous section [Matching Rules](#matching-rules) ## Not Found ```javascript ``` -`NotFound` also supports dynamic routing. +`notFound` also supports dynamic routing. ```javascript import Routing from 'less-router'; import ChildRoute from './child'; const Parent = ({ path }) => (
@@ -258,7 +258,7 @@ Wrapped Component settings. - parentPath - autoFocus - basename -- NotFound +- notFound ### Props injected to Origin Component diff --git a/package.json b/package.json index ac9df16..af3758d 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/src/helper.js b/src/helper.js deleted file mode 100644 index 6ba01fb..0000000 --- a/src/helper.js +++ /dev/null @@ -1,13 +0,0 @@ -export const promiseAndCallback = (exec, callback) => - typeof window.Promise === 'function' - ? new window.Promise(exec).then(callback) - : exec(callback); - -export function cacheable(func) { - const cache = {}; - return function (arg) { - if (!(arg in cache)) - cache[arg] = func(arg); - return cache[arg]; - }; -} diff --git a/src/index.jsx b/src/index.jsx index 551206b..4198ebc 100644 --- a/src/index.jsx +++ b/src/index.jsx @@ -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; @@ -28,15 +28,12 @@ const Routing = arg => { return ; }; - case 'object': { - if (!proxy.router) { - throw new Error(ROOT); - } - const { children } = arg; - return {children}; - } + case 'object': + if (!proxy.router) throw new Error(ROOT); + + return {arg.children}; } -}; +} export default Routing; export { diff --git a/src/path/helper.js b/src/path/helper.js new file mode 100644 index 0000000..470526b --- /dev/null +++ b/src/path/helper.js @@ -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, +}; diff --git a/src/path/match.js b/src/path/match.js index b23e4e1..3a1666a 100644 --- a/src/path/match.js +++ b/src/path/match.js @@ -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) => { diff --git a/src/path/path.js b/src/path/path.js deleted file mode 100644 index ec575c3..0000000 --- a/src/path/path.js +++ /dev/null @@ -1,32 +0,0 @@ -const locationState = (basename) => ({ - pathname: decodeURIComponent( - location.pathname.replace( - new RegExp(`^${basename}`), '' - ) || '/' - ), - search: location.search, -}); - -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(/^(?=[^/])/, '/'); - -export { - locationState, - join, - addHeadRemoveTail, -}; diff --git a/src/path/query.js b/src/path/query.js deleted file mode 100644 index e6809bb..0000000 --- a/src/path/query.js +++ /dev/null @@ -1,17 +0,0 @@ -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 { - separate, -}; diff --git a/src/path/regex.js b/src/path/regex.js index 2cc6198..cda8abe 100644 --- a/src/path/regex.js +++ b/src/path/regex.js @@ -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-~]+'; diff --git a/src/router.jsx b/src/router.jsx index 0bc5e87..78ba62d 100644 --- a/src/router.jsx +++ b/src/router.jsx @@ -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 { @@ -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; diff --git a/test/cache.test.js b/test/cache.test.js index a2199c1..7ace449 100644 --- a/test/cache.test.js +++ b/test/cache.test.js @@ -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); @@ -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); }); }); \ No newline at end of file diff --git a/test/path.test.js b/test/path.test.js index 1d9a93b..7972a2c 100644 --- a/test/path.test.js +++ b/test/path.test.js @@ -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', () => {