-
Notifications
You must be signed in to change notification settings - Fork 0
/
transformations.ts
161 lines (121 loc) · 3.73 KB
/
transformations.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
import jp from 'jsonpath';
/**
* Replace
*/
interface ReplaceOptions {
replaceWith: string,
search: string
}
function replace(options: ReplaceOptions, text: string) : string {
if(!text) throw new Error("Text undefined");
if(!options?.search) throw new Error("Missing 'search' option");
if(options.replaceWith === undefined || options.replaceWith === null) throw new Error("Missing 'replaceWith' option");
return text.replaceAll(options.search, options.replaceWith);
}
/**
* RegexReplace
*/
interface RegexReplaceOptions {
pattern: string,
output: string,
}
function regexReplace(options: RegexReplaceOptions, value: string) : string {
if(!options?.pattern) throw new Error("Missing 'pattern' option");
if(!options.output) throw new Error("Missing 'output' option");
return value.replace(new RegExp(options.pattern), options.output);
}
/**
* RegexCompose
*/
interface RegexComposeOptions {
pattern: string,
output: string,
}
function regexCompose(options: RegexComposeOptions, value: string) : string | null {
if(!options?.pattern) throw new Error("Missing 'pattern' option");
if(!options.output) throw new Error("Missing 'output' option");
let re = new RegExp(options.pattern, 'gm');
let m = re.exec(value);
if(!m || m.length <= 1)
return null;
return m[0].replace(re, options.output);
}
/**
* TypeCast
*/
interface TypecastOptions {
targetType: string
}
function typecast(options: TypecastOptions, value: any) : any {
if(!options?.targetType) throw new Error("Missing 'targetType' option");
switch(options.targetType) {
case 'number': return Number(value);
case 'string': return String(value);
default: return null;
}
}
/**
* TextToJson
*/
interface TextToJsonOptions {
}
function textToJson(options: TextToJsonOptions, value: any) : any {
return JSON.parse(value);
}
/**
* Restructure
*/
interface RestructureOptions {
template: Record<string, any>
}
function restructure(options: RestructureOptions, value: any) : any {
if(!options.template) throw new Error("Missing 'template' option in restructure transformation");
function recursiveBuild(_template: Record<string, any>) {
let obj : Record<string, any> = {};
Object.entries(_template).forEach( ([key, val]) => {
if(typeof(val) === 'string') {
const result = jp.query(value, val);
if(result.length === 1)
obj[key] = result[0];
else
obj[key] = result; // If we have multiple match, keep in an array
}
else if(typeof(val) === 'object') {
obj[key] = recursiveBuild(val);
}
});
return obj;
}
return recursiveBuild(options.template);
}
/**
* Count elements of an array or characters of a string
*/
interface CountOptions {}
function count(options: CountOptions, value: any[]|string) : number {
return value.length;
}
/**
* PrintValue
*/
interface PrintValueOptions {}
function printValue(options: PrintValueOptions, value: any) : any {
console.log(value);
return value;
}
/**
* Function to apply a transformation on a value, by its name
*/
export function applyTransformation(name: string, options: Record<string, any>, value: any) : any {
switch(name) {
case 'regexReplace': return regexReplace(options as RegexReplaceOptions, value);
case 'regexCompose': return regexCompose(options as RegexComposeOptions, value);
case 'replace': return replace(options as ReplaceOptions, value);
case 'typecast': return typecast(options as TypecastOptions, value);
case 'textToJson': return textToJson(options as TextToJsonOptions, value);
case 'restructure': return restructure(options as RestructureOptions, value);
case 'print': return printValue(options as PrintValueOptions, value);
case 'count': return count(options as CountOptions, value);
default: throw new Error(`Unknown transformation '${name}'`);
}
}