Skip to content

Instantly share code, notes, and snippets.

@amdilley
amdilley / tabToggle.js
Last active December 11, 2015 20:41
Handler for switching browser tabs
function tabToggle (e) {
if (window.prevType !== e.type) {
switch (e.type) {
case 'blur':
// tab blur handler
break;
case 'focus':
// tab focus handler
break;
@amdilley
amdilley / debounce.js
Last active January 28, 2019 06:51
Sample debounce function that creates a wrapper for functions that need to execute a certain period after event listener has been triggered.
const ONE_MINUTE = 60000; // 1 minute
/**
* Restricts callback to tail end of frequently recurring event listener.
* Examples include scroll, mousemove, keydown.
* @param {Function} fn function to debounce
* @param {Number} wait interval in milliseconds to wait between fn call
* @param {Boolean} immediate whether or not to call fn in front of wait interval
*/
const debounce = (fn, wait, immediate) => {
@amdilley
amdilley / iterator.js
Last active March 24, 2016 04:34
JavaScript iterator implementation transforming arrays into iterators.
/**
* Transform any array into an iterator.
* @param {array} arr array to be transformed
* @return methods object
*/
var Iterator = function (arr) {
return {
index: -1,
hasNext: function () { return this.index < arr.length - 1; },
hasPrevious: function () { return this.index > 0; },
@amdilley
amdilley / anchorQuestions.js
Created March 30, 2016 05:19
Series of front-end questions around anchor tags testing knowledge of prototyping and event delegation.
// Write a function that takes a DOM element and determines if it's an anchor tag
function isElementAnchorTag(el) {
return el.tagName.toLowerCase() === 'a'; // toLowerCase nice to have as not all browsers necessarily have the same case for tagNames
}
// Rewrite this function as method of the Element prototype
Element.prototype.isAnchorTag = function () {
return this.tagName.toLowerCase() === 'a';
};
/*
* Verifies script has already been loaded on page.
* @param {function} test function returning true if script is on page
* @param {string} scriptURL URL of the script to load if it doesn't already exist on page
* @param {function} callback function to be executed once script has been verified
*/
function verifyScript (test, scriptURL, callback) {
var selfDestructCallback = once(callback);
if (test()) {
/**
* Given a string of only {, [, (, ), ], or } characters determine
* whether or not the parenthese are properly closed.
* @param {String} str
* value of parentheses string to validate
* @return {Boolean}
*/
const isValidParens = (str) => {
const innerParens = /(\[\]|{}|\(\))/g;
/**
* Extend funtion to curried instance.
* @param {Function} fn
* function to be curried
* @return {Function} curried function
*/
const curry = (fn, ...args) => {
return (...nArgs) => fn.apply(this, [...args, ...nArgs])
}
@amdilley
amdilley / twin_node.js
Last active January 24, 2018 01:03
Given two identical DOM trees and a node in one this will fetch the corresponding node in the other.
/**
* Extends indexOf method to array-like objects.
* @param {Object} pseudoArray
* array-like object
* @param {Object} target
* target to test existance of in pseudoArray
* @return {Number} index of target
*/
const indexOf = (pseudoArray, target) => {
return Array.prototype.indexOf.call(pseudoArray, target)
/*
* Caches results of executed functions.
* @param {function} fn function to cache
* @return {function}
*/
function cacheFn (fn) {
var cache = {};
return function () {
var argsStr = JSON.stringify(arguments);
@amdilley
amdilley / closest_ancestor.js
Last active May 25, 2016 00:50
Traverses DOM tree to find closest common ancestor of two specified nodes
/*
* Returns array containing nodes starting
* with root and traversing down to node.
* @param {node} node node to trace to root
* @return {array}
*/
function pathToNode (node) {
var path = [node];
var curr = node;