Categories
JavaScript Answers

How to identify card type from card number with JavaScript?

Sometimes, we want to identify card type from card number with JavaScript.

In this article, we’ll look at how to identify card type from card number with JavaScript.

How to identify card type from card number with JavaScript?

To identify card type from card number with JavaScript, we can check against regexes for card numbers of different types of cards.

For instance, we write:

const getCardType = (number) => {
  if (number.match(new RegExp("^4")) !== null) {
    return "Visa";
  } else if (/^(5[1-5][0-9]{14}|2(22[1-9][0-9]{12}|2[3-9][0-9]{13}|[3-6][0-9]{14}|7[0-1][0-9]{13}|720[0-9]{12}))$/.test(number)) {
    return "Mastercard";
  } else if (number.match(new RegExp("^3[47]")) !== null) {
    return "AMEX";
  } else if (number.match(new RegExp("^(6011|622(12[6-9]|1[3-9][0-9]|[2-8][0-9]{2}|9[0-1][0-9]|92[0-5]|64[4-9])|65)")) !== null) {
    return "Discover";
  } else if (number.match(new RegExp("^36")) !== null) {
    return "Diners";
  } else if (number.match(new RegExp("^30[0-5]")) !== null) {
    return "Diners - Carte Blanche";
  } else if (number.match(new RegExp("^35(2[89]|[3-8][0-9])")) !== null) {
    return "JCB";
  } else if (number.match(new RegExp("^(4026|417500|4508|4844|491(3|7))")) !== null) {
    return "Visa Electron";
  }
  return "";
}

console.log(getCardType('5105105105105100'))
console.log(getCardType('4111111111111111'))

We define the getCardType function that takes the card number.

Then we use a series of if-else statements to check whether number matches regexes for different card types with the string’s match method.

If match returns anything other than null, that means the number matches the given regex pattern.

Therefore, 'Mastercard' and 'Visa' are logged since we the first number is a test Mastercard number and the 2nd is a test Visa card number.

Conclusion

To identify card type from card number with JavaScript, we can check against regexes for card numbers of different types of cards.

Categories
JavaScript Answers Nodejs

How to decode base64 to hexadecimal string with JavaScript and Node.js?

Sometimes, we want to decode base64 to hexadecimal string with JavaScript and Node.js.

In this article, we’ll look at how to decode base64 to hexadecimal string with JavaScript and Node.js.

How to decode base64 to hexadecimal string with JavaScript and Node.js?

To decode base64 to hexadecimal string with JavaScript and Node.js, we can use the Buffer.from method to convert the base64 string to a buffer.

Then we can call the buffer’s toString method with 'hex' to convert it to a hex string.

For instance, we write:

const rawData = 'data:image/png;base64, iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=='
const buffer = Buffer.from(rawData, 'base64');
const bufString = buffer.toString('hex');
console.log(bufString)

to call Buffer.from with the rawData base64 string and 'base64' to convert the rawData base64 string to a buffer.

Then we call buffer.toString with 'hex' to convert the buffer to a hex string.

As a result, we get that bufString is '75ab5a8a66a07bfa6781b6ac7bae22541391c3428682800000035252111480000001400000014201800000235bc9b940000007125110550235d8fe3fffcff0dfc1880170c804a1340c7c609633410003bd4d72f4638387c0000000125153912b909820'.

Conclusion

To decode base64 to hexadecimal string with JavaScript and Node.js, we can use the Buffer.from method to convert the base64 string to a buffer.

Then we can call the buffer’s toString method with 'hex' to convert it to a hex string.

Categories
JavaScript Answers

How to convert file size to MB in JavaScript?

Sometimes, we want to convert file size to MB in JavaScript.

In this article, we’ll look at how to convert file size to MB in JavaScript.

How to convert file size to MB in JavaScript?

To convert file size to MB in JavaScript, we can divide the number of bytes by 1024 raise to the power of 2.

For instance, we write:

const bytesToMegaBytes = bytes => bytes / (1024 ** 2);
console.log(bytesToMegaBytes(4675758))

to define the bytesToMegaBytes function that divide bytes by 1024 ** 2.

Therefore, we get 4.459150314331055 logged as a result.

Conclusion

To convert file size to MB in JavaScript, we can divide the number of bytes by 1024 raise to the power of 2.

Categories
JavaScript Answers

How to get the week number of the month of a given date with JavaScript?

Sometimes, we want to get the week number of the month of a given date with JavaScript.

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

How to get the week number of the month of a given date with JavaScript?

To get the week number of the month of a given date with JavaScript, we can calculate it by getting the first date of the month of the given date.

Then we add the day of the month of the given date plus the first day of the month minus 1, divide everything by 7 and take the ceiling of the whole expression.

For instance, we write:

const getWeekNumOfMonthOfDate = (d) => {
  const firstDay = new Date(d.getFullYear(), d.getMonth(), 1).getDay();
  return Math.ceil((d.getDate() + (firstDay - 1)) / 7);
}

const weekNumOfDate = getWeekNumOfMonthOfDate(new Date(2022, 1, 20))
console.log(weekNumOfDate)

to define the getWeekNumOfMonthOfDate function to do the calculation.

We get the first day of the month which is given by date d with:

const firstDay = new Date(d.getFullYear(), d.getMonth(), 1).getDay()

Then we add the date of the month with (d.getDate() + (firstDay - 1)).

And then we get the week number by dividing by 7.

Finally, we take the ceiling of the with Math.ceil to round that up to a whole number.

Therefore, the console log should log 3 since Feb 20, 2022 is in the 3rd week of February, 2022.

Conclusion

To get the week number of the month of a given date with JavaScript, we can calculate it by getting the first date of the month of the given date.

Then we add the day of the month of the given date plus the first day of the month minus 1, divide everything by 7 and take the ceiling of the whole expression.

Categories
JavaScript Answers

How to use Promise.race() with JavaScript?

Sometimes, we want to use Promise.race() with JavaScript.

In this article, we’ll look at how to use Promise.race() with JavaScript.

How to use Promise.race() with JavaScript?

To use Promise.race() with JavaScript, we can call it with an array of promises.

For instance, we write:

const p1 = new Promise((resolve, reject) => {
  setTimeout(resolve, 500, 'one');
});
const p2 = new Promise((resolve, reject) => {
  setTimeout(resolve, 100, 'two');
});

(async () => {
  const value = await Promise.race([p1, p2])
  console.log(value);
})()

We call Promise.race with [p1, p2] which will return the promise that is settled first.

Therefore, value should be 'two' since the setTimeout callback is run in 100 seconds after the callback is queued for running.

Conclusion

To use Promise.race() with JavaScript, we can call it with an array of promises.