-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
143 lines (121 loc) · 2.86 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
"use strict";
const hasOwnProperty = Function.call.bind(Object.prototype.hasOwnProperty);
function parseExpression(expr) {
const parts = expr.trim().split(" ");
const key = parts[0];
const operator = parts[1];
const value = parts[2];
const values = (value || "")
.slice(1, -1)
.split(",")
.map(val => val.trim());
switch (operator) {
case undefined:
return key[0] === "!" ? {
operator : "DoesNotExist",
key : key.slice(1)
} : {
operator : "Exists",
key
};
case "=":
case "==":
return {
operator : "=",
key,
value
};
case "!=":
return {
operator : "NotIn",
values : [value],
key
};
case "in":
return {
operator : "In",
key,
values
};
case "notin":
return {
operator : "NotIn",
key,
values
};
default:
}
throw new Error(`Invalid expression: ${expr}`);
}
function parse(labelSelector) {
const expressions = labelSelector
.split(/,(?![^(]*\))/)
.map(parseExpression);
const matchLabels = expressions
.filter(expr => hasOwnProperty(expr, "value"))
.reduce((labels, expr) => Object.assign(labels, {
[expr.key] : expr.value
}), {});
const matchExpressions = expressions
.filter(expr => !hasOwnProperty(expr, "value"));
return { matchLabels, matchExpressions };
}
function stringifyExpression(expr) {
const operator = expr.operator.toLowerCase();
const key = expr.key;
const values = expr.values;
switch (operator) {
case "exists": return key;
case "doesnotexist": return `!${key}`;
default:
}
return `${key} ${operator} (${values.join(",")})`;
}
function stringify(opts) {
const matchLabels = opts.matchLabels || {};
const matchExpressions = opts.matchExpressions || [];
return Object
.keys(matchLabels)
.map(key => `${key} = ${matchLabels[key]}`)
.concat(matchExpressions.map(stringifyExpression))
.join(",");
}
function getMatchExpressions(opts) {
if (typeof opts === "string") {
return getMatchExpressions(parse(opts));
}
const matchExpressions = opts.matchExpressions || [];
const matchLabels = opts.matchLabels || {};
return Object
.keys(matchLabels)
.map(label => ({
operator : "In",
key : label,
values : [matchLabels[label]]
}))
.concat(matchExpressions);
}
function isExprMatch(expr, labels) {
const op = expr.operator;
const key = expr.key;
const values = expr.values;
const label = labels[key];
switch (op) {
case "Exists": return hasOwnProperty(labels, key);
case "DoesNotExist": return !hasOwnProperty(labels, key);
case "In": return values.indexOf(label) >= 0;
case "NotIn": return values.indexOf(label) < 0;
default:
}
throw new Error(`Invalid operator: ${op}`);
}
function Selector(opts) {
const expressions = getMatchExpressions(opts);
return labels => expressions
.every(expr => isExprMatch(expr, labels || {}));
}
module.exports = {
stringify,
parse,
Selector
};