Skip to content

Instantly share code, notes, and snippets.

@dabroder
dabroder / express-tools.js
Created April 16, 2019 16:40
Small promises wrappers
const httpError = require('http-errors')
exports.wrap = (f, status = 200) => (req, res, next) => {
Promise.resolve(req).then(f).then(r => {
if (status === 204) res.status(204).end()
else res.status(status).json(r)
}).catch(next)
}
exports.httpError = httpError
@dabroder
dabroder / i3-gaps.sh
Created June 6, 2018 14:21
Install i3-gaps on ubuntu 18.04
#!/bin/bash
sudo apt install -y libxcb1-dev libxcb-keysyms1-dev libpango1.0-dev libxcb-util0-dev libxcb-icccm4-dev libyajl-dev libstartup-notification0-dev libxcb-randr0-dev libev-dev libxcb-cursor-dev libxcb-xinerama0-dev libxcb-xkb-dev libxkbcommon-dev libxkbcommon-x11-dev autoconf libxcb-xrm0 libxcb-xrm-dev automake
cd /tmp
# clone the repository
git clone https://www.github.com/Airblader/i3 i3-gaps
cd i3-gaps
# compile & install
@dabroder
dabroder / .editorconfig
Created April 3, 2018 12:51
.editorconfig
root = true
[*]
charset = utf-8
indent_style = space
indent_size = 2
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
@dabroder
dabroder / logger.js
Last active January 21, 2021 17:58
Simple Logger Module
const { inspect } = require('util')
const os = require('os')
const isProd = process.env.NODE_ENV === 'production'
const isDev = process.env.NODE_ENV === 'development'
const eff = f => x => ((f(x), x))
const noop = () => {}
const now = () => new Date().toISOString()
const { promisify } = require('util')
/**
* Mysql tools factory
* @param {(Connection|Pool|PoolCluster)} db Conection
* @returns {Object}
*/
module.exports = function tools (db) {
const query = promisify(db.query).bind(db)
const end = promisify(db.end).bind(db)
@dabroder
dabroder / onClose.js
Last active February 20, 2018 13:13
OnClose handler
const events = ['uncaughtException', 'SIGINT', 'SIGTERM', 'SIGQUIT', 'SIGUSR2']
module.exports = function onClose (cb) {
let called = false
const handlers = {}
events.forEach(ev => {
const handler = (...arg) => {
if (called) return
called = true
cb(ev, ...arg)
@dabroder
dabroder / index.js
Created December 28, 2017 17:00
Dotenv conditional require
if (process.env.NODE_ENV !== 'production') require('dotenv').load()
@dabroder
dabroder / debounce.js
Created November 7, 2017 21:20
small debounce implementation
export default (fn, delay, id = null) => (...arg) => {
clearTimeout(id)
id = setTimeout(fn, delay, ...arg)
}