Skip to content

Instantly share code, notes, and snippets.

View jimfleming's full-sized avatar

Jim Fleming jimfleming

View GitHub Profile
@jimfleming
jimfleming / String.prototype.repeat.js
Created August 25, 2010 15:47
repeats a given string x times (source: stackoverflow)
String.prototype.repeat = function(times) {
return new Array(isNaN(times) ? 1 : ++times).join(this);
}
var zero_pad = function(value, length) {
value = value.toString();
while (value.length < length)
value = '0' + value;
return value;
};
var Timespan = function(milliseconds) {
this.milliseconds = milliseconds;
this.seconds = this.milliseconds / 1000;
this.minutes = this.seconds / 60;
this.hours = this.minutes / 60;
this.days = this.hours / 24;
this.weeks = this.days / 7;
this.months = this.days / 30;
this.years = this.months / 12;
this.centuries = this.years / 100;
String.prototype.replaceAt = function(index, char) {
return this.substr(0, index) + char + this.substr(index + char.length);
}
@jimfleming
jimfleming / Array.js
Created November 10, 2010 16:19
Some useful array extensions (dependent on javascript 1.6)
Array.prototype.intersect = function(b) {
return this.filter(function(n) {
return (b.indexOf(n) != -1);
});
}
Array.prototype.diff = function(a) {
return this.filter(function(n) {
return !(a.indexOf(n) > -1);
});
@jimfleming
jimfleming / Emission.js
Created January 7, 2011 07:25
Should emit events.
var sys = require('sys'),
events = require('events');
var Emission = module.exports = function () {
events.EventEmitter.call(this);
this.cycles = 0;
};
sys.inherits(Emission, events.EventEmitter);
@jimfleming
jimfleming / server.js
Created January 7, 2011 07:27
Listens for events from emission...
var sys = require('sys'),
Emission = require('./emission');
var emission = new Emission();
emission.cycle();
emission.on('recycle', function(cycles) {
if (cycles < 10) {
sys.puts(cycles);
}
Browse:
https://oauth.twitter.com/2/authorize?oauth_callback_url=http://www.skyrock.com/&oauth_mode=flow_web_client&oauth_client_identifier=7ynojZQ3uVE2DifWftbS3w
After authentication, user gets redirected to:
http://www.skyrock.com/#oauth_access_token=RnEJAQAAAABpYH8NAAAAAGKSBQAAAAAAr9G2hLrnXaznri8LlSIaR0HuNBI=HLx1r47oqpobPncAm9DNeRCdySaMqoTKJcCLwnhIP&oauth_bridge_code=2wxWDd3IIZ0UW1y4oFpioWfbzTeaAlSGoJk5L6qMpGQ
...Use the API:
@jimfleming
jimfleming / _.missing
Created April 20, 2011 01:48
returns an object describing any missing attributes nested within an object
_.mixin({
missing: function(obj, requirements) {
var missing = {};
_.each(requirements, function(requirement, field) {
if (_.isNull(obj[field]) || _.isUndefined(obj[field])) {
missing[field] = false;
return;
}
if (typeof obj[field] === 'object') {
potential = _.missing(obj[field], requirement);
@jimfleming
jimfleming / rand_data.coffee
Created January 23, 2012 04:31
Generates random data for a redis database
#!/usr/bin/env coffee
guid = (length = 32) ->
chars = '0123456789abcdef'
result = ''
while length--
result += chars[Math.floor(Math.random() * chars.length)]
return result