/
index.ts
207 lines (173 loc) · 6.1 KB
/
index.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import jexlCore from 'jexl';
import * as ts from '@terascope/utils';
import { FieldType } from '@terascope/types';
import { FieldTransform } from '../transforms';
import { FieldValidator, RecordValidator } from '../validations';
import {
Repository, InputType, RepoConfig, ExtractFieldConfig, RecordInput
} from '../interfaces';
class Jexl extends jexlCore.Jexl {
_context = {};
evalSync(expression: string, _context: ts.AnyObject | undefined) {
let context;
if (ts.isNil(_context)) {
context = this._context;
} else {
context = _context;
}
this._context = context;
const exprObj = this.createExpression(expression);
const results = exprObj.evalSync(context);
this._context = {};
return results;
}
}
const jexl = new Jexl();
const bridgeToJexl = (fn: any) => {
// @ts-expect-error
const jexlInstance = this ? this.jexl : undefined;
return (value: any, _context: ts.AnyObject | undefined, _config: any) => {
let config;
let context;
if (ts.isNil(config) && jexlInstance) {
context = jexlInstance._context;
config = _context;
} else {
config = _config;
context = _context;
}
return fn(value, context, config as any);
};
};
function setup(operationClass: any) {
for (const config of Object.values(operationClass.repository as Repository)) {
jexl.addTransform(config.fn.name, bridgeToJexl(config.fn));
}
}
setup(FieldTransform);
setup(FieldValidator);
setup(RecordValidator);
jexl.addTransform(extract.name, bridgeToJexl(extract));
jexl.addTransform(transformRecord.name, bridgeToJexl(transformRecord));
export { jexl };
export const extractConfig: RepoConfig = {
fn: extract,
config: {
regex: { type: FieldType.String },
isMultiValue: { type: FieldType.Boolean },
jexlExp: { type: FieldType.String },
start: { type: FieldType.String },
end: { type: FieldType.String }
},
output_type: FieldType.Any,
primary_input_type: InputType.String
};
export function extract(
input: any,
parentContext: ts.AnyObject,
{
regex, isMultiValue = true, jexlExp, start, end
}: ExtractFieldConfig
): RecordInput|null {
if (ts.isNil(input)) return null;
function getSubslice() {
const indexStart = input.indexOf(start);
if (indexStart !== -1) {
const sliceStart = indexStart + start.length;
let endInd = input.indexOf(end, sliceStart);
if (endInd === -1) endInd = input.length;
const extractedSlice = input.slice(sliceStart, endInd);
if (extractedSlice) return input.slice(sliceStart, endInd);
}
return null;
}
type Cb = (data: any) => string|string[]|null;
function extractField(data: any, fn: Cb) {
if (typeof data === 'string') {
return fn(data);
}
if (FieldValidator.isArray(data)) {
const results: string[] = [];
data.forEach((subData: any) => {
if (typeof subData === 'string') {
const extractedSlice = fn(subData);
if (extractedSlice) {
if (Array.isArray(extractedSlice)) {
results.push(...extractedSlice);
} else {
results.push(extractedSlice);
}
}
}
});
if (results.length > 0) {
if (isMultiValue) return results;
return results[0];
}
}
return null;
}
function matchRegex() {
const results = ts.matchAll(regex as string, input);
if (isMultiValue) return results;
return results ? results[0] : results;
}
function callExpression() {
try {
return jexl.evalSync(jexlExp as string, parentContext);
} catch (err) {
throw new ts.TSError(err, {
message: `Invalid jexl expression: ${jexlExp}`
});
}
}
function extractValue() {
let extractedResult;
if (regex) {
extractedResult = extractField(input, matchRegex);
} else if (start && end) {
extractedResult = extractField(input, getSubslice);
} else if (jexlExp) {
extractedResult = callExpression();
} else {
extractedResult = input;
}
return extractedResult;
}
const results = extractValue();
if (results == null) return null;
return results;
}
export const transformRecordConfig: RepoConfig = {
fn: transformRecord,
config: {
jexlExp: { type: FieldType.String },
field: { type: FieldType.String },
},
output_type: FieldType.Object,
primary_input_type: InputType.Object
};
export function transformRecord(
input: RecordInput,
parentContext: RecordInput,
args: { jexlExp: string; field: string }
): RecordInput|null {
if (ts.isNil(input)) return null;
if (ts.isNil(args)) throw new Error('Argument parameters must be provided');
if (!FieldValidator.isString(args.jexlExp) || !FieldValidator.isLength(args.jexlExp, parentContext, { min: 1 })) throw new Error('Argument parameter jexlExp must must be provided and be a string value');
if (!FieldValidator.isString(args.field) || !FieldValidator.isLength(args.field, parentContext, { min: 1 })) throw new Error('Argument parameter field must must be provided and be a string value');
if (FieldValidator.isArray(input)) {
return input
.map((data: any) => {
if (!ts.isObjectEntity(data)) return null;
const value = jexl.evalSync(args.jexlExp, data);
if (ts.isNotNil(value)) data[args.field] = value;
return data;
})
.filter(ts.isNotNil);
}
if (!ts.isObjectEntity(input)) return null;
const value = jexl.evalSync(args.jexlExp, input);
if (ts.isNotNil(value)) input[args.field] = value;
return input;
}