Skip to content
This repository was archived by the owner on Oct 11, 2023. It is now read-only.

Conversation

irokas
Copy link
Contributor

@irokas irokas commented Jul 17, 2018

Closes #1 .

irokas added 4 commits July 17, 2018 12:28
This version displays the files given in alphabetical order.
Copy link
Member

@parisk parisk left a 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++) {
Copy link
Member

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 😄.

Copy link
Member

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++) {
Copy link
Member

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){
Copy link
Member

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) <= 0

Based 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");
Copy link
Member

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")
Copy link
Member

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:

  1. Use filter to filter directories and files. Example:

    const files = allEntries.filter(entry => entry.type == 'file');
  2. You can sort an array using the sort method and providing your own sortFunction argument, as described in the documentation.

Copy link
Member

@parisk parisk left a 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:

  1. Formatting
  2. Making it a library
  3. Setting up tests

😄

src/main.js Outdated
return a.name > b.name ? 1 : b.name > a.name ? -1 : 0;
});

dirList = iterator(dirItem);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2 things here:

  1. We should use a single ul for all items (as described in Implement conversion of single-level payload to unordered list #1)
  2. We should declare the ul using const.

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;
Copy link
Member

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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2 things here:

  1. iterator is not a descriptive function name. What about something like convertItemsToUnorderedList?
  2. array is not a descriptive argument name and also might cause confusion with the Array object. What about something like items?

src/main.js Outdated
@@ -0,0 +1,23 @@
function convertItemsToUnorderedList(listItems) {
const ulList = document.createElement("ul");
Copy link
Member

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 => {
Copy link
Member

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"),
Copy link
Member

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");
Copy link
Member

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).

@parisk parisk changed the title WIP: implement basic functionality Implement basic functionality Jul 19, 2018
Copy link
Member

@parisk parisk left a 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!

@parisk parisk merged commit 0ce2f10 into sourcelair:master Jul 19, 2018
@irokas irokas deleted the poc branch July 20, 2018 09:53
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants