Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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 / 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 / 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 / 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;
}
// Source data
var exampleData = [
{
id: 1,
first: 'Janice',
last: 'Howell',
},
{
id: 2,
@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);
}
@heyimalex
heyimalex / History.js
Created June 12, 2015 21:23
Undo/Redo helper for immutable state changes.
export default function History(initialState) {
this.reset(initialState);
}
History.prototype = {
reset: function(initialState) {
this.history = [initialState];
this.pointer = 0;
this.tail = 0;
@heyimalex
heyimalex / Semaphore.js
Last active August 29, 2015 14:11
Asynchronous concurreny control in Javascript
/**
* For limiting concurrency, Javascript style.
*
* Created for a document viewer; basically a number of thumbnails and
* a full image of the current page. "Changing the page" entailed
* changing the src of the full image, and the expectation was for that
* page to load. However, the thumbnails would saturate the browser's
* download limit, so the page wouldn't change until _all_ of the
* thumbnails had finished downloading. With 300+ page documents and