Skip to content

Instantly share code, notes, and snippets.

@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
# 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))"
// 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
@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;