Categories
JavaScript Answers

How to convert a string into a math operator in JavaScript?

Sometimes, we want to convert a string into a math operator in JavaScript.

In this article, we’ll look at how to convert a string into a math operator in JavaScript.

How to convert a string into a math operator in JavaScript?

To convert a string into a math operator in JavaScript, we use the mathjs package.

For instance, we write

npm install mathjs

Then we write

import { evaluate } from "mathjs";

const myArray = ["225", "+", "15", "-", "10"];
const result = evaluate(myArray.join(" "));

to call evaluate with the string created by combining the substrings into 1 string with join to get the result of the expression.

Conclusion

To convert a string into a math operator in JavaScript, we use the mathjs package.

Categories
JavaScript Answers

How to do something after all files are uploaded with dropzone.js and JavaScript?

Sometimes, we want to do something after all files are uploaded with dropzone.js and JavaScript.

In this article, we’ll look at how to do something after all files are uploaded with dropzone.js and JavaScript.

How to do something after all files are uploaded with dropzone.js and JavaScript?

To do something after all files are uploaded with dropzone.js and JavaScript, we listen for the complete event.

For instance, we write

Dropzone.options.filedrop = {
  init() {
    this.on("complete", (file) => {
      if (
        this.getUploadingFiles().length === 0 &&
        this.getQueuedFiles().length === 0
      ) {
        doSomething();
      }
    });
  },
};

to set the Dropzone.options.filedrop.init property to listen to the complete event with the 'on' method.

In the event listener, we get the uploading file’s length and queued file’s length and check if they’re 0.

If they are, then all file uploads are done.

Conclusion

To do something after all files are uploaded with dropzone.js and JavaScript, we listen for the complete event.

Categories
JavaScript Answers

How to use axios download file stream and write file with Node.js and JavaScript?

Sometimes, we want to use axios download file stream and write file with Node.js and JavaScript.

In this article, we’ll look at how to use axios download file stream and write file with Node.js and JavaScript.

How to use axios download file stream and write file with Node.js and JavaScript?

To use axios download file stream and write file with Node.js and JavaScript, we set the responseType to 'stream'.

For instance, we write

const response = await axios({
  method: "get",
  url: "https://example.com/my.pdf",
  responseType: "stream",
});
response.data.pipe(fs.createWriteStream("/temp/my.pdf"));

to call axios to make a get request to a file.

We set the responseType to 'stream' so axios returns a promise with the file’s stream.

Then we call response.data.pipe with fs.createWriteStream("/temp/my.pdf") to save the stream to /temp/my.pdf.

Conclusion

To use axios download file stream and write file with Node.js and JavaScript, we set the responseType to 'stream'.

Categories
JavaScript Answers

How to use Google Text-To-Speech in JavaScript?

Sometimes, we want to use Google Text-To-Speech in JavaScript.

In this article, we’ll look at how to use Google Text-To-Speech in JavaScript.

How to use Google Text-To-Speech in JavaScript?

To use Google Text-To-Speech in JavaScript, we use the SpeechSynthesisUtterance constructor.

For instance, we write

const msg = new SpeechSynthesisUtterance("Hello World");
window.speechSynthesis.speak(msg);

to create a SpeechSynthesisUtterance object with the text string we want the computer to speak.

Then we call window.speechSynthesis.speak with msg to speak it.

Conclusion

To use Google Text-To-Speech in JavaScript, we use the SpeechSynthesisUtterance constructor.

Categories
JavaScript Answers

How to parse XLSX with Node and create JSON with JavaScript?

Sometimes, we want to parse XLSX with Node and create JSON with JavaScript.

In this article, we’ll look at how to parse XLSX with Node and create JSON with JavaScript.

How to parse XLSX with Node and create JSON with JavaScript?

To parse XLSX with Node and create JSON with JavaScript, we use the xlsx package.

To install it, we run

npm i xlsx

Then we use it by writing

const XLSX = require("xlsx");
const workbook = XLSX.readFile("Master.xlsx");
const sheetNameList = workbook.SheetNames;
console.log(XLSX.utils.sheet_to_json(workbook.Sheets[sheetnamelist[0]]));

We call readFile with the path of the file to read.

We get an array of sheet names with workbook.SheetNames.

And we parse the first sheet to JSON with XLSX.utils.sheet_to_json(workbook.Sheets[sheetnamelist[0]]).

Conclusion

To parse XLSX with Node and create JSON with JavaScript, we use the xlsx package.