I'm fairly new to working with Vue
and JS
. Let's consider an array of objects where each object contains a "name" field, representing various processes or commands. For instance:
/disks/tools/itools/odor/src/bin/lib/script.py -path /disks/tools/itools/ps/char/leno/tool/src/work/dir -i ./report/dir/bin/a -report -id 12345 --name my_/name
I have written a piece of code that takes this array, iterates through it, and creates a shorter version for each entry. Using the example above, the output would be:
script.py -path dir -i a -report -id 12345 --name my_/name
The goal is to shorten any substring containing a "path". To achieve this, I check if the first symbol of the substring is either /
or ./
, and then extract the basename.
Here's the code snippet I've implemented:
addShort: function(arr) {
for (var i of arr) {
if (i.name) {
i.fullname = i.name;
i.name = '';
var splitedArr = i.fullname.split(' ');
for (var path of splitedArr) {
i.name += this.basename(path);
}
}
}
},
basename: function(str) {
var nStr = str.replace(/^.*[\\\/]/, '');
return newStr | '';
}
As a beginner in Vue
and JS
, I have some doubts about the quality of this code. I believe it can be optimized for better clarity and efficiency. Additionally, I am unsure if my regular expression covers all necessary cases. Moreover, I noticed that each string has an extra space at the end, prompting me to revise the basename
function as follows:
basename: function(str) {
var nStr = str.replace(/^.*[\\\/]/, '');
return newStr | '';
}
I also considered adding a space to the line i.name += this.basename(path);
like so:
i.name += this.basename(path).' ';
. However, this approach retains the trailing space. Is there a way to determine the last iteration of the loop so I can avoid adding the extra space?
Please forgive me for taking up your time with my uncertainties.