Skip to content

Instantly share code, notes, and snippets.

View peterjwest's full-sized avatar

Peter West peterjwest

  • Oxford, UK
View GitHub Profile
@peterjwest
peterjwest / showFilter.js
Last active September 24, 2015 01:28
showFilter
(function($){
$.fn.showFilter = function(selectors, criteria, options) {
this.data("lastVal", this.val());
var id = 1;
while($(".showFilter"+id).length > 0) id++;
this.addClass(".showFilter"+id);
var input = this;
var filterInput = function(input) { return $.trim(input).toLowerCase().replace(/\s+/, " ") }
var test = function() {
var c = criteria($(this));
@peterjwest
peterjwest / outerHtml.jquery.js
Created July 24, 2011 01:18
Better outerHtml plugin
(function($){
$.fn.outerHtml = function() {
if (this.length == 0) return false;
var elem = this[0], name = elem.tagName.toLowerCase();
if (elem.outerHTML) return elem.outerHTML;
var attrs = $.map(elem.attributes, function(i) { return i.name+'="'+i.value+'"'; });
return "<"+name+(attrs.length > 0 ? " "+attrs.join(" ") : "")+">"+elem.innerHTML+"</"+name+">";
};
})(jQuery);
@peterjwest
peterjwest / gist:3368643
Created August 16, 2012 09:08
Text limiter jQuery plugin
// Limits the number of characters in a textarea and displays characters remaining
// Accepts a jQuery element statusElem which displays how many characters are remaining
$.fn.textLimiter = function(statusElem, length) {
return this.each(function() {
var input = $(this);
var visible = true;
var initial = statusElem.text();
@peterjwest
peterjwest / gist:3713112
Created September 13, 2012 09:16
Mandango embedded woes
public function setSegments($segments)
{
$mandango = $this->getMandango();
$segments = array_map(function($name) use ($mandango) {
return $mandango->create('Model\Intervention\InterventionSetSegment')->setName($name);
}, $segments);
$segmentGroup = parent::getSegments();
$segmentGroup->remove($segmentGroup->all());
@peterjwest
peterjwest / specifity.md
Last active March 4, 2016 13:47
CSS Specificity

Some html:

<a id="redLink" class="red link" href="#">Click me!</a>

CSS specifity, low to high:

a { color: red; }
body a { color: red; }
a.red { color: red; }

body a.red { color: red; }

@peterjwest
peterjwest / gist:5169077
Last active December 14, 2015 23:58
Highlighting Chrome HTML5 validation bug (now fixed)
<!DOCTYPE html>
<html>
<head><title>Novalidate bug test</title></head>
<style>
button {
border: 0 background: red;
}
button span {
background: green;
}
@peterjwest
peterjwest / gist:5459304
Last active August 26, 2023 23:29
var_dump() sucks

Better variable dumping for PHP

  • debug($variable) prints html and source code friendly variable information
  • inspect($variable) returns the variable information as a string

Associative arrays are printed as:

{
  key: 'value'

'key 2': 'value 2'

@peterjwest
peterjwest / gist:6540612
Created September 12, 2013 16:47
Understanding blocks in ruby
# Normally a block is passed after the arguments
[1,2,3].map(){ |i| i.next }
# & passes the argument as a block
[1,2,3].map(&:next)
# & arguments are cast to a Proc
[1,2,3].map(&:next.to_proc)
# Procs are already procs
@peterjwest
peterjwest / 0-README.md
Last active August 29, 2015 13:59
Intercepting a request Rails, express.js and Symfony2

How to intercept any request that matches a route (in this case '/admin') in three different languages/frameworks.

@peterjwest
peterjwest / pad.js
Last active August 29, 2015 14:02
Left pad a string in javascript
var pad = function(string, length, char) {
return (new Array(length).join(char[0] || '0') + string).slice(-Math.max(string.toString().length, length));
};