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
// 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);
}
/**
* 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);
};
// 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
// 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];
const getRandomHexColor = () => {
const randomValue = Math.floor(Math.random() * 0xffffff);
const hexValue = randomValue.toString(16);
return hexValue.padStart(6, "0");
};
const elementIsInFocus = (el) => (el === document.activeElement);
// Returns true if the element is in focus, otherwise returns false
elementIsInFocus(anyElement)
const isTabActive = () => !document.hidden;isTabActive()
// true|false
const judgeDeviceType =
() => /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|OperaMini/i.test(navigator.userAgent) ? 'Mobile' : 'PC';
judgeDeviceType()
// PC | Mobile
const copyText = async (text) => await navigator.clipboard.writeText(text)
copyText('One Line of Code Front End World')
const getSelectedText = () => window.getSelection().toString();
getSelectedText(); //Return selected content