Skip to content

Instantly share code, notes, and snippets.

@jhartikainen
jhartikainen / scrape.py
Created Oct 6, 2011
Very quick Battlelog soldier statistics scraper
View scrape.py
#
# 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 Feb 6, 2015
ESLint git commit hook
View commit-msg
#!/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 Aug 20, 2011
Module for loading modules dynamically in Haskell
View DynLoad.hs
{-# 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
Take screenshot with Clojure
View screen.clj
(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))))]
View gist:889f3843ba8299677f21de16afcbb882
//guess where this fails.
this.x = function() {
hello();
}
(foo || bar).call();
@jhartikainen
jhartikainen / lol.js
Last active Dec 13, 2016
JavaScript doesn't have classes lololo
View lol.js
//java
class X extends Y {
public X() {
super();
}
public void method() {
}
public static staticMethod() {
@jhartikainen
jhartikainen / foo.js
Last active Dec 4, 2016
Using angular isolated scopes as event emitters
View foo.js
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 Nov 25, 2016
Avoiding multiple asserts
View test.js
//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);
View timeouts.js
var moves = []..
function makeMove() {
var m = moves.shift();
if(!m) {
return everythingDone();
}
setTimeout(function() {
activateButton(m);
View chain.js
//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());