123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245 |
- import Utils from "../Utils.js";
- /**
- * Tidy operations.
- *
- * @author n1474335 [n1474335@gmail.com]
- * @copyright Crown Copyright 2016
- * @license Apache-2.0
- *
- * @namespace
- */
- const Tidy = {
- /**
- * @constant
- * @default
- */
- REMOVE_SPACES: true,
- /**
- * @constant
- * @default
- */
- REMOVE_CARIAGE_RETURNS: true,
- /**
- * @constant
- * @default
- */
- REMOVE_LINE_FEEDS: true,
- /**
- * @constant
- * @default
- */
- REMOVE_TABS: true,
- /**
- * @constant
- * @default
- */
- REMOVE_FORM_FEEDS: true,
- /**
- * @constant
- * @default
- */
- REMOVE_FULL_STOPS: false,
- /**
- * Remove whitespace operation.
- *
- * @param {string} input
- * @param {Object[]} args
- * @returns {string}
- */
- runRemoveWhitespace: function (input, args) {
- let removeSpaces = args[0],
- removeCariageReturns = args[1],
- removeLineFeeds = args[2],
- removeTabs = args[3],
- removeFormFeeds = args[4],
- removeFullStops = args[5],
- data = input;
- if (removeSpaces) data = data.replace(/ /g, "");
- if (removeCariageReturns) data = data.replace(/\r/g, "");
- if (removeLineFeeds) data = data.replace(/\n/g, "");
- if (removeTabs) data = data.replace(/\t/g, "");
- if (removeFormFeeds) data = data.replace(/\f/g, "");
- if (removeFullStops) data = data.replace(/\./g, "");
- return data;
- },
- /**
- * Remove null bytes operation.
- *
- * @param {byteArray} input
- * @param {Object[]} args
- * @returns {byteArray}
- */
- runRemoveNulls: function (input, args) {
- const output = [];
- for (let i = 0; i < input.length; i++) {
- if (input[i] !== 0) output.push(input[i]);
- }
- return output;
- },
- /**
- * @constant
- * @default
- */
- APPLY_TO_EACH_LINE: false,
- /**
- * @constant
- * @default
- */
- DROP_START: 0,
- /**
- * @constant
- * @default
- */
- DROP_LENGTH: 5,
- /**
- * Drop bytes operation.
- *
- * @param {byteArray} input
- * @param {Object[]} args
- * @returns {byteArray}
- */
- runDropBytes: function(input, args) {
- let start = args[0],
- length = args[1],
- applyToEachLine = args[2];
- if (start < 0 || length < 0)
- throw "Error: Invalid value";
- if (!applyToEachLine)
- return input.slice(0, start).concat(input.slice(start+length, input.length));
- // Split input into lines
- let lines = [],
- line = [],
- i;
- for (i = 0; i < input.length; i++) {
- if (input[i] === 0x0a) {
- lines.push(line);
- line = [];
- } else {
- line.push(input[i]);
- }
- }
- lines.push(line);
- let output = [];
- for (i = 0; i < lines.length; i++) {
- output = output.concat(lines[i].slice(0, start).concat(lines[i].slice(start+length, lines[i].length)));
- output.push(0x0a);
- }
- return output.slice(0, output.length-1);
- },
- /**
- * @constant
- * @default
- */
- TAKE_START: 0,
- /**
- * @constant
- * @default
- */
- TAKE_LENGTH: 5,
- /**
- * Take bytes operation.
- *
- * @param {byteArray} input
- * @param {Object[]} args
- * @returns {byteArray}
- */
- runTakeBytes: function(input, args) {
- let start = args[0],
- length = args[1],
- applyToEachLine = args[2];
- if (start < 0 || length < 0)
- throw "Error: Invalid value";
- if (!applyToEachLine)
- return input.slice(start, start+length);
- // Split input into lines
- let lines = [],
- line = [];
- let i;
- for (i = 0; i < input.length; i++) {
- if (input[i] === 0x0a) {
- lines.push(line);
- line = [];
- } else {
- line.push(input[i]);
- }
- }
- lines.push(line);
- let output = [];
- for (i = 0; i < lines.length; i++) {
- output = output.concat(lines[i].slice(start, start+length));
- output.push(0x0a);
- }
- return output.slice(0, output.length-1);
- },
- /**
- * @constant
- * @default
- */
- PAD_POSITION: ["Start", "End"],
- /**
- * @constant
- * @default
- */
- PAD_LENGTH: 5,
- /**
- * @constant
- * @default
- */
- PAD_CHAR: " ",
- /**
- * Pad lines operation.
- *
- * @param {string} input
- * @param {Object[]} args
- * @returns {string}
- */
- runPad: function(input, args) {
- let position = args[0],
- len = args[1],
- chr = args[2],
- lines = input.split("\n"),
- output = "",
- i = 0;
- if (position === "Start") {
- for (i = 0; i < lines.length; i++) {
- output += Utils.padLeft(lines[i], lines[i].length+len, chr) + "\n";
- }
- } else if (position === "End") {
- for (i = 0; i < lines.length; i++) {
- output += Utils.padRight(lines[i], lines[i].length+len, chr) + "\n";
- }
- }
- return output.slice(0, output.length-1);
- },
- };
- export default Tidy;
|