Add an element to an indexed object, mutating it. Returns object.
add = (indexed, item) ->
Array.prototype.push.call(indexed, item)
indexedGeneric reduction over anything. This will come in handy for iterating
| 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 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 |
| // 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. | |
| // |
| // 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. | |
| // |
| // 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; |
| // 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`. |
| // Foldable.js | |
| // ============================================================================= | |
| // | |
| // Fold DOM elements based on whether they match a query string. | |
| // | |
| // How: | |
| // | |
| // var foldableParagraphs = document.querySelectorAll('p.foldable'); | |
| // foldable(foldableParagraphs) | |
| // |
| // 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; | |
| } |
| 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); |