Skip to content

Instantly share code, notes, and snippets.

View tshaddix's full-sized avatar

Tyler Shaddix tshaddix

View GitHub Profile
@tshaddix
tshaddix / validators.js
Created August 2, 2013 18:09
Just the validators from the node-validator project. Still want the validation, just don't want the fluff around it.
var validators = module.exports = {
isEmail: function(str) {
return str.match(/^(?:[\w\!\#\$\%\&\'\*\+\-\/\=\?\^\`\{\|\}\~]+\.)*[\w\!\#\$\%\&\'\*\+\-\/\=\?\^\`\{\|\}\~]+@(?:(?:(?:[a-zA-Z0-9](?:[a-zA-Z0-9\-](?!\.)){0,61}[a-zA-Z0-9]?\.)+[a-zA-Z0-9](?:[a-zA-Z0-9\-](?!$)){0,61}[a-zA-Z0-9]?)|(?:\[(?:(?:[01]?\d{1,2}|2[0-4]\d|25[0-5])\.){3}(?:[01]?\d{1,2}|2[0-4]\d|25[0-5])\]))$/);
},
isUrl: function(str) {
//A modified version of the validator from @diegoperini / https://gist.github.com/729294
return str.length < 2083 && str.match(/^(?!mailto:)(?:(?:https?|ftp):\/\/)?(?:\S+(?::\S*)?@)?(?:(?:(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[0-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]+-?)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]+-?)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,})))|localhost)(?::\d{2,5})?(?:\/[^\s]*)?$/i);
},
//node-js-core
isIP : function(str) {
@tshaddix
tshaddix / ngjson.js
Created August 4, 2013 00:20
Very basic modification of Express JS res.json(). Adds the safety prefix for Angular JS to the body of all json responses. Extends the response object with one method: ngJSON. Simply require file somewhere in application (normally app.js) before executing ngJSON responses.
var http = require('http');
var NG_PREFIX = ")]}',\n";
http.ServerResponse.prototype.ngJSON = function(obj){
// allow status / body
if (2 == arguments.length) {
// res.json(body, status) backwards compat
if ('number' == typeof arguments[1]) {
this.statusCode = arguments[1];
@tshaddix
tshaddix / angular.js
Last active December 21, 2015 00:58
Allowing CORS with ExpressJS and AngularJS
// Add COR ability
$httpProvider.defaults.useXDomain = true;
delete $httpProvider.defaults.headers.common['X-Requested-With'];
$httpProvider.defaults.withCredentials = true;
@tshaddix
tshaddix / geochart.js
Last active December 21, 2015 09:59
Using a geochart with OOcharts
oo.load(function(){
var regionQuery = new oo.Query('PROFILE ID', "30d");
//Add metrics, dimensions, and filter for region
regionQuery.addMetric('ga:visits');
regionQuery.addDimension('ga:metro');
regionQuery.setFilter('ga:region==California');
regionQuery.execute(function(data){
@tshaddix
tshaddix / delta.js
Last active December 23, 2015 17:09
Delta Service
/*
* Checks two Date objects based on human date (mm/dd/yyyy) two test equality
*
* @param {Date} date1
* @param {Date} date2
* @return {Boolean}
*/
function sameHumanDate(date1, date2){
if (date1.getDate() === date2.getDate() &&
date1.getFullYear() === date2.getFullYear() &&
@tshaddix
tshaddix / upload-controller.js
Last active February 21, 2018 22:33
Busboy + GridFS + Express4
var Busboy = require('busboy'),
Grid = require('gridfs-stream'),
mongoose = require('mongoose'),
router = module.exports = require('express').Router();
var gfs = new Grid(mongoose.connection.db, mongoose.mongo);
router
.get('/:item/receipt/info', function(req, res, next){
gfs.files.find({
@tshaddix
tshaddix / ns-cents.js
Created May 29, 2014 17:31
Angular cents directive for validation
/**
* Created by tyler on 5/1/14.
*/
(function(){
'use strict';
angular.module('fsApp')
.directive('nsCents', [function () {
@tshaddix
tshaddix / param.go
Created September 4, 2014 19:34
Gorilla Mux param decoder for tshaddix/parcel package. Decodes params into struct fields and allows for slice population based on deliminator.
package pogs
import (
"net/http"
"reflect"
"strings"
"github.com/gorilla/mux"
"github.com/tshaddix/parcel/encoding"
)
@tshaddix
tshaddix / app-errors.es6.js
Last active January 18, 2016 19:29
App errors for JS applications that loosely match the specs of JSON API.
import util from 'util';
class AppError {
constructor(message) {
Error.call(this);
Error.captureStackTrace(this, this.constructor.name);
this.name = this.constructor.name;
this.message = message;
}
@tshaddix
tshaddix / ed.js
Last active March 31, 2016 14:59
The simplest service strictifier ever
import Joi from 'joi';
import _ from 'lodash';
import {
BadRequestError
} from './errors';
const _promisedValidate = (msg, schema) => {
return new Promise((resolve, reject) => {
Joi.validate(msg, schema, {