Skip to content

Instantly share code, notes, and snippets.

@criso
criso / primer.js
Created September 16, 2010 14:36 — forked from makinde/primer.js
!function() {
var doc = document,
htm = doc.documentElement,
lct = null, // last click target
nearest = function(elm, tag) {
while (elm && elm.nodeName != tag) {
elm = elm.parentNode;
}
return elm;
};
Math[flag ? "min" : "max"](a, b)
// from uglifyjs
if (foo) bar(); else baz(); ==> foo?bar():baz();
if (!foo) bar(); else baz(); ==> foo?baz():bar();
if (foo) bar(); ==> foo&&bar();
if (!foo) bar(); ==> foo||bar();
@criso
criso / api.js
Created July 24, 2011 15:43 — forked from fwielstra/api.js
An example NodeJS / Mongoose / Express application based on their respective tutorials
/* The API controller
Exports 3 methods:
* post - Creates a new thread
* list - Returns a list of threads
* show - Displays a thread and its posts
*/
var Thread = require('../models/thread.js');
var Post = require('../models/post.js');
@criso
criso / api-test.js
Created July 24, 2011 15:44 — forked from fwielstra/api-test.js
An example unit test for our save thread method
var nodeunit = require('nodeunit');
exports['API'] = nodeunit.testCase({
'The show() API method returns a specific thread by title and its replies by threadid': function(test) {
test.expect(4);
// our mock data
var posts = [{post: 'test'}, {post: 'test2'}];
var thread = {_id: '1234', title: 'my thread'};
@criso
criso / api.js
Created July 24, 2011 15:44 — forked from fwielstra/api.js
Our controller refactored
module.exports = function(Thread, Post) {
return {
post: function(req, res) {
new Thread({title: req.body.title, author: req.body.author}).save();
},
list: function(req, res) {
Thread.find(function(err, threads) {
res.send(threads);
});
@criso
criso / exports.readonly.js
Created August 13, 2011 23:07 — forked from 3rd-Eden/exports.readonly.js
Different ways of configuring nodejs
/**
* Configuration. This file is read only
*
* @api private
*/
module.exports = Object.freeze({
pewpew: "moo"
});
@criso
criso / gist:1149560
Created August 16, 2011 17:06
date shenanigans
var time = +new Date(); // get time
var d = new Date();
var month = ['jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec'][d.getMonth()];
@criso
criso / equivalent.js
Created August 24, 2011 14:20 — forked from tj/equivalent.js
example of backbone-style routing with Express
app.get('/help', function(req, res){
res.send('some help');
});
app.get('/search/:query/p:page', function(req, res){
var query = req.params.query
, page = req.params.page;
res.send('search "' + query + '", page ' + (page || 1));
});
@criso
criso / propCheck.js
Created November 16, 2012 15:13
checking existence of props
var props = {
foo: true,
bar: 1,
baz: false
};
if (props.baz) {
console.log("Won't work")
}
@criso
criso / separat-context.js
Created November 16, 2012 15:21
context separation @ded style
(function () {
with (this) {
!function (context) {
context.hello = function (msg) {
alert(msg);
};