Categories
JavaScript Answers

How to count the number of files in a directory using JavaScript and Node.js?

Sometimes, we want to count the number of files in a directory using JavaScript and Node.js.

In this article, we’ll look at how to count the number of files in a directory using JavaScript and Node.js.

How to count the number of files in a directory using JavaScript and Node.js?

To count the number of files in a directory using JavaScript and Node.js, we can use the fs.readdir with the irectory to get the array of files data in the directory.

Then we can use the length property to get the number of files in the directory.

For instance, we write:

const { promises: fs } = require('fs');
const dir = '/';

const getNumFiles = async (dir) => {
  const files = await fs.readdir(dir)
  console.log(files.length)
}
getNumFiles(dir)

to define the getNumFiles function that takes the dir directory string.

In the function, we call fs.readdir with dir to read the files data in dir into an array.

Then we get the number of files from the length property.

Conclusion

To count the number of files in a directory using JavaScript and Node.js, we can use the fs.readdir with the irectory to get the array of files data in the directory.

Then we can use the length property to get the number of files in the directory.

Categories
JavaScript Answers

How to capitalize first letter of each word in JavaScript?

Sometimes, we want to capitalize first letter of each word in JavaScript.

In this article, we’ll look at how to capitalize first letter of each word in JavaScript.

How to capitalize first letter of each word in JavaScript?

To capitalize first letter of each word in JavaScript, we can use the string’s replace method.

For instance, we write:

const str = 'foo bar baz'
const newStr = str.toLowerCase().replace(/^\w|\s\w/g, (letter) => {
  return letter.toUpperCase();
})
console.log(newStr)

We call replace with a regex to match the first letter of each word and a callback that replace those letters with an upper case letter.

Therefore, newStr is 'Foo Bar Baz'.

Conclusion

To capitalize first letter of each word in JavaScript, we can use the string’s replace method.

Categories
JavaScript Answers Nodejs

How to get the ID parameter from the URL with Express and Node.js?

Sometimes, we want to get the ID parameter from the URL with Express and Node.js.

In this article, we’ll look at how to get the ID parameter from the URL with Express and Node.js.

How to get the ID parameter from the URL with Express and Node.js?

To get the ID parameter from the URL with Express and Node.js, we can get it from the req.params object.

For instance, we write:

const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => {
  res.send('Hello World!')
})

app.get('/user/:id/docs', (req, res) => {
  const { id } = req.params;
  res.send(id)
});

app.listen(port, () => {
  console.log(`Example app listening on port ${port}`)
})

to add the /user/:id/docs route with:

app.get('/user/:id/docs', (req, res) => {
  const { id } = req.params;
  res.send(id)
});

':id' is the placeholder for the id URL parameter.

We get the ID value from req.params.

Therefore, if we go to /user/1/docs, we see 1 returned as the response.

Conclusion

To get the ID parameter from the URL with Express and Node.js, we can get it from the req.params object.

Categories
JavaScript Answers

How to convert enums to array of values with JavaScript?

Sometimes, we want to convert enums to array of values with JavaScript.

In this article, we’ll look at how to convert enums to array of values with JavaScript.

How to convert enums to array of values with JavaScript?

To convert enums to array of values with JavaScript, we can use the Object.values method.

For instance, we write:

const types = {
  "WHITE": 0,
  "BLACK": 1
}
const vals = Object.values(types)
console.log(vals)

to call Object.values with types to return an array of the property values.

Therefore, vals is [0, 1].

Conclusion

To convert enums to array of values with JavaScript, we can use the Object.values method.

Categories
JavaScript Answers

How to change the 1-24 hour return from the JavaScript date’s getHours method to a 1-12 hour?

Sometimes, we want to change the 1-24 hour return from the JavaScript date’s getHours method to a 1-12 hour.

In this article, we’ll look at how to change the 1-24 hour return from the JavaScript date’s getHours method to a 1-12 hour.

How to change the 1-24 hour return from the JavaScript date’s getHours method to a 1-12 hour?

To change the 1-24 hour return from the JavaScript date’s getHours method to a 1-12 hour, we can use the % operator.

For instance, we write:

const hours12 = (date) => {
  return (date.getHours() + 24) % 12 || 12;
}

const d = new Date('December 25, 2022 23:15:30')
console.log(hours12(d))

We define the hours12 to return the hours of the date in 12 hour format instead of 24.

In the function, we call date.getHours to get the hours, which is between 1 and 24.

Then we convert it to 12 hour format by adding 24 and then use the % to get the remainder when the result is divided by 12.

If the remainder is 0, then we return 12.

Therefore, the console log should log 11.

Conclusion

To change the 1-24 hour return from the JavaScript date’s getHours method to a 1-12 hour, we can use the % operator.