Skip to content

Instantly share code, notes, and snippets.

View greggnakamura's full-sized avatar

Gregg Nakamura greggnakamura

View GitHub Profile
@greggnakamura
greggnakamura / gist:14420a7581e0c59adb6b
Last active August 29, 2015 14:22
Javascript: Number of prime numbers under 100 (HackerRank)
// variables
var number = 2,
count = 0;
// while prime count
while (number < 100) {
if (isPrime(number)) {
count++;
}
number++;
@greggnakamura
greggnakamura / gist:2bf83e37b201797ddc50
Created April 8, 2015 04:33
Express: Middleware and Request Flow
var express = require('express'),
bodyParser = require('body-parser'),
app = express();
// third party middleware
app.use(bodyParser.urlencoded());
// custom middleware
app.use(function (req, res, next) {
console.log('this will log on every request');
@greggnakamura
greggnakamura / gist:f6070c3752fcbffbd98b
Last active August 29, 2015 14:18
Express: Basic 'all', 'get', 'post', 'next' example with multiple callbacks (log); configuration examples
var express = require('express'),
bodyParser = require('body-parser'),
app = express();
// configurations
app.set('env', 'development'); // default: process.env.NODE_ENV
app.enable('trust proxy'); // for reverse proxy; disabled by default
app.set('jsonp callback name', 'cb'); // json with padding
app.set('json replacer', function (attr, val) {
if (attr === 'passwordHash') {
@paulirish
paulirish / bling.js
Last active August 27, 2024 04:55
bling dot js
/* bling.js */
window.$ = document.querySelectorAll.bind(document);
Node.prototype.on = window.on = function (name, fn) {
this.addEventListener(name, fn);
};
NodeList.prototype.__proto__ = Array.prototype;
@greggnakamura
greggnakamura / OPTION 1 Result
Last active August 29, 2015 14:18
Javascript: REST Level 3: Hypermedia-Driven Relationships (Nettuts: How to Build a Hypermedia-Driven REST API)
{
"links": {
"next": "http://localhost:3050/movies?page=2"
},
"request_info": {
"seconds": 0.002,
"cached": false,
"result_count": 4
},
"items": [
@greggnakamura
greggnakamura / gist:6eeb4710e88776869b8c
Created April 1, 2015 02:12
Javascript: REST Level 2 w/Status Codes example (Nettuts: How to Build a Hypermedia-Driven REST API)
var express = require('express');
var app = express();
var Datastore = require('nedb');
var db = {};
var responder = require('./httpResponder');
var port = process.argv[2] || 3050;
var root = 'http://localhost:' + port;
// Connect to an NeDB database
@greggnakamura
greggnakamura / gist:2436822139e13556c0c7
Created February 20, 2015 04:17
Javascript: # of steps it takes to execute naive(a, b) as a function of 'a'
var naive = function (a, b) {
var x = a; // 1 unit
var y = b; // 1 unit
var z = 0; // 1 unit
while (x > 0) { // runs twice
z = z + y;
x = x - 1;
return z;
}
};
@greggnakamura
greggnakamura / jquery-child-event-delegation.js
Last active August 29, 2015 14:14
Javascript: Event delegation example
// event listener on each child node
$('#parent li').on('click', function(e) {
console.log('List item:', e.target.id, 'was clicked');
});
@greggnakamura
greggnakamura / eventUtility.js
Created February 1, 2015 20:44
Javascript: Feature Detection, Cross-Browser Event Handling example
// eventUtility.js
var eventUtility = {
addEvent: function (el, type, fn) {
/*
el: HTML element object (DOM object)
type: event type to listen to
fn: function
*/