-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot
More file actions
executable file
·198 lines (176 loc) · 5.18 KB
/
Copy pathplot
File metadata and controls
executable file
·198 lines (176 loc) · 5.18 KB
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#!/usr/bin/env deno
import { parseArgs } from "@std/cli";
import { readAll } from "@std/io";
import {
bar,
bullet,
scatter,
sparkline,
parseCategoricalData,
parseScatterData,
} from "chartex";
import { tryCatch } from "./utils/js/error-handlers.mjs";
const VALID_CHART_TYPES = ["vertical_bar", "line", "horizontal_bar", "scatter"];
/** * A utility function to create a pipeline of functions.
* It takes multiple functions as arguments and returns
* a new function that applies them sequentially to an input value.
* @param {...Function} fns - The functions to be pipelined.
* @returns {Function} A function that takes an input and applies the pipelined functions.
*/
const pipe =
(...fns) =>
(x) =>
fns.reduce((v, f) => f(v), x);
/**
* Retrieves and parses the data input for the chart.
* @param args {Record<string, any>} The command-line arguments.
* @returns {Promise<aObject[]>} The parsed data input.
*/
const getDataInput = async (args) => {
if (typeof args.data === "string" && args.data.trim().length > 0) {
const [result, error] = await tryCatch(() => JSON.parse(args.data));
if (error) {
console.error(
`Error: Invalid JSON data provided in --data. ${error.message}`
);
Deno.exit(1);
}
return result;
}
const buffer = await readAll(Deno.stdin);
const input = new TextDecoder().decode(buffer).trim();
if (!input) {
console.error("Error: No data provided via --data or stdin.");
Deno.exit(1);
}
const [result, error] = await tryCatch(() => JSON.parse(input));
if (error) {
console.error(
`Error: Invalid JSON data provided via stdin. ${error.message}`
);
Deno.exit(1);
}
return result;
};
/** * Validates the chart arguments to ensure they are correct.
* @param args {Record<string, any>} The command-line arguments.
* @returns {void} A void function that exits the process if validation fails.
*/
const validateChartArguments = (args) => {
if (!(args.type?.length && VALID_CHART_TYPES.includes(args.type))) {
console.error(
`Error: Invalid or missing chart type. Supported types are: ${VALID_CHART_TYPES.join(
", "
)}`
);
Deno.exit(1);
}
if (!args.xkey || !args.ykey) {
console.error("Error: --xkey and --ykey are required options.");
Deno.exit(1);
}
};
/**
* Displays the help message for the chart command.
* @returns {void}
*/
const showHelp = () =>
console.log(`
Usage: chart [options]
Options:
-d, --data Provide data for the chart in JSON format
-h, --help Show this help message
-t, --type Specify the type of chart (e.g., vertical_bar, line, pie)
-x, --xkey Specify the key for the x-axis
-y, --ykey Specify the key for the y-axis
`);
const options = {
boolean: ["help"],
string: ["type", "data", "xkey", "ykey"],
alias: {
d: "data",
h: "help",
t: "type",
x: "xkey",
y: "ykey",
},
};
/**
* Renders a vertical bar chart.
* @param data {Object[]} - The data to be visualized.
* @param xkey {string} - The key for the x-axis.
* @param ykey {string} - The key for the y-axis.
* @returns {void} A void function that renders the chart.
*/
const renderVerticalBarChart = (data, xkey, ykey) =>
pipe(
(d) => parseCategoricalData(d, xkey, ykey),
bar,
console.log
)(data);
/**
* Renders a line chart.
* @param data {Object[]} - The data to be visualized.
* @param xkey {string} - The key for the x-axis.
* @param ykey {string} - The key for the y-axis.
* @returns {void} A void function that renders the chart.
*/
const renderLineChart = (data, xkey, ykey) =>
pipe(
(d) => parseCategoricalData(d, xkey, ykey),
sparkline,
console.log
)(data);
/** * Renders a horizontal bar chart.
* @param data {Object[]} - The data to be visualized.
* @param xkey {string} - The key for the x-axis.
* @param ykey {string} - The key for the y-axis.
* @returns {void} A void function that renders the chart.
*/
const renderHorizontalBarChart = (data, xkey, ykey) =>
pipe(
(d) => parseCategoricalData(d, xkey, ykey),
bullet,
console.log
)(data);
/** * Renders a scatter chart.
* @param data {Object[]} - The data to be visualized.
* @param xkey {string} - The key for the x-axis.
* @param ykey {string} - The key for the y-axis.
* @returns {void} A void function that renders the chart.
*/
const renderScatterChart = (data, xkey, ykey) =>
pipe(
(d) => parseScatterData(d, xkey, xkey, ykey),
scatter,
console.log
)(data);
const chartRenderers = {
vertical_bar: renderVerticalBarChart,
line: renderLineChart,
horizontal_bar: renderHorizontalBarChart,
scatter: renderScatterChart,
};
/**
* Main function to parse arguments and render the chart.
* @returns {void}
*/
const main = async () => {
const args = parseArgs(Deno.args, options);
if (args.help) {
showHelp();
Deno.exit(0);
}
const dataInput = getDataInput(args);
validateChartArguments(args);
const renderChart = chartRenderers[args.type];
if (renderChart) {
renderChart(await dataInput, args.xkey, args.ykey);
} else {
console.error(`Error: Chart type ${args.type} is not implemented yet.`);
Deno.exit(1);
}
};
if (import.meta.main) {
main();
}