Skip to content

Commit 6c97187

Browse files
authored
Merge pull request #1667 from sw5678/master
Added file tree functionality
2 parents d8be3df + c3b89ef commit 6c97187

File tree

3 files changed

+116
-1
lines changed

3 files changed

+116
-1
lines changed

src/core/config/Categories.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,8 @@
296296
"Escape string",
297297
"Unescape string",
298298
"Pseudo-Random Number Generator",
299-
"Sleep"
299+
"Sleep",
300+
"File Tree"
300301
]
301302
},
302303
{

src/core/operations/FileTree.mjs

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/**
2+
* @author sw5678
3+
* @copyright Crown Copyright 2016
4+
* @license Apache-2.0
5+
*/
6+
7+
import Operation from "../Operation.mjs";
8+
import Utils from "../Utils.mjs";
9+
import {INPUT_DELIM_OPTIONS} from "../lib/Delim.mjs";
10+
11+
/**
12+
* Unique operation
13+
*/
14+
class FileTree extends Operation {
15+
16+
/**
17+
* Unique constructor
18+
*/
19+
constructor() {
20+
super();
21+
22+
this.name = "File Tree";
23+
this.module = "Default";
24+
this.description = "Creates file tree from list of file paths (Similar too tree linux command)";
25+
this.inputType = "string";
26+
this.outputType = "string";
27+
this.args = [
28+
{
29+
name: "File Path Delimiter",
30+
type: "binaryString",
31+
value: "/"
32+
},
33+
{
34+
name: "Delimiter",
35+
type: "option",
36+
value: INPUT_DELIM_OPTIONS
37+
}
38+
];
39+
}
40+
41+
/**
42+
* @param {string} input
43+
* @param {Object[]} args
44+
* @returns {string}
45+
*/
46+
run(input, args) {
47+
48+
// Set up arrow and pipe for nice output display
49+
const ARROW = "|---";
50+
const PIPE = "| ";
51+
52+
// Get args from input
53+
const fileDelim = args[0];
54+
const entryDelim = Utils.charRep(args[1]);
55+
56+
// Store path to print
57+
const completedList = [];
58+
const printList = [];
59+
60+
// Loop through all entries
61+
const filePaths = input.split(entryDelim).unique().sort();
62+
for (let i = 0; i < filePaths.length; i++) {
63+
// Split by file delimiter
64+
let path = filePaths[i].split(fileDelim);
65+
66+
if (path[0] === "") {
67+
path = path.slice(1, path.length);
68+
}
69+
70+
for (let j = 0; j < path.length; j++) {
71+
let printLine;
72+
let key;
73+
if (j === 0) {
74+
printLine = path[j];
75+
key = path[j];
76+
} else {
77+
printLine = PIPE.repeat(j-1) + ARROW + path[j];
78+
key = path.slice(0, j+1).join("/");
79+
}
80+
81+
// Check to see we have already added that path
82+
if (!completedList.includes(key)) {
83+
completedList.push(key);
84+
printList.push(printLine);
85+
}
86+
}
87+
}
88+
return printList.join("\n");
89+
}
90+
91+
}
92+
93+
export default FileTree;

tests/operations/tests/FileTree.mjs

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* @author sw5678
3+
* @copyright Crown Copyright 2023
4+
* @license Apache-2.0
5+
*/
6+
import TestRegister from "../../lib/TestRegister.mjs";
7+
8+
TestRegister.addTests([
9+
{
10+
"name": "Swap Case: basic example",
11+
"input": "/test_dir1/test_file1.txt\n/test_dir1/test_file2.txt\n/test_dir2/test_file1.txt",
12+
"expectedOutput": "test_dir1\n|---test_file1.txt\n|---test_file2.txt\ntest_dir2\n|---test_file1.txt",
13+
"recipeConfig": [
14+
{
15+
"op": "File Tree",
16+
"args": [
17+
],
18+
},
19+
],
20+
}
21+
]);

0 commit comments

Comments
 (0)