Since 2018

Usefool.js

JS package that gathers snippets that I use everyday

Some simple Javascript functions very useful that I gathered and optimized through the years. I use them quite often, so I decided to pack them in a single file.

Context

I was developing some personal project when I realised that I kept searching for the same functions on Google. “get random color javascript”, “random value from an array javascript” or “clip to clipboard javascript”. So I started gathering those functions in a file to make it easier for me. Now I include this file in a lot of projects. So let’s review some of the most useful snippets from usefool.js! I hope it will help you in some way!

Result

Get a random number between two values

Yes, this function was needed as I love to use random. A must-have.

function getRandomIntBetween(min, max) {
  // Output
  return Math.floor(Math.random() * (max - min + 1) + min);
}

Probability that something happens

This one is my favorite so far. Again, super simple, and I’m not pretending that it’s the best solution, but it’s easy to read and to use. It’s calling the function getRandomIntBetween() that I presented you just before.

function probability(probability, on = 100) {
  // Var(s)
  happened = false;
  // Process
  getRandomIntBetween(0, on) <= probability
    ? (happened = true)
    : (happened = false);
  // Output
  return happened;
}

Get a random color

Quite useful in some scenario. It uses a function from the package getRandomValueFromArray()

function getRandomColor() {
  // Var(s)
  var hexLetters = ["0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"];
  var randomColor = "#";
  // Process
  for (var i = 0; i < 6; i++) {
    randomColor += getRandomValueFromArray(hexLetters);
  }
  // Output
  return randomColor;
}

Know if the color is bright or dark

Have you ever needed to dynamically know how dark is a background to chose the color of the text on it?

function isLight(color) {
  // Var(s)
  const hex = color.replace("#", "");
  const r = parseInt(hex.substr(0, 2), 16);
  const g = parseInt(hex.substr(2, 2), 16);
  const b = parseInt(hex.substr(4, 2), 16);
  // Process
  const brightness = (r * 299 + g * 587 + b * 114) / 1000;
  // Output
  return brightness > 155;
}

Know if the letter is a vowel

Everything is in the name. I used it multiple times to know if the first character of a string was a vowel or not.

function isVowel(x) {
  // Output
  return ["a", "e", "i", "o", "u", "y"].indexOf(x.toLowerCase()) !== -1;
}

Copy to clipboard

It copies whatever you want in the user's clipboard.

function copyToClipboard(value) {
  // Var(s)
  var temporaryInput = document.createElement("input");
  // Process
  temporaryInput.setAttribute("value", value);
  document.body.appendChild(temporaryInput);
  temporaryInput.select();
  // Output
  document.execCommand("copy");
  console.log(
    '%c"' + value + '" successfully copied to clipboard!',
    "color: green"
  );
  // Cleaning
  document.body.removeChild(temporaryInput);
}

Search on Google

Originally made for a joke.

function searchOnGoogle(query) { 
  window.open("https://google.com/search?q=" + query, "newTab"); 
}

...and more on the Github!

tetris cube icon

Wow, that's some serious craftmanship Thomas!