Skip to content

Instantly share code, notes, and snippets.

@Rich-Harris
Rich-Harris / CSV Parser
Created October 11, 2012 17:15
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)
CSVParser = function ( options ) {
var defaults, delimiter, rowDelimiter, qualifier, qualified, unqualified;
options = options || {};
defaults = {
delimiter: ',',
rowDelimiter: '\n',
qualifier: '"'
};
@Rich-Harris
Rich-Harris / addCommaSeparators.js
Created June 11, 2013 12:05
Add comma separators (e.g. `addCommaSeparators(1234567) === '1,234,567'`)
(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 December 18, 2015 17:09
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)
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 19:57
A simple linear scale function generator, similar to D3's
(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 December 19, 2015 14:28
Reindex an array by a common property
// https://gist.github.com/Rich-Harris/5969002
// -------------------------------------------
//
// MIT licensed. Go nuts.
(function ( global ) {
'use strict';
var reindex = function ( array, idField ) {
// 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 July 21, 2013 17:03
Threshold scale generator, similar to D3's
(function ( global ) {
'use strict';
// Threshold scale generator, similar to D3's
// Usage:
//
// scale = thresholdScale([ 0, 20, 40, 100 ], [ 'red', 'green', 'blue' ]);
// scale( 10 ) -> 'red'
<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) {
@Rich-Harris
Rich-Harris / jsonp.js
Created August 5, 2013 11:38
jsonp helper
(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 ];
@Rich-Harris
Rich-Harris / GoogleSpreadsheetsParser.js
Created August 5, 2013 11:40
Google Spreadsheets parser
(function ( global ) {
'use strict';
var GoogleSpreadsheetsParser;
GoogleSpreadsheetsParser = function ( data ) {
this.data( data );
};