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
import path from 'node:path'
import { loadEnv } from 'vite'
export default ({ mode }) => {
return {
env: Object.assign(process.env, loadEnv(mode, process.cwd()))
}
}
import { createServer } from 'node:http'
import { createHmac } from 'node:crypto'
import { Printer, InMemory, Drawer, Style, Align, Model } from 'escpos-buffer'
const {
PORT = 3000,
HOST = 0,
GITHUB_WEBHOOK_SECRET,
PRINTER_MODEL = 'TM-T20',
PRINTER_COLUMNS = 56
@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 / functions.php
Last active May 6, 2021 15:46
Polylang REST API assign the translation
<?php
add_filter('rest_pre_echo_response', function($response, $object, $request) {
$post_id = $response['id'];
$type = get_post_type($post_id);
$from_post = $request->get_param('from_post');
if ($from_post && $type === 'hotels') {
pll_set_post_language($post_id, 'de');
pll_set_post_language($from_post, 'en');
pll_save_post_translations(array(
@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 / _page.vue
Last active September 3, 2020 19:38
[POC] Dynamic component by current route
<template>
<Component :is="is" />
</template>
<script>
import layout from '@/helpers/layout'
const components = {
vBuilding: () => import('@/components/pages/Building.vue' /* webpackChunkName: `building` */),
vSeminaire: () => import('@/components/pages/Seminaire.vue' /* webpackChunkName: 'components/page/seminaire' */),
vCoaching: () => import('@/components/pages/Coaching.vue' /* webpackChunkName: 'components/page/coaching' */),
document.querySelectorAll('input[type=search]').forEach(input => {
input.addEventListener('mouseup', e => {
if (input.value.length > 0) {
setTimeout(() => {
if (input.value.length === 0) {
// do reset action here
}
}, 5)
}
})
@daliborgogic
daliborgogic / localhost.sh
Created June 3, 2020 17:18
The simplest way to generate a private key and self-signed certificate for localhost is with this openssl.
openssl req -x509 -out localhost.crt -keyout localhost.key \
-newkey rsa:2048 -nodes -sha256 \
-subj '/CN=localhost' -extensions EXT -config <( \
printf "[dn]\nCN=localhost\n[req]\ndistinguished_name = dn\n[EXT]\nsubjectAltName=DNS:localhost\nkeyUsage=digitalSignature\nextendedKeyUsage=serverAuth")
alias foo=npm audit --json | grep '"type":"auditAdvisory"' | jq -r '{(.data.advisory.module_name):.data.advisory.patched_versions}' | jq -s add
@daliborgogic
daliborgogic / script.sh
Created May 8, 2020 17:00
Want to find out what files npm will publish into the tarball without actually publishing?
npm pack && tar -xvzf *.tgz && rm -rf package *.tgz