Skip to content

Commit

Permalink
more stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
cowboy committed Sep 29, 2009
1 parent 4e6fdc4 commit f594a20
Show file tree
Hide file tree
Showing 7 changed files with 262 additions and 0 deletions.
34 changes: 34 additions & 0 deletions jquery.ba-cond.js
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,34 @@
/*!
* cond - v0.1 - 6/10/2009
* http://benalman.com/projects/jquery-cond-plugin/
*
* Copyright (c) 2009 "Cowboy" Ben Alman
* Licensed under the MIT license
* http://benalman.com/about/license/
*
* Based on suggestions and sample code by Stephen Band and DBJDBJ in the
* jquery-dev Google group: http://bit.ly/jqba1
*/

jQuery.fn.cond = function() {
var undefined,
args = arguments,
i = 0,
test,
callback,
result;

while ( !test && i < args.length ) {
test = args[ i++ ];
callback = args[ i++ ];

test = jQuery.isFunction( test ) ? test.call( this ) : test;

result = !callback ? test
: test ? callback.call( this, test )
: undefined;
}

return result !== undefined ? result : this;
};

77 changes: 77 additions & 0 deletions jquery.ba-emailSpammerize.js
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,77 @@
/*!
* emailSpammerize - v1.1 - 3/14/2009
* http://benalman.com/
*
* Copyright (c) 2009 "Cowboy" Ben Alman
* Licensed under the MIT license
* http://benalman.com/about/license/
*/

(function($) {
'$:nomunge'; // Used by YUI compressor.

var str_emailSpammerize = 'emailSpammerize',
replaces = {
at: '@', dot: '.', plus: '+', dash: '-', underscore: '_', bang: '!',
hash: '#', dollar: '$', percent: '%', ampersand: '&', quote: "'",
asterisk: '*', slash: '/', equals: '=', question: '?', caret: '^',
backtick: '`', pipe: '|', tilde: '~'
};

$[ str_emailSpammerize ] = function( email, linkify ) {
var re;

$.each( replaces, function(k,v){
re = new RegExp( '\\s+' + k + '\\s+', 'gi' );
email = email.replace( re, v );
});

email = email.replace( /(\s+|\[|\])/g, '' );

email = $.map( email, function(v){
return '&#' + v.charCodeAt() + ';';
}).join('');

if ( linkify ) {
email = '<a href="mailto:' + email + '">' + email + '<\/a>';
}

return email;
};

$.fn[ str_emailSpammerize ] = function( linkify ) {
return this.each(function(){
var email = $(this).text();

$(this).html( $[ str_emailSpammerize ]( email, linkify ) );
});
};

})(jQuery);

// About:
//
// "With a little help from Randal Schwartz, who held my nose to the web standards"
//
// Convert your anti-spammerized links (like "foo at bar dot com") to their
// proper format, which spammers theoretically shouldn't be able to harvest
// since this all happens via JavaScript after page load, and all text is
// encoded into numeric entities. If an email address contains a part that is
// also the name of a replace, like "dot" or "at", just surround it with [].
//
// Sample Usage:
//
// Initial HTML:
// <span>foo ampersand bar at example dot com</span>
//
// $('span').emailSpammerize(true);
//
// Resulting HTML:
// <span><a href="mailto:&#102;&#111;&#111;&#38;&#98;&#97;&#114;&#64;&#101;
// &#120;&#97;&#109;&#112;&#108;&#101;&#46;&#99;&#111;&#109;">&#102;&#111;
// &#111;&#38;&#98;&#97;&#114;&#64;&#101;&#120;&#97;&#109;&#112;&#108;&#101;
// &#46;&#99;&#111;&#109;</a></span>
//
// Which renders just like this would, were you to code it by hand:
// <span><a href="mailto:foo&bar@example.com">foo&bar@example.com</a></span>

51 changes: 51 additions & 0 deletions jquery.ba-getClassData.js
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,51 @@
/*!
* getClassData - v1.1 - 3/30/2009
* http://benalman.com/
*
* Copyright (c) 2009 "Cowboy" Ben Alman
* Licensed under the MIT license
* http://benalman.com/about/license/
*/

