/
toJSON.ts
103 lines (99 loc) · 2.71 KB
/
toJSON.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
import {
isBigInt, bigIntToJSON, isNil,
isObjectEntity, toJSONCompatibleValue
} from '@terascope/utils';
import { FieldType } from '@terascope/types';
import {
FieldTransformConfig,
ProcessMode,
FunctionDefinitionType,
DataTypeFieldAndChildren,
FunctionDefinitionCategory
} from '../interfaces';
export const toJSONConfig: FieldTransformConfig = {
name: 'toJSON',
type: FunctionDefinitionType.FIELD_TRANSFORM,
process_mode: ProcessMode.FULL_VALUES,
description: 'Converts whole input to JSON format',
category: FunctionDefinitionCategory.JSON,
examples: [
{
args: {},
config: {
version: 1,
fields: {
testField: {
type: FieldType.Long
}
}
},
field: 'testField',
input: BigInt(21) ** BigInt(20),
output: '278218429446951548637196400'
},
{
args: {},
config: {
version: 1,
fields: {
testField: {
type: FieldType.Boolean
}
}
},
field: 'testField',
input: false,
output: 'false'
},
{
args: {},
config: {
version: 1,
fields: {
testField: {
type: FieldType.Object
}
}
},
field: 'testField',
input: { some: 1234 },
output: '{"some":1234}'
},
{
args: {},
config: {
version: 1,
fields: {
testField: {
type: FieldType.Object
}
}
},
field: 'testField',
input: { bigNum: BigInt(21) ** BigInt(20) },
output: '{"bigNum":"278218429446951548637196400"}'
},
],
create() {
return (input: unknown) => {
if (isNil(input)) return null;
if (isBigInt(input)) {
return bigIntToJSON(input);
}
if (isObjectEntity(input)) {
const parsedData = toJSONCompatibleValue(input);
return JSON.stringify(parsedData);
}
return JSON.stringify(input);
};
},
accepts: [],
output_type(_inputConfig: DataTypeFieldAndChildren): DataTypeFieldAndChildren {
return {
field_config: {
type: FieldType.String,
array: false
},
};
}
};