Skip to content

Commit

Permalink
publish
Browse files Browse the repository at this point in the history
  • Loading branch information
chenshuai2144 committed May 6, 2020
1 parent 7ceca7d commit 2072306
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 37 deletions.
5 changes: 2 additions & 3 deletions .fatherrc.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { IBundleOptions } from 'father';

const options: IBundleOptions = {
cjs: 'rollup',
esm: 'rollup',
doc: { typescript: true },
cjs: 'babel',
esm: 'babel',
};

export default options;
17 changes: 17 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: Test CI
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest

steps:
- name: checkout
uses: actions/checkout@master

- name: install
run: npm install

- name: lint
run: npm run lint && npm run tsc
- name: test
run: npm run test
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@
/dist
/.docz
/node_modules
es/
lib/
yarn-error.log
12 changes: 11 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,27 @@
"name": "chenshuai2144",
"email": "[email protected]"
},
"files": [
"/lib",
"/es",
"/dist"
],
"repository": "https://github.com/umijs/route-utils",
"scripts": {
"build": "father build",
"test": "umi-test"
"test": "umi-test",
"prepublishOnly": "npm run test && npm run build && np --no-cleanup --yolo --no-publish --any-branch"
},
"devDependencies": {
"@types/jest": "^25.2.1",
"@types/lodash": "^4.14.150",
"father": "^2.16.0",
"typescript": "^3.3.3"
},
"license": "MIT",
"dependencies": {
"np": "^6.2.3",
"path-to-regexp": "2.4.0",
"umi-test": "^1.9.6"
}
}
4 changes: 3 additions & 1 deletion src/getFlatMenus/getFlatMenus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ export const getFlatMenus = (
): {
[key: string]: MenuDataItem;
} => {
let menus = {};
let menus: {
[key: string]: MenuDataItem;
} = {};
menuData.forEach(item => {
if (!item || item.hideInMenu) {
return;
Expand Down
16 changes: 4 additions & 12 deletions src/getMatchMenu/getMatchMenu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,9 @@ export const genKeysToArray = (menuKey: string) => {
return keyArray;
};

export const getMenuMatches = (
flatMenuKeys: string[] = [],
path: string,
): string | undefined =>
export const getMenuMatches = (flatMenuKeys: string[] = [], path: string): string | undefined =>
flatMenuKeys
.filter((item) => {
.filter(item => {
if (item === '/' && path === '/') {
return true;
}
Expand Down Expand Up @@ -63,10 +60,7 @@ export const getMenuMatches = (
* @param menuData
* @returns MenuDataItem[]
*/
export const getMatchMenu = (
pathname: string,
menuData: MenuDataItem[],
): MenuDataItem[] => {
export const getMatchMenu = (pathname: string, menuData: MenuDataItem[]): MenuDataItem[] => {
const flatMenus = getFlatMenu(menuData);
const flatMenuKeys = Object.keys(flatMenus);
const menuPathKey = getMenuMatches(flatMenuKeys, pathname || '/');
Expand All @@ -75,9 +69,7 @@ export const getMatchMenu = (
}
const menuItem = flatMenus[menuPathKey] || { parentKeys: '', key: '' };

const parentItems = (menuItem.parentKeys || [])
.map((key) => flatMenus[key])
.filter((item) => item);
const parentItems = (menuItem.parentKeys || []).map(key => flatMenus[key]).filter(item => item);

if (menuItem.key) {
parentItems.push(menuItem);
Expand Down
32 changes: 12 additions & 20 deletions src/transformRoute/transformRoute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const reg = /(((^https?:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+(?::\d+)?|(
export const isUrl = (path: string): boolean => reg.test(path);

export function guid() {
return 'xxxxxxxx'.replace(/[xy]/g, (c) => {
return 'xxxxxxxx'.replace(/[xy]/g, c => {
// eslint-disable-next-line no-bitwise
const r = (Math.random() * 16) | 0;
// eslint-disable-next-line no-bitwise
Expand All @@ -29,7 +29,10 @@ export const getKeyByPath = (item: MenuDataItem) => {
}
// 如果还是没有,用对象的hash 生成一个
try {
return hash.sha256().update(JSON.stringify(item)).digest('hex');
return hash
.sha256()
.update(JSON.stringify(item))
.digest('hex');
} catch (error) {
// dom some thing
}
Expand All @@ -42,10 +45,7 @@ export const getKeyByPath = (item: MenuDataItem) => {
* @param item
* @param parentName
*/
const getItemLocaleName = (
item: MenuDataItem,
parentName: string,
): string | false => {
const getItemLocaleName = (item: MenuDataItem, parentName: string): string | false => {
const { name, locale } = item;

// 如果配置了 locale 并且 locale 为 false或 ""
Expand Down Expand Up @@ -94,7 +94,7 @@ function formatter(
return [];
}
return data
.filter((item) => {
.filter(item => {
if (!item) return false;
if (item.routes || item.children) return true;
if (item.name && item.path) return true;
Expand Down Expand Up @@ -148,10 +148,7 @@ const memoizeOneFormatter = memoizeOne(formatter, isEqual);
*/
const defaultFilterMenuData = (menuData: MenuDataItem[] = []): MenuDataItem[] =>
menuData
.filter(
(item: MenuDataItem) =>
item && item.name && !item.hideInMenu && !item.redirect,
)
.filter((item: MenuDataItem) => item && item.name && !item.hideInMenu && !item.redirect)
.map((item: MenuDataItem) => {
if (
item.children &&
Expand All @@ -164,19 +161,17 @@ const defaultFilterMenuData = (menuData: MenuDataItem[] = []): MenuDataItem[] =>
}
return { ...item, children: undefined };
})
.filter((item) => item);
.filter(item => item);

/**
* 获取面包屑映射
* @param MenuDataItem[] menuData 菜单配置
*/
const getBreadcrumbNameMap = (
menuData: MenuDataItem[],
): Map<string, MenuDataItem> => {
const getBreadcrumbNameMap = (menuData: MenuDataItem[]): Map<string, MenuDataItem> => {
// Map is used to ensure the order of keys
const routerMap = new Map<string, MenuDataItem>();
const flattenMenuData = (data: MenuDataItem[], parent?: MenuDataItem) => {
data.forEach((menuItem) => {
data.forEach(menuItem => {
if (!menuItem) {
return;
}
Expand All @@ -192,10 +187,7 @@ const getBreadcrumbNameMap = (
return routerMap;
};

const memoizeOneGetBreadcrumbNameMap = memoizeOne(
getBreadcrumbNameMap,
isEqual,
);
const memoizeOneGetBreadcrumbNameMap = memoizeOne(getBreadcrumbNameMap, isEqual);

/**
* @param routes 路由配置
Expand Down
22 changes: 22 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
export interface Route extends MenuDataItem {
routes?: Route[];
}

export interface MessageDescriptor {
id: any;
description?: string;
defaultMessage?: string;
}

export interface MenuDataItem {
children?: MenuDataItem[];
hideChildrenInMenu?: boolean;
hideInMenu?: boolean;
icon?: React.ReactNode;
locale?: string | false;
name?: string;
key?: string;
parentKeys?: string[];
path?: string;
[key: string]: any;
}
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"sourceMap": true,
"baseUrl": ".",
"strict": true,
"skipLibCheck": true,
"paths": {
"@/*": ["src/*"]
},
Expand Down

0 comments on commit 2072306

Please sign in to comment.