This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Splice in a string between `begin` and `end` indexes. | |
// Returns a new string. | |
function spliceString(string, insert, begin, end) { | |
return string.slice(0, begin) + insert + string.slice(end); | |
} | |
// Search a string with a regular expression, where `step` callback will | |
// be called at each match, allowing you to perform extra logic and create a | |
// replacement string. | |
// |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Hashtext: the no-markup markup language. | |
// Quick-and-dirty Twitter-style text rendering. | |
// | |
// * Auto-paragraphatize and linebreak newlines. | |
// * Auto-link URLs (and prettify them, too). | |
// * Expand #hashtags into something useful (your choice). | |
// | |
// Copyright (c) 2014 Gordon Brander. | |
// Released under the MIT license http://opensource.org/licenses/MIT. | |
// |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* This Source Code Form is subject to the terms of the Mozilla Public | |
* License, v. 2.0. If a copy of the MPL was not distributed with this | |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | |
/* | |
Pull structured content out of the DOM. | |
- Hero images | |
- Title | |
- Summary |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const reduce = Array.reduce; | |
// Call function `f` with value `x`. | |
const callWith = (x, f) => f(x); | |
// Pipe value `x` through many single-argument functions in succession. | |
// This is kind of like a Unix pipe. The value of the previous function is | |
// passed to the next function, etc. Note that functions are called | |
// from left-to-right, with left being first. | |
export const pipe = (x, ...f) => reduce(f, callWith, x); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Create a finite state switching function. | |
// Given a list of states, returns a function that will take a `prev` state | |
// and `next` state and will only advance state to `next` if it is one of | |
// the defined states. | |
const states = (...states) => (prev, next) => | |
states.indexOf(next) !== -1 ? next : prev; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Automatically namespace all CSS selectors. | |
Eliminates most problems with style leak | |
.active {} | |
...becomes | |
.button-active {} | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const progress = (t, start, end) => | |
t < start ? 0 : | |
t > end ? 1 : | |
(t / (end - start)); | |
export const tween = (start, end, step, state, t) => | |
step(state, progress(t, start, end)); | |
export const event = (time, step, state, t) => | |
t < time ? step(state, 0) : step(state, 1); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// A message bus. Dispatches messages to `receive` in order (FIFO). | |
const Bus = (receive) => { | |
var isDraining = false; | |
const queue = []; | |
// Define a function to send a message to the queue. | |
const send = (msg) => { | |
queue.push(msg); | |
// If we're not already draining the queue, start draining. | |
// We only want to kick this loop off once, since send can be called |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const $$ = (selector) => document.querySelector(selector); | |
export const $ = (selector) => document.querySelectorAll(selector); | |
// Apply a side effect to `n` items. | |
export const sets = (f, n, ...rest) => { | |
for (var i = 0; i < n.length; i++) f(n[i], ...rest); | |
} | |
export const toggleClass = (el, classname, isAdding) => | |
isAdding ? el.classList.add(classname) : el.classList.remove(classname); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const Spring = (x, y, mass, stiffness, viscosity) => ({ | |
prevX: x, | |
prevY: y, | |
currX: x, | |
currY: y, | |
mass, stiffness, viscosity | |
}); | |
Spring.copy = (spring) => ({ | |
prevX: spring.prevX, |