Skip to content

Instantly share code, notes, and snippets.

View kensnyder's full-sized avatar

Ken Snyder kensnyder

View GitHub Profile
// Prototype object that works a lot like jQuery:
var NodeList = Class.create(Enumerable);
Object.extend(NodeList.prototype, {
initialize: function(selector) {
this.elements = $$(selector);
},
_each: function(iterator) {
this.elements.each(iterator);
return this;
}
// Prototype object that works a lot like jQuery:
var NodeList = Class.create(Enumerable);
Object.extend(NodeList.prototype, {
initialize: function(selector) {
this.elements = $$(selector);
},
_each: function(iterator) {
this.elements.each(iterator);
return this;
}
// Truncate a string to the closest word
String.prototype.truncateToWord = function(len) {
return (len = Number(len)) >= 0 ?
this.match(new RegExp('^([\\s\\S]{0,' + len + '})(:?\\s|$)'))[1] :
undefined;
};
// Examples
// The "v" marks the first character out of bounds.
#! /bin/sh
# oops need to first install http://packages.debian.org/sid/i386/libmozjs2d/download
# then http://packages.debian.org/sid/i386/libmozjs-dev/download
# init
mkdir nodejs-couchdb
cd nodejs-couchdb
# nodejs
@kensnyder
kensnyder / gist:821334
Created February 10, 2011 21:00
Hide spammy posts on google groups
var blacklist = {
title: [
/\bsexy?\b/i,
/online order/i,
/buy \w+ online/i,
/hot videos/i,
/earn money/i,
/prescription/i
],
body: [
@kensnyder
kensnyder / router.php
Created April 30, 2012 19:00
Create a rule for how a URL should be routed including the view name and the $_GET variables to set
<?php
function getViewPath($routes, $requestUri) {
// extract the path part of our url (e.g. '/news/3/1')
// and trim slashes
$url = trim( parse_url($requestUri, PHP_URL_PATH), '/');
if ($url == '') {
return 'views/home.php';
}
// get the values sent in the url
@kensnyder
kensnyder / app--plugins--find--models--behaviors--better_find.php
Created May 25, 2012 17:27
CakePHP Behavior to add Find Plugins to your models
<?php
/**
* Add custom find types to your model (CakePHP 1.3)
*/
class BetterFindBehavior extends ModelBehavior {
/**
* Trick to register methods in this class as custom find types
*
@kensnyder
kensnyder / curry.php
Created June 12, 2012 15:55
Curry a function in PHP 5.3
<?php
class CurriedFunction {
public function __construct($callback/*[, $arg1][, $arg2][, $argN]*/) {
$this->args = func_get_args();
$this->callback = array_shift($this->args);
}
public function __invoke(/*[, $arg1][, $arg2][, $argN]*/) {
@kensnyder
kensnyder / ppr.php
Created December 6, 2012 19:43
Pretty print_r
<?php
// DEFAULT OPTIONS
$GLOBALS['ppr_mode'] = 'screen'; // mode = screen | popup | email | log | off
$GLOBALS['ppr_char_limit'] = 200;//1024 * 500;
$GLOBALS['ppr_str_limit'] = 1024 * 4;
$GLOBALS['ppr_backtrace_str_limit'] = 100;
$GLOBALS['ppr_backtrace_array_limit'] = 6;
$GLOBALS['ppr_backtrace_arg_limit'] = 5;
$GLOBALS['ppr_mailto'] = @$_SERVER['SERVER_ADMIN'];
@kensnyder
kensnyder / myplugin.js
Created December 11, 2012 22:07
AMD Compatibility - Does this work?
function defineMyPlugin($) {
/*...*/
}
if (typeof define == 'function') {
define('MyPlugin', ['jquery'], defineMyPlugin);
}
else {
defineMyPlugin(jQuery);
}