Usefool is a small collection of plain JavaScript helpers for everyday tasks: random values, arrays, formatting, DOM styling, clipboard actions, browser utilities, and responsive checks.
It is mostly meant as a personal utility file: simple functions, no build step required, no dependency, and no framework assumptions.
Include usefool.js in a page, then call the helpers directly.
<script src="usefool.js"></script>
<script>
console.log(getRandomColor());
</script>Use usefool.min.js if you want the minified version.
Returns a random integer between min and max, including both limits. If min is greater than max, the function swaps them.
getRandomIntBetween(1, 6);
// 4Returns a random floating-point number between min and max. If decimals is provided, the result is rounded to that number of decimal places.
getRandomFloatBetween(1, 2, 2);
// 1.37Returns a random valid index from an array-like value. Returns -1 when the input is missing or empty.
getRandomIdFromArray(["red", "green", "blue"]);
// 2
getRandomIdFromArray([]);
// -1Returns a random value from an array-like value. Returns undefined when the input is missing or empty.
getRandomValueFromArray(["red", "green", "blue"]);
// "green"Returns a shuffled copy of an array. The original array is not modified.
const numbers = [1, 2, 3, 4];
shuffleArray(numbers);
// [3, 1, 4, 2]
numbers;
// [1, 2, 3, 4]Returns true randomly according to the given probability. By default, probability is read as a value out of 100.
probability(25);
// true roughly 25% of the time
probability(1, 2);
// true roughly 50% of the timeReturns a random hexadecimal color string.
getRandomColor();
// "#3ecdef"Returns a new array with duplicate primitive values removed.
uniqueArray(["a", "a", "b", "c", "c"]);
// ["a", "b", "c"]Returns a new array without falsy values such as false, 0, "", null, undefined, and NaN.
compactArray([0, "hello", false, 42, "", null]);
// ["hello", 42]Splits an array into smaller arrays of the given size.
chunkArray([1, 2, 3, 4, 5], 2);
// [[1, 2], [3, 4], [5]]Returns true when x is one of the supported consonant letters.
isConsonant("b");
// true
isConsonant("a");
// falseReturns true when x is one of the supported vowel letters: a, e, i, o, u, or y.
isVowel("u");
// true
isVowel("z");
// falseFormats a number-like value with spaces between thousands.
beautifyNumber(10000000);
// "10 000 000"Keeps a number inside a range. If the number is lower than min, it returns min; if it is higher than max, it returns max.
clampNumber(120, 0, 100);
// 100
clampNumber(-10, 0, 100);
// 0Rounds a number to a given number of decimal places.
roundNumber(3.14159, 2);
// 3.14
roundNumber(3.7);
// 4Maps a number from one range to another.
mapNumber(5, 0, 10, 0, 100);
// 50Counts how many times character appears in string.
countCharacter("banana", "a");
// 3Uppercases the first character of a string and keeps the rest unchanged.
capitalize("hello");
// "Hello"Converts a string into a lowercase URL-friendly slug. Accents are removed, spaces and punctuation become hyphens.
slugify("Hello, cafe!");
// "hello-cafe"Shortens a string to a maximum length. If truncation happens, the suffix is added when there is enough room.
truncate("Too much text", 8);
// "Too m..."
truncate("Too much text", 8, "!");
// "Too muc!"Escapes HTML-sensitive characters so a string can be displayed as text instead of being interpreted as markup.
escapeHtml('<button class="danger">Delete</button>');
// "<button class="danger">Delete</button>"Returns true when a hexadecimal color is visually light. Supports both short and long hex formats.
isLight("#ffffff");
// true
isLight("#111");
// falseApplies a random organic border-radius value to an element.
const card = document.querySelector(".card");
transformToBlob(card);
// card.style.borderRadius is now a random blob-like shapeSets a CSS custom property on an element. By default, it writes to document.documentElement, which means the variable is available globally.
setCssVariable("--brand-color", "#3ecdef");
// document.documentElement now has --brand-color set to "#3ecdef"Reads a CSS custom property from an element and returns the trimmed value.
getCssVariable("--brand-color");
// "#3ecdef"Copies a value to the clipboard using the modern Clipboard API. Returns a promise that resolves to true when copying worked, or false when the Clipboard API is unavailable.
This requires a secure context, such as HTTPS or localhost.
await copyToClipboard("Hello clipboard");
// trueOpens a new tab with a Google search for the given query.
searchOnGoogle("useful javascript helpers");
// Opens https://google.com/search?q=useful%20javascript%20helpersOpens a new tab with a Google Images search for the given query.
searchOnGoogleImage("blue gradient");
// Opens a Google Images searchOpens a URL in a new tab.
openUrl("https://example.com");
// Opens https://example.comReads a query parameter from a URL. If no URL is provided, it reads from the current page URL.
getUrlParameter("page", "https://example.com/articles?page=2");
// "2"Changes the page title when the window loses focus, then restores the original title when the window gets focus again.
changeTitleOnBlur("Come back!");
// The document title changes to "Come back!" when the tab is blurred.Returns true when the current browser looks like a mobile or touch-oriented device.
isMobile();
// true or falseReturns a promise that resolves after the given number of milliseconds. Useful inside async functions.
await sleep(500);
// waits for half a secondReturns a debounced version of a function. The callback runs only after calls have stopped for the given delay.
const saveSearch = debounce(function (query) {
console.log(query);
}, 300);
saveSearch("use");
saveSearch("useful");
// Only "useful" is logged after 300msReturns a throttled version of a function. The callback runs immediately, then at most once per delay.
const onScroll = throttle(function () {
console.log(window.scrollY);
}, 200);
window.addEventListener("scroll", onScroll);
// Logs scroll position at most once every 200msThe JavaScript code is released under the MIT License. See LICENSE.
The ASCII arts are credited in this README when the author is known.