Last active
October 17, 2021 09:31
-
-
Save aeadedoyin/f9fb0817762bc49ad8a856cd0b16ca97 to your computer and use it in GitHub Desktop.
Cool Utils/Boilerplate for your JS(Vue) Projects
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
import jqueryLib from 'jquery' | |
import momentLib from 'moment' | |
// Extended JSON Parse | |
export const JSONParse = (str, defaultVal = []) => { | |
try { | |
if (str != null) { | |
return JSON.parse(str) | |
} else { | |
return defaultVal | |
} | |
} catch (e) { | |
return defaultVal | |
} | |
} | |
// DeAssociate Object/Array | |
export const deAssociate = (obj) => { | |
return obj ? JSON.parse(JSON.stringify(obj)) : {} | |
} | |
export const removeEmptyKeys = (obj) => { | |
return Object.fromEntries(Object.entries(obj).filter(([_, v]) => v != null && v !== '')) | |
} | |
// InitWebflow | |
export const initWebflow = () => { | |
setTimeout(() => { | |
jqueryLib(document).ready(function () { | |
if (window.Webflow !== undefined) { | |
window.Webflow.destroy() | |
window.Webflow.ready() | |
window.Webflow.require('ix2').init() | |
// window.Webflow.require('lottie').init() | |
// window.Webflow.require('slider').redraw() | |
document.dispatchEvent(new Event('readystatechange')) | |
} | |
}) | |
}, 500) | |
} | |
// Moment | |
export const moment = momentLib | |
// Intervals | |
export const interval = (callback, time) => setInterval(callback, time) | |
// Check Empty Object | |
export const isEmpty = (obj) => { | |
return Object.keys(obj).length === 0 | |
} | |
// Convert String to Number | |
export const parseNumber = (obj) => { | |
const res = {} | |
Object.keys(obj).forEach((key) => { | |
res[key] = !isNaN(obj[key]) ? | |
parseInt(obj[key], 10) : | |
obj[key] | |
}) | |
return res | |
} | |
// Rename Object Key | |
export const renameKey = (obj, oldKey, newKey) => { | |
if (oldKey !== newKey && Object.hasOwnProperty.call(obj, oldKey)) { | |
Object.defineProperty(obj, newKey, | |
Object.getOwnPropertyDescriptor(obj, oldKey)) | |
delete obj[oldKey] | |
} | |
} | |
// Rename Object Keys (Array) | |
export const renameKeys = (obj, oldKeys, newKeys) => { | |
if (oldKeys.length === newKeys.length) { | |
for (let i = 0; i < oldKeys.length; i++) { | |
renameKey(obj, oldKeys[i], newKeys[i]) | |
} | |
} | |
} | |
// Remove Object Keys | |
export const removeKeys = (obj, keys) => { | |
keys.forEach((key) => { | |
delete obj[key] | |
}) | |
} | |
// Querylize | |
export const querylize = (obj) => { | |
const str = [] | |
for (const prop in obj) { | |
if (Object.getOwnPropertyDescriptor(obj, prop)) { | |
if (obj[prop]) { | |
str.push(encodeURIComponent(prop) + '=' + encodeURIComponent(obj[prop])) | |
} | |
} | |
} | |
return '?' + str.join('&') | |
} | |
// Handle error response | |
export const errMessage = (errorObject) => { | |
if (errorObject.response) { | |
if (errorObject.response.data.errors) { | |
return errorObject.response.data.errors.join('\n') | |
} else { | |
return errorObject.response.data.message | |
} | |
} else { | |
return errorObject | |
} | |
} | |
// Validate email for edge cases | |
export const isEmail = (emailStr) => { | |
return /\S+@\S+\.\S+/.test( | |
emailStr | |
) | |
} | |
// Generate AlphaNum (Payment Refs) | |
export const generateAlphaNum = (_length) => { | |
if (typeof _length === 'string') { | |
_length = _length.length | |
} | |
let text = '' | |
const length = _length ?? 10 | |
const possible = | |
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789' | |
for (let i = 0; i < length; i++) { | |
text += possible.charAt(Math.floor(Math.random() * possible.length)) | |
} | |
return text | |
} | |
// SSR Safe Localstorage | |
export const ssrLocalStorage = { | |
getItem (key) { | |
if (process.client) { | |
return localStorage.getItem(key) | |
} | |
}, | |
setItem (key, value) { | |
if (process.client) { | |
localStorage.setItem(key, value) | |
} | |
}, | |
removeItem (key) { | |
if (process.client) { | |
localStorage.removeItem(key) | |
} | |
}, | |
clear () { | |
if (process.client) { | |
localStorage.clear() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment