-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexpression-utils.ts
167 lines (132 loc) · 5.79 KB
/
expression-utils.ts
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import {
ExpressionMetadata,
LambdaColumnMetadata,
LambdaExpressionMetadata,
LambdaPropertiesMetadata
} from './metadatas';
import { Expression, LambdaExpression } from './types';
import { REGEX_BETWEEN_PARENTHESIS, REGEX_BETWEEN_QUOTES } from './utils';
export class ExpressionUtils {
constructor(private _defaultSeparator: string = '_') {}
private getPropertiesByLambdaExpression<T>(expression: LambdaExpression<T>): LambdaPropertiesMetadata {
const expressionMetadada = this.getExpressionByLambdaExpression(expression);
return {
alias: expressionMetadada.alias,
operator: expressionMetadada.operator,
propertiesLeft: this.getPropertiesByExpressionString(expressionMetadada.expressionLeft, expressionMetadada.alias),
propertiesRight: this.getPropertiesByExpressionString(
expressionMetadada.expressionRight,
expressionMetadada.alias
)
};
}
private getAlias(startExpression: string): string {
// https://stackoverflow.com/a/17779833/2290538
const resultRegex = REGEX_BETWEEN_PARENTHESIS.exec(startExpression);
return resultRegex && resultRegex.length > 1 ? resultRegex[1] : '';
}
private hasAlias(value: string, alias: string | undefined): boolean {
if (!alias) {
return true;
}
return value.indexOf(`${alias}.`) > -1;
}
private hasBetweenQuotes(value: string): boolean {
return REGEX_BETWEEN_QUOTES.test(value);
}
private hasExpression(value: string, alias: string | undefined): boolean {
return !this.hasBetweenQuotes(value) && this.hasAlias(value, alias);
}
public getValueByExpression<T>(instance: T, expression: Expression<T>) {
return expression(instance);
}
public getValueByProperties(instance: any, properties: string[]) {
let result = instance;
properties.forEach(property => {
result = this.getValue(result, property);
});
return result;
}
public getValue(instance: any, property: string) {
if (property.indexOf('.') > -1) {
return this.getValueByProperties(instance, property.split('.'));
}
if (instance) {
return instance[property];
}
return void 0;
}
public getColumnByExpression<T>(expression: Expression<T>, separator: string = this._defaultSeparator): string {
return this.getColumnByProperties(this.getPropertiesByExpression(expression), separator);
}
public getColumnByProperties(properties: string[], separator: string = this._defaultSeparator): string {
return properties.join(separator);
}
public getPropertiesByExpression<T>(expression: Expression<T>): string[] {
const lambdaExpression = this.getExpressionMetadata(expression).body;
const properties = this.getPropertiesByExpressionString(lambdaExpression.split(' ')[0]);
return properties;
}
private getExpressionMetadata<T>(expression: Expression<T> | LambdaExpression<T>): ExpressionMetadata {
const expressionAsString = expression.toString();
if (expressionAsString.search('=>') !== -1) {
return this.getExpressionMetadataES2015(expressionAsString);
} else if (expressionAsString.search('return') !== -1) {
return this.getExpressionMetadataES5(expressionAsString);
} else {
throw Error(`Invalid expression: ${expressionAsString}`);
}
}
private getExpressionMetadataES5(expressionAsString: string): ExpressionMetadata {
const expressionMetadata = {} as ExpressionMetadata;
const paramsRegEx = REGEX_BETWEEN_PARENTHESIS.exec(expressionAsString);
expressionMetadata.params = paramsRegEx && paramsRegEx.length ? paramsRegEx[1] : '';
expressionMetadata.body = expressionAsString.slice(
expressionAsString.indexOf('return') + 7,
expressionAsString.indexOf(';')
);
return expressionMetadata;
}
private getExpressionMetadataES2015(expressionAsString: string): ExpressionMetadata {
const expressionMetadata = {} as ExpressionMetadata;
const params = expressionAsString.slice(0, expressionAsString.indexOf(' =>'));
expressionMetadata.params = REGEX_BETWEEN_PARENTHESIS.test(params) ? this.getAlias(params) : params;
expressionMetadata.body = expressionAsString.slice(expressionAsString.indexOf('=>') + 3, expressionAsString.length);
return expressionMetadata;
}
public getPropertiesByExpressionString(expression: string, alias: string | undefined = void 0): string[] {
if (this.hasExpression(expression, alias)) {
const propertiesReferences = expression.split('.');
if (propertiesReferences.length && this.hasExpression(expression, alias)) {
propertiesReferences.shift();
} // remove alias
return propertiesReferences;
}
return [expression];
}
public getColumnByLambdaExpression<T>(expression: LambdaExpression<T>): LambdaColumnMetadata {
const propertiesMetadada = this.getPropertiesByLambdaExpression(expression);
return {
alias: propertiesMetadada.alias,
columnLeft: this.getColumnByProperties(propertiesMetadada.propertiesLeft),
columnRight: this.getColumnByProperties(propertiesMetadada.propertiesRight),
operator: propertiesMetadada.operator
};
}
public getExpressionByLambdaExpression<T>(expression: LambdaExpression<T>): LambdaExpressionMetadata {
const lambdaExpressionMetadata = {} as LambdaExpressionMetadata;
const { params, body } = this.getExpressionMetadata(expression);
lambdaExpressionMetadata.alias = params;
const strExpression = body.split(' ');
if (strExpression.length !== 3) {
throw new Error(`Lambda expression '${expression.toString()}'
not supported! Use simple expression with '{expressionLeft} {operador} {expressionRight}'`);
}
[
lambdaExpressionMetadata.expressionLeft,
lambdaExpressionMetadata.operator,
lambdaExpressionMetadata.expressionRight
] = strExpression;
return lambdaExpressionMetadata;
}
}