Jon Snow
20 April 2023
const randomString = () => Math.random().toString(36).slice(2)
randomString() // gi1qtdego0b
randomString() // f3qixv40mot
randomString() // eeelv1pm3ja
const prefersDarkMode = window.matchMedia( "(prefers-color-scheme: dark)").matches;
console.log(prefersDarkMode);
const email = "selemondev19@gmail.com";
const extractDomain = (email) => email.split('@')[1];
console.log(extractDomain(email)); // "gmail.com"
const removeDuplicates = (arr) => [...new Set(arr)]
console.log(removeDuplicates([1, 2, 2, 3, 3, 4, 4, 5, 5, 6])) // [1, 2, 3, 4, 5, 6]
const redirect = (url) => window.location.href = url;
redirect("https://www.example.com");
const flat = (arr) => arr.flat();
flat(['cat', ['lion', 'tiger']]); // ['cat', 'lion', 'tiger']
const removeFalsy = (arr) => arr.filter(Boolean)
removeFalsy([0, 'a string', '', NaN, true, 5, undefined, 'another string', false]) // ['a string', true, 5, 'another string']
const random = (min, max) => Math.floor(Math.random() * (max - min + 1) + min)
random(1, 50) // 25
random(1, 50) // 34
const round = (n, d) => Number(Math.round(n + "e" + d) + "e-" + d)
round(1.005, 2) //1.01
round(1.555, 2) //1.56
const diffDays = (date, otherDate) => Math.ceil(Math.abs(date - otherDate) / (1000 * 60 * 60 * 24));
diffDays(new Date("2021-11-3"), new Date("2022-2-1")) // 90
const dayOfYear = (date) => Math.floor((date - new Date(date.getFullYear(), 0, 0)) / (1000 * 60 * 60 * 24))
dayOfYear(new Date()) // 74
const randomColor = () => `#${Math.random().toString(16).slice(2, 8).padEnd(6, '0')}`
randomColor() // #9dae4f
randomColor() // #6ef10e
const rgbToHex = (r, g, b) => "#" + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1)
rgbToHex(255, 255, 255) // '#ffffff'
const clearCookies = () => document.cookie.split(';').forEach((c) => (document.cookie = c.replace(/^ +/, '').replace(/=.*/, `=;expires=${new Date().toUTCString()};path=/`)))
const isPalindrome = str => str === str.split('').reverse().join('');
console.log(isPalindrome('racecar')); // true
console.log(isPalindrome('hello')); // false
const take = (arr, n) => arr.slice(0, n);
console.log(take([1, 2, 3, 4, 5], 3)); // [1, 2, 3]
const takeRight = (arr, n) => arr.slice(-n);
console.log(takeRight([1, 2, 3, 4, 5], 3)); // [3, 4, 5]
const isEmpty = obj => Object.keys(obj).length === 0;
console.log(isEmpty({})); // true
console.log(isEmpty({ name: 'John' })); // false
const shuffle = arr => arr.sort(() => Math.random() - 0.5);
console.log(shuffle([1, 2, 3, 4, 5])); // [2, 5, 1, 4, 3] (output will vary)
const getTime = () => new Date().toLocaleTimeString();
console.log(getTime()); // "9:30:42 PM" (output will vary based on current time)
const contains = (str, substr) => str.includes(substr);
console.log(contains('hello world', 'world')); // true
console.log(contains('hello', 'world')); // false
const removeVowels = (str) => str.replace(/[aeiou]/gi, '')
removeVowels('hello world') // 'hll wrld'
const toTitleCase = (str) => str.replace(/\w\S*/g, (txt) => txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase())
toTitleCase('the quick brown fox jumps over the lazy dog') // 'The Quick Brown Fox Jumps Over The Lazy Dog'
const currentTimeInTimezone = (timezone) => new Date().toLocaleTimeString('en-US', {timeZone: timezone})
currentTimeInTimezone('Europe/London') // "8:28:00 AM" (for the current time in London)
const toKebabCase = (str) => str.toLowerCase().replace(/\s+/g, '-')
toKebabCase('Hello World') // 'hello-world'
toKebabCase('This is a Test') // 'this-is-a-test'