Categories
JavaScript Answers

How to get folder and file list with Node.js and JavaScript?

Sometimes, we want to get folder and file list with Node.js and JavaScript.

In this article, we’ll look at how to get folder and file list with Node.js and JavaScript.

How to get folder and file list with Node.js and JavaScript?

To get folder and file list with Node.js and JavaScript, we can use the Node.js fs module.

For instance, we write:

const filesystem = require("fs");

const getAllFilesFromFolder = (dir) => {
  let results = [];
  for (const file of filesystem.readdirSync(dir)) {
    const path = `${dir}/${file}`
    const stat = filesystem.statSync(path);
    if (stat && stat.isDirectory()) {
      results = [...results, ...getAllFilesFromFolder(file)]
    } else {
      results.push(file);
    }
  };
  return results;
};

console.log(getAllFilesFromFolder('/bin'))

We loop through the contents of the dir directory with readdirSync.

Then we call statSync with the path to get the stat directory.

And we use its isDirectory method to check if the path is a directory.

If it is, we add it the contents of the child directories to results.

Otherwise, we push the file into results.

Conclusion

To get folder and file list with Node.js and JavaScript, we can use the Node.js fs module.

Categories
JavaScript Answers

How to write text on top of image in HTML5 canvas with JavaScript?

Sometimes, we want to write text on top of image in HTML5 canvas with JavaScript.

In this article, we’ll look at how to write text on top of image in HTML5 canvas with JavaScript.

How to write text on top of image in HTML5 canvas with JavaScript?

To write text on top of image in HTML5 canvas with JavaScript, we can use the fillText and drawImage methods.

For instance, we write:

<canvas style='width: 200px; height: 300px'></canvas>

to add a canvas element.

Then we write:

const canvas = document.querySelector("canvas");
const context = canvas.getContext("2d");
const imageObj = new Image();
imageObj.onload = () => {
  context.drawImage(imageObj, 10, 10);
  context.font = "40pt Calibri";
  context.fillText("hello!", 20, 20);
};
imageObj.src = "https://i.picsum.photos/id/45/200/300.jpg?hmac=mW2p9asL-scUozua98sWn1c03g7CYv7w7IIHwnFp4cM";

We select the canvas with document.querySelector.

Then we get the context with getContext.

Next, we create a new Image instance and set its onload property to a function that calls drawImage to draw the image onto the canvas.

And then we set the font and call fillText to set the font style and size and draw the text over the image.

Finally, we set the src property of the image to load the image.

imageObj.onload runs after the src property is set.

Conclusion

To write text on top of image in HTML5 canvas with JavaScript, we can use the fillText and drawImage methods.

Categories
JavaScript Answers

How to check classList with contains if a class exists before add or remove with JavaScript?

Sometimes, we want to check classList with contains if a class exists before add or remove with JavaScript.

In this article, we’ll look at how to check classList with contains if a class exists before add or remove with JavaScript.

How to check classList with contains if a class exists before add or remove with JavaScript?

To check classList with contains if a class exists before add or remove with JavaScript, we can just call classList.add or classList.remove without doing the check.

For instance, we write:

<p>
  foo
</p>

to add a p element.

Then we write:

const element = document.querySelector('p')
element.classList.remove('info');
element.classList.add('hint');

We select the p element with document.querySelector.

Then we call element.classList.remove to remove the info class.

And call element.classList.add to add the hint class.

Conclusion

To check classList with contains if a class exists before add or remove with JavaScript, we can just call classList.add or classList.remove without doing the check.

Categories
JavaScript Answers

How to update placeholder color using JavaScript?

Sometimes, we want to update placeholder color using JavaScript.

In this article, we’ll look at how to update placeholder color using JavaScript.

How to update placeholder color using JavaScript?

To update placeholder color using JavaScript, we can insert our own CSS rule into a style element.

For instance, we write:

<input type="text" placeholder="I will be blue">

to add an input.

Then we write:

const style = document.createElement("style")
style.type = "text/css"
const {
  sheet
} = document.head.appendChild(style)

const rule = sheet.insertRule("::placeholder {}")
const placeholderStyle = sheet.rules[rule].style;
placeholderStyle.color = "blue";

We write:

const style = document.createElement("style")
style.type = "text/css"
const {
  sheet
} = document.head.appendChild(style)

to add a new style element into the head element.

Then we write:

const rule = sheet.insertRule("::placeholder {}")
const placeholderStyle = sheet.rules[rule].style;

to insert a style rule for the placeholder and return it.

And then we set the placeholder’s color with:

placeholderStyle.color = "blue";

Now we should see that the placeholder’s color is blue.

Categories
JavaScript Answers

How to do partial sums of array items in JavaScript?

Sometimes, we want to do partial sums of array items in JavaScript.

In this article, we’ll look at how to do partial sums of array items in JavaScript.

How to do partial sums of array items in JavaScript?

To do partial sums of array items in JavaScript, we can use the JavaScript array reduce method.

For instance, we write:

const arr = [0, 1, 2, 3, 4, 5]
const partialSums = arr.reduce((acc, el, i, arr) => {
  const slice = arr.slice(0, i + 1)
  const sum = slice.reduce((a, b) => a + b, 0)
  return [...acc, sum]
}, [])
console.log(partialSums)

We call reduce with a callback that takes the index i and array arr parameters.

Then we take the parts of of arr from index 0 to i with slice.

Next, we compute the partial sum with reduce.

And then we return the acc array spread into the array and the new sum.

Then in the 2nd argument we set the initial partialSums value to an empty array.

Therefore, partialSums is [0, 1, 3, 6, 10, 15].

Conclusion

To do partial sums of array items in JavaScript, we can use the JavaScript array reduce method.