Skip to content

Instantly share code, notes, and snippets.

View AutoSponge's full-sized avatar

Paul Grenier AutoSponge

View GitHub Profile
const debug = require('debug')(__filename.replace(/^.*[/]([^.]+)\.js/, '$1')) // file-name
const debug = require('debug')(__filename.replace(/^.*[/](.*)[/]([^.]+)\.js/, '$1:$2')) // folder:file-name
@AutoSponge
AutoSponge / esnextbin.md
Created October 11, 2016 00:52
esnextbin sketch

getting-started

(from guest wireless)

  • install homebrew
    • /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
    • install cask
      • brew tap caskroom/cask
  • install nvm
  • brew install nvm
@AutoSponge
AutoSponge / mem.js
Last active January 21, 2016 14:46
memoize that allows functions and understands context
import {List} from 'immutable'
export default fn => {
const cache = {}
const cacheKey = List()
return function (...args) {
const hashCode = cacheKey.push(this, ...args).hashCode()
if (!(hashCode in cache)) {
cache[hashCode] = fn.apply(this, args)
}
@AutoSponge
AutoSponge / 1.js
Last active December 11, 2015 18:35
handle promise or value
const getUser = async $id => {
const id = await Promise.resolve($id);
return new Promise((resolve, reject) => {
setTimeout(() => resolve({id}), 100);
});
}
const asyncId = new Promise((resolve, reject) => {
setTimeout(() => resolve(5), 100);
});
@AutoSponge
AutoSponge / x.js
Created December 8, 2015 13:51
Find what element has focus (tracking keyboard navigation)
document.addEventListener('focus', function (e) {console.clear(); console.log(e.target)}, true);
@AutoSponge
AutoSponge / map.js
Last active October 17, 2015 01:19
composing lazy evaluation functions
function* random(limit = 4) {
let count = 0
while(count < limit) {
yield Math.random();
count++;
}
}
function* map(f, gen){
let next;
(function () {
var textarea;
function createTextArea() {
if (textarea) {
return textarea;
}
textarea = document.createElement('textarea');
textarea.style.position = 'absolute';
textarea.style.left = '-9999px';
textarea.style.top = (window.pageYOffset || document.documentElement.scrollTop) + 'px';
@AutoSponge
AutoSponge / .babelrc
Last active September 24, 2018 19:19
webpack-hot-middleware with separate host
{
"stage": 0,
"env": {
"development": {
"plugins": ["react-transform"],
"extra": {
"react-transform": {
"transforms": [{
"transform": "react-transform-hmr",
"imports": ["react"],
@AutoSponge
AutoSponge / script_jwt.js
Created October 1, 2015 16:00
Babel npm scripts
#! /usr/bin/env babel-node
import dotenv from 'dotenv';
import program from 'commander';
import jwt from 'jsonwebtoken';
program
.usage('[options]')
.option('-u, --user <user id>', 'user id')
.option('-E, --env [value]', 'environment, defaults to development', 'development')
.parse(process.argv);