Categories
JavaScript Answers

How to read and write file lines with Node.js and JavaScript?

Sometimes, we want to read and write file lines with Node.js and JavaScript.

In this article, we’ll look at how to read and write file lines with Node.js and JavaScript.

How to read and write file lines with Node.js and JavaScript?

To read and write file lines with Node.js and JavaScript, we can call the readFileSync method to read the file into a string.

Then we call JavaScript string’s split method to split the string into an array.

And we loop through the array to write each line to the file with appendFileSync.

For instance, we write:

const fs = require("fs");

for (const line of fs.readFileSync('./input.txt').toString().split('\n')) {
  console.log(line);
  fs.appendFileSync("./output.txt", line.toString() + "\n");
};

We call readFileSync with the path to the file.

Then we call toString to convert the file into a string.

Next, we call split to split the string into an array.

Then we use the for-of loop to loop through each line.

And finally, we call appendFileSync to write the line into the output.txt file.

Conclusion

To read and write file lines with Node.js and JavaScript, we can call the readFileSync method to read the file into a string.

Then we call JavaScript string’s split method to split the string into an array.

And we loop through the array to write each line to the file with appendFileSync.

Categories
JavaScript Answers

How to convert binary data to base64 with JavaScript?

Sometimes, we want to convert binary data to base64 with JavaScript.

In this article, we’ll look at how to convert binary data to base64 with JavaScript.

How to convert binary data to base64 with JavaScript?

To convert binary data to base64 with JavaScript, we can use a FileReader instance.

For instance, we write:

<input type="file" />

to add a file input.

Then we write:

const input = document.querySelector('input')
const reader = new FileReader()
reader.onload = (e) => {
  console.log(e.target.result);
}

input.onchange = (e) => {
  const [file] = e.target.files
  reader.readAsDataURL(new Blob([file]));
}

to select the input with document.querySelector.

Then we create the FileReader instance and set its onload property to a function that has the file reader result.

Then we set the input.onchange property to a function that gets the file from the input.

And then we call reader.readAsDataURL with a Blob instance containing the file.

Conclusion

To convert binary data to base64 with JavaScript, we can use a FileReader instance.

Categories
JavaScript Answers

How to get the week of the month with JavaScript?

Sometimes, we want to get the week of the month with JavaScript.

In this article, we’ll look at how to get the week of the month with JavaScript.

How to get the week of the month with JavaScript?

To get the week of the month with JavaScript, we can use some date methods.

For instance, we write:

const d = new Date();
const date = d.getDate();
const day = d.getDay();
const weekOfMonth = Math.ceil((date - 1 - day) / 7);
console.log(weekOfMonth)

We use the getDate and getDay methods to get the day of the month and the day of the week respectively.

Then we use Math.ceil((date - 1 - day) / 7) to get the week of the month by subtracting the date from 1 + day divided by 7.

Conclusion

To get the week of the month with JavaScript, we can use some date methods.

Categories
JavaScript Answers

How to detect paste in input box with JavaScript?

Sometimes, we want to detect paste in input box with JavaScript.

In this article, we’ll look at how to detect paste in input box with JavaScript.

How to detect paste in input box with JavaScript?

To detect paste in input box with JavaScript, we can listen to the paste event of the input.

For instance, we write:

<input type="text" />

to add an input.

Then we write:

const input = document.querySelector('input')
input.onpaste = (e) => {
  setTimeout(() => {
    console.log(e.target.value)
  }, 0)
}

to select the input with document.querySelector.

Then we set the input.onpaste property to a function that gets the pasted value from e.target.value.

We put the log inside the setTimeout callback since the input is only updated one tick after pasting the content.

Conclusion

To detect paste in input box with JavaScript, we can listen to the paste event of the input.

Categories
JavaScript Answers

How to get an element position relative to the top of the viewport with JavaScript?

Sometimes, we want to get an element position relative to the top of the viewport with JavaScript.

In this article, we’ll look at how to get an element position relative to the top of the viewport with JavaScript.

How to get an element position relative to the top of the viewport with JavaScript?

To get an element position relative to the top of the viewport with JavaScript, we can add the top CSS value to the pageYOffset value.

For instance, we write:

<p>
  hello
</p>

to add a p element.

Then we write:

const el = document.querySelector('p')
const t = el.getBoundingClientRect().top +
  el.ownerDocument.defaultView.pageYOffset
console.log(t)

to select the p element with querySelector.

Then we get the p element’s position relative to the top of the viewport by writing:

const t = el.getBoundingClientRect().top +
  el.ownerDocument.defaultView.pageYOffset

Conclusion

To get an element position relative to the top of the viewport with JavaScript, we can add the top CSS value to the pageYOffset value.