Skip to content

Instantly share code, notes, and snippets.

View benwells's full-sized avatar
:octocat:

Ben Wells benwells

:octocat:
  • GitHub Staff
  • Charleston, SC
View GitHub Profile
@benwells
benwells / reduce-example.js
Created May 12, 2016 13:40
Using Array.reduce to sum a property in an array of objects
var accounts = [
{ name: 'James Brown', msgCount: 123 },
{ name: 'Stevie Wonder', msgCount: 22 },
{ name: 'Sly Stone', msgCount: 16 },
{ name: 'Otis Redding', msgCount: 300 } // Otis has the most messages
];
// get sum of msgCount prop across all objects in array
var msgTotal = accounts.reduce(function(prev, cur) {
return prev + cur.msgCount;
@benwells
benwells / open_pull_request.sh
Created April 13, 2016 15:56
Open a Pull Request for current branch from OSX Terminal
# put this function in your .bash_profile or .zshrc to open the github compare screen
# for your current branch. Of course, you must push your branch up remotely first for this to work.
# The function parses your current working branch from the output of git branch
# and opens github in Chrome, right to the compare page for your branch.
#
# Edit myaccountname and myreponame and enter the account and repo of your choice.
#
function openpr () {
ACCOUNT=myaccountname # replace with your account name
REPO=myreponame # replace with your repo name
@benwells
benwells / init.coffee
Last active April 2, 2016 19:12
Override escape with jk for use with atom's vim-mode
# ~/.atom/init.coffee
atom.commands.add 'atom-text-editor', 'exit-insert-mode-if-preceded-by-j': (e) ->
editor = @getModel()
pos = editor.getCursorBufferPosition()
range = [pos.traverse([0,-1]), pos]
lastChar = editor.getTextInBufferRange(range)
if lastChar != "j"
e.abortKeyBinding()
else
@benwells
benwells / batch_rename.sh
Created April 7, 2014 15:22
Batch renaming files in a directory using bash
# Batch renaming/mv/cp/etc... using bash
# Step 1: to make a backup copy of all pdf files in a directory
for i in *.pdf ; do mv "$i" "${i}.backup" ; done
# Step 2: to copy one file over all of the original files in this directory
for i in *.pdf ; do cp "~/source.pdf" "${i}" ; done
@benwells
benwells / oo.js
Created March 26, 2014 13:10
Define Object Methods inside class vs. added to prototype
/**
* Define a method inside of a JavaScript Object. In this instance, the method gets
* recreated with each Class Instance. Sometimes desirable, sometimes not.
*/
function Wizard () {
this.name = "The Wiz";
this.level = 1;
this.shoot = function () {
alert('Imma Shoot You!');
};
@benwells
benwells / array_filter.js
Created March 13, 2014 15:59
filtering an array in javascript
var test = ["this is an element", "this is an element", "so is this"];
var unique = test.filter(function(elem, pos, self) {
return self.indexOf(elem) == pos;
});
console.dir(unique);
require 'sinatra'
require 'vimeo'
require 'shotgun'
require 'haml'
enable :sessions
set :protection, :except => :frame_options
get '/:key/:secret/:access_token/:access_token_secret/:user_id' do
@benwells
benwells / banner_images.js
Created March 11, 2014 16:39
Setting a background image at random.
/**
* choose a random background image on
* page load from a number of sequentially
* numbered image files
*/
var numImages = 4;
var index = Math.floor(Math.random() * numImages);
$('div.application-header').css({'background': 'url(https://www.gdg.do/images/banner_imgs/' + index + '.jpg)');
@benwells
benwells / fadeout.css
Last active August 29, 2015 13:56
Applying a css "fade out" transition animation upon clicking a link before traveling to the next page.
/* fadeOut animation css */
.fadeOut {
animation-name: fadeOut;
-webkit-animation-name: fadeOut;
animation-duration: 0.4s;
-webkit-animation-duration: 0.4s;
animation-timing-function: ease-in-out;
-webkit-animation-timing-function: ease-in-out;