/
replaceLiteral.ts
58 lines (54 loc) · 1.87 KB
/
replaceLiteral.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
import { FieldType } from '@terascope/types';
import {
FieldTransformConfig, ProcessMode, FunctionDefinitionType,
FunctionDefinitionExample, FunctionDefinitionCategory,
} from '../interfaces';
export interface ReplaceLiteralArgs {
search: string;
replace: string;
}
const examples: FunctionDefinitionExample<ReplaceLiteralArgs>[] = [
{
args: { search: 'bob', replace: 'mel' },
config: { version: 1, fields: { testField: { type: FieldType.String } } },
field: 'testField',
input: 'Hi bob',
output: 'Hi mel'
},
{
args: { search: 'bob', replace: 'mel' },
config: { version: 1, fields: { testField: { type: FieldType.String } } },
field: 'testField',
input: 'Hi Bob',
output: 'Hi Bob',
description: 'Does not replace as it is not an exact match'
},
];
export const replaceLiteralConfig: FieldTransformConfig<ReplaceLiteralArgs> = {
name: 'replaceLiteral',
type: FunctionDefinitionType.FIELD_TRANSFORM,
process_mode: ProcessMode.INDIVIDUAL_VALUES,
category: FunctionDefinitionCategory.STRING,
description: 'Returns a string with the searched value replaced by the replace value',
examples,
create({ args: { replace, search } }) {
return (input: unknown) => replaceFn(input as string, search, replace);
},
accepts: [FieldType.String],
argument_schema: {
search: {
type: FieldType.String,
array: false,
description: 'The characters that will be replaced'
},
replace: {
type: FieldType.String,
array: false,
description: 'The value that will replace what is set in search'
}
},
required_arguments: ['search', 'replace']
};
function replaceFn(input: string, search: string, newVal: string) {
return input.replace(search, newVal);
}