-
-
Notifications
You must be signed in to change notification settings - Fork 630
Expand file tree
/
Copy pathConverter.js
More file actions
97 lines (74 loc) · 2.82 KB
/
Copy pathConverter.js
File metadata and controls
97 lines (74 loc) · 2.82 KB
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
import { OPERATORS, ALIASES } from './Constants.js';
export default class {
fromBlueprint(conditions, prefix = null) {
return Object.entries(conditions).flatMap(([field, condition]) =>
this.wrap(condition).map((condition) => this.splitRhs(field, condition, prefix)),
);
}
toBlueprint(conditions) {
let converted = {};
conditions.forEach((condition) => {
const field = condition.field;
const value = this.combineRhs(condition);
if (field in converted) {
converted[field] = this.wrap(converted[field]).concat(value);
} else {
converted[field] = value;
}
});
return converted;
}
wrap(value) {
return Array.isArray(value) ? value : [value];
}
splitRhs(field, condition, prefix = null) {
return {
field: this.getScopedFieldHandle(field, prefix),
operator: this.getOperatorFromRhs(condition),
value: this.getValueFromRhs(condition),
};
}
getScopedFieldHandle(field, prefix) {
if (field.startsWith('$root.') || field.startsWith('root.')) {
return field;
}
if (field.startsWith('$parent.')) {
return field;
}
return prefix ? prefix + field : field;
}
getOperatorFromRhs(condition) {
let operator = '==';
this.getOperatorsAndAliases()
.filter((value) => new RegExp(`^${value} [^=]`).test(this.normalizeConditionString(condition)))
.forEach((value) => (operator = value));
return this.normalizeOperator(operator);
}
normalizeOperator(operator) {
return ALIASES[operator] ? ALIASES[operator] : operator;
}
getValueFromRhs(condition) {
let rhs = this.normalizeConditionString(condition);
this.getOperatorsAndAliases()
.filter((value) => new RegExp(`^${value} [^=]`).test(rhs))
.forEach((value) => (rhs = rhs.replace(new RegExp(`^${value}[ ]*`), '')));
return rhs;
}
combineRhs(condition) {
let operator = condition.operator ? condition.operator.trim() : '';
let value = condition.value.trim();
return `${operator} ${value}`.trim();
}
getOperatorsAndAliases() {
return OPERATORS.concat(Object.keys(ALIASES));
}
normalizeConditionString(value) {
// You cannot `null.toString()`, so we'll manually cast it here to prevent error.
if (value === null) return 'null';
// Note: We don't document the use of an '' empty string in the yaml,
// but for the people that manually add this to their yaml, we'll
// treat it as an `empty` check so that it doesn't feel broken.
if (value === '') return 'empty';
return value.toString();
}
}