2018-05-14 18:46:54 +02:00
|
|
|
/**
|
|
|
|
* @author n1474335 [n1474335@gmail.com]
|
|
|
|
* @copyright Crown Copyright 2016
|
|
|
|
* @license Apache-2.0
|
|
|
|
*/
|
|
|
|
|
2019-07-09 13:23:59 +02:00
|
|
|
import Operation from "../Operation.mjs";
|
|
|
|
import Utils from "../Utils.mjs";
|
|
|
|
import {INPUT_DELIM_OPTIONS} from "../lib/Delim.mjs";
|
2022-04-14 19:08:16 +02:00
|
|
|
import {caseInsensitiveSort, ipSort, numericSort, hexadecimalSort} from "../lib/Sort.mjs";
|
2018-05-14 18:46:54 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sort operation
|
|
|
|
*/
|
|
|
|
class Sort extends Operation {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sort constructor
|
|
|
|
*/
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
|
|
|
|
this.name = "Sort";
|
|
|
|
this.module = "Default";
|
|
|
|
this.description = "Alphabetically sorts strings separated by the specified delimiter.<br><br>The IP address option supports IPv4 only.";
|
|
|
|
this.inputType = "string";
|
|
|
|
this.outputType = "string";
|
|
|
|
this.args = [
|
|
|
|
{
|
|
|
|
"name": "Delimiter",
|
|
|
|
"type": "option",
|
|
|
|
"value": INPUT_DELIM_OPTIONS
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "Reverse",
|
|
|
|
"type": "boolean",
|
|
|
|
"value": false
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "Order",
|
|
|
|
"type": "option",
|
2018-10-11 13:21:13 +02:00
|
|
|
"value": ["Alphabetical (case sensitive)", "Alphabetical (case insensitive)", "IP address", "Numeric", "Numeric (hexadecimal)"]
|
2018-05-14 18:46:54 +02:00
|
|
|
}
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} input
|
|
|
|
* @param {Object[]} args
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
run(input, args) {
|
|
|
|
const delim = Utils.charRep(args[0]),
|
|
|
|
sortReverse = args[1],
|
|
|
|
order = args[2];
|
|
|
|
let sorted = input.split(delim);
|
|
|
|
|
|
|
|
if (order === "Alphabetical (case sensitive)") {
|
|
|
|
sorted = sorted.sort();
|
|
|
|
} else if (order === "Alphabetical (case insensitive)") {
|
2022-04-14 19:08:16 +02:00
|
|
|
sorted = sorted.sort(caseInsensitiveSort);
|
2018-05-14 18:46:54 +02:00
|
|
|
} else if (order === "IP address") {
|
2022-04-14 19:08:16 +02:00
|
|
|
sorted = sorted.sort(ipSort);
|
2018-05-14 18:46:54 +02:00
|
|
|
} else if (order === "Numeric") {
|
2022-04-14 19:08:16 +02:00
|
|
|
sorted = sorted.sort(numericSort);
|
2018-10-11 13:21:13 +02:00
|
|
|
} else if (order === "Numeric (hexadecimal)") {
|
2022-04-14 19:08:16 +02:00
|
|
|
sorted = sorted.sort(hexadecimalSort);
|
2018-05-14 18:46:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (sortReverse) sorted.reverse();
|
|
|
|
return sorted.join(delim);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
export default Sort;
|