Skip to content

Instantly share code, notes, and snippets.

View nkcmr's full-sized avatar

nick comer nkcmr

View GitHub Profile
@nkcmr
nkcmr / the-beast-of-a-regex.js
Created March 16, 2015 13:14
best working email regex for javascript [updated]
/**
* i say updated because previously, the regex wouldn't
* like if you entered, 'mikey@rocks.email', which with
* recently added TLDs is a perfectly valid email address.
* So i looked up the maximum length for a TLD (which is
* 63 characters), and put it in here. Now it works pretty well.
* It even acceptes those GMail aliases (mikey+facebook@rocks.email)
*/
var EMAIL_REGEX = /^[-a-z0-9~!$%^&*_=+}{\'?]+(\.[-a-z0-9~!$%^&*_=+}{\'?]+)*@([a-z0-9_][-a-z0-9_]*(\.[-a-z0-9_]+)*\.({2,63})|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(:[0-9]{1,5})?$/i
@nkcmr
nkcmr / scrollIntoView.js
Created February 4, 2015 21:52
$.fn.scrollIntoView
define([], function() {
function stripWrapper(ele) {
return (ele instanceof jQuery || ele instanceof angular.element) ? ele[0] : ele;
}
function getRect(el) {
el = stripWrapper(el);
return _.pick(el.getBoundingClientRect(), 'top', 'left', 'bottom', 'right', 'width', 'height');
}

Send a request to: http://giphy.com/services/oembed/?url=http://giphy.com/gifs/ijJ0Ltz4UuD4s, and it returns this:

{
  "width": 360,
  "author_url": "http://giphy.com",
  "title": "Idk Animated GIF",
  "url": "http://giphy.com/gifs/zLOr4ChnIorxS",
  "image": "https://media3.giphy.com/media/zLOr4ChnIorxS/giphy.gif",
 "provider_url": "http://giphy.com/",
@nkcmr
nkcmr / gist:3853630202c157fc7686
Created May 16, 2014 16:03
please-uphold-strict-net-neutrality.eml
Date: Fri, 16 May 2014 11:50:25 -0400
From: Nick Comer
To: openinternet@fcc.gov
Message-ID: <etPan.537633c1.6b8b4567.2c2@alpha.local>
Subject: Please Uphold Strict Net Neutrality
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline
I could write a drawn out, multi-page essay on the reasons that net
@nkcmr
nkcmr / spdy.js
Created February 11, 2014 17:36
replacing https with spdy
var express = require("express");
var app = express();
//BEFORE
var https = require("https");
https.createServer({ /*options and ssl stuff*/ }, app).listen(443);
//AFTER
var spdy = require("spdy");
spdy.createServer({ /*options and ssl stuff*/ }, app).listen(443);
@nkcmr
nkcmr / torrent_file_processing.js
Created October 10, 2013 12:55
It took 2 days for me to figure out how to process torrent files in JavaScript. So I am publishing my findings. Fully commented and helpful links are dropped in. I hope if you find this it saves you a few hours of hair-pulling and cursing at your screen. Spoiler alert: If you don't know a whole lot about encodings, you're gonna have a bad time ;-;
var debug = require("debug")("torrent:read"); //npm install debug
var bencode = require("bencode"); //npm install bencode - https://en.wikipedia.org/wiki/Bencode
var fs = require("fs");
var crypto = require("crypto");
var _ = require("underscore"); //npm install underscore
var percent_encoding = {
encode: function(buffer) {
var ret = "", a2z, AtoZ, zero2nine, other_valid_symbols, all_unreserved_symbols;
@nkcmr
nkcmr / window-watcher.js
Created December 11, 2012 14:54
this bit of code watches the conditions of the viewport and runs the appropriate query.
var supportsOrientationChange = "onorientationchange" in window, //Check if device supports orientation change listening
orientationEvent = supportsOrientationChange ? "orientationchange" : "resize"; //If so listen for orientation change, if not listen for resizing
window.addEventListener(orientationEvent, function() {
console.log('Orientation did Change!!');
for(i=0;i<queries.length;i++){ //Parse through queries
var mq = window.matchMedia(queries[i].match_this);
if(mq.matches){
queries[i].action();
break;
}
@nkcmr
nkcmr / cssmq-js.js
Created December 11, 2012 14:53
css media query actions in javascript
var queries = [{
'match_this': 'only screen and (-webkit-min-device-pixel-ratio : 1.5), only screen and (min-device-pixel-ratio : 1.5)',
'action': function () {
$('.post .entry-content img').each(function (index, element) {
$(this).attr('width', '100%');
$(this).removeAttr('height');
});
}
}, {
'match_this': 'only screen and (min-width : 321px)',
@nkcmr
nkcmr / hsl2hex.php
Created December 11, 2012 14:51
HSL to HEX triplet algorithm
<?php
function HSL2HEX($h,$s,$l){
function hue2rgb($v1, $v2, $vH){
if($vH<0) $vH += 1;
if($vH>1) $vH -= 1;
if((6*$vH)<1) return $v1+($v2-$v1)*6*$vH;
if((2*$vH)<1) return $v2;
if((3*$vH)<2) return $v1+($v2-$v1)*((2/3)-$vH)*6;
return $v1;