(function($) {
'$:nomunge'; // Used by YUI compressor.

var jq_getClassData,
str_getClassData = 'getClassData';

$[ str_getClassData ] = jq_getClassData = function( class_string, prefix, delimiter ) {
var re,
arr,
data = [];

delimiter = delimiter || '-';

re = new RegExp( '(?:^|\\s)' + prefix + delimiter + '(\\S+)', 'g' );
while ( arr = re.exec(class_string) ) {
data.push( arr[1] );
}

return data.join(' ');
};

$.fn[ str_getClassData ] = function( prefix, delimiter ) {
return jq_getClassData( $(this).attr('class'), prefix, delimiter );
};

})(jQuery);

// About:
//
// If you're not yet using HTML 5 data- attributes, you can store basic data
// in an element's class attribute for easy retrieval. Just give each datum a
// prefix, which you can then use to select it.
//
// Note: data can't contain spaces, and prefix is case-sensitive.
//
// Sample Usage:
//
// <div id="foo" class="bar-123 baz.test baz.xyz">Look, I'm a div!</div>
//
// $('#foo').getClassData('bar'); // returns '123'
// $('#foo').getClassData('baz', '.'); // returns 'test xyz'

27 changes: 27 additions & 0 deletions jquery.ba-getUniqueClass.js
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,27 @@
/*!
* getUniqueClass - v1.0 - 1/27/2009
* http://benalman.com/
*
* Copyright (c) 2009 "Cowboy" Ben Alman
* Licensed under the MIT license
* http://benalman.com/about/license/
*/

jQuery.getUniqueClass = function() {
var name, i = 0;
while ( $('.' + (name = 'BA-' + (+new Date) + (i++))).length ) { };
return name;
}

// About:
//
// For when you really need a unique classname, (like when you're cloning a
// whole bunch of elements and don't exactly know where they're going, but
// need to do something with them after they've gotten there).
//
// Sample Usage:
//
// var c = $.getUniqueClass(); // c set to 'BA-12352576545660' or so
// $('p').addClass(c);
// .. haphazardly clone a bunch of <p> elements ..
// $('.' + c).removeClass(c).doSomething();
17 changes: 17 additions & 0 deletions jquery.ba-iff.js
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,17 @@
/*!
* iff - v0.2 - 6/3/2009
* http://benalman.com/projects/jquery-iff-plugin/
*
* Copyright (c) 2009 "Cowboy" Ben Alman
* Licensed under the MIT license
* http://benalman.com/about/license/
*/

jQuery.fn.iff = function( test ) {
var elems = !test || jQuery.isFunction( test )
&& !test.apply( this, Array.prototype.slice.call(arguments, 1) )
? []
: this;
return this.pushStack( elems, 'iff', test );
};

38 changes: 38 additions & 0 deletions jquery.ba-nth-last-child.js
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,38 @@
/*!
* nth-last-child - v0.1 - 5/5/2009
* http://benalman.com/
*
* Copyright (c) 2009 "Cowboy" Ben Alman
* Licensed under the MIT license
* http://benalman.com/about/license/
*/

(function($) {
'$:nomunge'; // Used by YUI compressor.

$.expr[':']['nth-last-child'] = function( elem, i, match ) {
var count = 0,
node = elem.parentNode.firstChild;

if ( elem.nodeIndex ) {
count = $.sibling( node, elem ).length;
} else {
do {
if ( node && node.nodeType === 1 ) {
node.nodeIndex = ++count;
}
} while ( node = node.nextSibling );
}

return elem.nodeIndex === count + 2 - (match[3] || 0);
};

})(jQuery);

// About:
//
// Like :nth-child(N) but counts from the end, not the beginning.
//
// Sample Usage:
//
// $('ul li:nth-last-child(2)').addClass( 'penultimate-listitem' );
18 changes: 18 additions & 0 deletions jquery.ba-unwrap.js
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,18 @@
/*!
* unwrap - v0.1 - 7/18/2009
* http://benalman.com/projects/jquery-unwrap-plugin/
*
* Copyright (c) 2009 "Cowboy" Ben Alman
* Licensed under the MIT license
* http://benalman.com/about/license/
*/

jQuery.fn.unwrap = function() {
this.parent(':not(body)')
.each(function(){
jQuery(this).replaceWith( this.childNodes );
});

return this;
};

0 comments on commit f594a20

Please sign in to comment.