Tidy.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. import Utils from "../Utils.js";
  2. /**
  3. * Tidy operations.
  4. *
  5. * @author n1474335 [n1474335@gmail.com]
  6. * @copyright Crown Copyright 2016
  7. * @license Apache-2.0
  8. *
  9. * @namespace
  10. */
  11. const Tidy = {
  12. /**
  13. * @constant
  14. * @default
  15. */
  16. REMOVE_SPACES: true,
  17. /**
  18. * @constant
  19. * @default
  20. */
  21. REMOVE_CARIAGE_RETURNS: true,
  22. /**
  23. * @constant
  24. * @default
  25. */
  26. REMOVE_LINE_FEEDS: true,
  27. /**
  28. * @constant
  29. * @default
  30. */
  31. REMOVE_TABS: true,
  32. /**
  33. * @constant
  34. * @default
  35. */
  36. REMOVE_FORM_FEEDS: true,
  37. /**
  38. * @constant
  39. * @default
  40. */
  41. REMOVE_FULL_STOPS: false,
  42. /**
  43. * Remove whitespace operation.
  44. *
  45. * @param {string} input
  46. * @param {Object[]} args
  47. * @returns {string}
  48. */
  49. runRemoveWhitespace: function (input, args) {
  50. let removeSpaces = args[0],
  51. removeCariageReturns = args[1],
  52. removeLineFeeds = args[2],
  53. removeTabs = args[3],
  54. removeFormFeeds = args[4],
  55. removeFullStops = args[5],
  56. data = input;
  57. if (removeSpaces) data = data.replace(/ /g, "");
  58. if (removeCariageReturns) data = data.replace(/\r/g, "");
  59. if (removeLineFeeds) data = data.replace(/\n/g, "");
  60. if (removeTabs) data = data.replace(/\t/g, "");
  61. if (removeFormFeeds) data = data.replace(/\f/g, "");
  62. if (removeFullStops) data = data.replace(/\./g, "");
  63. return data;
  64. },
  65. /**
  66. * Remove null bytes operation.
  67. *
  68. * @param {byteArray} input
  69. * @param {Object[]} args
  70. * @returns {byteArray}
  71. */
  72. runRemoveNulls: function (input, args) {
  73. const output = [];
  74. for (let i = 0; i < input.length; i++) {
  75. if (input[i] !== 0) output.push(input[i]);
  76. }
  77. return output;
  78. },
  79. /**
  80. * @constant
  81. * @default
  82. */
  83. APPLY_TO_EACH_LINE: false,
  84. /**
  85. * @constant
  86. * @default
  87. */
  88. DROP_START: 0,
  89. /**
  90. * @constant
  91. * @default
  92. */
  93. DROP_LENGTH: 5,
  94. /**
  95. * Drop bytes operation.
  96. *
  97. * @param {byteArray} input
  98. * @param {Object[]} args
  99. * @returns {byteArray}
  100. */
  101. runDropBytes: function(input, args) {
  102. let start = args[0],
  103. length = args[1],
  104. applyToEachLine = args[2];
  105. if (start < 0 || length < 0)
  106. throw "Error: Invalid value";
  107. if (!applyToEachLine)
  108. return input.slice(0, start).concat(input.slice(start+length, input.length));
  109. // Split input into lines
  110. let lines = [],
  111. line = [],
  112. i;
  113. for (i = 0; i < input.length; i++) {
  114. if (input[i] === 0x0a) {
  115. lines.push(line);
  116. line = [];
  117. } else {
  118. line.push(input[i]);
  119. }
  120. }
  121. lines.push(line);
  122. let output = [];
  123. for (i = 0; i < lines.length; i++) {
  124. output = output.concat(lines[i].slice(0, start).concat(lines[i].slice(start+length, lines[i].length)));
  125. output.push(0x0a);
  126. }
  127. return output.slice(0, output.length-1);
  128. },
  129. /**
  130. * @constant
  131. * @default
  132. */
  133. TAKE_START: 0,
  134. /**
  135. * @constant
  136. * @default
  137. */
  138. TAKE_LENGTH: 5,
  139. /**
  140. * Take bytes operation.
  141. *
  142. * @param {byteArray} input
  143. * @param {Object[]} args
  144. * @returns {byteArray}
  145. */
  146. runTakeBytes: function(input, args) {
  147. let start = args[0],
  148. length = args[1],
  149. applyToEachLine = args[2];
  150. if (start < 0 || length < 0)
  151. throw "Error: Invalid value";
  152. if (!applyToEachLine)
  153. return input.slice(start, start+length);
  154. // Split input into lines
  155. let lines = [],
  156. line = [];
  157. let i;
  158. for (i = 0; i < input.length; i++) {
  159. if (input[i] === 0x0a) {
  160. lines.push(line);
  161. line = [];
  162. } else {
  163. line.push(input[i]);
  164. }
  165. }
  166. lines.push(line);
  167. let output = [];
  168. for (i = 0; i < lines.length; i++) {
  169. output = output.concat(lines[i].slice(start, start+length));
  170. output.push(0x0a);
  171. }
  172. return output.slice(0, output.length-1);
  173. },
  174. /**
  175. * @constant
  176. * @default
  177. */
  178. PAD_POSITION: ["Start", "End"],
  179. /**
  180. * @constant
  181. * @default
  182. */
  183. PAD_LENGTH: 5,
  184. /**
  185. * @constant
  186. * @default
  187. */
  188. PAD_CHAR: " ",
  189. /**
  190. * Pad lines operation.
  191. *
  192. * @param {string} input
  193. * @param {Object[]} args
  194. * @returns {string}
  195. */
  196. runPad: function(input, args) {
  197. let position = args[0],
  198. len = args[1],
  199. chr = args[2],
  200. lines = input.split("\n"),
  201. output = "",
  202. i = 0;
  203. if (position === "Start") {
  204. for (i = 0; i < lines.length; i++) {
  205. output += Utils.padLeft(lines[i], lines[i].length+len, chr) + "\n";
  206. }
  207. } else if (position === "End") {
  208. for (i = 0; i < lines.length; i++) {
  209. output += Utils.padRight(lines[i], lines[i].length+len, chr) + "\n";
  210. }
  211. }
  212. return output.slice(0, output.length-1);
  213. },
  214. };
  215. export default Tidy;