Skip to content

Instantly share code, notes, and snippets.

function opinionSort(items = []) {
if (items.length === 0) {
return [];
}
let left = [];
let right = [];
const pivot = items[0];
for (let i=1, l=items.length; i<l; i++) {
const isLessThanPivot= window.confirm(`Confirm if you like ${pivot} more
// A simple, insecure hashing function
function hashCode(value){
// Include the year in the hash function so the output will
// change every Christmas
value+= new Date().getFullYear();
let hash = 0;
if (value.length == 0) return hash;
for (i = 0; i < value.length; i++) {
char = value.charCodeAt(i);
<main>
<p id="result"></p>
</main>
<script>
var testRoot = document.querySelector('main');
var result = document.querySelector('#result');
function addMessage(text) {
var el = document.createElement('p');
el.textContent = text;
function combineFunctions(fn1, fn2, context) {
context = context || null;
return function() {
var args = toArray(arguments);
fn1.apply(context, args);
return fn2.apply(context, args);
}
}
@codyromano
codyromano / GlobalEventListener.js
Last active September 20, 2016 02:06
An event listener that works even if the element hasn't been added to the page yet
/**
* @module GlobalEventListener
*/
(function(MutationObserver, exports) {
'use strict';
var proto = GlobalEventListener.prototype;
/**
* @this GlobalEventListener
/*
* The problem:
* Imagine you are on a long business trip with many connecting
* flights. You have a handful of tickets - each with an origin city
* and a destination - but the tickets are not in order. This algorithm
* sorts the tickets chronologically, starting with your home city and
* ending with your final destination. See bottom for an example.
*/
/*
/**
* @desc A type-aware wrapper around window.localStorage
* @module Store
*/
(function(exports) {
'use strict';
var proto = Store.prototype;
/* Name of the properties that store type annotations. These
// Test object featuring a variety of basic data types
var person = {
name: 'Jim',
age: 30,
city: 'Chicago',
favoriteFoods: [
{
name: 'Pizza',
cost: 5
},
interface StoreOptions {
/* The configuration object passed to the constructor of Store.
This is where you specify the application's initial state
along with reducers, pure functions that handle actions. */
state: StoreState;
reducers: Array<StoreReducer>;
}
interface StoreAction {
/* Actions are object literals that basically express how the application
interface BackoffConfig {
// How many times a process can be retried before it fails permanently
attempts?: number;
// How long a process can run per attempt
timeout?: number;
// Pause between attempts
delay?: number;
// Controls how quickly the delay will increase with each attempt
backoffExponent?: number;
// For a good explanation of decorrelated jitter, see: