Rocky_Mountain_Vending/.pnpm-store/v10/files/a8/5b74f19a414b33d45e41a9b647fdb035b64c22a7c1491221879e639342a89e3f6e36f6164c1bc0433a0fec356e6f3e522a9e94515296138faefd33f3162fca
DMleadgen 46d973904b
Initial commit: Rocky Mountain Vending website
Next.js website for Rocky Mountain Vending company featuring:
- Product catalog with Stripe integration
- Service areas and parts pages
- Admin dashboard with Clerk authentication
- SEO optimized pages with JSON-LD structured data

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-02-12 16:22:15 -07:00

55 lines
1.5 KiB
Text

/**
* @param pathname A pathname string, such as `/foo` or `/foo/bar`
* @param routingRule The routing rule, such as `/foo/*`
* @returns True if pathname matches the routing rule
*
* / -> /
* /* -> /*
* /foo -> /foo
* /foo* -> /foo, /foo-bar, /foo/*
* /foo/* -> /foo, /foo/bar
*/
export function isRoutingRuleMatch(
pathname: string,
routingRule: string
): boolean {
// sanity checks
if (!pathname) {
throw new Error("Pathname is undefined.");
}
if (!routingRule) {
throw new Error("Routing rule is undefined.");
}
const ruleRegExp = transformRoutingRuleToRegExp(routingRule);
return pathname.match(ruleRegExp) !== null;
}
function transformRoutingRuleToRegExp(rule: string): RegExp {
let transformedRule;
if (rule === "/" || rule === "/*") {
transformedRule = rule;
} else if (rule.endsWith("/*")) {
// make `/*` an optional group so we can match both /foo/* and /foo
// /foo/* => /foo(/*)?
transformedRule = `${rule.substring(0, rule.length - 2)}(/*)?`;
} else if (rule.endsWith("/")) {
// make `/` an optional group so we can match both /foo/ and /foo
// /foo/ => /foo(/)?
transformedRule = `${rule.substring(0, rule.length - 1)}(/)?`;
} else if (rule.endsWith("*")) {
transformedRule = rule;
} else {
transformedRule = `${rule}(/)?`;
}
// /foo* => /foo.* => ^/foo.*$
// /*.* => /*\.* => /.*\..* => ^/.*\..*$
transformedRule = `^${transformedRule
.replaceAll(/\./g, "\\.")
.replaceAll(/\*/g, ".*")}$`;
// ^/foo.*$ => /^\/foo.*$/
return new RegExp(transformedRule);
}