Categories
JavaScript Answers

How to check if token expired using this JWT library with JavaScript?

Sometimes, we want to check if token expired using this JWT library with JavaScript.

In this article, we’ll look at how to check if token expired using this JWT library with JavaScript.

How to check if token expired using this JWT library with JavaScript?

To check if token expired using this JWT library with JavaScript, we use the jwt.verify method.

For instance, we write

const jwt = require("jsonwebtoken");

const decoded = jwt.verify(token, secret);

to call jwt.verify with token and secret to verify the JWT token string against the secret string.

It’ll also check if token has expired.

Conclusion

To check if token expired using this JWT library with JavaScript, we use the jwt.verify method.

Categories
JavaScript Answers

How to perform a global replace on string with a variable inside ‘/’ and ‘/g’ in JavaScript?

Sometimes, we want to perform a global replace on string with a variable inside ‘/’ and ‘/g’ in JavaScript.

In this article, we’ll look at how to perform a global replace on string with a variable inside ‘/’ and ‘/g’ in JavaScript.

How to perform a global replace on string with a variable inside ‘/’ and ‘/g’ in JavaScript?

To perform a global replace on string with a variable inside ‘/’ and ‘/g’ in JavaScript, we can use the RegExp constructor.

For instance, we write

const myString = "hello world test world";
const find = "world";
const regex = new RegExp(find, "g");
console.log(myString.replace(regex, "yay"));

to create a regex that matches all instances of the find string by calling it with find and 'g'.

Then we call myString.replace with regex and 'yay' to replace all instances of 'world' with 'yay'.

Conclusion

To perform a global replace on string with a variable inside ‘/’ and ‘/g’ in JavaScript, we can use the RegExp constructor.

Categories
JavaScript Answers

How to mock globals in Jest and JavaScript?

Sometimes, we want to mock globals in Jest and JavaScript.

In this article, we’ll look at how to mock globals in Jest and JavaScript.

How to mock globals in Jest and JavaScript?

To mock globals in Jest and JavaScript, we can use the spyOn and mockImplementation methods.

For instance, in the beforeAll callback, we write

jest.spyOn(window, "navigator", "get").mockImplementation(() => {
  //...
});

to call spyOn to create a mock for the window.navigator.get method.

And then we call mockImplemenetation on the returned mocked object with a callback with the mocked implementation of window.navigator.get.

Then in the afterAll callback, we write

jest.restoreAllMocks();

to clear all the mocks.

Conclusion

To mock globals in Jest and JavaScript, we can use the spyOn and mockImplementation methods.

Categories
JavaScript Answers

How to set a cookie with expire time with JavaScript?

Sometimes, we want to set a cookie with expire time with JavaScript.

In this article, we’ll look at how to set a cookie with expire time with JavaScript.

How to set a cookie with expire time with JavaScript?

To set a cookie with expire time with JavaScript, we can set the expires value in the cookie.

For instance, we write

const display = () => {
  const now = new Date();
  const time = now.getTime();
  const expireTime = time + 1000 * 36000;
  now.setTime(expireTime);
  document.cookie = `cookie=ok;expires=${now.toUTCString()};path=/`;
};

to define the display function.

In it, we set document.cookie to a string that has the expires value set to the date string converted from the now date.

We call toUTCString to get a UTC date time string.

time and expireTime are timestamps in milliseconds

Conclusion

To set a cookie with expire time with JavaScript, we can set the expires value in the cookie.

Categories
JavaScript Answers

How to get binary (base64) data from HTML5 canvas with JavaScript?

Sometimes, we want to get binary (base64) data from HTML5 canvas with JavaScript.

In this article, we’ll look at how to get binary (base64) data from HTML5 canvas with JavaScript.

How to get binary (base64) data from HTML5 canvas with JavaScript?

To get binary (base64) data from HTML5 canvas with JavaScript, we can use the canvas toDataURL method.

For instance, we write

const jpegUrl = canvas.toDataURL("image/jpeg");
const pngUrl = canvas.toDataURL();

to call toDataURL with the MIME type of the image to return.

The canvas data will be returned as a base64 string.

If no argument is set, then a PNG image base64 string is returned.

Conclusion

To get binary (base64) data from HTML5 canvas with JavaScript, we can use the canvas toDataURL method.