UNPKG

@broxus/js-core

Version:

MobX-based JavaScript Core library

36 lines (35 loc) 956 B
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Route = void 0; exports.generatePath = generatePath; const path_to_regexp_1 = require("path-to-regexp"); const cache = {}; const cacheLimit = 10000; let cacheCount = 0; function compilePath(path) { if (cache[path]) { return cache[path]; } const generator = (0, path_to_regexp_1.compile)(path); if (cacheCount < cacheLimit) { cache[path] = generator; // eslint-disable-next-line no-plusplus cacheCount++; } return generator; } function generatePath(path = '/', params = {}) { return path === '/' ? path : compilePath(path)(params); } 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('/'); } } exports.Route = Route;