mirror of
https://github.com/gchq/CyberChef.git
synced 2025-05-07 23:15:14 -04:00
add formatter
This commit is contained in:
parent
c4e7c41a6e
commit
ce30989adc
693 changed files with 51226 additions and 26671 deletions
|
@ -14,13 +14,17 @@ import Utils from "../Utils.mjs";
|
|||
*/
|
||||
export const RECORD_DELIMITER_OPTIONS = ["Line feed", "CRLF"];
|
||||
|
||||
|
||||
/**
|
||||
* @constant
|
||||
* @default
|
||||
*/
|
||||
export const FIELD_DELIMITER_OPTIONS = ["Space", "Comma", "Semi-colon", "Colon", "Tab"];
|
||||
|
||||
export const FIELD_DELIMITER_OPTIONS = [
|
||||
"Space",
|
||||
"Comma",
|
||||
"Semi-colon",
|
||||
"Colon",
|
||||
"Tab",
|
||||
];
|
||||
|
||||
/**
|
||||
* Default from colour
|
||||
|
@ -30,10 +34,9 @@ export const FIELD_DELIMITER_OPTIONS = ["Space", "Comma", "Semi-colon", "Colon",
|
|||
*/
|
||||
export const COLOURS = {
|
||||
min: "white",
|
||||
max: "black"
|
||||
max: "black",
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Gets values from input for a plot.
|
||||
*
|
||||
|
@ -44,26 +47,30 @@ export const COLOURS = {
|
|||
* @param {number} length
|
||||
* @returns {Object[]}
|
||||
*/
|
||||
export function getValues(input, recordDelimiter, fieldDelimiter, columnHeadingsAreIncluded, length) {
|
||||
export function getValues(
|
||||
input,
|
||||
recordDelimiter,
|
||||
fieldDelimiter,
|
||||
columnHeadingsAreIncluded,
|
||||
length,
|
||||
) {
|
||||
let headings;
|
||||
const values = [];
|
||||
|
||||
input
|
||||
.split(recordDelimiter)
|
||||
.forEach((row, rowIndex) => {
|
||||
const split = row.split(fieldDelimiter);
|
||||
if (split.length !== length) throw new OperationError(`Each row must have length ${length}.`);
|
||||
input.split(recordDelimiter).forEach((row, rowIndex) => {
|
||||
const split = row.split(fieldDelimiter);
|
||||
if (split.length !== length)
|
||||
throw new OperationError(`Each row must have length ${length}.`);
|
||||
|
||||
if (columnHeadingsAreIncluded && rowIndex === 0) {
|
||||
headings = split;
|
||||
} else {
|
||||
values.push(split);
|
||||
}
|
||||
});
|
||||
if (columnHeadingsAreIncluded && rowIndex === 0) {
|
||||
headings = split;
|
||||
} else {
|
||||
values.push(split);
|
||||
}
|
||||
});
|
||||
return { headings, values };
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Gets values from input for a scatter plot.
|
||||
*
|
||||
|
@ -73,25 +80,32 @@ export function getValues(input, recordDelimiter, fieldDelimiter, columnHeadings
|
|||
* @param {boolean} columnHeadingsAreIncluded - whether we should skip the first record
|
||||
* @returns {Object[]}
|
||||
*/
|
||||
export function getScatterValues(input, recordDelimiter, fieldDelimiter, columnHeadingsAreIncluded) {
|
||||
export function getScatterValues(
|
||||
input,
|
||||
recordDelimiter,
|
||||
fieldDelimiter,
|
||||
columnHeadingsAreIncluded,
|
||||
) {
|
||||
let { headings, values } = getValues(
|
||||
input,
|
||||
recordDelimiter,
|
||||
fieldDelimiter,
|
||||
columnHeadingsAreIncluded,
|
||||
2
|
||||
2,
|
||||
);
|
||||
|
||||
if (headings) {
|
||||
headings = {x: headings[0], y: headings[1]};
|
||||
headings = { x: headings[0], y: headings[1] };
|
||||
}
|
||||
|
||||
values = values.map(row => {
|
||||
values = values.map((row) => {
|
||||
const x = parseFloat(row[0]),
|
||||
y = parseFloat(row[1]);
|
||||
|
||||
if (Number.isNaN(x)) throw new OperationError("Values must be numbers in base 10.");
|
||||
if (Number.isNaN(y)) throw new OperationError("Values must be numbers in base 10.");
|
||||
if (Number.isNaN(x))
|
||||
throw new OperationError("Values must be numbers in base 10.");
|
||||
if (Number.isNaN(y))
|
||||
throw new OperationError("Values must be numbers in base 10.");
|
||||
|
||||
return [x, y];
|
||||
});
|
||||
|
@ -99,7 +113,6 @@ export function getScatterValues(input, recordDelimiter, fieldDelimiter, columnH
|
|||
return { headings, values };
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Gets values from input for a scatter plot with colour from the third column.
|
||||
*
|
||||
|
@ -109,25 +122,33 @@ export function getScatterValues(input, recordDelimiter, fieldDelimiter, columnH
|
|||
* @param {boolean} columnHeadingsAreIncluded - whether we should skip the first record
|
||||
* @returns {Object[]}
|
||||
*/
|
||||
export function getScatterValuesWithColour(input, recordDelimiter, fieldDelimiter, columnHeadingsAreIncluded) {
|
||||
export function getScatterValuesWithColour(
|
||||
input,
|
||||
recordDelimiter,
|
||||
fieldDelimiter,
|
||||
columnHeadingsAreIncluded,
|
||||
) {
|
||||
let { headings, values } = getValues(
|
||||
input,
|
||||
recordDelimiter, fieldDelimiter,
|
||||
recordDelimiter,
|
||||
fieldDelimiter,
|
||||
columnHeadingsAreIncluded,
|
||||
3
|
||||
3,
|
||||
);
|
||||
|
||||
if (headings) {
|
||||
headings = {x: headings[0], y: headings[1]};
|
||||
headings = { x: headings[0], y: headings[1] };
|
||||
}
|
||||
|
||||
values = values.map(row => {
|
||||
values = values.map((row) => {
|
||||
const x = parseFloat(row[0]),
|
||||
y = parseFloat(row[1]),
|
||||
colour = row[2];
|
||||
|
||||
if (Number.isNaN(x)) throw new OperationError("Values must be numbers in base 10.");
|
||||
if (Number.isNaN(y)) throw new OperationError("Values must be numbers in base 10.");
|
||||
if (Number.isNaN(x))
|
||||
throw new OperationError("Values must be numbers in base 10.");
|
||||
if (Number.isNaN(y))
|
||||
throw new OperationError("Values must be numbers in base 10.");
|
||||
|
||||
return [x, y, Utils.escapeHtml(colour)];
|
||||
});
|
||||
|
@ -144,23 +165,30 @@ export function getScatterValuesWithColour(input, recordDelimiter, fieldDelimite
|
|||
* @param {boolean} columnHeadingsAreIncluded - whether we should skip the first record
|
||||
* @returns {Object[]}
|
||||
*/
|
||||
export function getSeriesValues(input, recordDelimiter, fieldDelimiter, columnHeadingsAreIncluded) {
|
||||
export function getSeriesValues(
|
||||
input,
|
||||
recordDelimiter,
|
||||
fieldDelimiter,
|
||||
columnHeadingsAreIncluded,
|
||||
) {
|
||||
const { values } = getValues(
|
||||
input,
|
||||
recordDelimiter, fieldDelimiter,
|
||||
recordDelimiter,
|
||||
fieldDelimiter,
|
||||
false,
|
||||
3
|
||||
3,
|
||||
);
|
||||
|
||||
let xValues = new Set();
|
||||
const series = {};
|
||||
|
||||
values.forEach(row => {
|
||||
values.forEach((row) => {
|
||||
const serie = row[0],
|
||||
xVal = row[1],
|
||||
val = parseFloat(row[2]);
|
||||
|
||||
if (Number.isNaN(val)) throw new OperationError("Values must be numbers in base 10.");
|
||||
if (Number.isNaN(val))
|
||||
throw new OperationError("Values must be numbers in base 10.");
|
||||
|
||||
xValues.add(xVal);
|
||||
if (typeof series[serie] === "undefined") series[serie] = {};
|
||||
|
@ -172,7 +200,7 @@ export function getSeriesValues(input, recordDelimiter, fieldDelimiter, columnHe
|
|||
const seriesList = [];
|
||||
for (const seriesName in series) {
|
||||
const serie = series[seriesName];
|
||||
seriesList.push({name: seriesName, data: serie});
|
||||
seriesList.push({ name: seriesName, data: serie });
|
||||
}
|
||||
|
||||
return { xValues, series: seriesList };
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue