Skip to content

Instantly share code, notes, and snippets.

View oguzhancvdr's full-sized avatar
:electron:
Focusing

Oguzhan C. oguzhancvdr

:electron:
Focusing
View GitHub Profile
const elementIsInFocus = (el) => (el === document.activeElement);
// Returns true if the element is in focus, otherwise returns false
elementIsInFocus(anyElement)
const getRandomHexColor = () => {
const randomValue = Math.floor(Math.random() * 0xffffff);
const hexValue = randomValue.toString(16);
return hexValue.padStart(6, "0");
};
// for lightweight strings this is well-enough
const reverse = str => str.split('').reverse().join('');
// more performant way for not lightweight strings :)
const reverseString = (string) => {
let reversedString = "";
for (let i = string.length - 1; i >= 0; i--)
reversedString += string[i];
// The below source code performs 20 times faster than the one line version
const isObjectEmpty = (object) => {
if (object.constructor !== Object) return false;
// Iterates over the keys of an object, if
// any exist, return false.
for (_ in object) return false;
return true;
};
// one line version
/**
* Returns the provided URLs search parameters
* as a set of key-value pairs.
*/
const getURLParameters = (url) => {
const { searchParams } = new URL(url);
return Object.fromEntries(searchParams);
};
// for big array it is more performant
const removeDuplicates = (array) => {
const uniqueValues = [];
const seenMap = {};
for (const item of array) {
if (seenMap[item]) continue;
seenMap[item] = true;
uniqueValues.push(item);
}