-
Notifications
You must be signed in to change notification settings - Fork 1
Implement basic functionality #2
Conversation
This version displays the files given in alphabetical order.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alright, great start! I want to see some simplifications in the code before moving on with further code review.
Let's move this on 🚀
src/main.js
Outdated
| const fileList = document.createElement("ul"); | ||
|
|
||
| var i, j; | ||
| for (i=0; i<input.length; i++) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead, of using a for loop, let's use the forEach method instead, which is available in all arrays 😄.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, the map method could make a lot of sense here, in order to map array entries to new li elements. Example:
const entries = [{"name": "blim", "type": "file"}, {"name": "blom", "type": "directory"}];
const elements = entries.map(entry => {
const element = document.createElement("li");
element.textContent = entry.name;
return element;
});
src/main.js
Outdated
| fileItem.splice(checkAndPush(input[i], fileItem), 0, tempItem); | ||
| } | ||
|
|
||
| for (i=0; i<dirItem.length; i++) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here; let's use forEach.
src/main.js
Outdated
| @@ -0,0 +1,42 @@ | |||
| function strComp (str1, str2) { | |||
| if (str1.localeCompare(str2) === -1 || str1.localeCompare(str2) === 0){ | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can simplify this to:
return str1.localeCompare(str2) <= 0Based on the documentation of localeCompare, its return value is:
A negative number if the reference string occurs before the compare string; positive if the reference string occurs after the compare string; 0 if they are equivalent.
src/main.js
Outdated
| const dirItem = []; | ||
| const fileItem = []; | ||
|
|
||
| const dirList = document.createElement("ul"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should use one list here. We want both directories and files in the same list (as described in #1).
src/main.js
Outdated
| for (i=0; i<input.length; i++) { | ||
| const tempItem = document.createElement("li"); | ||
| tempItem.textContent = input[i].name; | ||
| if (input[i].type === "directory") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can simplify the code here in two ways:
- Use
filterto filter directories and files. Example:
const files = allEntries.filter(entry => entry.type == 'file');
- You can sort an array using the
sortmethod and providing your ownsortFunctionargument, as described in the documentation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Made a few comments, that I would like to see corrected before we move on with:
- Formatting
- Making it a library
- Setting up tests
😄
src/main.js
Outdated
| return a.name > b.name ? 1 : b.name > a.name ? -1 : 0; | ||
| }); | ||
|
|
||
| dirList = iterator(dirItem); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
2 things here:
- We should use a single
ulfor all items (as described in Implement conversion of single-level payload to unordered list #1) - We should declare the
ulusingconst.
src/main.js
Outdated
| return a.name > b.name ? 1 : b.name > a.name ? -1 : 0; | ||
| }); | ||
| fileItem.sort(function(a, b) { | ||
| return a.name > b.name ? 1 : b.name > a.name ? -1 : 0; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Extract the compare function to a named one (e.g. alphabeticCompare) and pass the named function as parameter.
Here we have implemented the same function twice.
src/main.js
Outdated
| @@ -0,0 +1,25 @@ | |||
| function iterator(array) { | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
2 things here:
iteratoris not a descriptive function name. What about something likeconvertItemsToUnorderedList?arrayis not a descriptive argument name and also might cause confusion with theArrayobject. What about something likeitems?
src/main.js
Outdated
| @@ -0,0 +1,23 @@ | |||
| function convertItemsToUnorderedList(listItems) { | |||
| const ulList = document.createElement("ul"); | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suggest renaming this to ulElement (or something like that), as ulList essentially means "unordered list list", which is too verbose 😂.
src/main.js
Outdated
| @@ -0,0 +1,23 @@ | |||
| function convertItemsToUnorderedList(listItems) { | |||
| const ulList = document.createElement("ul"); | |||
| listItems.map(a => { | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We will have to change things here, as there is a bad practice. What happens is that there is a side-effect inside the map function argument; you are appending elements to ulList. When using map we better just return values and have no side effects:
const liElements = listItems.map(item => {
const liElement = document.createElement("li");
liElement.textContent = item.name;
return liElement;
});
liElements.forEach(liElement => ulElement.appendChild(liElement));Alternatively, you can use forEach which is semantically better for such use cases;
listItems.forEach(item => {
const liElement = document.createElement("li");
liElement.textContent = item.name;
ulElement.appendChild(liElement)
});Up to you!
src/main.js
Outdated
| } | ||
|
|
||
| function renderInput(input, container) { | ||
| const dirItem = input.filter(inputEl => inputEl.type === "directory"), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since this constant is an array of values, we better rename it to plural form (e.g. dirItems).
src/main.js
Outdated
|
|
||
| function renderInput(input, container) { | ||
| const dirItem = input.filter(inputEl => inputEl.type === "directory"), | ||
| fileItem = input.filter(inputEl => inputEl.type === "file"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here, since this constant is an array of values, we better rename it to plural form (e.g. fileItems).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks and works great!
Closes #1 .