Skip to content

Instantly share code, notes, and snippets.

@awran5
Last active February 22, 2020 21:08
Show Gist options
  • Save awran5/852a847619f3e7c0f3ecb05f400f5325 to your computer and use it in GitHub Desktop.
Save awran5/852a847619f3e7c0f3ecb05f400f5325 to your computer and use it in GitHub Desktop.
Shorthand JavaScript Functions
// Some useful shorthanded JavaScript functions
// select an element
const select = e => document.querySelector(e)
// Select multiple elements
const selectAll = e => document.querySelectorAll(e)
// Show DOM element
const show = e => (e.style.display = '')
// Hide DOM element
const hide = e => (e.style.display = 'none')
// Fade In
const fadeIn = e => (e.style = `height: 0;opacity: 0;transition: opacity 1s;`)
// Fade Out
const fadeOut = e => (e.style = `height: auto;opacity: 1;transition: opacity 1s;`)
// Get random key from an Array
const random = array => array[Math.floor(Math.random() * array.length)]
// Regex: Check if the giving String is text only
const isText = RegExp(/^[A-Z ]{3,}$/i)
// Regex: Check if the giving String is a valid Email
const isEmail = RegExp(/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$/i)
// Regex: Check if the giving String is a valid Phone
const isPhone = RegExp(/^\D?(\d{3})\D?\D?(\d{3})\D?(\d{4,6})$/)
// Regex: Check if the giving String is a Numbers onlu
const isNumber = RegExp(/^\d+$/)
// Convert first letter of a word to capital
const capitalize = word => word.charAt(0).toUpperCase() + word.slice(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment