route-ui-sync 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +1 -37
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +1 -35
- package/dist/index.js.map +1 -1
- package/package.json +2 -1
package/dist/index.cjs
CHANGED
|
@@ -1,38 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
// 一个与 UI 框架无关的"小核心",用于根据当前路由信息计算选中 key。
|
|
4
|
-
/**
|
|
5
|
-
* 根据当前路由信息和一组规则,计算"当前应该选中的 key"
|
|
6
|
-
*
|
|
7
|
-
* - 不依赖 React / react-router / 任何 UI 库
|
|
8
|
-
* - 只关心最基础的 location 对象和规则数组
|
|
9
|
-
* - 未来做成 npm 包时,只需要把这个函数原样搬过去即可
|
|
10
|
-
*/
|
|
11
|
-
function getSelectedKeyFromLocation(location, rules) {
|
|
12
|
-
if (!location || !rules || rules.length === 0) {
|
|
13
|
-
return undefined;
|
|
14
|
-
}
|
|
15
|
-
const { pathname, search = "", hash = "" } = location;
|
|
16
|
-
const safeLocation = { pathname, search, hash };
|
|
17
|
-
// 找出所有命中的规则
|
|
18
|
-
const matched = rules.filter((rule) => {
|
|
19
|
-
try {
|
|
20
|
-
return rule.match(safeLocation);
|
|
21
|
-
}
|
|
22
|
-
catch {
|
|
23
|
-
// 某个规则内部异常时,忽略该规则,避免影响整体
|
|
24
|
-
return false;
|
|
25
|
-
}
|
|
26
|
-
});
|
|
27
|
-
if (matched.length === 0)
|
|
28
|
-
return undefined;
|
|
29
|
-
// 如果只有一个命中,直接返回
|
|
30
|
-
if (matched.length === 1)
|
|
31
|
-
return matched[0].key;
|
|
32
|
-
// 多个命中时,按 priority 从高到低排序,取第一个
|
|
33
|
-
matched.sort((a, b) => (b.priority ?? 0) - (a.priority ?? 0));
|
|
34
|
-
return matched[0].key;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
exports.getSelectedKeyFromLocation = getSelectedKeyFromLocation;
|
|
1
|
+
"use strict";exports.getSelectedKeyFromLocation=function(t,e){if(!t||!e||0===e.length)return;const{pathname:r,search:h="",hash:n=""}=t,a={pathname:r,search:h,hash:n},c=e.filter(t=>{try{return t.match(a)}catch{return!1}});return 0!==c.length?(1===c.length||c.sort((t,e)=>(e.priority??0)-(t.priority??0)),c[0].key):void 0};
|
|
38
2
|
//# sourceMappingURL=index.cjs.map
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","sources":["../src/index.ts"],"sourcesContent":[null],"names":[
|
|
1
|
+
{"version":3,"file":"index.cjs","sources":["../src/index.ts"],"sourcesContent":[null],"names":["location","rules","length","pathname","search","hash","safeLocation","matched","filter","rule","match","sort","a","b","priority","key"],"mappings":"gDA+BM,SACJA,EACAC,GAEA,IAAKD,IAAaC,GAA0B,IAAjBA,EAAMC,OAC/B,OAGF,MAAMC,SAAEA,EAAQC,OAAEA,EAAS,GAAEC,KAAEA,EAAO,IAAOL,EACvCM,EAAe,CAAEH,WAAUC,SAAQC,QAGnCE,EAAUN,EAAMO,OAAQC,IAC5B,IACE,OAAOA,EAAKC,MAAMJ,EACpB,CAAE,MAEA,OAAO,CACT,IAGF,OAAuB,IAAnBC,EAAQL,QAGW,IAAnBK,EAAQL,QAGZK,EAAQI,KAAK,CAACC,EAAGC,KAAOA,EAAEC,UAAY,IAAMF,EAAEE,UAAY,IAHzBP,EAAQ,GAAGQ,UAH5C,CAQF"}
|
package/dist/index.js
CHANGED
|
@@ -1,36 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
/**
|
|
3
|
-
* 根据当前路由信息和一组规则,计算"当前应该选中的 key"
|
|
4
|
-
*
|
|
5
|
-
* - 不依赖 React / react-router / 任何 UI 库
|
|
6
|
-
* - 只关心最基础的 location 对象和规则数组
|
|
7
|
-
* - 未来做成 npm 包时,只需要把这个函数原样搬过去即可
|
|
8
|
-
*/
|
|
9
|
-
function getSelectedKeyFromLocation(location, rules) {
|
|
10
|
-
if (!location || !rules || rules.length === 0) {
|
|
11
|
-
return undefined;
|
|
12
|
-
}
|
|
13
|
-
const { pathname, search = "", hash = "" } = location;
|
|
14
|
-
const safeLocation = { pathname, search, hash };
|
|
15
|
-
// 找出所有命中的规则
|
|
16
|
-
const matched = rules.filter((rule) => {
|
|
17
|
-
try {
|
|
18
|
-
return rule.match(safeLocation);
|
|
19
|
-
}
|
|
20
|
-
catch {
|
|
21
|
-
// 某个规则内部异常时,忽略该规则,避免影响整体
|
|
22
|
-
return false;
|
|
23
|
-
}
|
|
24
|
-
});
|
|
25
|
-
if (matched.length === 0)
|
|
26
|
-
return undefined;
|
|
27
|
-
// 如果只有一个命中,直接返回
|
|
28
|
-
if (matched.length === 1)
|
|
29
|
-
return matched[0].key;
|
|
30
|
-
// 多个命中时,按 priority 从高到低排序,取第一个
|
|
31
|
-
matched.sort((a, b) => (b.priority ?? 0) - (a.priority ?? 0));
|
|
32
|
-
return matched[0].key;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
export { getSelectedKeyFromLocation };
|
|
1
|
+
function t(t,r){if(!t||!r||0===r.length)return;const{pathname:e,search:h="",hash:n=""}=t,a={pathname:e,search:h,hash:n},i=r.filter(t=>{try{return t.match(a)}catch{return!1}});return 0!==i.length?(1===i.length||i.sort((t,r)=>(r.priority??0)-(t.priority??0)),i[0].key):void 0}export{t as getSelectedKeyFromLocation};
|
|
36
2
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":["../src/index.ts"],"sourcesContent":[null],"names":[
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../src/index.ts"],"sourcesContent":[null],"names":["getSelectedKeyFromLocation","location","rules","length","pathname","search","hash","safeLocation","matched","filter","rule","match","sort","a","b","priority","key"],"mappings":"AA+BM,SAAUA,EACdC,EACAC,GAEA,IAAKD,IAAaC,GAA0B,IAAjBA,EAAMC,OAC/B,OAGF,MAAMC,SAAEA,EAAQC,OAAEA,EAAS,GAAEC,KAAEA,EAAO,IAAOL,EACvCM,EAAe,CAAEH,WAAUC,SAAQC,QAGnCE,EAAUN,EAAMO,OAAQC,IAC5B,IACE,OAAOA,EAAKC,MAAMJ,EACpB,CAAE,MAEA,OAAO,CACT,IAGF,OAAuB,IAAnBC,EAAQL,QAGW,IAAnBK,EAAQL,QAGZK,EAAQI,KAAK,CAACC,EAAGC,KAAOA,EAAEC,UAAY,IAAMF,EAAEE,UAAY,IAHzBP,EAAQ,GAAGQ,UAH5C,CAQF"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "route-ui-sync",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"description": "A lightweight, framework-agnostic utility for automatically matching routes to menu keys. Zero dependencies, TypeScript support, for navigation menus.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.cjs",
|
|
@@ -37,6 +37,7 @@
|
|
|
37
37
|
"license": "MIT",
|
|
38
38
|
"devDependencies": {
|
|
39
39
|
"@rollup/plugin-typescript": "^12.1.1",
|
|
40
|
+
"@rollup/plugin-terser": "^0.4.4",
|
|
40
41
|
"@types/node": "^22.7.4",
|
|
41
42
|
"@vitest/coverage-v8": "^4.0.15",
|
|
42
43
|
"rollup": "^4.22.0",
|