change-string-case/node_modules/@octokit/endpoint/dist-src/util/url-template.js

129 lines
3.9 KiB
JavaScript
Raw Normal View History

2022-11-04 00:55:07 +08:00
function encodeReserved(str) {
2023-10-26 04:27:07 +08:00
return str.split(/(%[0-9A-Fa-f]{2})/g).map(function(part) {
if (!/%[0-9A-Fa-f]/.test(part)) {
part = encodeURI(part).replace(/%5B/g, "[").replace(/%5D/g, "]");
}
return part;
}).join("");
2022-11-04 00:55:07 +08:00
}
function encodeUnreserved(str) {
2023-10-26 04:27:07 +08:00
return encodeURIComponent(str).replace(/[!'()*]/g, function(c) {
return "%" + c.charCodeAt(0).toString(16).toUpperCase();
});
2022-11-04 00:55:07 +08:00
}
function encodeValue(operator, value, key) {
2023-10-26 04:27:07 +08:00
value = operator === "+" || operator === "#" ? encodeReserved(value) : encodeUnreserved(value);
if (key) {
return encodeUnreserved(key) + "=" + value;
} else {
return value;
}
2022-11-04 00:55:07 +08:00
}
function isDefined(value) {
2023-10-26 04:27:07 +08:00
return value !== void 0 && value !== null;
2022-11-04 00:55:07 +08:00
}
function isKeyOperator(operator) {
2023-10-26 04:27:07 +08:00
return operator === ";" || operator === "&" || operator === "?";
2022-11-04 00:55:07 +08:00
}
function getValues(context, operator, key, modifier) {
2023-10-26 04:27:07 +08:00
var value = context[key], result = [];
if (isDefined(value) && value !== "") {
if (typeof value === "string" || typeof value === "number" || typeof value === "boolean") {
value = value.toString();
if (modifier && modifier !== "*") {
value = value.substring(0, parseInt(modifier, 10));
}
result.push(
encodeValue(operator, value, isKeyOperator(operator) ? key : "")
);
} else {
if (modifier === "*") {
if (Array.isArray(value)) {
value.filter(isDefined).forEach(function(value2) {
result.push(
encodeValue(operator, value2, isKeyOperator(operator) ? key : "")
);
});
} else {
Object.keys(value).forEach(function(k) {
if (isDefined(value[k])) {
result.push(encodeValue(operator, value[k], k));
2022-11-04 00:55:07 +08:00
}
2023-10-26 04:27:07 +08:00
});
2022-11-04 00:55:07 +08:00
}
2023-10-26 04:27:07 +08:00
} else {
const tmp = [];
if (Array.isArray(value)) {
value.filter(isDefined).forEach(function(value2) {
tmp.push(encodeValue(operator, value2));
});
} else {
Object.keys(value).forEach(function(k) {
if (isDefined(value[k])) {
tmp.push(encodeUnreserved(k));
tmp.push(encodeValue(operator, value[k].toString()));
2022-11-04 00:55:07 +08:00
}
2023-10-26 04:27:07 +08:00
});
2022-11-04 00:55:07 +08:00
}
2023-10-26 04:27:07 +08:00
if (isKeyOperator(operator)) {
result.push(encodeUnreserved(key) + "=" + tmp.join(","));
} else if (tmp.length !== 0) {
result.push(tmp.join(","));
2022-11-04 00:55:07 +08:00
}
2023-10-26 04:27:07 +08:00
}
2022-11-04 00:55:07 +08:00
}
2023-10-26 04:27:07 +08:00
} else {
if (operator === ";") {
if (isDefined(value)) {
result.push(encodeUnreserved(key));
}
} else if (value === "" && (operator === "&" || operator === "?")) {
result.push(encodeUnreserved(key) + "=");
} else if (value === "") {
result.push("");
}
}
return result;
2022-11-04 00:55:07 +08:00
}
2023-10-26 04:27:07 +08:00
function parseUrl(template) {
return {
expand: expand.bind(null, template)
};
2022-11-04 00:55:07 +08:00
}
function expand(template, context) {
2023-10-26 04:27:07 +08:00
var operators = ["+", "#", ".", "/", ";", "?", "&"];
return template.replace(
/\{([^\{\}]+)\}|([^\{\}]+)/g,
function(_, expression, literal) {
if (expression) {
let operator = "";
const values = [];
if (operators.indexOf(expression.charAt(0)) !== -1) {
operator = expression.charAt(0);
expression = expression.substr(1);
2022-11-04 00:55:07 +08:00
}
2023-10-26 04:27:07 +08:00
expression.split(/,/g).forEach(function(variable) {
var tmp = /([^:\*]*)(?::(\d+)|(\*))?/.exec(variable);
values.push(getValues(context, operator, tmp[1], tmp[2] || tmp[3]));
});
if (operator && operator !== "+") {
var separator = ",";
if (operator === "?") {
separator = "&";
} else if (operator !== "#") {
separator = operator;
}
return (values.length !== 0 ? operator : "") + values.join(separator);
} else {
return values.join(",");
2022-11-04 00:55:07 +08:00
}
2023-10-26 04:27:07 +08:00
} else {
return encodeReserved(literal);
}
}
);
2022-11-04 00:55:07 +08:00
}
2023-10-26 04:27:07 +08:00
export {
parseUrl
};