UNPKG

dalijs

Version:
34 lines (29 loc) 1.2 kB
import {log} from '../util/util'; import {decorate} from '../util/util'; import {Router} from './Router'; log('RouterConfig.js'); function pathToRegexp(path, keys, sensitive, strict) { if (path instanceof RegExp) return path; if (path instanceof Array) path = '(' + path.join('|') + ')'; path = path .concat(strict ? '' : '/?') .replace(/\/\(/g, '(?:/') .replace(/\+/g, '__plus__') .replace(/(\/)?(\.)?:(\w+)(?:(\(.*?\)))?(\?)?/g, function (_, slash, format, key, capture, optional) { keys.push({name: key, optional: !!optional}); slash = slash || ''; return '' + (optional ? '' : slash) + '(?:' + (optional ? slash : '') + (format || '') + (capture || (format && '([^/.]+?)' || '([^/]+?)')) + ')' + (optional || ''); }) .replace(/([\/.])/g, '\\$1') .replace(/__plus__/g, '(.+)') .replace(/\*/g, '(.*)'); return new RegExp('^' + path + '$', sensitive ? '' : 'i'); } function RouterConfigHandlerDescriptor(target, value) { value.url = value.path; value.path = pathToRegexp(value.path, [], false, false); Router.routes.push({target, value}); } export function RouterConfig(arg) { return decorate(RouterConfigHandlerDescriptor, arg); }