Categories
JavaScript Answers

How to match string against the array of regular expressions with JavaScript?

Sometimes, we want to match string against the array of regular expressions with JavaScript.

In this article, we’ll look at how to match string against the array of regular expressions with JavaScript.

How to match string against the array of regular expressions with JavaScript?

To match string against the array of regular expressions with JavaScript, we use the array some method.

For instance, we write

const regexList = [/apple/, /pear/];
const text = "banana pear";
const isMatch = regexList.some((rx) => rx.test(text));

to call regexList.some with a callback that calls test on each regex to see if text matches the regex pattern.

If any regex matches text, then some returns true.

Conclusion

To match string against the array of regular expressions with JavaScript, we use the array some method.

Categories
JavaScript Answers

How to save binary data as file using JavaScript from a browser?

Sometimes,. we want to save binary data as file using JavaScript from a browser.

In this article, we’ll look at how to save binary data as file using JavaScript from a browser.

How to save binary data as file using JavaScript from a browser?

To save binary data as file using JavaScript from a browser, we create a link programmatically.

For instance, we write

const a = document.createElement("a");
const blob = new Blob(data, { type: "octet/stream" });
const url = window.URL.createObjectURL(blob);
a.href = url;
a.download = name;
a.click();
window.URL.revokeObjectURL(url);

to create a link with createElement.

Then we create a Blob from the data string which has the data to download.

Next, we convert it to a base64 URL string with createObjectURL.

Then we set the href to the url.

Next we set the file name of the downloaded file by setting the download property.

Then we call click to download the file.

Finally we call revokeObjectURL to clear the URL resource.

Conclusion

To save binary data as file using JavaScript from a browser, we create a link programmatically.

Categories
JavaScript Answers

How to get current HTML page title with JavaScript?

Sometimes, we want to get current HTML page title with JavaScript.

In this article, we’ll look at how to get current HTML page title with JavaScript.

How to get current HTML page title with JavaScript?

To get current HTML page title with JavaScript, we use the get the inner text of the title element.

For instance, we write

const [titleElement] = document.getElementsByTagName("title");
const title = titleElement.innerText;

to get the title element with getElementsByTagName and destructuring.

Then we get the title element’s inner text value with innerText.

Conclusion

To get current HTML page title with JavaScript, we use the get the inner text of the title element.

Categories
JavaScript Answers

How to convert text to binary code in JavaScript?

Sometimes, we want to convert text to binary code in JavaScript.

In this article, we’ll look at how to convert text to binary code in JavaScript.

How to convert text to binary code in JavaScript?

To convert text to binary code in JavaScript, we use the charCodeAt method.

For instance, we write

const b = Array.from("abc")
  .map((each) => each.charCodeAt(0).toString(2))
  .join(" ");

to call Array.from with 'abc' to convert the string to an array of character strings.

Then we call map with a callback that returns the character code of the character string as a binary value with charCodeAt and toString as an array.

Next we call join to join the binary character code strings into 1 string with each value separated by a space.

Conclusion

To convert text to binary code in JavaScript, we use the charCodeAt method.

Categories
JavaScript Answers

How to test file inputs with Cypress and JavaScript?

Sometimes, we want to test file inputs with Cypress and JavaScript.

In this article, we’ll look at how to test file inputs with Cypress and JavaScript.

How to test file inputs with Cypress and JavaScript?

To test file inputs with Cypress and JavaScript, we use the cypress-file-upload package.

To install it, we run

npm install --save-dev cypress-file-upload

Then we use it by writing

import "cypress-file-upload";

const fixtureFile = "photo.png";
cy.get('[data-cy="file-input"]').attachFile(fixtureFile);

to call cy.get to select the file input with the data-cy attribute set to file-input.

And then we call attachFile to set the file input to the fixtureFile path.

Conclusion

To test file inputs with Cypress and JavaScript, we use the cypress-file-upload package.