Skip to content

Commit 76ba630

Browse files
committed
Added file tree functionality
1 parent 6ed9d45 commit 76ba630

3 files changed

Lines changed: 123 additions & 1 deletion

File tree

src/core/config/Categories.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,8 @@
294294
"Escape string",
295295
"Unescape string",
296296
"Pseudo-Random Number Generator",
297-
"Sleep"
297+
"Sleep",
298+
"File Tree"
298299
]
299300
},
300301
{

src/core/operations/FileTree.mjs

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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 file_delim = args[0];
54+
const entry_delim = Utils.charRep(args[1]);
55+
56+
// Store path to print
57+
let completed_list = [];
58+
let print_list = [];
59+
60+
// Loop through all entries
61+
const file_paths = input.split(entry_delim).unique().sort();
62+
for (var i = 0; i < file_paths.length; i++)
63+
{
64+
// Split by file delimiter
65+
let path = file_paths[i].split(file_delim);
66+
67+
if (path[0] == '')
68+
{
69+
path = path.slice(1, path.length);
70+
}
71+
72+
for (var j = 0; j < path.length; j++)
73+
{
74+
let print_line;
75+
let key;
76+
if (j == 0)
77+
{
78+
print_line = path[j];
79+
key = path[j];
80+
}
81+
else
82+
{
83+
print_line = PIPE.repeat(j-1) + ARROW + path[j]
84+
key = path.slice(0, j+1).join("/");
85+
}
86+
87+
// Check to see we have already added that path
88+
if (!completed_list.includes(key))
89+
{
90+
completed_list.push(key);
91+
print_list.push(print_line);
92+
}
93+
}
94+
}
95+
return print_list.join("\n");
96+
}
97+
98+
}
99+
100+
export default FileTree;
Lines changed: 21 additions & 0 deletions
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)