Last active
March 22, 2024 05:20
-
-
Save ashwin1014/59df8587e02973d8582ed3c7fdcabf41 to your computer and use it in GitHub Desktop.
Some handy javascript utility functions to use in a project
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const isEmpty = (obj) => [Object, Array].includes((obj || {}).constructor) && !Object.entries(obj || {}).length; | |
export const uid = () => Date.now().toString(36) + Math.random().toString(36).substr(2, 5); | |
export const uniqueObjArray = (arr, prop) => { | |
return Array.from(new Set(arr.map((a) => a[prop]))).map((item) => { | |
return arr.find((a) => a[prop] === item); | |
}); | |
}; | |
export const uniq = (arr) => { | |
return [...new Set(arr)]; | |
}; | |
export const omitValue = (key, obj) => { | |
const { [key]: _, ...rest } = obj; | |
return rest; | |
}; | |
export const headOrTail = (arr, type = 'head') => { | |
const [head, ...tail] = arr; | |
if (type === 'tail') return tail; | |
return head; | |
}; | |
export const times = (num) => { | |
return [...Array(num).keys()]; | |
}; | |
export const fillArray = (value, len) => { | |
return [...Array(len).keys()].map((_x) => value); | |
}; | |
export const noop = () => undefined; | |
export const removeArrayItemByIndex = (arr, index) => { | |
return [...arr.slice(0, index), ...arr.slice(index + 1)]; | |
}; | |
export const addArrayItemByIndex = (arr, index, newItem) => [ | |
...arr.slice(0, index), | |
newItem, | |
...arr.slice(index), | |
]; | |
export const initialsGenerator = (name) => { | |
const nameSplit = name.split(' '); | |
return nameSplit[0].charAt(0).toUpperCase() + (nameSplit[1] ? nameSplit[1].charAt(0).toUpperCase() : nameSplit[0].charAt(1).toLowerCase()); | |
}; | |
export const getKeyByValue = (object, value) => { | |
return Object.keys(object).find((key) => object[key] === value); | |
}; | |
export const loadScript = (src, id = null, async = false) => { | |
return new Promise((resolve) => { | |
const script = document.createElement('script'); | |
script.src = src; | |
script.async = async; | |
if (id) { | |
script.id = id; | |
} | |
script.onload = () => { | |
resolve(true); | |
}; | |
script.onerror = () => { | |
resolve(false); | |
}; | |
document.body.appendChild(script); | |
}); | |
}; | |
export const removeScriptById = (id) => { | |
const script = document.querySelector(`#${id}`); | |
script.parentNode.removeChild(script); | |
}; | |
export const getUrlParams = (param) => new URLSearchParams(window.location.search).get(param); | |
export const getUrlPathName = () => window.location.pathname; | |
export const capitalFirst = (name) => { | |
if (typeof name !== 'string') return ''; | |
return name.charAt(0).toUpperCase() + name.slice(1); | |
}; | |
export const scrollToTop = () => window.scrollTo({ top: 0, behavior: 'smooth' }); | |
export const getRandomNumber = (max) => Math.floor(Math.random() * Math.floor(max)); | |
export const generateQueryString = (params) => | |
Object.keys(params) | |
.map((key) => `${encodeURIComponent(key)}=${encodeURIComponent(params[key])}`) | |
.join('&'); | |
export const removeUnderscore = (name) => { | |
if (typeof name !== 'string') return ''; | |
return name.replace(/_/g, ' '); | |
}; | |
const arrayEquality = (a,b) => { | |
if (a.length !== b.length) return false; | |
a.sort(); | |
b.sort(); | |
return a.every((ele, index) => { | |
return ele === b[index]; | |
}) | |
} | |
const memoize = (fn) => { | |
const cache = {}; | |
return (arg) => { | |
if (cache[arg] === undefined) { | |
cache[arg] = fn(arg); | |
} | |
return cache[arg]; | |
}; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment