Categories
JavaScript Answers

How to check the format of date with moment.js and JavaScript?

Sometimes, we want to check the format of date with moment.js and JavaScript.

In this article, we’ll look at how to check the format of date with moment.js and JavaScript.

How to check the format of date with moment.js and JavaScript?

To check the format of date with moment.js and JavaScript, we can use the moment.js’ isValid method.

For instance, we write:

const isValid = (d) => moment(d, 'DD-MMM-YYYY HH:mm a', true).isValid()

console.log(isValid('02-Mar-2022 01:01 AM'))
console.log(isValid('2022-02-03 01:01 AM'))

We define the isValid function that calls moment to try to parse the datetime string in 'DD-MMM-YYYY HH:mm a' format.

Then we call isValid to check if the datetime string is valid in the given format.

Therefore, datetimes that starts with a date, month and year separated by dashes and hours, minutes and AM or PM are valid.

As a result, the first console log is true and the 2nd one is false.

Conclusion

To check the format of date with moment.js and JavaScript, we can use the moment.js’ isValid method.

Categories
JavaScript Answers

How to detect whether a string is in URL format using JavaScript?

Sometimes, we want to detect whether a string is in URL format using JavaScript.

In this article, we’ll look at how to detect whether a string is in URL format using JavaScript.

How to detect whether a string is in URL format using JavaScript?

To detect whether a string is in URL format using JavaScript, we can use a regex.

For instance, we write:

const isUrl = (s) => {
  const regexp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/
  return regexp.test(s);
}

console.log(isUrl('http://example.com'))
console.log(isUrl('abc'))

to define the isUrl function to check if the string s matches the /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/ regex with test.

We check if s starts with 'ftp', 'http', or 'https'.

And then we check if there’re colon, 2 slashes, 'www' and characters after that.

Therefore, we see true and false logged.

Conclusion

To detect whether a string is in URL format using JavaScript, we can use a regex.

Categories
JavaScript Answers

How to create a 2D array of zeroes in JavaScript?

Sometimes, we want to create a 2D array of zeroes in JavaScript.

In this article, we’ll look at how to create a 2D array of zeroes in JavaScript.

How to create a 2D array of zeroes in JavaScript?

To create a 2D array of zeroes in JavaScript, we can use some array methods.

For instance, we write:

const zeroes = Array(3).fill().map(() => Array(3).fill(0));
console.log(zeroes)

to create an array with Array to create an array with length 3.

Then we call fill and map to fill the empty slots and map them to arrays with 0’s in it.

Therefore, zeroes is:

[
  [
    0,
    0,
    0
  ],
  [
    0,
    0,
    0
  ],
  [
    0,
    0,
    0
  ]
]

Conclusion

To create a 2D array of zeroes in JavaScript, we can use some array methods.

Categories
JavaScript Answers

How to attach an click event handler to all links with JavaScript?

Sometimes, we want to attach an click event handler to all links with JavaScript.

In this article, we’ll look at how to attach an click event handler to all links with JavaScript.

How to attach an click event handler to all links with JavaScript?

To attach an click event handler to all links with JavaScript, we can use event delegation.

For instance, we write:

<a>foo</a>
<a>bar</a>
<p>
  baz
</p>

to add some elements.

Then we write:

window.onclick = (e) => {
  if (e.target.tagName !== 'A') {
    return
  }
  console.log('clicked a')
}

to set the window.onclick property to a click event handler function.

In it, we check if tagName of the clicked element in the window is 'A'.

If it’s not, we stop running the function.

Otherwise, we run console.log.

Therefore, when we clicked an a element, we see 'clicked a' logged.

Conclusion

To attach an click event handler to all links with JavaScript, we can use event delegation.

Categories
JavaScript Answers

How to add the JavaScript equivalent of Python’s rsplit method?

Sometimes, we want to add the JavaScript equivalent of Python’s rsplit method.

In this article, we’ll look at how to add the JavaScript equivalent of Python’s rsplit method.

How to add the JavaScript equivalent of Python’s rsplit method?

To add the JavaScript equivalent of Python’s rsplit method, we can create our own function.

Fir instance, we write:

const rsplit = (str, sep, maxsplit) => {
  const split = str.split(sep);
  return maxsplit ? [split.slice(0, -maxsplit).join(sep)].concat(split.slice(-maxsplit)) : split;
}

const a = rsplit("blah,derp,blah,beep", ",", 1)
console.log(a)

to create the rsplit function.

In it, we call str.split with the sep separator to split the string into an array.

Then we combine the string with split.slice(0, -maxsplit).join(sep) concatenated with split.slice(-maxsplit) to return an array with the combined and split string.

If maxsplit isn’t set, then we return the split array as is.

Therefore, we get ['blah,derp,blah', 'beep'] from the console log.

Conclusion

To add the JavaScript equivalent of Python’s rsplit method, we can create our own function.