deef-router
Advanced tools
Comparing version 0.0.1 to 0.1.0
@@ -7,2 +7,7 @@ 'use strict'; | ||
var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }(); /** | ||
* Created by baidu on 17/6/27. | ||
*/ | ||
var _isString = require('lodash/isString'); | ||
@@ -30,19 +35,27 @@ | ||
var ruleList = []; /** | ||
* Created by baidu on 17/6/27. | ||
*/ | ||
var ruleList = []; | ||
var on = function on(_ref) { | ||
var history = _ref.history; | ||
return function (rule, _ref2) { | ||
var history = _ref.history, | ||
_ref$compareMatchKeys = _ref.compareMatchKeys, | ||
compareMatchKeys = _ref$compareMatchKeys === undefined ? ['path', 'params', 'url'] : _ref$compareMatchKeys; | ||
return function (rule, _ref2, key) { | ||
var onMatch = _ref2.onMatch, | ||
onMiss = _ref2.onMiss, | ||
key = _ref2.key; | ||
onBreakMatch = _ref2.onBreakMatch; | ||
// 一个rule只生效一次 | ||
var ruleKey = (0, _isString2.default)(rule) ? rule : rule.path && JSON.stringify(rule) || ((0, _isString2.default)(key) ? key : ''); | ||
var ruleKey = (0, _isString2.default)(key) && key; | ||
if (!ruleKey) { | ||
throw new Error('When (rule || rule.path) is not string must declare a globally unique `key` prop in the 2nd param'); | ||
if ((0, _isString2.default)(rule)) { | ||
ruleKey = rule; | ||
} else if (rule.path) { | ||
ruleKey = JSON.stringify(rule); | ||
} else if ((0, _isString2.default)(rule.pathname) || !(0, _isFunction2.default)(rule.search)) { | ||
ruleKey = '' + rule.pathname + getSearchKey(rule.search); | ||
} | ||
} | ||
if (!ruleKey) { | ||
throw new Error('When rule is function must pass a globally unique `key` in the 3nd param'); | ||
} | ||
if (ruleList.some(function (item) { | ||
@@ -58,3 +71,3 @@ return item.ruleKey === ruleKey; | ||
ruleKey: ruleKey, | ||
callbacks: { onMatch: onMatch, onMiss: onMiss } | ||
callbacks: { onMatch: onMatch, onBreakMatch: onBreakMatch } | ||
}); | ||
@@ -71,12 +84,13 @@ onHistoryChange(history.location); | ||
var rule = item.rule, | ||
ruleKey = item.ruleKey, | ||
_item$callbacks = item.callbacks, | ||
onMatch = _item$callbacks.onMatch, | ||
onMiss = _item$callbacks.onMiss; | ||
onBreakMatch = _item$callbacks.onBreakMatch; | ||
var match = matchRule(location, rule); | ||
if (match && !(0, _isEqual2.default)((0, _pick2.default)(item.lastMatch, ['path', 'params', 'url']), (0, _pick2.default)(match, ['path', 'params', 'url']))) { | ||
if (match && !(0, _isEqual2.default)((0, _pick2.default)(item.lastMatch, compareMatchKeys), (0, _pick2.default)(match, compareMatchKeys))) { | ||
onMatch && execList.push({ ruleKey: ruleKey, onMatch: onMatch.bind(null, match, item.lastMatch) }); | ||
item.lastMatch = match; | ||
} else if (!match && item.lastMatch !== null) { | ||
onMiss && execList.push({ ruleKey: ruleKey, onMiss: onMiss.bind(null, item.lastMatch) }); | ||
onBreakMatch && execList.push({ ruleKey: ruleKey, onBreakMatch: onBreakMatch.bind(null, item.lastMatch) }); | ||
item.lastMatch = null; | ||
@@ -86,5 +100,5 @@ } | ||
}); | ||
// 先执行onMiss,执行顺序按注册顺序从后到早 | ||
execList.reverse().forEach(function (item) { | ||
item.onMiss && item.onMiss(); | ||
// 先执行onBreakMatch,执行顺序按注册顺序从后到早 | ||
[].concat(execList).reverse().forEach(function (item) { | ||
item.onBreakMatch && item.onBreakMatch(); | ||
}); | ||
@@ -108,2 +122,24 @@ // 再执行onMatch | ||
function matchRule(location, rule) { | ||
if ((0, _isString2.default)(rule.pathname) || rule.search && !(0, _isFunction2.default)(rule.search)) { | ||
var ret = { | ||
path: rule.pathname, | ||
params: {} | ||
}; | ||
if (rule.pathname && rule.pathname !== location.pathname) { | ||
ret = null; | ||
} | ||
if (ret && rule.search) { | ||
var searchRule = (0, _isString2.default)(rule.search) ? parseSearch(rule.search) : rule.search; | ||
var searchParams = parseSearch(location.search); | ||
Object.keys(searchRule).every(function (key) { | ||
var valRule = searchRule[key]; | ||
if ((0, _isString2.default)(valRule) && valRule !== searchParams[key] || (0, _isFunction2.default)(valRule) && !valRule(searchParams[key])) { | ||
return false; | ||
} | ||
ret.params[key] = searchParams[key]; | ||
return true; | ||
}) || (ret = null); | ||
} | ||
return ret; | ||
} | ||
if ((0, _isString2.default)(rule) || (0, _isString2.default)(rule.path)) { | ||
@@ -115,2 +151,19 @@ return (0, _matchPath2.default)(location.pathname || location.path, rule); | ||
} | ||
} | ||
function parseSearch(str) { | ||
var ret = {}; | ||
str.replace(/(^\?|&$)/g, '').split('&').forEach(function (item) { | ||
var _item$split = item.split('='), | ||
_item$split2 = _slicedToArray(_item$split, 2), | ||
key = _item$split2[0], | ||
value = _item$split2[1]; | ||
ret[key] = value; | ||
}); | ||
return ret; | ||
} | ||
function getSearchKey(val) { | ||
return val && '?' + ((0, _isString2.default)(val) ? val.replace(/(^\?|&$)/g, '') : Object.keys(val).join('&')); | ||
} |
@@ -0,0 +0,0 @@ 'use strict'; |
{ | ||
"name": "deef-router", | ||
"version": "0.0.1", | ||
"version": "0.1.0", | ||
"description": "deef router", | ||
@@ -5,0 +5,0 @@ "main": "./lib/index.js", |
@@ -13,8 +13,20 @@ /** | ||
const on = ({history}) => (rule, {onMatch, onMiss, key}) => { | ||
const on = ({history, compareMatchKeys = ['path', 'params', 'url']}) => (rule, {onMatch, onBreakMatch}, key) => { | ||
// 一个rule只生效一次 | ||
const ruleKey = isString(rule) ? rule : (rule.path && JSON.stringify(rule) || (isString(key) ? key : '')); | ||
let ruleKey = isString(key) && key; | ||
if (!ruleKey) { | ||
throw new Error('When (rule || rule.path) is not string must declare a globally unique `key` prop in the 2nd param'); | ||
if (isString(rule)) { | ||
ruleKey = rule; | ||
} | ||
else if (rule.path) { | ||
ruleKey = JSON.stringify(rule); | ||
} | ||
else if (isString(rule.pathname) || !isFunction(rule.search)) { | ||
ruleKey = `${rule.pathname}${getSearchKey(rule.search)}`; | ||
} | ||
} | ||
if (!ruleKey) { | ||
throw new Error('When rule is function must pass a globally unique `key` in the 3nd param'); | ||
} | ||
if (ruleList.some(item => item.ruleKey === ruleKey)) { | ||
@@ -28,3 +40,3 @@ return; | ||
ruleKey, | ||
callbacks: {onMatch, onMiss} | ||
callbacks: {onMatch, onBreakMatch} | ||
}); | ||
@@ -40,5 +52,5 @@ onHistoryChange(history.location); | ||
ruleList.forEach(item => { | ||
const {rule, callbacks: {onMatch, onMiss}} = item; | ||
const {rule, ruleKey, callbacks: {onMatch, onBreakMatch}} = item; | ||
const match = matchRule(location, rule); | ||
if (match && !isEqual(pick(item.lastMatch, ['path', 'params', 'url']), pick(match, ['path', 'params', 'url']))) { | ||
if (match && !isEqual(pick(item.lastMatch, compareMatchKeys), pick(match, compareMatchKeys))) { | ||
onMatch && execList.push({ruleKey, onMatch: onMatch.bind(null, match, item.lastMatch)}); | ||
@@ -48,3 +60,3 @@ item.lastMatch = match; | ||
else if (!match && item.lastMatch !== null) { | ||
onMiss && execList.push({ruleKey, onMiss: onMiss.bind(null, item.lastMatch)}); | ||
onBreakMatch && execList.push({ruleKey, onBreakMatch: onBreakMatch.bind(null, item.lastMatch)}); | ||
item.lastMatch = null; | ||
@@ -54,5 +66,5 @@ } | ||
}); | ||
// 先执行onMiss,执行顺序按注册顺序从后到早 | ||
execList.reverse().forEach(item => { | ||
item.onMiss && item.onMiss(); | ||
// 先执行onBreakMatch,执行顺序按注册顺序从后到早 | ||
[...execList].reverse().forEach(item => { | ||
item.onBreakMatch && item.onBreakMatch(); | ||
}); | ||
@@ -73,2 +85,25 @@ // 再执行onMatch | ||
function matchRule(location, rule) { | ||
if (isString(rule.pathname) || rule.search && !isFunction(rule.search)) { | ||
let ret = { | ||
path: rule.pathname, | ||
params: {} | ||
}; | ||
if (rule.pathname && rule.pathname !== location.pathname) { | ||
ret = null; | ||
} | ||
if (ret && rule.search) { | ||
const searchRule = isString(rule.search) ? parseSearch(rule.search) : rule.search; | ||
const searchParams = parseSearch(location.search); | ||
Object.keys(searchRule).every(key => { | ||
const valRule = searchRule[key]; | ||
if ((isString(valRule) && valRule !== searchParams[key]) | ||
|| (isFunction(valRule) && !valRule(searchParams[key]))) { | ||
return false; | ||
} | ||
ret.params[key] = searchParams[key]; | ||
return true; | ||
}) || (ret = null); | ||
} | ||
return ret; | ||
} | ||
if (isString(rule) || isString(rule.path)) { | ||
@@ -80,2 +115,19 @@ return matchPath(location.pathname || location.path, rule); | ||
} | ||
} | ||
function parseSearch(str) { | ||
const ret = {}; | ||
str.replace(/(^\?|&$)/g, '').split('&').forEach(item => { | ||
const [key, value] = item.split('='); | ||
ret[key] = value; | ||
}); | ||
return ret; | ||
} | ||
function getSearchKey(val) { | ||
return val && '?' + ( | ||
isString(val) | ||
? val.replace(/(^\?|&$)/g, '') | ||
: Object.keys(val).join('&') | ||
); | ||
} |
@@ -0,0 +0,0 @@ /** |
No README
QualityPackage does not have a README. This may indicate a failed publish or a low quality package.
Found 1 instance in 1 package
16501
354
5
2
0