Instantly share code, notes, and snippets.

View findParentByClass.js
export const findParentByClass = (el, className) => {
while (el.parentNode) {
el = el.parentNode
if (el.classList && el.classList.contains(className)) return el
}
return null
}
View DateMask.vue
<template>
<div
class="q-date-mask"
@keyup.capture="updateValue">
<input
v-if="showDay"
ref="day"
v-model="day"
class="q-date-mask__input q-date-mask__input--day"
type="number"
View clipboard.js
export const copy = async (text) => {
try {
await navigator.clipboard.writeText(text)
console.log('Text copied:', text)
} catch (err) {
console.error('Failed to copy:', err)
}
}
export const paste = async (selector, readOnly = false) => {
View fetchy.js
/** Small module to expose get and post methods around fetch for API communication (bearer token in headers, etc.) */
const host = process.env.API_DOMAIN
const prefix = `/api/v1`
export const getData = ({ url, accessToken, paymentId, headers, noJson }) =>
request({ url, accessToken, paymentId, headers, noJson, method: 'GET' })
export const postData = ({ url, accessToken, paymentId, headers, data, method }) => {
method = method || 'POST'
View hash.js
// Generates a small hash (useful for CSRF state params, mocking a token in localStorage for development, etc.)
export const generateHash = str => '_' + Math.random().toString(36).substr(2, 9)
View isPrime.js
function isPrimeNumber(n) {
for (var i = 2; i < n; i++) { // i will always be less than the parameter so the condition below will never allow parameter to be divisible by itself ex. (7 % 7 = 0) which would return true
if (n % i === 0) return false // when parameter is divisible by i, it's not a prime number so return false
}
return n > 1 // otherwise it's a prime number so return true (it also must be greater than 1, reason for the n > 1 instead of true)
}
View keepLatest.sh
# Remove all subdirs/files except latest 5 (starting from the 6th here)
rm -r `ls -t | tail -n +6`
View authRouterGuard.js
router.beforeEach((to, from, next) => {
if (to.matched.some(record => record.meta.requiresAuth)) {
// this route requires auth, check if logged in if not, redirect to login page.
const isAuthenticated = false // TODO implement real auth check
if (!isAuthenticated) {
next({ path: '/login', query: { redirect: to.fullPath } })
} else {
next()
}
} else {
View scrollBehaviour.js
import VueRouter from 'vue-router'
export default new VueRouter({
scrollBehavior(to, from, savedPosition) {
if (to.hash) {
return { selector: to.hash }
}
return savedPosition || { x: 0, y: 0 }
}
})
View reverseObject.js
const reverseObjIntoArray = obj => Object.keys(obj).sort().reverse().map(key=> ({ key, ...obj[key] }))
reverseObjIntoArray({
124: {
a: 'foo',
b: 'bar'
},
119: {
a: 'foo',
b: 'bart'