Categories
JavaScript Answers

How to invert a regular expression in JavaScript?

Sometimes, we want to invert a regular expression in JavaScript.

In this article, we’ll look at how to invert a regular expression in JavaScript.

How to invert a regular expression in JavaScript?

To invert a regular expression in JavaScript, we use negative lookahead.

For instance, we write

const notFoobar = /^(?!.*foobar)/.test("foobar@example.com");

to check if "foobar@example.com" does not have 'foobar'

We use ?! right after the opening parenthesis to start negative lookahead to look for the negation of the pattern after it.

How to invert a regular expression in JavaScript?

To invert a regular expression in JavaScript, we use negative lookahead.

Categories
JavaScript Answers

How to zip files using JavaScript?

Sometimes, we want to zip files using JavaScript.

In this article, we’ll look at how to zip files using JavaScript.

How to zip files using JavaScript?

To zip files using JavaScript, we use the jszip and file-saver packages.

To install them, we run

npm install jszip --save
npm install file-saver --save

Then we write

import JSZip from "jszip";
import FileSaver from "file-saver";

const zip = new JSZip();
zip.file("idlist.txt", "PMID:29651880\r\nPMID:29303721");

(async () => {
  const content = await zip.generateAsync({ type: "blob" });
  FileSaver.saveAs(content, "download.zip");
})();

to call zip.file to zip the files listed in the path arguments.

Then we call generateAsync to return a promise with the blob of the zip file.

Finally we call FileSaver.saveAs to save content as 'download.zip'.

Conclusion

To zip files using JavaScript, we use the jszip and file-saver packages.

Categories
JavaScript Answers

How to listen to window events from components with React.js?

Sometimes, we want to listen to window events from components with React.js.

In this article, we’ll look at how to listen to window events from components with React.js.

How to listen to window events from components with React.js?

To listen to window events from components with React.js, we call window.addEventListener in the useEffect callback.

For instance, we write

useEffect(() => {
  const onScroll = (event) => console.info("scrolling", event);

  window.addEventListener("scroll", onScroll);

  return () => {
    window.removeEventListener("scroll", onScroll);
  };
}, []);

to call the useEffect with a callback that calls window.addEventListener to listen to the scroll event on window.

We set the event listener to onScroll.

We call useEffect with an empty array to call the callback only when the component mounts.

Conclusion

To listen to window events from components with React.js, we call window.addEventListener in the useEffect callback.

Categories
JavaScript Answers

How to fix ‘is not defined react/jsx-no-undef’ error in React?

Sometimes, we want to fix ‘is not defined react/jsx-no-undef’ error in React.

In this article, we’ll look at how to fix ‘is not defined react/jsx-no-undef’ error in React.

How to fix ‘is not defined react/jsx-no-undef’ error in React?

To fix ‘is not defined react/jsx-no-undef’ error in React, we should make sure our variables are defined.

For instance, we write

import Map from "./Map";

to import the Map variable from the Map module.

The error should go away if the module and the variable exists in the module exists.

Conclusion

To fix ‘is not defined react/jsx-no-undef’ error in React, we should make sure our variables are defined.

Categories
JavaScript Answers

How to convert HTML5 Canvas to PNG File with JavaScript?

Sometimes, we want to convert HTML5 Canvas to PNG File with JavaScript.

In this article, we’ll look at how to convert HTML5 Canvas to PNG File with JavaScript.

How to convert HTML5 Canvas to PNG File with JavaScript?

To convert HTML5 Canvas to PNG File with JavaScript, we call the toDataURL method.

For instance, we write

const dlCanvas = (e) => {
  const dt = canvas.toDataURL("image/png");
  e.target.href = dt.replace(
    /^data:image\/[^;]/,
    "data:application/octet-stream"
  );
};

dl.addEventListener("click", dlCanvas, false);

to define the dlCanvas.

In it, we call canvas.toDataURL to return the canvas contents as a base64 string in the format specified by the argument.

Then we call dt.replace to replace the MIME type part with "data:application/octet-stream".

And we set that as the value of href to start the download.

We call addEventListener to add a click listener for the dl link.

Conclusion

To convert HTML5 Canvas to PNG File with JavaScript, we call the toDataURL method.