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 / utils.ts
Last active October 14, 2024 22:38
132 bytes i18n in Nuxt.js
// export function useI18n(translations) {
// return (key, ...args) => {
// const value = key.trim().split('.').reduce((obj, k) => obj?.[k], translations) ?? key
// return typeof value === 'function' ? value(...args) : value
// }
// }
export function useI18n(t){return(n,...e)=>{const r=n.trim().split('.').reduce(((t,n)=>t?.[n]),t)??n;return'function'==typeof r?r(...e):r}}
export const t = (x, y) => useRoute().meta.t(x, y)
import DOMPurify from 'dompurify'
export default defineNuxtPlugin(async (nuxtApp) => {
let JSDOM
if (import.meta.server) {
const { JSDOM: dom } = await import('jsdom')
JSDOM = dom
}
nuxtApp.vueApp.directive('sanitize', {
@daliborgogic
daliborgogic / package.json
Last active July 14, 2024 08:07
Pdfmake on Cloudflare Workers. Total Upload: 2324.17 KiB / gzip: 983.62 KiB
{
"type": "module",
"scripts": {
"dev": "wrangler dev --port 3000 worker.mjs",
"deploy": "wrangler deploy"
},
"devDependencies": {
"pdfmake": "^0.3.0-beta.8",
"wrangler": "^3.63.1"
}
@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 / 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 / package.json
Last active October 14, 2022 16:58
Checks for a valid postcode
{
"scripts": {
"test": "vitest"
},
"devDependencies": {
"vitest": "^0.24.3"
}
}
@daliborgogic
daliborgogic / api.mjs
Last active September 12, 2022 06:51
Gateway as a Service (GaaS)
function createApi(baseUrl, defaults = {}) {
const callable = () => {}
callable.url = baseUrl
return new Proxy(callable, {
get({ url }, key) {
const method = key.toUpperCase()
if (['GET', 'POST', 'PUT', 'DELETE', 'PATCH'].includes(method)) {
return (data, overrides = {}) => {
const payload = { method, ...defaults, ...overrides }
switch (method) {
@daliborgogic
daliborgogic / worker.mjs
Last active August 22, 2022 19:45
Prepend/appen to stream
import { createApp } from './dist/app.mjs'
import { pipeToWebWritable } from 'vue/server-renderer'
const prepend = String.raw`<!doctype html>
<html lang="en">
<title>Title</title>
<div id="app">`
const append = String.raw`</div>
<script type="importmap">
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