compare-path
Version:
An easy-to-use package to detect if two URLs match each other by comparing their abstract paths
76 lines • 3.21 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.comparePath = comparePath;
const clean_path_1 = require("./clean-path");
/**
* The comparePath function returns either a matching tuple [true, params] or [false, null] if the
* URL does not match the shape. The generic signature leverages the ExtractRouteParams type.
*/
function comparePath(shape, path) {
const cleanedShape = (0, clean_path_1.cleanPath)(shape);
const cleanedPath = (0, clean_path_1.cleanPath)(path);
const shapeParts = cleanedShape.split('/');
const pathParts = cleanedPath.split('/');
let params = {};
// Check if shape contains a wildcard segment "**"
const wildcardIndex = shapeParts.indexOf('**');
if (wildcardIndex === -1) {
// No wildcard → the segments must match in count.
if (shapeParts.length !== pathParts.length)
return [false, null];
for (let i = 0; i < shapeParts.length; i++) {
const shapeSegment = shapeParts[i];
const pathSegment = pathParts[i];
if (shapeSegment.startsWith(':')) {
// Parameter segment: capture it.
const paramName = shapeSegment.slice(1);
params[paramName] = pathSegment;
}
else if (shapeSegment !== pathSegment) {
// Static segment mismatch.
return [false, null];
}
}
return [true, params];
}
else {
// Wildcard exists.
// Split into pre-wildcard and post-wildcard segments.
const preParts = shapeParts.slice(0, wildcardIndex);
const postParts = shapeParts.slice(wildcardIndex + 1);
// The URL must have at least as many segments as preParts + postParts.
if (pathParts.length < preParts.length + postParts.length) {
return [false, null];
}
// Match the pre-wildcard segments.
for (let i = 0; i < preParts.length; i++) {
const shapeSegment = preParts[i];
const pathSegment = pathParts[i];
if (shapeSegment.startsWith(':')) {
const paramName = shapeSegment.slice(1);
params[paramName] = pathSegment;
}
else if (shapeSegment !== pathSegment) {
return [false, null];
}
}
// Match the post-wildcard segments from the end.
for (let i = 0; i < postParts.length; i++) {
const shapeSegment = postParts[postParts.length - 1 - i];
const pathSegment = pathParts[pathParts.length - 1 - i];
if (shapeSegment.startsWith(':')) {
const paramName = shapeSegment.slice(1);
params[paramName] = pathSegment;
}
else if (shapeSegment !== pathSegment) {
return [false, null];
}
}
// The wildcard ("**") captures the middle segments.
const restStart = preParts.length;
const restEnd = pathParts.length - postParts.length;
params['rest'] = pathParts.slice(restStart, restEnd);
return [true, params];
}
}
//# sourceMappingURL=compare-path.js.map