Skip to content

Instantly share code, notes, and snippets.

@heyimalex
heyimalex / sqlmap.go
Last active November 22, 2022 11:35
Functions for querying directly into map[string]interface{}
View sqlmap.go
// Package sqlmap provides functions for querying directly into
// map[string]interface{}.
//
// In developing really simple api endpoints, I found the boilerplate needed
// to take the results of a database query and output them as JSON to be
// really fucking annoying; make a custom struct, scan into that struct, if
// there are multiple rows do the whole rows.Next() song and dance, and if
// anything changes update the three spots each column of the result is now
// dependent on. Even when using libraries like sqlx, there's still a lot of
// extraneous code that needs to be written.
@heyimalex
heyimalex / History.js
Created June 12, 2015 21:23
Undo/Redo helper for immutable state changes.
View History.js
export default function History(initialState) {
this.reset(initialState);
}
History.prototype = {
reset: function(initialState) {
this.history = [initialState];
this.pointer = 0;
this.tail = 0;
@heyimalex
heyimalex / github_desktop_ps_sign.py
Created March 17, 2017 16:41
Script for signing GitHub powershell scripts with code signing certificate on Windows
View github_desktop_ps_sign.py
#!/usr/bin/env python
# This script signs github for desktop powershell scripts.
#
# Powershell scripts are locked down here (via sysadmin decree), but the GitHub
# Desktop app uses powershell scripts when you right click > "Open in Git
# Shell". This is pretty much a requirement since the application can only
# handle fairly simple git operations.
#
# In order for these scripts to work they need to be signed. I don't remember
@heyimalex
heyimalex / typed-immutable.ts
Last active November 23, 2021 23:45
Pragmatic typed immutable.js records using typescript 2.1+
View typed-immutable.ts
// Pragmatic typed immutable.js records using typescript 2.1+
// Comment with any suggestions/improvements!
import * as fs from 'fs'
import { Record, Map } from 'immutable'
type Stats = fs.Stats;
// Define the basic shape. All properties should be readonly. This model
// defines a folder because it seemed easy C:
@heyimalex
heyimalex / example.js
Created September 21, 2016 16:15
Using mock-fs with jest
View example.js
// __tests__/example.js
jest.mock('fs');
it('should serve as a nice example', () => {
const fs = require('fs')
// fs can be set up at any point by calling __configureFs.
fs.__configureFs({
'/test': {
@heyimalex
heyimalex / main.rs
Created December 19, 2019 02:52
Combine a bunch of pngs into an ico file.
View main.rs
use std::{env, fs, process};
use std::error::Error;
use std::convert::{TryFrom,TryInto};
use std::io::{Write,BufWriter};
static HELP_TEXT: &str = "ico-join [<PNG>...] <DEST>
<PNG> Path to png files you want to combine.
<DEST> Path to the output file.
";
@heyimalex
heyimalex / WebStorage.js
Last active May 1, 2020 09:17
localStorage sync with redux
View WebStorage.js
export default class WebStorage {
constructor(key, storageArea = window.localStorage) {
this.key = key;
this.storageArea = storageArea;
}
load(defaultValue) {
const serialized = this.storageArea.getItem(this.key);
return serialized === null ? defaultValue : this.deserialize(serialized);
}
@heyimalex
heyimalex / runtime-env-build.js
Created June 7, 2017 22:28
create-react-app build script with optional runtime env var injection
View runtime-env-build.js
const fs = require("fs");
const crypto = require("crypto");
const spawnSync = require("child_process").spawnSync;
// Load the environment using the same code react-scripts uses.
process.env.NODE_ENV = "production";
const clientEnv = require("react-scripts/config/env")().raw;
const REACT_APP_RUNTIME = /^REACT_APP_RUNTIME_/i;
@heyimalex
heyimalex / autosave.py
Last active January 30, 2020 17:41
Nexhealth toy project
View autosave.py
import subprocess as sp
import pyautogui
import time
child = sp.Popen(["write.exe"])
time.sleep(1)
pyautogui.typewrite('Dae Choi is the best.\nGive me a job!', interval=0.1)
# Save
@heyimalex
heyimalex / PromiseState.ts
Last active June 21, 2019 19:45
Primitives for representing promise states
View PromiseState.ts
// PromiseState provides primitives for representing a promise's "state" as a
// value.
//
// That's kinda abstract so let's think through it. At any given point in time
// a promise is either pending, resolved, or rejected. There's no real
// synchronous way to access this promise "state", promises only give us `then`
// and callbacks.
//
// In react we usually care about rendering _all_ of these possible states. For
// example, we want to show a spinner when it's loading, the results when it