Skip to content

Instantly share code, notes, and snippets.

@heyimalex
heyimalex / WebStorage.js
Last active May 1, 2020 09:17
localStorage sync with redux
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);
}
// Source data
var exampleData = [
{
id: 1,
first: 'Janice',
last: 'Howell',
},
{
id: 2,
@heyimalex
heyimalex / trackable.js
Created January 13, 2016 23:35
Monkey patches webpack/tapable with some debug information
var Tapable = require("tapable");
Tapable.prototype.plugin = function plugin(name, fn) {
if(Array.isArray(name)) {
name.forEach(function(name) {
this.plugin(name, fn);
}, this);
return;
}
@heyimalex
heyimalex / pingdb.go
Created May 31, 2016 19:31
Ping database with timeout in go
func pingDatabase(db *sql.DB, seconds int) {
errc := make(chan error, 1)
go func() {
errc <- db.Ping()
}()
select {
case err := <-errc:
return err
case <-time.After(time.Second * seconds):
@heyimalex
heyimalex / example.js
Created September 21, 2016 16:15
Using mock-fs with jest
// __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 / typed-immutable.ts
Last active November 23, 2021 23:45
Pragmatic typed immutable.js records using typescript 2.1+
// 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 / jsonpp.rs
Created November 23, 2016 19:28
Streaming json pretty printer
extern crate rustc_serialize;
use std::io;
use rustc_serialize::json::Json;
fn main() {
loop {
let mut input = String::new();
io::stdin().read_line(&mut input).unwrap();
@heyimalex
heyimalex / unimported.js
Created January 31, 2017 17:51
Hacky script to find un-imported files
const fs = require('fs');
const path = require('path');
const entry = 'src\\index.js';
const importRegex = /^import.+?from ['"]([^'"]+)['"];?\s*$/gm;
function resolve(name) {
try {
@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
#!/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 / sqlmap.go
Last active April 6, 2024 14:25
Functions for querying directly into map[string]interface{}
// 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.