Categories
JavaScript Answers

How to get the string from a blob with JavaScript?

Sometimes, we want to get the string from a blob with JavaScript.

In this article, we’ll look at how to get the string from a blob with JavaScript.

How to get the string from a blob with JavaScript?

To get the string from a blob with JavaScript, we can use the Response object’s text method.

For instance, we write

const text = await new Response(blob).text();

to create a new Response object from blob.

Then we call text to return a promise with the string converted from the blob.

And we get the resolve value of the promise with await.

Conclusion

To get the string from a blob with JavaScript, we can use the Response object’s text method.

Categories
JavaScript Answers

How to set top and left CSS attributes with JavaScript?

Sometimes, we want to set top and left CSS attributes with JavaScript.

In this article, we’ll look at how to set top and left CSS attributes with JavaScript.

How to set top and left CSS attributes with JavaScript?

To set top and left CSS attributes with JavaScript, we can call the setProperty method.

For instance, we write

document.getElementById("divName").style.setProperty("top", "100px");

to select the element with getElementById.

Then we call its style.setProperty method with the CSS property name and value to set.

As a result, we set the top style to 100px.

Conclusion

To set top and left CSS attributes with JavaScript, we can call the setProperty method.

Categories
JavaScript Answers

How to test a Jest console.log with Jest and JavaScript?

To test a Jest console.log with Jest and JavaScript, we can spy on the console.log method.

For instance, we write

it('calls console.log with "hello"', () => {
  const consoleSpy = jest.spyOn(console, "log");

  console.log("hello");

  expect(consoleSpy).toHaveBeenCalledWith("hello");
});

to call jest.spyOn to spy on the console.log method.

Then we call console.log with the value we want to check.

Next we check the value console.log is called with with toHaveBeenCalledWith.

Categories
JavaScript Answers

How to check file size using HTML5 and JavaScript?

Sometimes, we want to check file size using HTML5 and JavaScript.

In this article, we’ll look at how to check file size using HTML5 and JavaScript.

How to check file size using HTML5 and JavaScript?

To check file size using HTML5 and JavaScript, we can use a file input and check the file’s size after selecting the file.

For instance, we write

document.getElementById("fileInput").onchange = () => {
  const fileSize = document.getElementById("fileInput").files[0].size;
  console.log(fileSize);
};

to select the file input with getElementById.

Then we set its onchange property to a function that gets the first selected file with

document.getElementById("fileInput").files[0]

when we select it.

And then we get the file’s size in bytes with size.

Conclusion

To check file size using HTML5 and JavaScript, we can use a file input and check the file’s size after selecting the file.

Categories
JavaScript Answers

How to JSON stringify a JavaScript Date and preserve time zone?

Sometimes, we want to JSON stringify a JavaScript Date and preserve time zone.

In this article, we’ll look at how to JSON stringify a JavaScript Date and preserve time zone.

How to JSON stringify a JavaScript Date and preserve time zone?

To JSON stringify a JavaScript Date and preserve time zone, we can create our own replacer function.

For instance, we write

const replacer = function (key, value) {
  if (this[key] instanceof Date) {
    return this[key].toUTCString();
  }

  return value;
};

console.log(JSON.stringify(new Date(), replacer));
console.log(JSON.stringify({ myProperty: new Date() }, replacer));

to define the replacer function.

In it, we check if the this[key] property is a Date.

this is the object being stringified.

If it is, then we stringify it as a UTC string with toUTCString.

Otherwise, we return value as is.

Then we call JSON.stringify with values we want to convert to JSON strings and use replacer to do stringify the properties or values.

Conclusion

To JSON stringify a JavaScript Date and preserve time zone, we can create our own replacer function.