Categories
JavaScript Answers

How to get a microtime in Node.js?

Sometimes, we want to get a microtime in Node.js.

In this article, we’ll look at how to get a microtime in Node.js.

How to get a microtime in Node.js?

To get a microtime in Node.js, we can use the process.hrtime method.

For instance, we write

const [seconds, nanoSeconds] = process.hrtime();
console.log(seconds * 1000000 + nanoSeconds / 1000);

to call process.hrtime to return an array with the current timestamp as an array with the seconds and nanoSeconds.

Then we use seconds * 1000000 + nanoSeconds / 1000 to convert seconds and nanoSeconds to microseconds and add them together.

Conclusion

To get a microtime in Node.js, we can use the process.hrtime method.

Categories
JavaScript Answers

How to sort JavaScript array by two numeric fields?

Sometimes, we want to sort JavaScript array by two numeric fields.

In this article, we’ll look at how to sort JavaScript array by two numeric fields.

How to sort JavaScript array by two numeric fields?

To sort JavaScript array by two numeric fields, we use the || operator.

For instance, we write

const sortedArr = grouperArray.sort((a, b) => {
  return a.gSize - b.gSize || a.glow - b.glow;
});

to call grouperArray.sort with a callback that sorts by the values of the gSize and glow properties in each entries a and b.

We sort by gSize first and then glow.

Conclusion

To sort JavaScript array by two numeric fields, we use the || operator.

Categories
JavaScript Answers

How to use a condition in switch case with JavaScript?

Sometimes, we want to use a condition in switch case with JavaScript.

In this article, we’ll look at how to use a condition in switch case with JavaScript.

How to use a condition in switch case with JavaScript?

To use a condition in switch case with JavaScript, we replace the switch statement with if statements.

For instance, we write

if (liCount === 0) {
  setLayoutState("start");
} else if (liCount <= 5) {
  setLayoutState("upload1Row");
} else if (liCount <= 10) {
  setLayoutState("upload2Rows");
}

to use if statements to check the value of liCount.

In each block, we call setLayoutState with various values according to the liCount value.

Conclusion

To use a condition in switch case with JavaScript, we replace the switch statement with if statements.

Categories
JavaScript Answers

How to convert hex to RGBA with JavaScript?

Sometimes, we want to convert hex to RGBA with JavaScript.

In this article, we’ll look at how to convert hex to RGBA with JavaScript.

How to convert hex to RGBA with JavaScript?

To convert hex to RGBA with JavaScript, we call the string match method.

For instance, we write

const hex2rgba = (hex, alpha = 1) => {
  const [r, g, b] = hex.match(/\w\w/g).map((x) => parseInt(x, 16));
  return `rgba(${r},${g},${b},${alpha})`;
};

to define the hex2rgba function.

In it, we call hex.match with a regex to match groups of 2 digits.

Then we call map on the returned array with a callback to parse the hex number with parseInt called with x and 16.

Finally, we return the string with the r, g, b and alpha values inside.

Conclusion

To convert hex to RGBA with JavaScript, we call the string match method.

Categories
JavaScript Answers

How to send a message to a particular client with socket.io and JavaScript?

Sometimes, we want to send a message to a particular client with socket.io and JavaScript.

In this article, we’ll look at how to send a message to a particular client with socket.io and JavaScript.

How to send a message to a particular client with socket.io and JavaScript?

To send a message to a particular client with socket.io and JavaScript, we call the socket.emit and socket.on method.

For instance, on client side, we write

const socket = io.connect("http://localhost");
socket.emit("join", { email: "user1@example.com" });

to call socket.emit to emit the join event with some event data.

Then on server side, we write

const io = require("socket.io").listen(80);

io.sockets.on("connection", (socket) => {
  socket.on("join", (data) => {
    socket.join(data.email);
  });
});

to call io.sockets.on with 'connection' to listen to the connection event.

In the callback, we call socket.on with 'join' to listen to the join event.

In the join callback, we get the email value we sent from data.email.

Conclusion

To send a message to a particular client with socket.io and JavaScript, we call the socket.emit and socket.on method.