Skip to content

Instantly share code, notes, and snippets.

@jimkang
jimkang / mailstuff.sh
Created September 3, 2013 19:08
Bash script that emails what you pipe to it via stdin. (Probably a jillion of these out there; so many that I had trouble Googling one.) Example usage: tail /var/logs/stuff.log | ./mailstuff.sh
#!/bin/bash
# Mails whatever it gets from stdin.
to="you@your.org"
subject="Something is happening on $HOSTNAME"
body="The first line of the email body.\n";
while read -r line; do
[[ $line = \#* ]] && continue
body="$body$line\n"
@jimkang
jimkang / setup-port-forwarding
Created September 20, 2013 01:36
For future reference: Setting up port forwarding on boot. (Symlink this from /etc/init.d/ to /etc/rc2.d/S98setup-port-forwarding.)
#!/bin/sh
iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 3000
@jimkang
jimkang / mutationsnippet.js
Created October 2, 2013 12:24
Basic MutationObserver usage.
var mutationObserver = new MutationObserver(
function respondToMutation(mutations, observer) {
if (mutations.length < 1 || mutations[0].addedNodes.length !== 1) {
alert('Could not load a player to play the videos. Sorry.');
}
else {
// Do a thing!
}
}
);
@jimkang
jimkang / pluck.js
Created October 7, 2013 19:09
A simple pluck function, like _.pluck from Underscore.js.
function pluck(array, property) {
var plucked = [];
for (var i = 0; i < array.length; ++i) {
plucked.push(array[i][property]);
}
return plucked;
}
@jimkang
jimkang / README.md
Last active December 25, 2015 03:49
Using animateMotion and paths to move circles on orbits.

An example that creates orbit paths and uses animateMotion to move several circles on those orbits. The code switches between plain JavaScript/Web API and d3 because I started out doing it in plain JS, then started using d3 to get over the namespace problems. Now that I've done that, it makes sense to do it all the d3 way, with data definitions of the 'planets' and enter/exit, but I gotta leave this for now.

@jimkang
jimkang / mediaquery.js
Created October 10, 2013 13:05
Running a CSS media query in JavaScript.
var isMobileMediaQuery = 'only screen and (max-device-height: 568px)';
if (window.matchMedia(isMobileMediaQuery).matches) {
transformString += ('rotate(' +
(textPaneIsHidden ? '90deg' : '-90deg') + ') ');
}
@jimkang
jimkang / load_a_typekit.js
Created October 28, 2013 00:51
If you have more than one TypeKit that you need to select at runtime, this function is a way to do that. This uses d3 but is easily convertible to plain JS.
// Adds a script tag using the given TypeKit url. When the TypeKit script
// arrives, it loads the TypeKit and calls the callback.
function loadATypeKit(typekitURL, done) {
var head = d3.select('head');
var typekitScript = head.append('script').attr({
type: 'text/javascript',
src: typekitURL
});
@jimkang
jimkang / README.md
Last active December 30, 2015 17:59 — forked from mbostock/.block

This example, using satirical data from The Onion, demonstrates how to wrap long axis labels to fit on multiple lines.

Update (jimkang): Added a data accessor function as a parameter to mbostock's wrap() so that it can fetch the text from the data source rather than using text().

The problem with text() is that it is built on textContent which "returns the text content of this node and its descendants." If a text element has several tspan descendants, it joins their contents without spaces. e.g. a text element with three tspans each containing 'why', 'are', and 'we' will give back 'whyarewe' through text(). The rest of wrap will proceed to treat that as one word, naturally.

This becomes an issue when wrap() is called multiple times on text elements that have already been processed by wrap().

@jimkang
jimkang / flattenTreeDepthFirst.js
Created December 17, 2013 04:10
A function to flatten a tree, depth-first. It's an implementation of this algorithm: http://cl.ly/image/1X1i0b1v1H2d Assumes the tree is built via nodes that have a property named 'children' which is an array of other nodes.
function flattenTreeDepthFirst(rootNode) {
var nodes = [rootNode];
var childArraysQueue = [];
if (rootNode.children) {
childArraysQueue.push(rootNode.children);
}
while (childArraysQueue.length > 0) {
var children = childArraysQueue[0];
@jimkang
jimkang / NodeList.forEach.js
Created January 3, 2014 17:45
Tacking forEach onto NodeList to reduce annoyance at it being different from [].
NodeList.prototype.forEach = function nodeListEach(callback, thisArg) {
for (var i = 0; i < this.length; ++i) {
var value = this[i];
if (thisArg) {
callback.bind(thisArg)(value, i, this);
}
else {
callback(value, i, this);
}
}