Categories
Vue Answers

How to use local storage with Vue.js?

Sometimes, we want to use local storage with Vue.js.

In this article, we’ll look at how to use local storage with Vue.js.

How to use local storage with Vue.js?

To use local storage with Vue.js, we call the localStorage methods.

For instance, we write

localStorage.setItem("item", response.data);

to call localStorage.setItem with the key and value of the item.

We get the item by the key by writing

localStorage.getItem("item");

We call getItem with the key of the item.

And we remove an item from local storage by writing

localStorage.removeItem("item");

to call removeItem to remove the item with the key.

Conclusion

To use local storage with Vue.js, we call the localStorage methods.

Categories
JavaScript Answers

How to extract parameter value from URL using regular expressions and JavaScript?

Sometimes, we want to extract parameter value from URL using regular expressions and JavaScript.

In this article, we’ll look at how to extract parameter value from URL using regular expressions and JavaScript.

How to extract parameter value from URL using regular expressions and JavaScript?

To extract parameter value from URL using regular expressions and JavaScript, we use the regex match method.

For instance, we write

const regex = /http\:\/\/www\.youtube\.com\/watch\?v=([\w-]{11})/;
const url = "http://www.youtube.com/watch?v=Ahg6qcgoay4";
const [, id] = url.match(regex);

to call url.match with the regex that has a capture group ([\w-]{11}) to get the value of the v query parameter.

Then we call url.match with regex to return an array with the video’s id as the 2nd value of the array.

Conclusion

To extract parameter value from URL using regular expressions and JavaScript, we use the regex match method.

Categories
JavaScript Answers

How to compose multipart/form-data with a different Content-Type on each part with JavaScript?

Sometimes, we want to compose multipart/form-data with a different Content-Type on each part with JavaScript.

In this article, we’ll look at how to compose multipart/form-data with a different Content-Type on each part with JavaScript.

How to compose multipart/form-data with a different Content-Type on each part with JavaScript?

To compose multipart/form-data with a different Content-Type on each part with JavaScript, we call the form data append method with a blob.

For instance, we write

const formData = new FormData();

formData.append(
  "items",
  new Blob(
    [
      JSON.stringify({
        name: "Book",
        quantity: "12",
      }),
    ],
    {
      type: "application/json",
    }
  )
);

to call formData.append with the key and a blob.

In the blob, we have the JSON content as a string in an array.

And we set the MIME type of the content by setting type to "application/json".

Conclusion

To compose multipart/form-data with a different Content-Type on each part with JavaScript, we call the form data append method with a blob.

Categories
JavaScript Answers

How to hide the address bar on iPhone with JavaScript?

Sometimes, we want to hide the address bar on iPhone with JavaScript.

In this article, we’ll look at how to hide the address bar on iPhone with JavaScript.

How to hide the address bar on iPhone with JavaScript?

To hide the address bar on iPhone with JavaScript, we scroll 1 pixel down.

For instance, we write

window.scrollTo(0, 1);

to call scrollTo to scroll the browser tab or window 1 pixel down.

Conclusion

To hide the address bar on iPhone with JavaScript, we scroll 1 pixel down.

Categories
JavaScript Answers

How to use Jest to spy on a method call with JavaScript?

Sometimes, we want to use Jest to spy on a method call with JavaScript.

In this article, we’ll look at how to use Jest to spy on a method call with JavaScript.

How to use Jest to spy on a method call with JavaScript?

To use Jest to spy on a method call with JavaScript, we call the spyOn method.

For instance, we write

const spy = jest.spyOn(Component.prototype, "methodName");
const wrapper = mount(<Component {...props} />);
wrapper.instance().methodName();
expect(spy).toHaveBeenCalled();

in our test to call spyOn to add a spy to the Component‘s methodName instance method.

The we call the methodName method on the component.

And we check if it’s called with with toHaveBeenCalled.

Conclusion

To use Jest to spy on a method call with JavaScript, we call the spyOn method.