Skip to content

Instantly share code, notes, and snippets.

View daliborgogic's full-sized avatar
:octocat:
In Git we trust!

Dalibor Gogic daliborgogic

:octocat:
In Git we trust!
View GitHub Profile
@daliborgogic
daliborgogic / router.js
Created July 14, 2023 10:32
Vue Router Back/forward cache
import { createRouter, createWebHistory } from 'vue-router'
const routes = []
const router = createRouter({
history: createWebHistory(),
routes
})
let dbPromise
@daliborgogic
daliborgogic / InputTypeFile.vue
Last active October 6, 2023 13:03
Input type file component with drag and drop (Vue.js)
<template>
<div ref="div">
<label>
<strong v-if="progress === 0">
{{ label }}
<svg width="24" height="24" viewBox="0 0 24 24"><path d="M19 13h-6v6h-2v-6H5v-2h6V5h2v6h6v2z"/><path d="M0 0h24v24H0z" fill="none"/></svg>
</strong>
<strong v-if="progress > 0 && progress < 100"> {{ progress }}%</strong>
<span v-else>{{ name }}</span>
<input ref="input" type="file" :accept="accept" @change="onFileChange"/>
@daliborgogic
daliborgogic / elementTiming.js
Last active August 18, 2023 17:09
Vue.js Element Timing API directive as plugin
export default {
install: (app, options) => {
let observer
const obj = {
created(el, binding) {
if (options?.observe) {
el.setAttribute('elementtiming', binding.arg)
observer = new PerformanceObserver(list => {
for (const entry of list.getEntries()) {
const { identifier, loadTime } = entry
@daliborgogic
daliborgogic / Dockerfile
Last active August 18, 2023 06:47
Best-Practice Docker Image and GitHub Workflow for Node.js app. [Continuous Integration/Delivery/Deployment]
ARG VERSION=12.10.0
# Development ##################################################################
FROM mhart/alpine-node:${VERSION} AS dev
WORKDIR /app
COPY package*.json .gitignore ./
ENV HOST=0.0.0.0
ENV PORT=${PORT}
RUN npm ci --prefer-offline
COPY . .
module.exports = {
apps : [{
name: 'API',
script: 'app.js',
// Options reference: https://pm2.io/doc/en/runtime/reference/ecosystem-file/
args: 'one two',
instances: 1,
autorestart: true,
watch: false,
@daliborgogic
daliborgogic / main.css
Last active March 17, 2023 22:14
Dynamic Metrics Inter font postcss
/* css variables does't work in math... */
/* using postcss-simple-vars */
$a: -0.0223;
$b: 0.185;
$c: -0.1745;
$lineHeight: 1.4;
$fontSize: 16;
@import url('https://rsms.me/inter/inter.css');
@daliborgogic
daliborgogic / vitals.js
Last active January 27, 2023 18:25
[POC] Sending Vite.js Core Web Vitals to Google Analytics
const transformer = options => ({
apply: 'post',
transform({ code, isBuild }) {
if (!isBuild) return code
return code.replace('</body>', `<script defer type="module">
const KEY = 'ga:user'
const UID = (localStorage[KEY] = localStorage[KEY] || Math.random() + '.' + Math.random())
const onError = err => console.error('[vite vitals] ', err)
const onDebug = (label, payload) => console.log(label, payload)
@daliborgogic
daliborgogic / index.js
Last active December 9, 2022 03:40
amqp async/await node.js
const amqp = require('amqplib')
const eventEmitter = require('events')
class OopsEmitter extends eventEmitter {}
const oopsEmitter = new OopsEmitter()
;(async () => {
try {
const conn = await amqp.connect('amqp://localhost?heartbeat=5s')
const ch = await conn.createChannel()
@daliborgogic
daliborgogic / i18n.js
Last active October 26, 2022 18:34
[POC] i18n in 142 bytes 💥
export default function (locale, path, ...args) {
return (path, ...args) => {
const keys = path.trim().split('.')
const fn = keys.reduce((prev, curr) => prev && prev[curr], locale)
return typeof fn === 'function' ? fn(...args) : fn
}
}
// export default function(t,e,...n){return(e,...n)=>{const r=e.trim().split(".").reduce((t,e)=>t&&t[e],t);return"function"==typeof r?r(...n):r}}
@daliborgogic
daliborgogic / package.json
Last active October 14, 2022 16:58
Checks for a valid postcode
{
"scripts": {
"test": "vitest"
},
"devDependencies": {
"vitest": "^0.24.3"
}
}