Skip to content

Instantly share code, notes, and snippets.

@simenbrekken
simenbrekken / hide-ios-address-bar.js
Created August 27, 2012 14:53
Hide iOS address bar on ready and ajax load.
var hideAddressBar = function() {
_.defer(window.scrollTo, 0, 0);
};
$(document).ready(hideAddressBar).on('ajaxComplete', hideAddressBar);
@simenbrekken
simenbrekken / .htaccess
Created August 27, 2012 18:53
Apache mod_rewrite cookie based localization
RewriteEngine On
# Set locale cookie if lang query string is set (90 days expiry time)
RewriteCond %{QUERY_STRING} lang=(.*)
RewriteRule .* ?locale=%1 [cookie=locale:%1:.%{HTTP_HOST}:129600:/]
# Append locale to query string if:
# - Query string hasn't already been rewritten
# - Locale cookie is set
# - Request URI isn't a valid file (for assets etc.)
@simenbrekken
simenbrekken / pluck.php
Created September 4, 2012 10:50
Vae property plucker.
<?
function vae_pluck($path, $properties, $json = true) {
$results = array();
$context = vae($path);
foreach ($context as $entry) {
$result = array();
foreach ($properties as $key) {
$property = $entry[$key];
@simenbrekken
simenbrekken / Makefile
Created September 6, 2012 00:01
Annotated Makefile for combining and minifying LESS/CSS and JavaScript assets.
# Specify where our binaries are (I'm using package.json and npm to handle dependencies)
LESSC = node_modules/.bin/lessc
UGLIFYJS = node_modules/.bin/uglifyjs
# Our LESS input file(s)
LESS = css/base.less
# Our CSS list (replaces .less with .css in the list)
CSS = $(LESS:.less=.css)
@simenbrekken
simenbrekken / filtering.js
Created September 17, 2012 12:32
Backbone tag collection filtering with levenshtein
var Tags = Backbone.Collection.extend({
suggest: function(suggestion, threshold) {
return this.filter(function(tag) {
return levenshtein(tag.get('name'), suggestion) < (threshold || 3);
})
}
});
var tags = new Tags([
{name: 'Cat'},
@simenbrekken
simenbrekken / export.gs
Created October 24, 2012 21:17
Export Google Spreadsheet as JSON
function exportJSON() {
var sheets = SpreadsheetApp.getActiveSpreadsheet().getSheets();
var result = sheets.reduce(function(result, sheet) {
var id = sheet.getName().toLowerCase();
var data = sheet.getDataRange();
var values = data.getValues();
result[id] = values.map(function(row) {
return row.map(function(column) {
return Math.round(column) || column;
@simenbrekken
simenbrekken / Makefile
Created November 9, 2012 11:17
Makefile extraordinaire
STYLUS = node_modules/.bin/stylus
COFFEE = node_modules/.bin/coffee
UGLIFY = node_modules/.bin/uglifyjs
SPRITER = node_modules/.bin/spriter
RETINA = node_modules/.bin/retina
SCRIPTS = $(shell find src/scripts -name "*.coffee")
STYLES = $(shell find src/styles -name "*.styl")
SPRITES = $(shell find css/images/generated/sprites -name "*.png")
@simenbrekken
simenbrekken / Makefile
Created November 14, 2012 08:15
Variable expansion in Makefile
SPRITES = $(shell find lib/images/generated/sprites -name "*.png")
lib/%.min.css: lib/%.css
$(SPRITER) -t images/generated/sprites/$(shell basename $< .css) -f images/sprites $< | $(CLEANCSS) > $@
optipng -o7 -quiet $(SPRITES) # No such file
advdef -z -4 -q $(SPRITES) # No such file
@simenbrekken
simenbrekken / SublimeLinter.sublime-settings
Created November 15, 2012 14:57
Sublime linter settings
{
"jshint_options":
{
"browser": true, // This option defines globals exposed by modern browsers: all the way from good ol' document and navigator to the HTML5 FileReader and other new developments in the browser world.
"devel": true, // This option defines globals that are usually used for logging poor-man's debugging: console, alert, etc. It is usually a good idea to not ship them in production because, for example, console.log breaks in legacy versions of Internet Explorer.
"es5": true, // This option tells JSHint that your code uses ECMAScript 5 specific features such as getters and setters. Note that not all browsers implement these features.
"expr": true, // This option suppresses warnings about the use of expressions where normally you would expect to see assignments or function calls. Most of the time, such code is a typo. However, it is not forbidden by the spec and that's why this warning is optional.
"jquery": true, // This option defines globals expo
@simenbrekken
simenbrekken / Preferences.sublime-settings
Created November 17, 2012 13:27
Sublime Text 2 settings
{
"binary_file_patterns":
[
"*.jpg",
"*.jpeg",
"*.png",
"*.gif",
"*.ttf",
"*.tga",
"*.dds",