Skip to content

Instantly share code, notes, and snippets.

@headquarters
headquarters / php-relative-date-class.php
Last active August 29, 2015 13:56
A PHP class to turn a timestamp into more user-friendly relative date.
/**
* A class for turning timestamps into more user-friendly relative dates.
*
$date = new Relative_Date();
echo $date->relative_formatted_date; //"Today"
*/
class Relative_Date {
const DAY = 86400;
@headquarters
headquarters / calculate_bmi.rb
Last active August 29, 2015 14:04
Calculate BMI in Ruby. Weight and height are both coerced to floats to prevent integer division.
def calculate_bmi(weight_in_pounds, height_in_inches)
return ((weight_in_pounds.to_f/height_in_inches.to_f**2) * 703)
end
@headquarters
headquarters / masonry.js
Last active August 29, 2015 14:11
Reflow items on different screen sizes.
var REFLOW = (function(window, $){
var self = {};
var nth;
var itemsAfterFirstRow;
var allItems = $('#items .col');
self.init = function(){
self.adjustHeights();
@headquarters
headquarters / fetch-plos.org-article-data.rb
Last active August 29, 2015 14:20
Collect article data from the Plos.org API
# This script uses the Plos.org API to collect article data.
# Necessary libraries
require 'csv'
require 'work_queue'
require 'rest-client'
# The file name for the CSV file that will be READ
CSV_FILENAME = "9_10_2014-Cumulative-Report-With-Subject-Area-and-Article-Type.csv"
# The file name for the CSV file that will be WRITTEN
CSV_OUTPUT_FILENAME = "9_10_2014-Cumulative-Report-With-ALM-Data.csv"
@headquarters
headquarters / scrape-plos.org-article-data.rb
Created May 6, 2015 16:02
Scrape data from Plos.org articles.
# This script scrapes the PlosOne.org website to extract data for articles. At the time, this data was not available in the API.
# Necessary libraries
require 'CSV'
require 'nokogiri'
require 'open-uri'
require 'work_queue'
require 'benchmark'
CSV_FILENAME = "9_10_2014-Cumulative-Report.csv"
@headquarters
headquarters / tab-click-profiling
Created May 27, 2015 16:16
Profile tab click times
jQuery(document.body).on("mouseup", function(){ console.time("Tab showing") });
jQuery(".nav-tabs li:nth-child(1)").on("mouseup", function(){ console.timeEnd("Tab showing"); })
var target = document.querySelector(".nav-tabs li:nth-child(2)");
var observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
console.timeEnd("Tab showing");
});
});
@headquarters
headquarters / SassMeister-input-HTML.html
Last active August 29, 2015 14:27
Generated by SassMeister.com.
<a href="#nogo" class="button--PRIMARY">Primary Button</a>
<br />
<a href="#nogo" class="button--SECONDARY">Secondary button</a>
@headquarters
headquarters / prevent_widows.rb
Last active September 6, 2015 06:34
Prevent widows in Ruby with this simple function. It replaces only the last space character with a non-breaking space in a string.
def prevent_widows(text)
if text.kind_of? String
trimmed_text = text.rstrip
last_space_index = trimmed_text.rindex(" ")
if !last_space_index.nil?
text[last_space_index] = "&nbsp;"
end
end
@headquarters
headquarters / array-indexing
Last active December 10, 2015 07:48
A reminder to myself of how to handle array indexing in a "round robin" fashion, using the modulus operator.
//assume variables currentIndex, nextIndex, arrayLength exist
//moving "forward", positive incrementing
nextIndex = (currentIndex + 1) % arrayLength;
//moving "backward", negative incrementing
if(currentIndex == 0){
nextIndex = arrayLength - 1;
} else {
nextIndex = (currentIndex - 1) % arrayLength;
@headquarters
headquarters / translate-slashdot-rot13
Created April 1, 2013 15:52
Happy April Fool's Day! If you'd like to translate all the ROT13 encrypted articles on Slashdot's homepage today, just run this gist through your browser's console.
var alphabet=['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'];
var rot13 = function(letter){
var l = letter.toLowerCase();
var index = alphabet.indexOf(l);
var newIndex = (index + 13) % alphabet.length;
return alphabet[newIndex];