Skip to content

Instantly share code, notes, and snippets.

View ZacharyGodfrey's full-sized avatar

Zachary Godfrey ZacharyGodfrey

View GitHub Profile
@ZacharyGodfrey
ZacharyGodfrey / EventListener.js
Last active December 21, 2017 17:56
Javascript pub/sub.
function eventManager(){
var set = {};
return function(e){
return {
listen(cb) {
if (typeof e !== "string" || typeof cb !== "function") return this;
set[e] = (set[e] || []).concat(cb);
return this;
},
emit(data) {
@ZacharyGodfrey
ZacharyGodfrey / LoremIpsum.txt
Created October 27, 2016 17:15
Placeholder text.
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin porttitor scelerisque lacus, ornare fermentum odio bibendum ac. Duis eget vulputate elit, dignissim tincidunt eros. In pretium dictum ipsum, et tempor velit. Aliquam tincidunt purus augue, placerat egestas neque iaculis quis. Aliquam a diam rhoncus neque semper accumsan. Morbi laoreet non mi auctor tristique. Praesent tincidunt, dolor ac posuere consectetur, enim enim viverra eros, eu dictum libero est eu massa. Vestibulum congue metus non erat sagittis varius. Nam consequat dui enim. Mauris augue eros, ultricies vitae eleifend et, mollis ultricies neque. Quisque elementum ante et est scelerisque, at condimentum ipsum ornare.
@ZacharyGodfrey
ZacharyGodfrey / Extensions.cs
Last active July 17, 2019 15:36
C# extension methods.
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
namespace Experimentation
{
public static class Extensions
{
#region IEnumerable Extensions
@ZacharyGodfrey
ZacharyGodfrey / 00-complete-fizzbuzz.js
Last active September 28, 2019 13:31
Javascript implementation of FizzBuzz.
const range = (start, end) => {
switch (true) {
case start > end: return range(end, start).reverse();
default: return Array.from({ length: end - start + 1 }, (_, index) => start + index);
}
};
const toFizzBuzz = (number) => {
const isFizz = number % 3 == 0;
const isBuzz = number % 5 == 0;
@ZacharyGodfrey
ZacharyGodfrey / Range.js
Last active June 10, 2019 20:40
Showing the evolution of my range function.
/*
The range function must take a start integer and end integer
and generate an array of the consecutive integers between them (inclusively).
Example 1: range(1, 5) == [1, 2, 3, 4, 5];
Example 2: range(2, -2) == [2, 1, 0, -1, -2];
Example 3: range(4, 4) == [4];
*/
// Method #1: an imperative approach with a loop and mutation
function range(start, end){
@ZacharyGodfrey
ZacharyGodfrey / Component.js
Last active December 15, 2017 14:21
Uses Mustache and jQuery to create declarative components
// Requires Mustache and jQuery
window.component = (function () {
var collection = {};
return function (arg) {
switch (typeof arg) {
case "string":
return collection[arg] || null;
case "object":
@ZacharyGodfrey
ZacharyGodfrey / Immutable.js
Last active January 30, 2018 03:13
JS implementation of immutable objects.
// Factory Function pattern of object creation
function newUser(id, first, last){
// Validation of inputs happens in only one place - the constructor
if (typeof id !== "number") throw new Error("Invalid ID");
if (typeof first !== "string") throw new Error("Invalid FirstName");
if (typeof last !== "string") throw new Error("Invalid LastName");
return {
// Getters return the values used to create the object
get ID(){ return id },
@ZacharyGodfrey
ZacharyGodfrey / TryCatchWrap.cs
Last active February 6, 2018 22:42
Wraps an action in a Try/Catch block for you.
private T TryCatchWrap<T>(Func<T> action, T defaultValue)
{
try
{
return action();
}
catch (Exception e)
{
// Log the exception somewhere
return defaultValue;
@ZacharyGodfrey
ZacharyGodfrey / Debounce.js
Created February 22, 2018 13:34
An alternative debounce method in JS.
// Runs <fn> immediately if not run in the last <ms> milliseconds
// Skips execution of <fn> otherwise
function debounce(fn, ms) {
var lastRun = 0;
return function () {
var now = new Date().getTime();
if (now - ms >= lastRun) {
lastRun = now;
@ZacharyGodfrey
ZacharyGodfrey / window.close.js
Last active March 16, 2018 21:24
Close a tab after opening it from another window
// On the first tab, we need to hijack any links that would open in a new tab
// and open them with JavaScript so that we can store a reference to the newly opened tab
// Vanilla JavaScript
Array.from(document.querySelectorAll("a[target='_blank']")).forEach(function(a){
a.addEventListener("click", function(e){
e.preventDefault();
e.stopPropagation();
window.newTabReference = window.open(a.attributes.href, "_blank");