ExtractFilePaths.mjs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /**
  2. * @author n1474335 [n1474335@gmail.com]
  3. * @copyright Crown Copyright 2016
  4. * @license Apache-2.0
  5. */
  6. import Operation from "../Operation";
  7. import { search } from "../lib/Extract";
  8. /**
  9. * Extract file paths operation
  10. */
  11. class ExtractFilePaths extends Operation {
  12. /**
  13. * ExtractFilePaths constructor
  14. */
  15. constructor() {
  16. super();
  17. this.name = "Extract file paths";
  18. this.module = "Regex";
  19. this.description = "Extracts anything that looks like a Windows or UNIX file path.<br><br>Note that if UNIX is selected, there will likely be a lot of false positives.";
  20. this.inputType = "string";
  21. this.outputType = "string";
  22. this.args = [
  23. {
  24. "name": "Windows",
  25. "type": "boolean",
  26. "value": true
  27. },
  28. {
  29. "name": "UNIX",
  30. "type": "boolean",
  31. "value": true
  32. },
  33. {
  34. "name": "Display total",
  35. "type": "boolean",
  36. "value": false
  37. }
  38. ];
  39. }
  40. /**
  41. * @param {string} input
  42. * @param {Object[]} args
  43. * @returns {string}
  44. */
  45. run(input, args) {
  46. const includeWinPath = args[0],
  47. includeUnixPath = args[1],
  48. displayTotal = args[2],
  49. winDrive = "[A-Z]:\\\\",
  50. winName = "[A-Z\\d][A-Z\\d\\- '_\\(\\)~]{0,61}",
  51. winExt = "[A-Z\\d]{1,6}",
  52. winPath = winDrive + "(?:" + winName + "\\\\?)*" + winName +
  53. "(?:\\." + winExt + ")?",
  54. unixPath = "(?:/[A-Z\\d.][A-Z\\d\\-.]{0,61})+";
  55. let filePaths = "";
  56. if (includeWinPath && includeUnixPath) {
  57. filePaths = winPath + "|" + unixPath;
  58. } else if (includeWinPath) {
  59. filePaths = winPath;
  60. } else if (includeUnixPath) {
  61. filePaths = unixPath;
  62. }
  63. if (filePaths) {
  64. const regex = new RegExp(filePaths, "ig");
  65. return search(input, regex, null, displayTotal);
  66. } else {
  67. return "";
  68. }
  69. }
  70. }
  71. export default ExtractFilePaths;