Skip to content

Instantly share code, notes, and snippets.

@orip
orip / jQuery.validate.requiredIfVisible.js
Created June 28, 2010 16:12
make a field required only if it's visible
// jQuery.validate.requiredIfVisible.js
// Copyright (c) 2010 Ori Peleg, http://orip.org, distributed under the MIT license
(function($) {
$.validator.addMethod(
"requiredIfVisible",
function(value, element, params) {
function isVisible(e) {
// the element and all of its parents must be :visible
// inspiration: http://remysharp.com/2008/10/17/jquery-really-visible/
return e.is(":visible") && e.parents(":not(:visible)").length == 0;
// jQuery plugins to focus a fragment
//
// Uses a StackOverflow-like "flashing" effect.
// Degrades gracefully: assumes anchors have a fragment, so
// the scrolling will work without JS.
//
// Written by Ori Peleg
// Copyright (c) 2010 CloudShare, http://www.cloudshare.com/
// Distributed under the MIT license
# one-liner for generating a 14-char password
# On Windows this is a secure password, based on CryptGenRandom
# On *nix this is based on /dev/urandom which is intended to be a cryptographically-secure PRNG but
# usually isn't. It should still be good enough for passwords.
python -c "import random, string; r = random.SystemRandom(); print ''.join(r.choice(string.letters + string.digits) for i in xrange(14))"
@orip
orip / sorter.js
Created January 24, 2012 15:39
Javascript advanced sorter
/**
* Create a compare function for Javascript's Array.sort()
* Inspired by Triptych's http://stackoverflow.com/a/979325/37020
*
* my_array.sort( sorter({key: 'size', reverse: true}) );
* my_array.sort( sorter({key: function(x) { return parseInt(x.tag); }) );
*/
var sorter = function(spec) {
if (spec.key === undefined)
spec.key = function(x) { return x; }; // identity
@orip
orip / .gitignore
Created May 1, 2012 18:03
fdkiller in Ruby
.*.swp
@orip
orip / bugsense_bugs_to_json.js
Created June 26, 2012 12:14
Dump JSON describing android bugs on BugSense
console.log(JSON.stringify($('#error-sum-table .e').map(function(i, x) {
var $x = $(x);
var message = $x.find('.error-name').text().trim();
var count = parseInt($x.find('.counter').text().trim());
var filegroups = $x.find('.class-file').text().trim().match(/File: ([^ ]*)/);
var appvers = $x.find('.app_version').map(function(i, x) {
return $(x).text().trim();
});
var bugid = $x.find('.middle').data('id');
return {
@orip
orip / temp_chdir.py
Created July 22, 2012 19:22
temp_chdir
import os, contextlib
@contextlib.contextmanager
def temp_chdir(path):
"""
Usage:
>>> with temp_chdir(gitrepo_path):
... subprocess.call('git status')
"""
starting_directory = os.getcwd()
#!/bin/sh
# inspired by http://www.cimgf.com/2011/02/20/revisiting-git-tags-and-building/git_hash=`git rev-parse --short HEAD 2>/dev/null || echo "nohash"`
TARGET_FILE=${PROJECT_TEMP_DIR}/InfoPlist.h
echo "#define GIT_HASH $git_hash" > $TARGET_FILE
touch $INFOPLIST_FILE
@orip
orip / weakself_macro.h
Created August 23, 2012 11:02
WEAKSELF macro for Objective-C and ARC
/*
Usage:
WEAKSELF_T weakSelf = self;
dispatch_async(queue, ^{
[weakSelf coolStuff];
});
*/
#if __has_feature(objc_arc_weak)
#define WEAKSELF_T __weak __typeof__(self)
@orip
orip / GsonHelper.java
Created September 5, 2012 11:22
Gson type adapter to serialize and deserialize byte arrays in base64
import java.lang.reflect.Type;
import android.util.Base64;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;