Skip to content

Instantly share code, notes, and snippets.

View slattery's full-sized avatar

Mike Slattery slattery

View GitHub Profile
@slattery
slattery / gist:cd324d59ee972fbef87f9500c9be7330
Created September 23, 2016 13:58 — forked from davisford/gist:5039064
git clone into non-empty directory

Let's say you start a project locally, and do some editing.

$ mkdir -p ~/git/foo && cd ~/git/foo
$ touch NEWFILE

Now you decide you want to create a new github repo and track it, but the directory is non-empty so git won't let you clone into it. You can fix this, thusly:

@slattery
slattery / percentile.js
Created August 19, 2016 22:16 — forked from IceCreamYou/percentile.js
Utility functions to calculate percentiles and percent ranks in a JavaScript array.
// Returns the value at a given percentile in a sorted numeric array.
// "Linear interpolation between closest ranks" method
function percentile(arr, p) {
if (arr.length === 0) return 0;
if (typeof p !== 'number') throw new TypeError('p must be a number');
if (p <= 0) return arr[0];
if (p >= 1) return arr[arr.length - 1];
var index = arr.length * p,
lower = Math.floor(index),
@slattery
slattery / bucketNumbers.js
Created August 19, 2016 22:15 — forked from IceCreamYou/bucketNumbers.js
Put numbers into buckets either by bucket size or range size.
/**
* Utility method to round numbers to a given number of decimal places.
*
* Usage:
* 3.5.round(0) // 4
* Math.random().round(4) // 0.8179
* var a = 5532; a.round(-2) // 5500
* Number.prototype.round(12345.6, -1) // 12350
* 32..round(-1) // 30 (two dots required since the first one is a decimal)
*/
@slattery
slattery / js-observables-binding.md
Created August 14, 2016 23:37 — forked from austinhyde/js-observables-binding.md
Vanilla JavaScript Data Binding

Observables

You don't really need a framework or fancy cutting-edge JavaScript features to do two-way data binding. Let's start basic - first and foremost, you need a way to tell when data changes. Traditionally, this is done via an Observer pattern, but a full-blown implementation of that is a little clunky for nice, lightweight JavaScript. So, if native getters/setters are out, the only mechanism we have are accessors:

var n = 5;
function getN() { return n; }
function setN(newN) { n = newN; }

console.log(getN()); // 5

setN(10);

@slattery
slattery / index.html
Created August 8, 2016 18:01 — forked from rengel-de/index.html
Timeline for d3 - proof-of-concept
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="keywords" lang="de" content="Zeitleiste, Zeitlinie, Zeitkarte, Geschichte, Chronologie">
<meta name="keywords" lang="en" content="Timeline, Timemap, History, Chronology">
<title>Timeline - Proof-of-concept</title>
<!-- That's my local d3 path. When working locally, use your local path. -->
@slattery
slattery / gist:9a5001f9a50dce56cb1cba0d693df18f
Created July 7, 2016 13:38 — forked from vladimirtsyupko/gist:10964772
Git force pull to overwrite local files
git fetch --all
git reset --hard origin/master
git pull origin master
@slattery
slattery / index.html
Created May 21, 2016 19:26 — forked from vigorousnorth/index.html
D3 bar chart with custom SVG path icons
<!DOCTYPE html>
<meta charset="utf-8">
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script>
<script src="windchart.js"></script>
<style>
body {
<!DOCTYPE html>
<meta charset="utf-8">
<style>
.background {
fill: none;
pointer-events: all;
}
#states {
@slattery
slattery / .block
Created May 21, 2016 19:16 — forked from mbostock/.block
Point-Along-Path Interpolation
license: gpl-3.0
@slattery
slattery / .block
Last active May 21, 2016 17:58 — forked from mbostock/.block
Point-Along-Path Interpolation
license: gpl-3.0