Skip to content

Instantly share code, notes, and snippets.

@gordonbrander
gordonbrander / search-and-replace.js
Created July 19, 2014 04:20
searchAndReplace(string, pattern, step): step through RegExp matches and generate replacements with callback that has full access to string indexes.
// 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.
//
@gordonbrander
gordonbrander / hashtext.js
Last active November 6, 2022 09:19
Hashtext: twitter-style text rendering for Node and the web. Moved here https://github.com/gordonbrander/hashtext.
// 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.
//
@gordonbrander
gordonbrander / domconstruct.js
Last active November 6, 2022 09:18
domconstruct.js
/* 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
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);
@gordonbrander
gordonbrander / states.js
Created July 16, 2015 22:04
Finite state switcher
// 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;
@gordonbrander
gordonbrander / css-namespace.js
Last active November 6, 2022 09:18
CSS namespacing
/*
Automatically namespace all CSS selectors.
Eliminates most problems with style leak
.active {}
...becomes
.button-active {}
*/
@gordonbrander
gordonbrander / scene.js
Last active November 6, 2022 09:19
scene.js - basic Timeline-driven animation functions
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);
@gordonbrander
gordonbrander / bus.js
Last active September 4, 2015 22:26
Simple app microframework based on a message bus
// 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
@gordonbrander
gordonbrander / yayquery.js
Created September 2, 2015 21:09
yayquery.js - All you really need
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);
@gordonbrander
gordonbrander / boing.js
Last active November 6, 2022 09:20
boing.js - minimal 2D spring physics
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,