UNPKG

react-enroute

Version:
99 lines (94 loc) 2.51 kB
import { pathToRegexp, compile, match } from 'path-to-regexp'; /** * Generate location based on path and params * @alias loc * genLocation('/users/:id', {id: '42'}) => '/users/42' * * @param {string} path * @param {object?} params * @param {object=} options - [path-to-regexp](https://github.com/pillarjs/path-to-regexp#usage) options * @return {string} */ export function genLocation(path, params, options) { if (!path) return ''; return compile(path, options)(params); } export { genLocation as loc }; /** * Check if location matches path * * isPath('/users/:id', '/users/42') => true * * @param {string} path * @param {string} location * @param {object=} options - [path-to-regexp](https://github.com/pillarjs/path-to-regexp#usage) options * @return {boolean} */ export function isPath(path, location, options) { return !!pathToRegexp(path, null, options).exec(location); } /** * Search path * * findPath(['/users', '/users/:id'], '/users/42') => { * path: '/users/:id, * params: {id: '42'}, * } * * @typedef SearchResult * @property {string} path * @property {object} params * * @param {[string]} paths * @param {string} location * @param {object} options - [path-to-regexp](https://github.com/pillarjs/path-to-regexp#usage) options * @return {SearchResult|undefined} */ export function findPath(paths, location, options) { for (var i = 0; i < paths.length; i++) { var path = paths[i]; var result = match(path, options)(location); if (!result) continue; var params = result.params; return { path: path, params: params }; } } /** * Search over object whose keys are paths * * findPathValue({ * '/users': UserListToolbar, * '/users/:id': UserToolbar, * }, '/users/42') * => { * path: '/users/:id', * value: UserToolbar, * params: {id: '42'}, * } * * @typedef SearchValueResult * @property {string} path * @property {*} value * @property {object} params * * @param {object} obj - keys are paths and values can be any * @param {string} location * @param {object=} options - [path-to-regexp](https://github.com/pillarjs/path-to-regexp#usage) options * @return {SearchValueResult|undefined} */ export function findPathValue(obj, location, options) { for (var path in obj) { var result = match(path, options)(location); if (!result) continue; var value = obj[path]; var params = result.params; return { path: path, value: value, params: params }; } }