Skip to content

Instantly share code, notes, and snippets.

View spoike's full-sized avatar
👓
I may be slow to respond.

Mikael Brassman spoike

👓
I may be slow to respond.
View GitHub Profile
@spoike
spoike / pluckRef.js
Created May 6, 2014 14:45
Handling irregular objects in an array, where inspecting chain may lead to TypeErrors due to undefined values. Useful on REST API's that for some reason returns irregular objects in an array.
// Adds pluckRef method to lodash
// Example: _.(array, 'key1.key2.key3');
// Goes through each object of the array and attempts to pluck objects that are
// in the specified object chain path. It filters away undefined values.
_.mixin({
'pluckRef': function(arr, keyPathRef) {
var i, j, keys, key, ref, output = [];
keys = keyPathRef.split('.');
// for each object in the array
@spoike
spoike / keyframesteps.scss
Created May 19, 2014 07:59
Sass mixin that emulates a CSS3 `steps()` animation function that works in earlier IE.CSS3 `steps()` function only works with >IE10 http://caniuse.com/css-animation
@mixin keyframesteps($name, $steps, $startx, $endx) {
@keyframes #{$name}
{
@for $i from 0 through $steps {
$diff: ($endx - $startx) / $steps;
#{($i/$steps*100)}% {
$x: ($i * $diff) + $startx;
background-position: $x 0;
}
}
@spoike
spoike / chai_assert.md
Created June 26, 2014 07:02
Chai Assert Cheatsheet

Chai assert Cheatsheet

Note that the assert object is not chainable. Link to assert docs.

  • assert(expression, message)

  • assert.fail(actual, expected, [message], [operator])

  • assert.ok(object, [message])

@spoike
spoike / requireBefore.js
Created June 26, 2014 13:27
Utility method for your Mocha tests that loads in your modules with RequireJS.
// Needs RequireJS's `require` and mocha's `before`
/**
* Works like mocha's `before` function but enables you to load in dependencies
* as well.
*
* @param deps [Array] the dependencies passed on to RequireJS
* @param callback [Function] the callback with loaded dependencies as arguments
*/
requireBefore = function(deps, callback) {
@spoike
spoike / normalRandom.js
Created August 19, 2014 10:55
Generate uniform (normal distributed) random numbers. Taken from http://www.protonfish.com/random.shtml
/**
* Generates normally distributed random numbers
*/
function normalRandom(mean, stdDev) {
// Generate a "close enough" uniform random
// number w. mean = 0 and std.dev = 1
var uniformRandom = (Math.random()*2-1)+(Math.random()*2-1)+(Math.random()*2-1);
return Math.round(uniformRandom * stdDev + mean);
}
var Errors = Reflux.createActions(['error1', 'error2' /*, 'error3' */]);
var ErrorNotificationStore = Reflux.createStore({
init: function() {
_.each(Errors, function(action) {
this.listenTo(action, this.onError);
}, this);
},
onError: function(errorMessage) {
/* whatever you want to do with errorMessage and this.trigger(...) */
@spoike
spoike / hubot-fika.coffee
Created February 20, 2015 07:50
Script for celebrating the Swedish past time of "Fika"
# Description:
# Hubot delivers a fika related pic
#
# Dependencies:
# cron
# google-images
#
# Configuration:
# None
#
@spoike
spoike / hubot-make-me-a-sandwich.coffee
Created February 20, 2015 09:26
Hubot make me a sandwich!
# Description:
# Hubot make me a sandwich
#
# Dependencies:
# google-images
#
# Configuration:
# None
#
# Commands:
@spoike
spoike / is_it_friday.coffee
Last active August 29, 2015 14:16
"Is it friday?" hubot script that displays media from /r/holdmybeer if the current day is a friday
# Description:
# Hubot delivers a pic from Reddit's /r/holdmybeer frontpage if it is a friday
# based on the aww script.
#
# Dependencies:
# moment
#
# Configuration:
# None
#
@spoike
spoike / slack_post.js
Created March 2, 2015 08:14
Simple Slack Incoming Webhook script for hubot to post useful messages (in pretty format) on channels/rooms. Also works without having hubot invited on the channel.
/**
* # Post messages using Slack's Webhook
*
* Use this in combination with node-cron to Schedule messages.
* No need to have the hubot invited on the actual channel to post.
*
* ## Dependencies
*
* superagent
* lodash