Skip to content

Instantly share code, notes, and snippets.

View xjamundx's full-sized avatar

Jamund Ferguson xjamundx

View GitHub Profile
@xjamundx
xjamundx / pagination.html
Created April 19, 2011 05:29
a partial view used with the express-pagination.js example
<nav class="pagination">
<% if (locals.prev) { %>
<a href="/pages/<%= (page + 1) %>">Previous</a>
<% } %>
<% if (locals.next) { %>
<a href="/pages/<%= (page - 1) %>">Next</a>
<% } %>
@xjamundx
xjamundx / sort-example.js
Created April 20, 2011 00:30
fancy object sort example
var locations = {
seattle: {reviews:[{title:"c"}]},
portland: {reviews:[{title:"v"}]},
provo: {reviews:[{title:"c"}]},
};
sort(locations).by("title").on("reviews");
function sort(obj) {
sort.obj = obj;
@xjamundx
xjamundx / tags.js
Created April 20, 2011 15:21
mongo group tag cloud stuff
app.get('/find/other', function(req, res) {
var reduce = function(obj, prev) {
obj.tags.forEach(function(tag) {
prev.tagCounts[tag] = prev.tagCounts[tag] ? prev.tagCounts[tag] + 1 : 1;
})
};
db.reviews.group({}, {}, {tagCounts:{}}, reduce, function(err, result) {
@xjamundx
xjamundx / random-disqus-winner.js
Created May 2, 2011 16:45
jQuery Code to Pick a Random Winner of Disqus Comments
// run this in the console of the page
$(".dsq-commenter-name").eq(Math.round(Math.random() * $(".dsq-commenter-name").length)).text();
@xjamundx
xjamundx / DateTime.js
Created May 12, 2011 03:57
Example of a client/server module
// NOTE: Avoid using any requires up here as
// this file may be included on the client.
function DateTime (dateOrString, time) {
var time;
this._date;
this.offset;
// if you're a DateTime object or an old timemodule object
@xjamundx
xjamundx / badJSON.js
Created June 4, 2011 22:16
error catching in js
function doStuff(cb) {
var badJSON = "asdlfihsdofy893q4fuiH:asdfh98#@!!!";
var bad = JSON.parse(badJSON)
console.log("bad", bad)
cb(bad)
}
try{
doStuff(function(str) {
console.log("hi", str)
@xjamundx
xjamundx / mr.js
Created June 7, 2011 05:35
map reduce example
// groups users by type
var map = function() {
emit(this.type, 1)
}
var reduce = function(key, values) {
var result = 0
values.forEach(function(value) {
result += value
})
return result
@xjamundx
xjamundx / dateHelperSample.js
Created August 4, 2011 17:24
Epress.js Date Helper Example
// main app
var helpers = require('./util/helpers')
app.helpers({"timeAgo": helpers.timeAgo})
// view code
<span class="created"><%= timeAgo(activity.created) %></span>
@xjamundx
xjamundx / timeAgo.js
Created August 4, 2011 17:27
ExpressJS TimeAgo View Helper
// helpers file
/*
* JavaScript Pretty Date
* Copyright (c) 2008 John Resig (jquery.com)
* Licensed under the MIT license.
*
* Small Modification by Jamund Ferguson to accept a real Date object
*/
exports.timeAgo = function(time) {
@xjamundx
xjamundx / DateTime.js
Created August 4, 2011 17:34
DateTime helper for client/server
// NOTE: Avoid using any requires up here as
// this file may be included on the client.
function DateTime (dateOrString, time) {
var time
this._date
this.offset
// generate a new datetime of now