Skip to content

Instantly share code, notes, and snippets.

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 / 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
@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 / 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 / indexesof.js
Created July 18, 2014 17:46
indexesOf: collect all indexes of pattern found in string.
// Returns an array of all indexes at which `pattern` can be found in `string`.
function indexesOf(string, pattern) {
var i = -1;
var indexes = [];
// We mutate `i` in place with the result of `indexOf`. We also use the last
// value of `i + 1` to continue seeking from. Any index found is pushed into
// the `indexes` array. If we ever get `-1` as the result of `indexOf`, we
// stop looping.
while((i = string.indexOf(pattern, i + 1)) !== -1) indexes.push(i);
return indexes;
@gordonbrander
gordonbrander / route.js
Last active April 30, 2022 01:26
route.js: a history.pushState microrouter without the routing fluff.
// Microrouter based on history.pushState.
// All thrills, no frills.
// Usage:
//
// var h = urlstate(callback);
// h.push('#foo')
function urlstate(callback) {
// Since `history.pushState` doesn't fire `popstate`, we can use this function
// instead. Will update history state and call `callback` with currently
// showing `url`.
@gordonbrander
gordonbrander / foldable.js
Last active November 6, 2022 07:34
Foldable -- fold elements based on search strings in hash
// Foldable.js
// =============================================================================
//
// Fold DOM elements based on whether they match a query string.
//
// How:
//
// var foldableParagraphs = document.querySelectorAll('p.foldable');
// foldable(foldableParagraphs)
//
@gordonbrander
gordonbrander / foldable.litcoffee
Last active August 29, 2015 14:03
Foldable -- fold elements based on textContent matches

Basic stuff

Add an element to an indexed object, mutating it. Returns object.

add = (indexed, item) ->
  Array.prototype.push.call(indexed, item)
  indexed

Generic reduction over anything. This will come in handy for iterating

@gordonbrander
gordonbrander / kv.js
Last active May 4, 2023 22:31
KV: key-value iteration functions for JavaScript
// KV.js
// Hash iteration functions
// Set a value on an object at field, returning object.
function set(object, key, value) {
// Set value on object at key.
object[key] = value;
// Return object.
return object;
}
@gordonbrander
gordonbrander / 1d-spring.js
Created June 3, 2014 20:31
1D Spring integration
function dampenedHookeForce(displacement, velocity, stiffness, damping) {
// Hooke's Law -- the basic spring force.
// <http://en.wikipedia.org/wiki/Hooke%27s_law>
//
// F = -kx
//
// Where:
// x is the vector displacement of the end of the spring from its equilibrium,
// k is a constant describing the tightness of the spring.
var hookeForce = -1 * (stiffness * displacement);