UNPKG

@broxus/js-core

Version:

MobX-based JavaScript Core library

31 lines (30 loc) 777 B
import { compile } from 'path-to-regexp'; const cache = {}; const cacheLimit = 10000; let cacheCount = 0; function compilePath(path) { if (cache[path]) { return cache[path]; } const generator = compile(path); if (cacheCount < cacheLimit) { cache[path] = generator; // eslint-disable-next-line no-plusplus cacheCount++; } return generator; } export function generatePath(path = '/', params = {}) { return path === '/' ? path : compilePath(path)(params); } export class Route { path; basePath; constructor(path, basePath) { this.path = path; this.basePath = basePath; } url(params) { return [this.basePath, generatePath(this.path, params)].filter(Boolean).join('/'); } }