Skip to content

Instantly share code, notes, and snippets.

View benpickles's full-sized avatar
🏎️

Ben Pickles benpickles

🏎️
View GitHub Profile
@benpickles
benpickles / gist:4755435
Last active December 12, 2015 09:59 — forked from olly/gist:4754077
Regexp for validating a UK postcode.
require 'rspec/autorun'
ValidUKPostcode = /\A(
[A-PR-UWYZ]\d{1,2} \s? \d[ABD-HJLNP-UWXYZ]{2}| # A9 9AA or A99 9AA
[A-PR-UWYZ][A-HK-Y]\d{1,2} \s? \d[ABD-HJLNP-UWXYZ]{2}| # AA9 9AA or AA99 9AA
[A-PR-UWYZ]\d[A-HJKSTUW] \s? \d[ABD-HJLNP-UWXYZ]{2}| # A9A 9AA
[A-PR-UWYZ][A-HK-Y]\d[ABEHMNPRVWXY] \s? \d[ABD-HJLNP-UWXYZ]{2}| # AA9A 9AA
GIR \s? 0AA
)\z/ix
@benpickles
benpickles / common-ancestor.js
Last active August 9, 2020 13:49
Written as a response to a Stack Overflow question "How to find the nearest common ancestors of two or more nodes?" https://stackoverflow.com/a/5350888/194664
function parents(node) {
var nodes = [node]
for (; node; node = node.parentNode) {
nodes.unshift(node)
}
return nodes
}
function commonAncestor(node1, node2) {
var parents1 = parents(node1)
@benpickles
benpickles / jquery.wordcount.js
Created May 11, 2012 15:33
Quick jQuery plugin to count the number of words in an input/textarea.
;(function($) {
var NONBLANK = /\S/
var SPLITTER = /[^a-z0-9-]+/i
function countWords(string) {
var trimmed = string.replace(/^\s+|\s+$/g, "")
if (NONBLANK.test(trimmed)) {
return trimmed.split(SPLITTER).length
} else {
@benpickles
benpickles / gist:2596158
Created May 4, 2012 16:48
WebMock.last_request
# Thankyou Bartosz Blimke!
# https://twitter.com/bartoszblimke/status/198391214247124993
module LastRequest
def last_request
@last_request
end
def last_request=(request_signature)
@last_request = request_signature
@benpickles
benpickles / gist:1312371
Created October 25, 2011 11:23
Finding model with custom IDs not working
var Activity = Model("activity", function() {
this.persistence(Model.REST, "/activity");
this.unique_key = '_id';
});
$(function() {
Activity.load(function() {
console.log(Activity.all())
var one_activity = Activity.find('fa7043e52b644cd3a7a2217edc0e8928');
console.log(one_activity)
@benpickles
benpickles / gist:1302039
Created October 20, 2011 19:21
Peity events example
<!DOCTYPE html>
<html>
<head>
<title>Peity events example</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
<script src="http://benpickles.github.com/peity/jquery.peity.js"></script>
<script>
// Apply Peity once.
$(function() {
$("#pie").peity("pie")
@benpickles
benpickles / repo.rb
Created September 27, 2011 12:11
Fetch curated list of Github repos sorted by watchers. Each repo is cached for a random time so only the first fetch will have to hit the API N times.
class Repo
REPOS = %w(
benpickles/js-model
benpickles/peity
ismasan/hash_mapper
ismasan/jbundle
markevans/dragonfly
mloughran/api_cache
mloughran/em-hiredis
mloughran/juggler
@benpickles
benpickles / blah.js
Created March 1, 2011 14:22
js-model `uid`s persist over localStorage page refreshes and are useful when you don't have a real `id`
////////////////
// IN CONSOLE //
////////////////
Blah = Model("blah", {
persistence: Model.localStorage(),
find_by_uid: function(uid) {
return this.detect(function() {
return this.uid == uid
@benpickles
benpickles / 2mp3
Created February 11, 2011 13:47
Convert multiple media files to MP3 and add them to iTunes in one line
#!/usr/bin/env sh
if [ $# -eq 0 ]; then
cat <<USAGE
Convert multiple media files to MP3 and add them to iTunes in one line.
Usage:
$ 2mp3 list of files to convert
USAGE
else
require 'test/unit'
require 'rubygems'
require 'mocha'
class Foo
def self.say_hello_to(name)
Bar.hello(name)
end
end