Skip to content

Instantly share code, notes, and snippets.

View gartenfeld's full-sized avatar

David Rosson gartenfeld

View GitHub Profile
@gartenfeld
gartenfeld / prime_sieve.js
Last active August 29, 2015 14:23
The Sieve of Eratosthenes.
function Eratosthenes(max) {
var elim = {},
primes = [];
for (var i=2; i<=max; i++) {
if ( !elim[i] ) {
primes.push(i);
for (var j=i*2; j<=max; j+=i) {
elim[j] = true;
}
}
@gartenfeld
gartenfeld / stringy.js
Created June 30, 2015 01:27
Experiment with stringifyJSON.
// this is what you would do if you liked things to be easy:
// var stringifyJSON = JSON.stringify;
// but you don't so you're going to write it from scratch:
var stringifyJSON = function(obj) {
if (obj===null) {
return 'null';
} else if (typeof obj === 'number' || 'boolean') {
@gartenfeld
gartenfeld / color_strip.js
Created July 9, 2015 23:49
Generating a hue spectrum.
var hue, cell,
grades = 100,
query = '';
for (var i = 0; i < grades; i++) {
hue = i * 360 / grades
query = '<div style="float:left;' +
'width:' + (100/grades) + '%;' +
'height:20px;' +
'background-color: hsl(' + hue + ', 100%, 75%);"></div>'
@gartenfeld
gartenfeld / perm.js
Created July 9, 2015 23:50
Generating permutations and logging each step.
function permutator(inputArr) {
// the output matrix of all permutations
var results = [];
// takes the array of character set
function permute(columnOptions, path) {
var place, newPath, path = path || [];
// for a given row
// go through the options for "which column"
@gartenfeld
gartenfeld / node_post.js
Created July 22, 2015 00:04
Extracting POST data in Node as the pieces arrive in a buffer stream.
var data = '';
request.on('data', function (partial) {
data += partial;
});
request.on('end', function () {
data = JSON.parse(data);
});
@gartenfeld
gartenfeld / powerset.js
Created July 23, 2015 17:10
Generating a powerset
function powerset(alphabet) {
var p = [[]];
for (var i=0; i < alphabet.length; i++) {
// here the value of len is set at the beginning
for (var j = 0, l = p.length; j < l; j++) {
p.push(p[j].concat(alphabet[i]));
// inspector
console.log(p);
}
}
@gartenfeld
gartenfeld / passport_session.js
Last active August 29, 2015 14:26
Passport authentication using basic credentials.
passport.use(new LocalStrategy( // a new Local instance
function(username, password, done) { // passing in a function
User.findOne({ username: username }, function (err, user) { // Mongo interface
if (err) { return done(err); } // generic error
if (!user) { return done(null, false); } // user not found
if (!user.verifyPassword(password)) { return done(null, false); } // wrong password
return done(null, user); // authenticated, returns 'user'
});
}
));
@gartenfeld
gartenfeld / backbone-collection-cycle.coffee
Last active August 29, 2015 14:26 — forked from rjz/backbone-collection-cycle.coffee
Find next model in a Backbone Collection
# Methods for cycling through the models in a Backbone Collection
# Usage:
#
# c = new MyCollection([...])
# nextModel = c.modelAfter(myModel)
# myModel == c.modelBefore(nextModel)
# # true
#
class MyCollection extends Backbone.Collection
@gartenfeld
gartenfeld / code_tag.css
Created August 6, 2015 23:53
Inline code styling.
code {
padding: 2px 3px;
color: #d14;
background-color: #f7f7f9;
border: 1px solid #e1e1e8;
white-space: nowrap;
}