@mittwald/kubernetes
Version:
Kubernetes client library
45 lines • 1.9 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseLabelSelector = exports.selectorToQueryString = exports.selectorToString = void 0;
function selectorToString(selector, separator = ";") {
const parts = Object.entries(selector).map(([label, value]) => {
if (typeof value === "string") {
return label + "=" + value;
}
return label + " " + value.operator + " (" + value.values.join(",") + ")";
});
return parts.join(separator);
}
exports.selectorToString = selectorToString;
const selectorToQueryString = (selector) => selectorToString(selector, ",");
exports.selectorToQueryString = selectorToQueryString;
const setBasedSelectorRegex = new RegExp("([^() ]*) +(in|notin) +\\((.*)\\)", "i");
/**
* Parse a Label Selector string to a Selector Object.
* Label Selectors are described in the [Kubernetes documentation](https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/#label-selectors).
*
* Uses `;` as a separator for multiple expressions in the string.
*/
function parseLabelSelector(input) {
const selector = {};
for (const item of input.split(";")) {
const regexResult = setBasedSelectorRegex.exec(item);
if (regexResult !== null) {
selector[regexResult[1]] = {
operator: regexResult[2],
values: regexResult[3].split(",").map((v) => v.trim()),
};
}
else {
const [key, operator, ...values] = item.split(/(=|!=|==)/);
const trimmedValues = values.map((v) => v.trim());
selector[key.trim()] =
operator === "="
? trimmedValues.join("")
: { operator: operator, values: trimmedValues };
}
}
return selector;
}
exports.parseLabelSelector = parseLabelSelector;
//# sourceMappingURL=label.js.map