Categories
JavaScript Answers

How to use JavaScript async/await with streams?

Sometimes, we want to use JavaScript async/await with streams.

In this article, we’ll look at how to use JavaScript async/await with streams.

How to use JavaScript async/await with streams?

To use JavaScript async/await with streams, we can use the stream/promises module.

For instance, we write

const { pipeline } = require("stream/promises");

const run = async () => {
  await pipeline(
    fs.createReadStream("archive.tar"),
    zlib.createGzip(),
    fs.createWriteStream("archive.tar.gz")
  );
  console.log("Pipeline succeeded.");
};

run().catch(console.error);

to define the run function that calls pipeline with the streams inside.

We call createReadStream to create a read stream for the archive.tar file.

Then we call createGzip to create a gzip file.

And then we call createWriteStream to write the gzipped archive.tar file to archive.tar.gz.

We can use the stream/promises module since Node 15.

Conclusion

To use JavaScript async/await with streams, we can use the stream/promises module.

Categories
JavaScript Answers

How to remove spaces with JavaScript regular expression?

Sometimes, we want to remove spaces with JavaScript regular expression.

In this article, we’ll look at how to remove spaces with JavaScript regular expression.

How to remove spaces with JavaScript regular expression?

To remove spaces with JavaScript regular expression, we can use the string replace method.

For instance, we write

const str = "foo is bar".replace(/ /g, "");

to call "foo is bar".replace with the / /g regex to match all spaces and replace them all with empty strings.

The g flag will make replace replace all matches of the regex.

Conclusion

To remove spaces with JavaScript regular expression, we can use the string replace method.

Categories
JavaScript Answers

How to scale an image to fit on canvas with JavaScript?

Sometimes, we want to scale an image to fit on canvas with JavaScript.

In this article, we’ll look at how to scale an image to fit on canvas with JavaScript.

How to scale an image to fit on canvas with JavaScript?

To scale an image to fit on canvas with JavaScript, we call the canvas context drawImage method.

For instance, we write

ctx.drawImage(
  img,
  0,
  0,
  img.width,
  img.height,
  0,
  0,
  canvas.width,
  canvas.height
);

to call drawImage with the img image, the img image width and height, and the canvas width and height to scale the img image into the canvas width and height.

Conclusion

To scale an image to fit on canvas with JavaScript, we call the canvas context drawImage method.

Categories
React Native Answers

How to use border radius only for 1 corner with React Native?

Sometimes, we want to use border radius only for 1 corner with React Native.

In this article, we’ll look at how to use border radius only for 1 corner with React Native.

How to use border radius only for 1 corner with React Native?

To use border radius only for 1 corner with React Native, we can use the borderBottomLeftRadius, borderBottomRightRadius, borderTopLeftRadius, or borderTopRightRadius properties.

For instance, we write

const styles = EStyleSheet.create({
  imgBox: {
    width: px(72),
    height: px(72),
    borderBottomLeftRadius: 2,
    borderTopLeftRadius: 2,
    overflow: "hidden",
  },
  img: {
    width: px(72),
    height: px(72),
  },
});

//...

const Comp = () => {
  //...
  return (
    <View style={styles.imgBox}>
      <Image source={{ uri: "your image url" }} style={styles.img} />
    </View>
  );
};

to create a stylesheet wiht create.

We set the borderBottomLeftRadius and borderTopLeftRadius properties to set the border radius for those 2 corners in pixels.

Then we apply the imgBox style to the View which has the border radius styles.

Conclusion

To use border radius only for 1 corner with React Native, we can use the borderBottomLeftRadius, borderBottomRightRadius, borderTopLeftRadius, or borderTopRightRadius properties.

Categories
JavaScript Answers

How to save HTML page as PDF using JavaScript?

Sometimes, we want to save HTML page as PDF using JavaScript.

In this article, we’ll look at how to save HTML page as PDF using JavaScript.

How to save HTML page as PDF using JavaScript?

To save HTML page as PDF using JavaScript, we can use the html2pdf library.

We can add it with

<script src="https://cdnjs.cloudflare.com/ajax/libs/html2pdf.js/0.10.1/html2pdf.bundle.min.js" integrity="sha512-GsLlZN/3F2ErC5ifS5QtgpiJtWd43JWSuIgh7mbzZ8zBps+dvLusV+eNQATqgA/HdeKFVgA5v3S/cIrLF7QnIg==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

Then we use it by writing

const element = document.getElementById("element-to-print");
const opt = {
  margin: 1,
  filename: "myfile.pdf",
  image: { type: "jpeg", quality: 0.98 },
  html2canvas: { scale: 2 },
  jsPDF: { unit: "in", format: "letter", orientation: "portrait" },
};

await html2pdf().set(opt).from(element).save();

in an async function to select the element with getElementById.

Then we set some options in the opt object.

We set the filename of the PDF, image type and quality, scaling, margin and format.

Then we use html2pdf().set(opt).from(element).save() to save the element’s content as a PDF.

save returns a promise.

Conclusion

To save HTML page as PDF using JavaScript, we can use the html2pdf library.