Skip to content

Instantly share code, notes, and snippets.

# preload them
source /usr/share/bash-completion/completions/git
## for `c` alias autocomplete
complete -W '$(git for-each-ref --sort=-committerdate --format="%(refname:short)" refs/heads)' -o bashdefault -o default c
## for `g` git alias (`complete -p git` output)
complete -o bashdefault -o default -o nospace -F __git_wrap__git_main g
@caub
caub / api.js
Last active December 21, 2021 17:06
Simple fetch wrapper example for your API calls
function fetchJson(path, {method, body}) {
return fetch(`${API_BASE_URL}${path}`, {
method,
// credentials: 'include', // Use either 1. this if using cookies-based authentication
headers: {
// Authorization: `Bearer ${localStorage.getItem('access_token')}`, // or 2. if using access token authentication
...body && {'Content-Type': 'application/json'},
},
body: body && JSON.stringify(body)
}).then(async r => {
@caub
caub / pages_slash_404.js
Last active May 27, 2022 04:38
Next static
import React from 'react';
import Html from '../components/html'; // wraps pages into <html> and some common layout
// you might want a webpack.config.js at least for styles
// typically we use a sass loader, and also @emotion/react in our components
// Html will take care to add <link> for built ./build/**/*.css
// and <script> for built ./build/**/*.js if any, you might even inline it <script>{content}</script> if short
// It's also possible to build css/js assets per page, we didn't do that
export async function getServerSideProps({ req }) {
@caub
caub / webpack.config.js
Last active June 6, 2021 17:17
react-scripts lite
require('dotenv/config');
const fs = require('fs-extra');
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const InterpolateHtmlPlugin = require('react-dev-utils/InterpolateHtmlPlugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
fs.emptyDirSync(__dirname + '/build');
fs.copySync(__dirname + '/public/', __dirname + '/build/', {
dereference: true,
@caub
caub / api.js
Last active October 28, 2021 18:18
Simple fetch wrapper
const API_URL = 'https://..'; // Your API base url
// returns true for plain object literals, like {foo: 'bar'}, false for other cases, like instances of classes, like lodash.isPlainObject
const isPlainObject = obj => obj && Object.getPrototypeOf(obj) === Object.prototype || Object.getPrototypeOf(obj) === null;
export function fetchJson(url, { body, headers, ...o } = {}) {
const isJson = isPlainObject(body); // most of the time we send plain 'json' objects
return fetch(url[0] === '/' ? API_URL + url : url, {
headers: {
...isJson && {'Content-Type': 'application/json'},

Mongo script gotchas

  • no console.log, use print instead
  • no Object.entries, Object.values use Object.keys
  • Map, Set are not spec-compliant, Set values are stringified, Map are not iterable, don't use them
  • for (const foo of [1]) {} is also broken, it needs a let/var instead of const (warning: let might not be block-scoped as per spec, they probably just replace it by var)
  • Use doc._id + '' to get the objectId as hex string, don't rely on template strings also, they seem to incorrectly invoke doc._id.toString() which returns 'ObjectId(hexstring)'
  • Outdated RegExp, not supporting look-behinds
@caub
caub / Sux.js
Last active June 28, 2019 20:33
simple react state management
import React from 'react';
const SuxContext = React.createContext();
export class SuxProvider extends React.Component {
state = {};
render() {
return (
<SuxContext.Provider value={{...this.state, dispatch: data => this.setState(data)}}>
const fetch = require('node-fetch');
const http = require('http');
class Express {
constructor(
notFoundHandler = (req, res) => {
res.statusCode = 404;
res.write('not found');
res.end()
},
@caub
caub / sublimetext.md
Last active April 15, 2021 19:31
VSCode's Format on save for SublimeText

requirement: TypeScript package should be installed

Step 1: Create a new plugin (Tools > Developer > new plugin) with the following, save it as format-on-save.py for example:

import sublime
import sublime_plugin

class FormatTypescriptOnSave(sublime_plugin.EventListener):
  def on_pre_save(self, view): 
@caub
caub / zendesk-chat.js
Last active November 15, 2021 17:23
Zendesk Chat API
const http = require('http');
const fetch = require('node-fetch');
const getToken = () => new Promise((resolve, reject) => {
const server = http.createServer(async (req, res) => {
res.end();
const params = new URLSearchParams(req.url.slice(1));
console.log('rec', req.url, params);
if (params.has('code')) {
resolve(params.get('code'));