/
record-transform.ts
330 lines (289 loc) · 8.84 KB
/
record-transform.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
import { FieldType } from '@terascope/types';
import * as ts from '@terascope/utils';
import { Repository, RecordInput, InputType } from '../interfaces';
import { isString, isArray } from '../validations/field-validator';
export const repository: Repository = {
renameField: {
fn: renameField,
config: {
from: {
type: FieldType.String
},
to: {
type: FieldType.String
}
},
primary_input_type: InputType.Object,
output_type: FieldType.Object,
},
setField: {
fn: setField,
config: {
field: {
type: FieldType.String
},
value: {
type: FieldType.Any
}
},
primary_input_type: InputType.Object,
output_type: FieldType.Object,
},
dropFields: {
fn: dropFields,
config: {
fields: {
type: FieldType.String,
array: true
}
},
primary_input_type: InputType.Object,
output_type: FieldType.Object,
},
copyField: {
fn: copyField,
config: {
from: {
type: FieldType.String
},
to: {
type: FieldType.String
}
},
primary_input_type: InputType.Object,
output_type: FieldType.Object
},
dedupe: {
fn: dedupe,
config: {},
output_type: FieldType.Any,
primary_input_type: InputType.Array
},
};
/**
* This will migrate a fields value to a new field name
*
* @example
*
* const obj = { hello: 'world' };
* const config = { from: 'hello', to: 'goodbye' };
* const results = RecordTransform.renameField(cloneDeep(obj), cloneDeep(obj), config);
* results === { goodbye: 'world' };
*
* @param {*} record
* @param {{ from: string; to: string }} args
* @returns object
*/
export function renameField(
input: RecordInput,
_parentContext: RecordInput,
args: { from: string; to: string }
): ts.AnyObject|null {
if (ts.isNil(input)) return null;
_validateArgs(args, ['from', 'to']);
const { from, to } = args;
if (!isString(from) || !isString(to)) throw new Error('Invalid parameters, from/to must be supplied be be a string');
if (isArray(input)) {
return input
.map((data: any) => _migrate(data, from, to))
.filter(ts.isNotNil);
// we filter afterwards to remove nulls
}
return _migrate(input, from, to);
}
function _migrate(doc: ts.AnyObject, from: string, to: string) {
if (!ts.isObjectEntity(doc)) return null;
doc[to] = doc[from];
delete doc[from];
return doc;
}
/**
* Sets a field on a record with the given value
*
* @example
*
* const obj = { hello: 'world' };
* const config = { field: 'other', value: 'stuff' };
* const results = RecordTransform.setField(cloneDeep(obj), cloneDeep(obj), config);
* results === { hello: 'world', other: 'stuff' };
*
* @param {*} record
* @param {{ field: string; value: any }} args
* @returns object
*/
export function setField(
input: RecordInput,
_parentContext: RecordInput,
args: { field: string; value: any }
): ts.AnyObject|null {
if (ts.isNil(input)) return null;
_validateArgs(args, ['field', 'value']);
const { field, value } = args;
if (!isString(field) || value === undefined) throw new Error('Invalid parameters, please set field/value');
if (isArray(input)) {
return input
.map((data: any) => {
if (!ts.isObjectEntity(data)) return null;
data[field] = value;
return data;
})
.filter(ts.isNotNil);
}
if (!ts.isObjectEntity(input)) return null;
input[field] = value;
return input;
}
/**
* removes fields from a record
*
* @example
*
* const obj = { hello: 'world', other: 'stuff', last: 'thing' };
* const config = { fields: ['other', 'last']} ;
* const results = RecordTransform.dropFields(cloneDeep(obj), cloneDeep(obj), config);
* results; // { hello: 'world' };
*
* @param {*} record
* @param {{ fields: string[] }} args
* @returns object
*/
export function dropFields(
input: RecordInput,
_parentContext: RecordInput,
args: { fields: string[] }
): ts.AnyObject|null {
if (ts.isNil(input)) return null;
_validateArgs(args, ['fields']);
const { fields } = args;
if (!fields.every(isString)) throw new Error('Invalid parameters, field must be supplied and be a string');
if (isArray(input)) {
return input
.map((data: any) => _removeKeys(data, fields))
.filter(ts.isNotNil);
}
return _removeKeys(input, fields);
}
function _removeKeys(obj: ts.AnyObject, fields: string[]) {
if (!ts.isObjectEntity(obj)) return null;
for (const field of fields) {
delete obj[field];
}
if (Object.keys(obj).length === 0) return null;
return obj;
}
/**
* Will copy a field to another field
*
* @example
* const obj = { hello: 'world', other: 'stuff' };
* const config = { from: 'other', to: 'myCopy' };
* const results = RecordTransform.copyField(cloneDeep(obj), cloneDeep(obj), config);
* results; // { hello: 'world', other: 'stuff', myCopy: 'stuff' };
*
* @param {*} record
* @param {{ from: string; to: string }} args
* @returns object
*/
export function copyField(
input: RecordInput,
_parentContext: RecordInput,
args: { from: string; to: string }
): ts.AnyObject|null {
if (ts.isNil(input)) return null;
_validateArgs(args, ['from', 'to']);
const { from, to } = args;
if (!isString(from) || !isString(to)) throw new Error('Invalid parameters, field/copyTo must be supplied and be a string');
if (isArray(input)) {
return input
.map((data: any) => _copyField(data, from, to))
.filter(ts.isNotNil);
}
return _copyField(input, from, to);
}
function _copyField(doc: ts.AnyObject, from: string, to: string) {
if (!ts.isObjectEntity(doc)) return null;
if (doc[from] !== undefined) doc[to] = doc[from];
return doc;
}
function _validateArgs(args: ts.AnyObject, fields: string[]) {
if (ts.isNil(args)) throw new Error('Paramter args must be provided');
for (const key of fields) {
if (args[key] === undefined) throw new Error(`key ${key} was not provided on args, it is required`);
}
}
/**
* Will execute a jexl expression. Can use data-mate functions inside the jexl expression.
* You do not need to specify the parent context argument as that is automatically
* the document used as to call it.
*
* @example
*
* const obj = { hello: 'world', other: 'stuff' };
* const config = { query: '[hello]', field: 'final' };
* const results = RecordTransform.transformRecord(clone, clone, config)
* results === { hello: 'world', other: 'stuff', final: ['world'] });
*
* const obj = { foo: 'bar' };
* const config = {
* jexlExp: 'foo|extract({ jexlExp: "foo|toUpperCase" })', field: 'final'
* };
*
* const mixedData = [obj, undefined, null];
*
* const results = RecordTransform.transformRecord(
* mixedData, mixedData, config
* )
*
* results === [{ foo: 'bar', final: 'BAR' }];
*
* @param {*} record
* @param {{ field: string; query: string }} args
* @returns object
*/
// this will be overwritten by transformRecord in jexl folder
export function transformRecord(
_input: RecordInput,
_parentContext: RecordInput,
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
_args: any
): ts.AnyObject|null { return null; }
/**
* returns an array with only unique values
*
* @example
*
* const results = FieldTransform.dedupe([1, 2, 2, 3, 3, 3, undefined, 4])
* results === [1, 2, 3, 4]
*
*
* const results = RecordTransform.dedupe([
* { hello: 'world' },
* { hello: 'world' },
* { other: 'obj' },
* ])
* results === [{ hello: 'world' }, { other: 'obj' }];
* @param {any[]} input
* @returns {any[] | null } returns null if input is null/undefined
*/
export function dedupe<T = any>(input: any[], _parentContext?: unknown[]): T[] | null {
if (ts.isNil(input)) return null;
if (!isArray(input)) throw new Error(`Input must be an array, received ${ts.getTypeOf(input)}`);
const deduped = new Map<any, true>();
const results: T[] = [];
for (const value of input) {
if (ts.isNotNil(value)) {
if (ts.isPlainObject(value) && !ts.isEmpty(value)) {
const sorted = ts.sortKeys(value, { deep: true });
const json = JSON.stringify(sorted);
if (!deduped.has(json)) {
results.push(value);
deduped.set(json, true);
}
} else if (!deduped.has(value)) {
results.push(value);
deduped.set(value, true);
}
}
}
return results;
}