2017-03-23 17:52:20 +00:00
|
|
|
import Utils from "../Utils.js";
|
2017-03-06 12:45:51 +00:00
|
|
|
|
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
/**
|
|
|
|
* Sequence utility operations.
|
|
|
|
*
|
|
|
|
* @author n1474335 [n1474335@gmail.com]
|
|
|
|
* @copyright Crown Copyright 2016
|
|
|
|
* @license Apache-2.0
|
|
|
|
*
|
|
|
|
* @namespace
|
|
|
|
*/
|
2017-03-23 17:52:20 +00:00
|
|
|
const SeqUtils = {
|
2016-11-28 10:42:58 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @constant
|
|
|
|
* @default
|
|
|
|
*/
|
|
|
|
DELIMITER_OPTIONS: ["Line feed", "CRLF", "Space", "Comma", "Semi-colon", "Colon", "Nothing (separate chars)"],
|
|
|
|
/**
|
|
|
|
* @constant
|
|
|
|
* @default
|
|
|
|
*/
|
|
|
|
SORT_REVERSE: false,
|
|
|
|
/**
|
|
|
|
* @constant
|
|
|
|
* @default
|
|
|
|
*/
|
|
|
|
SORT_ORDER: ["Alphabetical (case sensitive)", "Alphabetical (case insensitive)", "IP address"],
|
2017-02-09 15:09:33 +00:00
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
/**
|
|
|
|
* Sort operation.
|
|
|
|
*
|
|
|
|
* @param {string} input
|
|
|
|
* @param {Object[]} args
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
2017-01-31 18:24:56 +00:00
|
|
|
runSort: function (input, args) {
|
2017-04-13 18:08:50 +01:00
|
|
|
let delim = Utils.charRep[args[0]],
|
2017-01-31 18:24:56 +00:00
|
|
|
sortReverse = args[1],
|
2016-11-28 10:42:58 +00:00
|
|
|
order = args[2],
|
|
|
|
sorted = input.split(delim);
|
2017-02-09 15:09:33 +00:00
|
|
|
|
2016-12-14 16:39:17 +00:00
|
|
|
if (order === "Alphabetical (case sensitive)") {
|
2016-11-28 10:42:58 +00:00
|
|
|
sorted = sorted.sort();
|
2016-12-14 16:39:17 +00:00
|
|
|
} else if (order === "Alphabetical (case insensitive)") {
|
2017-01-31 18:24:56 +00:00
|
|
|
sorted = sorted.sort(SeqUtils._caseInsensitiveSort);
|
2016-12-14 16:39:17 +00:00
|
|
|
} else if (order === "IP address") {
|
2017-01-31 18:24:56 +00:00
|
|
|
sorted = sorted.sort(SeqUtils._ipSort);
|
2016-11-28 10:42:58 +00:00
|
|
|
}
|
2017-02-09 15:09:33 +00:00
|
|
|
|
2017-01-31 18:24:56 +00:00
|
|
|
if (sortReverse) sorted.reverse();
|
2016-11-28 10:42:58 +00:00
|
|
|
return sorted.join(delim);
|
|
|
|
},
|
2017-02-09 15:09:33 +00:00
|
|
|
|
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
/**
|
|
|
|
* Unique operation.
|
|
|
|
*
|
|
|
|
* @param {string} input
|
|
|
|
* @param {Object[]} args
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
2017-01-31 18:24:56 +00:00
|
|
|
runUnique: function (input, args) {
|
2017-04-13 18:08:50 +01:00
|
|
|
const delim = Utils.charRep[args[0]];
|
2016-11-28 10:42:58 +00:00
|
|
|
return input.split(delim).unique().join(delim);
|
|
|
|
},
|
2017-02-09 15:09:33 +00:00
|
|
|
|
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
/**
|
|
|
|
* @constant
|
|
|
|
* @default
|
|
|
|
*/
|
|
|
|
SEARCH_TYPE: ["Regex", "Extended (\\n, \\t, \\x...)", "Simple string"],
|
2017-02-09 15:09:33 +00:00
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
/**
|
|
|
|
* Count occurrences operation.
|
|
|
|
*
|
|
|
|
* @param {string} input
|
|
|
|
* @param {Object[]} args
|
|
|
|
* @returns {number}
|
|
|
|
*/
|
2017-01-31 18:24:56 +00:00
|
|
|
runCount: function(input, args) {
|
2017-04-13 18:08:50 +01:00
|
|
|
let search = args[0].string,
|
2016-11-28 10:42:58 +00:00
|
|
|
type = args[0].option;
|
2017-02-09 15:09:33 +00:00
|
|
|
|
2016-12-14 16:39:17 +00:00
|
|
|
if (type === "Regex" && search) {
|
2016-11-28 10:42:58 +00:00
|
|
|
try {
|
2017-04-13 18:08:50 +01:00
|
|
|
let regex = new RegExp(search, "gi"),
|
2016-11-28 10:42:58 +00:00
|
|
|
matches = input.match(regex);
|
|
|
|
return matches.length;
|
2017-02-09 15:09:33 +00:00
|
|
|
} catch (err) {
|
2016-11-28 10:42:58 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} else if (search) {
|
|
|
|
if (type.indexOf("Extended") === 0) {
|
2017-01-31 18:24:56 +00:00
|
|
|
search = Utils.parseEscapedChars(search);
|
2016-11-28 10:42:58 +00:00
|
|
|
}
|
|
|
|
return input.count(search);
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
},
|
2017-02-09 15:09:33 +00:00
|
|
|
|
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
/**
|
|
|
|
* @constant
|
|
|
|
* @default
|
|
|
|
*/
|
|
|
|
REVERSE_BY: ["Character", "Line"],
|
2017-02-09 15:09:33 +00:00
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
/**
|
|
|
|
* Reverse operation.
|
|
|
|
*
|
2017-01-31 18:24:56 +00:00
|
|
|
* @param {byteArray} input
|
2016-11-28 10:42:58 +00:00
|
|
|
* @param {Object[]} args
|
2017-01-31 18:24:56 +00:00
|
|
|
* @returns {byteArray}
|
2016-11-28 10:42:58 +00:00
|
|
|
*/
|
2017-01-31 18:24:56 +00:00
|
|
|
runReverse: function (input, args) {
|
2017-04-13 18:31:26 +01:00
|
|
|
let i;
|
2016-12-14 16:39:17 +00:00
|
|
|
if (args[0] === "Line") {
|
2017-04-13 18:08:50 +01:00
|
|
|
let lines = [],
|
2016-11-28 10:42:58 +00:00
|
|
|
line = [],
|
|
|
|
result = [];
|
2017-04-13 18:31:26 +01:00
|
|
|
for (i = 0; i < input.length; i++) {
|
2016-12-14 16:39:17 +00:00
|
|
|
if (input[i] === 0x0a) {
|
2016-11-28 10:42:58 +00:00
|
|
|
lines.push(line);
|
|
|
|
line = [];
|
|
|
|
} else {
|
|
|
|
line.push(input[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
lines.push(line);
|
|
|
|
lines.reverse();
|
|
|
|
for (i = 0; i < lines.length; i++) {
|
|
|
|
result = result.concat(lines[i]);
|
|
|
|
result.push(0x0a);
|
|
|
|
}
|
|
|
|
return result.slice(0, input.length);
|
|
|
|
} else {
|
|
|
|
return input.reverse();
|
|
|
|
}
|
|
|
|
},
|
2017-02-09 15:09:33 +00:00
|
|
|
|
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
/**
|
|
|
|
* Add line numbers operation.
|
|
|
|
*
|
|
|
|
* @param {string} input
|
|
|
|
* @param {Object[]} args
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
2017-01-31 18:24:56 +00:00
|
|
|
runAddLineNumbers: function(input, args) {
|
2017-04-13 18:08:50 +01:00
|
|
|
let lines = input.split("\n"),
|
2016-11-28 10:42:58 +00:00
|
|
|
output = "",
|
|
|
|
width = lines.length.toString().length;
|
2017-02-09 15:09:33 +00:00
|
|
|
|
2017-04-13 18:08:50 +01:00
|
|
|
for (let n = 0; n < lines.length; n++) {
|
2016-11-28 10:42:58 +00:00
|
|
|
output += Utils.pad((n+1).toString(), width, " ") + " " + lines[n] + "\n";
|
|
|
|
}
|
|
|
|
return output.slice(0, output.length-1);
|
|
|
|
},
|
2017-02-09 15:09:33 +00:00
|
|
|
|
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
/**
|
|
|
|
* Remove line numbers operation.
|
|
|
|
*
|
|
|
|
* @param {string} input
|
|
|
|
* @param {Object[]} args
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
2017-01-31 18:24:56 +00:00
|
|
|
runRemoveLineNumbers: function(input, args) {
|
2016-11-28 10:42:58 +00:00
|
|
|
return input.replace(/^[ \t]{0,5}\d+[\s:|\-,.)\]]/gm, "");
|
|
|
|
},
|
2017-02-09 15:09:33 +00:00
|
|
|
|
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
/**
|
|
|
|
* Expand alphabet range operation.
|
|
|
|
*
|
|
|
|
* @param {string} input
|
|
|
|
* @param {Object[]} args
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
2017-01-31 18:24:56 +00:00
|
|
|
runExpandAlphRange: function(input, args) {
|
|
|
|
return Utils.expandAlphRange(input).join(args[0]);
|
2016-11-28 10:42:58 +00:00
|
|
|
},
|
2017-02-09 15:09:33 +00:00
|
|
|
|
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
/**
|
|
|
|
* Comparison operation for sorting of strings ignoring case.
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
* @param {string} a
|
|
|
|
* @param {string} b
|
|
|
|
* @returns {number}
|
|
|
|
*/
|
2017-01-31 18:24:56 +00:00
|
|
|
_caseInsensitiveSort: function(a, b) {
|
2016-11-28 10:42:58 +00:00
|
|
|
return a.toLowerCase().localeCompare(b.toLowerCase());
|
|
|
|
},
|
2017-02-09 15:09:33 +00:00
|
|
|
|
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
/**
|
|
|
|
* Comparison operation for sorting of IPv4 addresses.
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
* @param {string} a
|
|
|
|
* @param {string} b
|
|
|
|
* @returns {number}
|
|
|
|
*/
|
2017-01-31 18:24:56 +00:00
|
|
|
_ipSort: function(a, b) {
|
2017-04-13 18:08:50 +01:00
|
|
|
let a_ = a.split("."),
|
2016-11-28 10:42:58 +00:00
|
|
|
b_ = b.split(".");
|
2017-02-09 15:09:33 +00:00
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
a_ = a_[0] * 0x1000000 + a_[1] * 0x10000 + a_[2] * 0x100 + a_[3] * 1;
|
|
|
|
b_ = b_[0] * 0x1000000 + b_[1] * 0x10000 + b_[2] * 0x100 + b_[3] * 1;
|
2017-02-09 15:09:33 +00:00
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
if (isNaN(a_) && !isNaN(b_)) return 1;
|
|
|
|
if (!isNaN(a_) && isNaN(b_)) return -1;
|
|
|
|
if (isNaN(a_) && isNaN(b_)) return a.localeCompare(b);
|
2017-02-09 15:09:33 +00:00
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
return a_ - b_;
|
|
|
|
},
|
2017-02-09 15:09:33 +00:00
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
};
|
2017-03-23 17:52:20 +00:00
|
|
|
|
|
|
|
export default SeqUtils;
|