Skip to content

Instantly share code, notes, and snippets.

@Rich-Harris
Rich-Harris / CSV Parser
Created Oct 11, 2012
A bog standard JavaScript CSV parser. Putting it here so I don't need to keep rewriting it or remembering where I put the last one. Usage self-explanatory, will break if you throw weird shit at it (cells with newlines in them, that sort of thing)
View CSV Parser
CSVParser = function ( options ) {
var defaults, delimiter, rowDelimiter, qualifier, qualified, unqualified;
options = options || {};
defaults = {
delimiter: ',',
rowDelimiter: '\n',
qualifier: '"'
};
@Rich-Harris
Rich-Harris / addCommaSeparators.js
Created Jun 11, 2013
Add comma separators (e.g. `addCommaSeparators(1234567) === '1,234,567'`)
View addCommaSeparators.js
(function ( global ) {
'use strict';
var addCommaSeparators = function ( num, precision ) {
var integer, decimal, remaining, result, lastThree;
integer = Math.floor( num );
decimal = num - integer;
@Rich-Harris
Rich-Harris / Player.js
Last active Dec 18, 2015
A solution to the problem posed by Mike Pennisi on http://weblog.bocoup.com/info-hiding-in-js/ - per-instance private variables and methods that don't forgo the benefits of prototypal inheritance or rely on obscurity (or ES6)
View Player.js
var Player = (function () {
var Player, generateGuid, maxCoins, secrets, get, set, cashIn;
maxCoins = 100;
// via http://stackoverflow.com/questions/105034/how-to-create-a-guid-uuid-in-javascript
generateGuid = function () {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r, v;
@Rich-Harris
Rich-Harris / linearScale.js
Last active May 4, 2016
A simple linear scale function generator, similar to D3's
View linearScale.js
(function ( global ) {
'use strict';
var linearScale = function ( domain, range ) {
var d0 = domain[0], r0 = range[0], multiplier = ( range[1] - r0 ) / ( domain[1] - d0 );
// special case
if ( r0 === range[1] ) {
return function () {
@Rich-Harris
Rich-Harris / reindex.js
Last active Dec 19, 2015
Reindex an array by a common property
View reindex.js
// https://gist.github.com/Rich-Harris/5969002
// -------------------------------------------
//
// MIT licensed. Go nuts.
(function ( global ) {
'use strict';
var reindex = function ( array, idField ) {
View eventListener.js
// addEventListener polyfill IE6+
if ( !window.addEventListener ) {
(function ( win, doc ) {
var Event, addEventListener, removeEventListener, head, style;
Event = function ( e, element ) {
var property, instance = this;
for ( property in e ) {
instance[ property ] = e[ property ];
@Rich-Harris
Rich-Harris / thresholdScale.js
Created Jul 21, 2013
Threshold scale generator, similar to D3's
View thresholdScale.js
(function ( global ) {
'use strict';
// Threshold scale generator, similar to D3's
// Usage:
//
// scale = thresholdScale([ 0, 20, 40, 100 ], [ 'red', 'green', 'blue' ]);
// scale( 10 ) -> 'red'
View MutationObserverLogger.js
<script type="text/javascript">
// See MDN: https://developer.mozilla.org/en-US/docs/DOM/MutationObserver?redirectlocale=en-US&redirectslug=DOM%2FDOM_Mutation_Observers
(function(){
// select the target node
var target = document.querySelector('body');
var MutationObserver = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver;
var i={};
// create an observer instance
var observer = new MutationObserver(function(mutations) {
View jsonp.js
(function ( global ) {
'use strict';
var jsonp = function ( url, callback ) {
var script, callbackName = 'jsonp_callback_' + Math.floor( Math.random() * 1000000 );
window[ callbackName ] = function ( data ) {
callback( data );
delete window[ callbackName ];
View GoogleSpreadsheetsParser.js
(function ( global ) {
'use strict';
var GoogleSpreadsheetsParser;
GoogleSpreadsheetsParser = function ( data ) {
this.data( data );
};