Skip to content

Instantly share code, notes, and snippets.

View alloyking's full-sized avatar
🎧
.

Tim Shultis alloyking

🎧
.
View GitHub Profile
@alloyking
alloyking / s3upload.js
Created May 2, 2012 13:13 — forked from masylum/s3upload.js
Upload files directly to s3
function upload (req, res) {
var Formidable = require('formidable'),
form = new Formidable.IncomingForm();
form.encoding = 'utf-8';
form.onPart = function (part) {
if (!part.filename) {
form.handlePart(part);
} else {
(function () {
@alloyking
alloyking / Closest Match
Last active October 7, 2015 08:38
Find Closest Number Match in an array
var numberArray = [1,3,6,15,25,50];
var testValue = 19;
var fn = closestMatch(numberArray, testValue);
alert(fn);
function closestMatch(numbers, value){
var closestValue, smallestDiff, absoluteDiff
var len = numbers.length;
@alloyking
alloyking / EventType
Created July 18, 2012 14:52
AS3 Custom Event class
package com.utils {
import flash.events.Event;
public class EventType extends Event {
public var arg:*;
public function EventType(type:String, bubbles:Boolean = false, cancelable:Boolean = false, ... a:*) {
super(type, bubbles, cancelable);
arg = a;
@alloyking
alloyking / getReplies.php
Created August 9, 2012 16:22
Wordpress get replies and child replies for a comment
function getReplies($comment_post_id, $commentID){
$wp_comments = $this->wpdb->prefix . "comments";
$childData = array();
$data = $this->wpdb->get_results( "SELECT * FROM $wp_comments
WHERE $wp_comments.comment_post_ID = $comment_post_id AND $wp_comments.comment_parent != 0");
$dataLength = count($data);
for($i = 0; $i< $dataLength; $i++){
@alloyking
alloyking / sql_timer.php
Created November 16, 2012 14:05
Timed Database Query
function StartTimer ($what='') {
global $MYTIMER; $MYTIMER=0; //global variable to store time
//if ($_SERVER['REMOTE_ADDR'] != '127.0.0.1') return; //only show for my IP address
echo '<p style="border:1px solid black; color: black; background: yellow;">';
echo "About to run <i>$what</i>. "; flush(); //output this to the browser
//$MYTIMER = microtime (true); //in PHP5 you need only this line to get the time
list ($usec, $sec) = explode (' ', microtime());
$MYTIMER = ((float) $usec + (float) $sec); //set the timer
@alloyking
alloyking / exists.js
Created November 27, 2012 14:24
Check if file exists
function doesFileExist(urlToFile)
{
var xhr = new XMLHttpRequest();
xhr.open('HEAD', urlToFile, false);
xhr.send();
if (xhr.status == "404") {
return false;
} else {
return true;
@alloyking
alloyking / font-exist.js
Created November 27, 2012 14:27
Check if font exists
//
// Call this function and pass in the name of the font you want to check for availability.
//
function doesFontExist(fontName) {
// creating our in-memory Canvas element where the magic happens
var canvas = document.createElement("canvas");
var context = canvas.getContext("2d");
// the text whose final pixel size I want to measure
var text = "abcdefghijklmnopqrstuvwxyz0123456789";
@alloyking
alloyking / array-duplicates.js
Created November 27, 2012 14:29
Remove duplicates from array
var myArray = ["bar", "foo", "zorb", "bar", "baz", "fum", "baz"];
Array.prototype.removeDuplicates = function() {
var input = this;
var hashObject = new Object();
for (var i = input.length - 1; i >= 0; i--) {
var currentItem = input[i];
if (hashObject[currentItem] == true) {
@alloyking
alloyking / preload.js
Created November 27, 2012 14:33
Preload Images
this.addEventListener("DOMContentLoaded", preloadImages, true);
var loadedImages = 0;
var imageArray = new Array("path-to-image", "path-to-image");
function preloadImages(e)
{
for (var i=0; i<imageArray.length; i++) {
var tempImage = new Image();
tempImage.addEventListener("load", trackProgress, true);
@alloyking
alloyking / gist:4162832
Created November 28, 2012 17:47 — forked from boucher/gist:1750368
Stripe sample checkout form
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<title>Stripe Sample Form</title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script type="text/javascript" src="https://ajax.aspnetcdn.com/ajax/jquery.validate/1.8.1/jquery.validate.min.js"></script>
<script type="text/javascript" src="https://js.stripe.com/v1/"></script>
<script type="text/javascript">