Skip to content

Instantly share code, notes, and snippets.

View alexreardon's full-sized avatar

Alex Reardon alexreardon

View GitHub Profile
@alexreardon
alexreardon / events.js
Created February 5, 2014 21:26
add multiple window.onload and onerror events
(function(){
function addWindowEvent (event, fn) {
var old = window[event];
if (typeof old !== 'function') {
window[event] = fn;
return;
}
window[event] = function () {
old.apply(window, arguments);
@alexreardon
alexreardon / Native.js
Last active June 18, 2024 16:28
Some vanilla JS methods and patterns
// Native selectors.
(function(window, document) {
'use strict';
var noop = function() {
};
// DOCUMENT LOAD EVENTS
// not needed at the bottom of the page
document.addEventListener('DOMContentLoaded', noop);
@alexreardon
alexreardon / regex-fiddle
Created February 22, 2015 21:24
Some basic regex rules
Good tester: http://www.regexr.com/
Cheat sheet: http://ole.michelsen.dk/tools/regex.html
| = or
(ab|ba)
() = capture
(Java|Ecma)Script
(?:Java|Ecma)Script => don't care about capture
// negated character classes
@alexreardon
alexreardon / inheritance.js
Created March 4, 2015 22:29
Simple JavaScript inheritence
(function() {
var person = {
sayName: function() {
console.log('my name is ' + this.name);
}
};
var bob = Object.create(person);
bob.name = 'bob';
@alexreardon
alexreardon / Simple event system
Created September 28, 2015 06:06
Simple event system
let _callbacks = {};
const events = {
on(name, cb) {
_callbacks[name] = _callbacks[name] || [];
_callbacks[name].push(cb);
},
dispatch(name, data) {
setTimeout(function() {
(_callbacks[name] || []).forEach(cb => cb(data));
});
@alexreardon
alexreardon / data.js
Last active October 27, 2015 00:46
mixed characters es6
const data = {
'key': 'mixed-characters',
'summary': 'Привет!'
};

The 'middleware listener' pattern:
better asynchronous actions in Redux

For reference, here is a standard synchronous Redux action creator:

export const simpleAction = () => {
    return {
        type: actionTypes.PERFORM_SIMPLE_ACTION
    };
}
@alexreardon
alexreardon / get-file-stream.js
Last active December 2, 2023 12:56
rxjs file stream
// @flow
import fs from 'fs';
import { Observable } from 'rxjs';
type Options = {
highWaterMark: number,
encoding: string,
}
const defaultOptions: Options = {
import React, { Component } from 'react';
class Student extends Component<{ student: Person }> {
render() {
// Renders out a draggable student
}
}
class InnerList extends Component<{ students: Person[] }> {
// do not re-render if the students list has not changed
@alexreardon
alexreardon / lazy-lookup.js
Last active October 1, 2019 19:31
Lazy lookup
const getIdMap = memoizeOne((array) => {
return array.reduce((previous, current) => {
previous[current.id] = array[current];
return previous;
}, {});
});
const foo = { id: 'foo' };
const bar = { id: 'bar' };