Skip to content

Instantly share code, notes, and snippets.

@jhartikainen
jhartikainen / scrape.py
Created October 6, 2011 20:43
Very quick Battlelog soldier statistics scraper
#
# This script scrapes your soldier statistics from Battlelog
# NOTE: This is just a very quick demonstration, it doesn't really do much as-is, besides
# authenticate to Battlelog and get you a dict of your soldier's statistics.
# There is no error handling, no anything. It will only work if everything goes well.
#
#
# Input your Origin username and password into the params dict below and it should work.
#
# In the very bottom, the stats dict will contain the data from the Battlelog JSON response.
@jhartikainen
jhartikainen / commit-msg
Created February 6, 2015 17:46
ESLint git commit hook
#!/bin/bash
files=$(git diff --cached --name-only | grep '\.js$')
# Prevent ESLint help message if no files matched
if [[ $files = "" ]] ; then
exit 0
fi
echo $files | xargs eslint
@jhartikainen
jhartikainen / DynLoad.hs
Created August 20, 2011 11:31
Module for loading modules dynamically in Haskell
{-# LANGUAGE ScopedTypeVariables #-}
module DynLoad (
loadSourceGhc,
execFnGhc
) where
import Control.Exception (throw)
import GHC hiding (loadModule)
import GHC.Paths (libdir)
import HscTypes (SourceError, srcErrorMessages)
@jhartikainen
jhartikainen / screen.clj
Created May 31, 2012 14:26
Take screenshot with Clojure
(import
'(java.awt Rectangle Dimension Robot Toolkit)
'(java.awt.image BufferedImage)
'(java.io File IOException)
'(javax.imageio ImageIO))
(defn take-screenshot []
(let [screen (.getScreenSize (Toolkit/getDefaultToolkit))
rt (new Robot)
img (.createScreenCapture rt (new Rectangle (int (.getWidth screen)) (int (.getHeight screen))))]
//guess where this fails.
this.x = function() {
hello();
}
(foo || bar).call();
@jhartikainen
jhartikainen / lol.js
Last active December 13, 2016 01:36
JavaScript doesn't have classes lololo
//java
class X extends Y {
public X() {
super();
}
public void method() {
}
public static staticMethod() {
@jhartikainen
jhartikainen / foo.js
Last active December 4, 2016 14:04
Using angular isolated scopes as event emitters
someModule.service('something', function($rootScope) {
var emitter = $rootScope.$new(true);
var data = whateverStuffHere;
return {
onDataUpdated: function(callback) {
//by returning the result from $on, we get
//the ability to remove event listeners easily
return emitter.$on('update', callback);
@jhartikainen
jhartikainen / test.js
Last active November 25, 2016 08:43
Avoiding multiple asserts
//easy way to check if object has the expected "shape":
it('tests something', function() {
var expectedObject = {
some: 'prop',
values: 'here'
};
var result = doStuff();
assert.deepEqual(result, expectedObject);
@jhartikainen
jhartikainen / timeouts.js
Created September 5, 2016 23:50
Chained timeouts
var moves = []..
function makeMove() {
var m = moves.shift();
if(!m) {
return everythingDone();
}
setTimeout(function() {
activateButton(m);
@jhartikainen
jhartikainen / chain.js
Created September 5, 2016 23:11
Chained promises
//let's say all the moves are in this array
var moves = [...];
moves.reduce(function(p, move) {
return p.then(activateWithDelay(move.button))
.then(deactivateWithDelay(move.button));
}, Promise.resolve());