Skip to content

Instantly share code, notes, and snippets.

View eiriklv's full-sized avatar

Eirik L. Vullum eiriklv

View GitHub Profile
@eiriklv
eiriklv / election.js
Created March 3, 2017 13:08
Modeling an election in JavaScript
/**
* Dependencies
*/
const sortBy = require('sort-by');
/**
* Representing an election as a data structure
*/
const votes = [
{ id: 0, voterId: 'a', zone: 'Oslo', party: 'Miljøpartiet De Grønne' },
@eiriklv
eiriklv / SyncPath.js
Created March 1, 2017 13:22 — forked from davideast/SyncPath.js
Firebase Social Network Client Fanout
export class SyncPath {
constructor(rootRef, path) {
this._rootRef = rootRef;
this.user = this._rootRef.getAuth();
this._userDataRef = this._rootRef.child(path).child(this.user.uid);
this.data = {};
this._userDataRef.on('value', (snap) => this.data = snap.val() || {});
}
keys() {
return Object.keys(this.data);

@kangax's ES6 quiz, explained

@kangax created a new interesting quiz, this time devoted to ES6 (aka ES2015). I found this quiz very interesting and quite hard (made myself 3 mistakes on first pass).

Here we go with the explanations:

Question 1:
(function(x, f = () => x) {
// Just paste this into the console on http://www.jshint.com/docs/options/
(function() {
var i, row, link, span, extraCol, checkbox, value;
var rows = document.querySelectorAll('table.options tr');
var links = document.querySelectorAll('table.options a');
// add checkboxes
for (var i = 0; i < rows.length; i++) {
row = rows[i];
@eiriklv
eiriklv / firebase_workers.js
Created January 2, 2017 11:02 — forked from anantn/firebase_workers.js
Firebase: Implementing a worker queue pattern using firebase_queue_pop.js
var Firebase = require("./firebase-node.js");
function Queue(ref) {
this._ref = ref;
}
Queue.prototype.pop = function(cb) {
this._ref.startAt().limit(1).once("child_added", this._pop.bind(this, cb));
}
@eiriklv
eiriklv / renameToHash.sh
Created November 27, 2016 09:19 — forked from SimplGy/renameToHash.sh
Rename files with a hash based on their contents. eg: `abc.jpg` to `3101ace8db9f.jpg`. Useful for detecting duplicates.
#!/bin/bash
# TODO: skip tiny files (so small they couldn't be photos)
# TODO: make sure sym links and other file system oddities are handled
#
# Constants
#
CHAR_COUNT=12
BLOCK_COUNT=6
SKIP_SIZE=3 # Every new block is sampled by skipping this amount of blocks to the next position
@eiriklv
eiriklv / main.js
Last active October 25, 2016 21:41
Handling effects declaratively with generators and custom runtimes (fun and learning inspired by redux-saga - but for broader use)
'use strict';
const { createStore, applyMiddleware } = require('./redux');
const { addActionTakersToStore, addLoggingToStore } = require('./middleware');
const { EventEmitter } = require('events');
const {
isPromise,
isFunction,
isObject,
@eiriklv
eiriklv / compose.js
Created October 24, 2016 21:46
Compose
function composeLeft(...funcs) {
return function(input) {
return funcs.reduce((result, func) => {
return func(result);
}, input);
}
}
function composeRight(...funcs) {
return function(input) {
@eiriklv
eiriklv / effects-and-descriptions.js
Last active October 21, 2016 14:25
Wrapping effects to make non-deterministic/non-pure function testable and predictable (for fun and learning)
'use strict';
/**
* Dependencies
*/
const expect = require('expect');
/**
* Factory for creating an effects handling "runtime"
* that stores a retrievable effects log
@eiriklv
eiriklv / corrective-saga.js
Last active October 20, 2016 20:22
Corrective saga
// function that creates an async operation that can either fail or succeed based on the argument (bool)
const createAsyncOperation = (opName) => (succeeds) => !succeeds ? Promise.resolve(opName + ' success') : Promise.reject(new Error(opName + ' failure'));
// corrective operations are operations that
// sematically undo the original operation
//
// i.e - you cannot unsend an email or delete the booking at an hotel,
// but you can send an apology email and cancel a reservation
//
// to enable (unlimited) of both operations and corrective operations