Skip to content

Instantly share code, notes, and snippets.

@sdthornton
sdthornton / gist:4235857
Created December 7, 2012 19:35
Days in a month
monthLength = 31;
if (monthNum==3 || monthNum==5 || monthNum==8 || monthNum==10) {
monthLength = 30;
} else if (monthNum==1) {
if (year % 4 == 0 ) {
if (year % 100 == 0) {
if (year % 400 == 0) {
monthLength = 29;
} else {
monthLength = 28; }
@sdthornton
sdthornton / git_branch_delete.sh
Last active December 14, 2015 12:29
Deletes a git branch both locally and remotely. To use simply type: delete branch-name
# Easily delete branches remotely and locally
# Simple type:
# delete feature-branch-name
function delete_branch() {
log=$(git log origin/$1..$1 --oneline)
if [[ -z "${log}" ]]; then
git push origin :$1
git branch -D $1
elif [[ "${log}" == *"path not in the working tree"* ]]; then
echo -e "\033[0;31mIt doesn't appear that your branch exists remotely."
@sdthornton
sdthornton / gist:5241150
Created March 25, 2013 21:52
Smooth removal of placeholder text on focus
input {
border: solid 1px #bbb;
padding: 5px;
}
input:focus::-webkit-input-placeholder {
color: transparent;
-webkit-transition: color 0.5s 0.5s ease;
transition: color 0.5s 0.5s ease;
}
@sdthornton
sdthornton / Reduce Browser Paint While Scrolling
Last active December 17, 2015 16:19
Small JS to add and remove the .hover class from the <html> tag during scrolling. Prepending the .hover class before all :hover or transition declarations in your css makes it so that hovers and transitions are only painted when not scrolling, which greatly increases page speed and scrolling smoothness.
var reducePaintOnScroll = (function() {
var enableTimer = 0;
window.addEventListener('scroll', function() {
clearTimeout(enableTimer);
removeHoverClass();
enableTimer = setTimeout(addHoverClass, 200);
}, false);
function removeHoverClass() {
@sdthornton
sdthornton / Detect Mobile Browser - CoffeeScript
Last active December 20, 2015 17:19
Detect mobile browser coffee script variable
@mobileWeb = /Mobile|iP(hone|od|ad)|Android|BlackBerry|IEMobile|Kindle|NetFront|Silk-Accelerated|(hpw|web)OS|Fennec|Minimo|Opera M(obi|ini)|Blazer|Dolfin|Dolphin|Skyfire|Zune/i.test(navigator.userAgent)
// Usage: if @mobileWeb then ...
@sdthornton
sdthornton / gist:6720490
Last active December 24, 2015 00:59
For checking mobile browsers (covers 90% of all mobile browsers but doesn't cover mobile browsers pre-2004, which should be okay)
Step 1 (add script to <head>):
<head>
<script>/Mobile|Android|BlackBerry/i.test(navigator.userAgent)&&(document.documentElement.className+=" mobile_browser");</script>
</head>
Step 2 (add css declaration):
.link_to_show {
@sdthornton
sdthornton / git-prompt
Last active August 29, 2015 13:57
Git Prompt
# For better git prompts
c_cyan=`tput setaf 6`
c_red=`tput setaf 1`
c_green=`tput sgr0 ; tput setaf 2`
c_sgr0=`tput sgr0`
branch_color ()
{
if git rev-parse --git-dir >/dev/null 2>&1
then
@sdthornton
sdthornton / placeholder_support
Created March 27, 2014 18:41
Robust Placeholder Support
// Use more robust placeholder support when specified
// Because it wraps with a div, it can potentially break an inputs position,
// so secondary placeholder support is provided as alternative
$('.placeholder_support').each(function(i, el) {
var $input = $(el),
holderId = $input.attr('id') + "_placeholder",
placeholder = $input.attr('placeholder'),
lineHeight = $input.css('line-height'),
paddingTop = parseInt($input.css('padding-top'), 10)
+ parseInt($input.css('border-top-width'), 10) + 'px',
@sdthornton
sdthornton / test_ajax_deferred.js
Last active September 6, 2018 08:00
Testing jQuery.ajax with deferred method - .done() and .fail()
// Probably best to have this within setup
var ajaxStub = sinon.stub($, "ajax", function(event) {
var result = $.Deferred();
result.args = event;
return result;
});
// Usage example
var call = theAjaxCallToTest()
// OR (if theAjaxCallToTest() doesn't return the actual ajax call)
parse_git_bullet ()
{
if git rev-parse --git-dir >/dev/null 2>&1
then
git_status="$(git status 2> /dev/null)"
branch_pattern="^On branch ([^${IFS}]*)"
if [[ ${git_status} =~ ${branch_pattern} ]]; then
branch=${BASH_REMATCH[1]}
echo "•"
fi