658f9ffa1eae5d55055bcc6f25218a620f54e40ac03e010d90dc2963699d89322610434c959ea5a49837c7e1d54a83d63be067c98b549a0d82ba0ac125fee7 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. /* @flow */
  2. import { warn } from './warn'
  3. import Regexp from 'path-to-regexp'
  4. // $flow-disable-line
  5. const regexpCompileCache: {
  6. [key: string]: Function
  7. } = Object.create(null)
  8. export function fillParams (
  9. path: string,
  10. params: ?Object,
  11. routeMsg: string
  12. ): string {
  13. params = params || {}
  14. try {
  15. const filler =
  16. regexpCompileCache[path] ||
  17. (regexpCompileCache[path] = Regexp.compile(path))
  18. // Fix #2505 resolving asterisk routes { name: 'not-found', params: { pathMatch: '/not-found' }}
  19. // and fix #3106 so that you can work with location descriptor object having params.pathMatch equal to empty string
  20. if (typeof params.pathMatch === 'string') params[0] = params.pathMatch
  21. return filler(params, { pretty: true })
  22. } catch (e) {
  23. if (process.env.NODE_ENV !== 'production') {
  24. // Fix #3072 no warn if `pathMatch` is string
  25. warn(typeof params.pathMatch === 'string', `missing param for ${routeMsg}: ${e.message}`)
  26. }
  27. return ''
  28. } finally {
  29. // delete the 0 if it was added
  30. delete params[0]
  31. }
  32. }