Skip to content

Instantly share code, notes, and snippets.

View snowman-repos's full-sized avatar

James snowman-repos

View GitHub Profile
@snowman-repos
snowman-repos / Verify that a given argument is a number
Created February 6, 2014 08:53
Verify that a given argument is a number
isNumber = (n) ->
not isNaN(parseFloat(n)) and isFinite(n)
@snowman-repos
snowman-repos / Transform the arguments object into an array
Created February 6, 2014 08:53
Transform the arguments object into an array
argArray = Array::slice.call(arguments_)
@snowman-repos
snowman-repos / A string trim function
Created February 6, 2014 08:48
A string trim function
String::trim = ->
@replace /^\s+|\s+$/g, ""
@snowman-repos
snowman-repos / Generate a random set of alphanumeric characters
Created February 6, 2014 08:47
Generate a random set of alphanumeric characters
generateRandomAlphaNum = (len) ->
rdmString = ""
while rdmString.length < len
rdmString += Math.random().toString(36).substr(2)
rdmString.substr 0, len
@snowman-repos
snowman-repos / Shuffle an array of numbers
Created February 6, 2014 08:46
Shuffle an array of numbers
numbers = [
5
458
120
-215
228
400
122205
-85411
]
@snowman-repos
snowman-repos / Generate an array of numbers with numbers from 0 to max
Created February 6, 2014 08:33
Generate an array of numbers with numbers from 0 to max
numbersArray = []
max = 100
i = 1 # numbers = [1,2,3 ... 100]
while numbersArray.push(i++) < max
@snowman-repos
snowman-repos / Get a random number in a specific range
Created February 6, 2014 08:33
Get a random number in a specific range
x = Math.floor(Math.random() * (max - min + 1)) + min
@snowman-repos
snowman-repos / Determine if an Element is in the Viewport
Created February 6, 2014 08:30
Determine if an Element is in the Viewport
# Determine if an element is in the visible viewport
isInViewport = (element) ->
rect = element.getBoundingClientRect()
html = document.documentElement
rect.top >= 0 and rect.left >= 0 and rect.bottom <= (window.innerHeight or html.clientHeight) and rect.right <= (window.innerWidth or html.clientWidth)
@snowman-repos
snowman-repos / Determining Max-Width of Responsive Images
Created February 6, 2014 08:29
Determining Max-Width of Responsive Images
# Get image's max-width:100%; in pixels
getMaxWidth = (img) ->
maxWidth = undefined
# Check if naturalWidth is supported
if img.naturalWidth isnt `undefined`
maxWidth = img.naturalWidth
# Not supported, use in-memory solution as fallback
else
@snowman-repos
snowman-repos / Manipulating the DOM
Created February 6, 2014 08:26
Manipulating the DOM, without jQuery
# Select an element
element = document.querySelector(".class")
# Clone it
clone = element.cloneNode(true)
# Do some manipulation off the DOM
clone.style.background = "#000"
# Replaces the original element with the new cloned one