abi.js
Version:
[![typescript-icon]][typescript-link] [![license-icon]][license-link] [![status-icon]][status-link] [![ci-icon]][ci-link] [![twitter-icon]][twitter-link]
81 lines (79 loc) • 2.52 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/matcher.ts
var matcher_exports = {};
__export(matcher_exports, {
match: () => match
});
module.exports = __toCommonJS(matcher_exports);
function match(subject, pattern) {
const _subject = normalize(subject);
let _pattern = normalize(pattern);
if (_pattern === _subject) {
return {};
}
const params = [];
const re = /\:([a-z]+)(?:\s*\<\s*(num(?:ber)|str(?:ing))\s*\>)?(?:\s*\=\s*(\w+))?/;
_pattern = _pattern.replace("(", "(?:");
while (re.test(_pattern)) {
const m = re.exec(_pattern);
const search = m[0];
const type = m[2] in ["number", "num"] ? "number" : "string";
const value = type === "number" ? Number(m[3]) : m[3];
const param = {
name: m[1],
type,
value
};
params.push(param);
const replace = `(${param.type === "number" ? "[0-9]+" : "[^\\/\\[\\]]+"})${value === void 0 ? "" : "?"}`;
_pattern = _pattern.replace(search, replace);
}
_pattern = _pattern.replace("/", "\\/");
_pattern = `^${_pattern}$`;
const matches = _subject.match(_pattern);
const options = {};
if (matches) {
let i = 0;
for (const param of params) {
let value = matches[i + 1] || param.value;
if (param.type === "number") {
value = Number(value);
}
options[i++] = value;
options[param.name] = value;
}
return options;
}
return false;
}
function normalize(subject) {
let _subject = subject.trim();
if (_subject.startsWith("/")) {
_subject = _subject.substring(1);
}
if (_subject.endsWith("/")) {
_subject = _subject.substring(0, _subject.length - 1);
}
return _subject;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
match
});