import * as R from 'ramda'; import {split} from '@ywfe/utils'; import { HistoryState } from '../router.interface'; import * as CONTANTS from '../contants'; import {matchParams} from './matchParams'; export const reduceUrl = (url: string): HistoryState => { if (url === '/') { return { title: CONTANTS.DEFAULT_TITLE, url: '/', path: '/', search: '', params: {} }; } const urls = split('?', url); const newUrl = R.dropLastWhile(x => x === '/', urls[0] as any) as any; const total = R.match(/\//g, newUrl).length; const search = urls[1] || ''; if(total === 1) { return { title: CONTANTS.DEFAULT_TITLE, url, path: newUrl, search: '', params: {} }; } const params = matchParams(newUrl, '/:moduleName/:moduleId/:menu?'); const {moduleId, moduleName, menu} = params; const res: HistoryState = { params: { moduleName, moduleId, menu, }, url, path: newUrl, parentPath: `/${moduleName}/${moduleId}`, search: search ? `?${search}` : '' }; return res; };