Categories
JavaScript Answers

How to parse large JSON file in Node.js?

Sometimes, we want to parse large JSON file in Node.js.

In this article, we’ll look at how to parse large JSON file in Node.js.

How to parse large JSON file in Node.js?

To parse large JSON file in Node.js, we call fs.createReadStream and use the JSONStream library.

To install it, we run

npm i JSONStream

Then, we write

const fs = require("fs");
const JSONStream = require("JSONStream");

const getStream = () => {
  const jsonData = "myData.json";
  const stream = fs.createReadStream(jsonData, { encoding: "utf8" });
  const parser = JSONStream.parse("*");
  return stream.pipe(parser);
};

getStream()
  .pipe(transform)
  .on("error", (err) => {
    // ...
  });

to call fs.createReadStream to read the file at path jsonData.

And then we call JSONStream.parse to create a parser object.

Next, we call stream.pipe with parser to parse the JSON that’s read from the stream.

Then we call getStream with pipe to transform the result with the transform function.

And we watch for errors with on.

Conclusion

To parse large JSON file in Node.js, we call fs.createReadStream and use the JSONStream library.

Categories
JavaScript Answers

How to call JavaScript parseInt() with strings with leading zeros?

Sometimes, we want to call JavaScript parseInt() with strings with leading zeros.

In this article, we’ll look at how to call JavaScript parseInt() with strings with leading zeros.

How to call JavaScript parseInt() with strings with leading zeros?

To call JavaScript parseInt() with strings with leading zeros, we call it with 10 as the 2nd argument.

For instance, we write

const n = parseInt("09", 10);

to call parseInt with '09' and 10 to parse '09' into a decimal number.

Therefore n is 9.

Conclusion

To call JavaScript parseInt() with strings with leading zeros, we call it with 10 as the 2nd argument.

Categories
JavaScript Answers

How to reset or clear a spy in Jest and JavaScript?

Sometimes, we want to reset or clear a spy in Jest and JavaScript.

In this article, we’ll look at how to reset or clear a spy in Jest and JavaScript.

How to reset or clear a spy in Jest and JavaScript?

To reset or clear a spy in Jest and JavaScript, we call the jest.clearAllMocks method.

For instance, we write

afterEach(() => {
  jest.clearAllMocks();
});

to call jest.clearAllMocks in the afterEach callback to clear all spies after each test is run.

Conclusion

To reset or clear a spy in Jest and JavaScript, we call the jest.clearAllMocks method.

Categories
JavaScript Answers

How to set background color of HTML element using CSS properties in JavaScript?

Sometimes, we want to set background color of HTML element using CSS properties in JavaScript.

In this article, we’ll look at how to set background color of HTML element using CSS properties in JavaScript.

How to set background color of HTML element using CSS properties in JavaScript?

To set background color of HTML element using CSS properties in JavaScript, we can set the backgroundColor style.

For instance, we write

const setColor = (element, color) => {
  element.style.backgroundColor = color;
};

const el = document.getElementById("elementId");
setColor(el, "green");

to create the setColor function.

In it, we set the lement.style.backgroundColor property to color to set the background color to color.

Then we select the element we want to set the background color for with getElementById.

And we call setColor with it and 'green' to set its background color to green.

Conclusion

To set background color of HTML element using CSS properties in JavaScript, we can set the backgroundColor style.

Categories
JavaScript Answers

How to create an accurate timer in JavaScript?

Sometimes, we want to create an accurate timer in JavaScript.

In this article, we’ll look at how to create an accurate timer in JavaScript.

How to create an accurate timer in JavaScript?

To create an accurate timer in JavaScript, we should get the time from the Date object.

For instance, we write

setInterval(() => {
  console.log(new Date().toUTCString());
}, 1000);

to call setInterval with a callback that gets the latest datetime with the Date constructor.

And we call toUTCString to return a human readable datetime string.

1000 is the 2nd argument so the callback runs about every 1000 milliseconds.

Conclusion

To create an accurate timer in JavaScript, we should get the time from the Date object.