/
truncate.ts
53 lines (51 loc) · 1.67 KB
/
truncate.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
import { truncateFP } from '@terascope/utils';
import { FieldType } from '@terascope/types';
import {
FieldTransformConfig,
ProcessMode,
FunctionDefinitionType,
FunctionDefinitionCategory
} from '../interfaces';
export interface TruncateConfig {
size: number;
}
export const truncateConfig: FieldTransformConfig<TruncateConfig> = {
name: 'truncate',
type: FunctionDefinitionType.FIELD_TRANSFORM,
process_mode: ProcessMode.INDIVIDUAL_VALUES,
category: FunctionDefinitionCategory.STRING,
description: 'Limits the size of the input string to a specific length, if the length is greater than the specified size, the excess is removed',
examples: [
{
args: { size: 4 },
config: { version: 1, fields: { testField: { type: FieldType.String } } },
field: 'testField',
input: 'thisisalongstring',
output: 'this'
},
{
args: { size: 8 },
config: { version: 1, fields: { testField: { type: FieldType.String } } },
field: 'testField',
input: 'Hello world',
output: 'Hello wo'
},
],
create({ args: { size } }) {
return truncateFP(size, false) as (value: unknown) => string;
},
accepts: [FieldType.String],
required_arguments: ['size'],
argument_schema: {
size: {
type: FieldType.Number,
array: false,
description: 'How long the string should be'
}
},
validate_arguments(args) {
if (args.size <= 0) {
throw new Error(`Invalid parameter size, expected a positive integer, got ${args.size}`);
}
}
};