forked from AIObjectives/talk-to-the-city-reports
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jq_v1.ts
99 lines (90 loc) · 2.39 KB
/
jq_v1.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
import { browser } from '$app/environment';
import nodes from '$lib/node_register';
import categories from '$lib/node_categories';
import * as jq_web from 'jq-web/jq.asm.bundle.js';
import type { DGNodeInterface, BaseData } from '$lib/node_data_types';
import _ from 'lodash';
export default class JqNodeV1 {
id: string;
data: JqData;
position: { x: number; y: number };
type: string;
constructor(node_data) {
const { id, data, position, type } = node_data;
this.id = id;
this.data = data;
this.position = position;
this.type = type;
}
async compute(
inputData: object,
context: string,
info: (arg: string) => void,
error: (arg: string) => void,
success: (arg: string) => void,
slug: string,
Cookies: any
) {
this.data.message = '';
this.data.dirty = false;
const input = _.head(_.values(inputData));
try {
if (this.data.text && (_.isPlainObject(input) || _.isArray(input))) {
if (browser) {
this.data.output = jq_web.json(input, this.data.text);
return this.data.output;
} else {
const jqModule = await import('node-jq');
const jq = jqModule.default;
const res: any = await jq.run(this.data.text, input, {
input: 'json',
output: 'json'
});
try {
this.data.output = JSON.parse(res);
return this.data.output;
} catch (e) {
try {
this.data.output = res.split('\n').map(JSON.parse);
return this.data.output;
} catch (e) {
this.data.output = res;
return this.data.output;
}
}
}
}
} catch (e) {
console.log(e);
console.error(e.toString());
this.data.message = e.toString();
return undefined;
}
}
}
interface JqData extends BaseData {
text: string;
output: any;
}
type JqNodeInterface = DGNodeInterface & {
data: JqData;
};
export const jq_node_data: JqNodeInterface = {
id: 'jq_v1',
data: {
label: 'jq',
text: '',
dirty: false,
output: {},
compute_type: 'jq_v1',
input_ids: { data: '' },
category: categories.lang.id,
icon: 'jq_v0',
show_in_ui: false,
message: ''
},
position: { x: 0, y: 0 },
type: 'jq_v1'
};
export const jq_v1_node = new JqNodeV1(jq_node_data);
nodes.register(JqNodeV1, jq_node_data);