Commits

Paul McLanahan  committed 912ef18

Initial import of the code.

  • Participants

Comments (0)

Files changed (3)

+{
+	"name": "jQuery-Array",
+	"version": 0.1,
+	"author": [
+		"John Resig <jeresig@gmail.com>",
+		"Paul McLanahan <pmclanahan@gmail.com>",
+		"Kenton Simpson"
+	],
+	"abstract": "A collection of jQuery plugins to make jQuery objects behave more like a JavaScript Array",
+	"license": "mit, gpl",
+	"distribution_type": "extension",
+	"requires": {
+		"jQuery": ">= 1.0, <= 1.2"
+	},
+	"provides": {
+		"jQuery.Array": {
+			"version": 0.2,
+			"file": "jquery.array.js"
+		}
+	},
+	"keywords": [
+		"array",
+		"JavaScript"
+	],
+	"meta-spec": {
+		"version": 1.3,
+		"url": "http://module-build.sourceforge.net/META-spec-v1.3.html"
+	},
+	"generated_by": "Yehuda Katz"
+}

File arrayTest.html

+<html id="html">
+<head>
+	<title>Array Plugins Test Suite</title>
+	<link rel="Stylesheet" media="screen" href="../../../qunit/testsuite.css" />
+	<script type="text/javascript" src="../../jquery/dist/jquery.js"></script>
+	<script type="text/javascript" src="../../../qunit/testrunner.js"></script>
+	<script type="text/javascript" src="jquery.array.js"></script>
+	<script type="text/javascript">
+	test("Requirements",function(){
+		expect(8);
+		ok(jQuery,"jQuery");
+		ok(jQuery.fn.jquery, "jQuery Version: "+jQuery.fn.jquery);
+		ok(Array.prototype.push, "Array.push()");
+		ok(Array.prototype.shift, "Array.shift()");
+		ok(Array.prototype.slice, "Array.slice()");
+		ok(Array.prototype.sort, "Array.sort()");
+		ok(Array.prototype.reverse, "Array.reverse()");
+		ok(jQuery.fn.reverse, "Array Plugins");
+	});
+	
+	module('Array');
+	
+	test("reverse",function(){
+		expect(3);
+		var $lis = $('#testList li');
+		isSet($lis.reverse().get(),q('sixth','fifth','fourth','third','second','first'),'Basic Reverse');
+		isSet($lis.get(),q('first','second','third','fourth','fifth','sixth'),'Non Destructive');
+		$lis.reverse(true);
+		isSet($lis.get(),q('sixth','fifth','fourth','third','second','first'),'Destructive');
+	});
+	
+	test('sort',function(){
+		expect(3);
+		var sortFn = function(a,b){return parseInt(a.innerHTML) - parseInt(b.innerHTML);};
+		var $lis = $('#testList li');
+		isSet($lis.sort(sortFn).get(),q('third','fourth','second','fifth','first','sixth'),'Basic Numeric Sort');
+		isSet($lis.get(),q('first','second','third','fourth','fifth','sixth'),'Non Destructive');
+		$lis.sort(sortFn,true);
+		isSet($lis.get(),q('third','fourth','second','fifth','first','sixth'),'Destructive');
+	});
+	
+	test("randomize",function(){
+		expect(3);
+		var $lis = $('#testList li');
+		// not sure how to test randomize other than to assert that the array is no longer in the same order
+		isNotSet($lis.randomize().get(),q('first','second','third','fourth','fifth','sixth'),'Basic Randomize');
+		isSet($lis.get(),q('first','second','third','fourth','fifth','sixth'),'Non Destructive');
+		$lis.randomize(true);
+		isNotSet($lis.get(),q('first','second','third','fourth','fifth','sixth'),'Destructive');
+	});
+	
+	test("slice",function(){
+		expect(3);
+		var $lis = $('#testList li');
+		isSet($lis.slice(1,4).get(),q('second','third','fourth'),'Basic Slice');
+		isSet($lis.get(),q('first','second','third','fourth','fifth','sixth'),'Non Destructive');
+		$lis.slice(1,4,true);
+		isSet($lis.get(),q('second','third','fourth'),'Destructive');
+	});
+	
+	test("pop",function(){
+		expect(4);
+		var $lis = $('#testList li'), returned = {};
+		isSet($lis.pop(false,returned).get(),q('first','second','third','fourth','fifth'),'Basic Pop');
+		ok($lis[$lis.length-1]==returned.pop,'Popped Element');
+		isSet($lis.get(),q('first','second','third','fourth','fifth','sixth'),'Non Destructive');
+		$lis.pop(true);
+		isSet($lis.get(),q('first','second','third','fourth','fifth'),'Destructive');
+	});
+	
+	test("shift",function(){
+		expect(4);
+		var $lis = $('#testList li'), returned = {};
+		isSet($lis.shift(false,returned).get(),q('second','third','fourth','fifth','sixth'),'Basic Shift');
+		ok($lis[0]==returned.shift,'Shifted Element');
+		isSet($lis.get(),q('first','second','third','fourth','fifth','sixth'),'Non Destructive');
+		$lis.shift(true);
+		isSet($lis.get(),q('second','third','fourth','fifth','sixth'),'Destructive');
+	});
+	
+	test("rotate",function(){
+		expect(4);
+		var $lis = $('#testList li');
+		isSet($lis.rotate().get(),q('second','third','fourth','fifth','sixth','first'),'Basic Rotate');
+		isSet($lis.rotate().rotate().get(),q('third','fourth','fifth','sixth','first','second'),'Double Rotate');
+		isSet($lis.get(),q('first','second','third','fourth','fifth','sixth'),'Non Destructive');
+		$lis.rotate(true);
+		isSet($lis.get(),q('second','third','fourth','fifth','sixth','first'),'Destructive');
+	});
+	
+/**
+ * Asserts that two arrays are NOT the same
+ * Used to test for randomness. Not a good solution.
+ * TODO: Write a better test
+ */
+function isNotSet(a, b, msg) {
+	var ret = true;
+	if ( a && b && a.length == b.length ) {
+		for ( var i in a )
+			if ( a[i] != b[i] )
+				ret = false;
+	} else
+		ret = false;
+	if ( ret )
+		_config.Test.push( [ !ret, msg + " expected: " + b + " result: " + a ] );
+	else 
+		_config.Test.push( [ !ret, msg ] );
+}
+	</script>
+</head>
+
+<body id="body">
+	<h1>Array Plugins Test Suite</h1>
+	<h2 id="banner"></h2>
+	<h2 id="userAgent"></h2>
+	
+	<!-- Test HTML -->
+	<dl style="display:none;">
+	<div id="main" style="display: none;">
+	<ul id="testList">
+		<li id="first">5</li>
+		<li id="second">3</li>
+		<li id="third">1</li>
+		<li id="fourth">2</li>
+		<li id="fifth">4</li>
+		<li id="sixth">6</li>
+	</ul>
+	</div>
+	</dl>
+	
+	<ol id="tests"></ol>
+</body>
+</html>

File jquery.array.js

+/*
+ * jQuery Array - A collection of jQuery plugins to make jQuery objects behave
+ * more like a Javascript Array
+ *
+ * Based on work by the jQuery Community (http://www.nabble.com/.each-backwards---tf2399145.html#a6690114)
+ * and Kenton Simpson (http://www.brainknot.com/code/jQarray.js)
+ *
+ * Copyright (c) 2007 Paul McLanahan, Kenton Simpson
+ *
+ * Dual licensed under the MIT and GPL licenses:
+ *   http://www.opensource.org/licenses/mit-license.php
+ *   http://www.gnu.org/licenses/gpl.html
+ *
+ * Revision: $Id$
+ *
+ * Version: 0.1
+ */
+ 
+/**
+ * Reverse the order elements appear in the jQuery element collection
+ *
+ * @example $("li").reverse().each(function(i) {
+ *   $(this).prepend(i+": ");
+ * });
+ * @before <li>First Item</li>
+ * <li>Second Item</li>
+ * <li>Third Item</li>
+ * @result <li>2: First Item</li>
+ * <li>1: Second Item</li>
+ * <li>0: Third Item</li>
+ *
+ * @name reverse
+ * @type jQuery
+ * @param Boolean destruct If true this function will act distructively on a cached jQuery object.
+ * @cat Plugins/Array
+ */
+jQuery.fn.reverse = function(destruct) {
+	return this[destruct?'setArray':'pushStack'](this.get().reverse());
+};
+
+/**
+ * Sort the order elements appear in the jQuery element collection
+ *
+ * @example $("li").sort(function( a, b ){
+ *   return( parseInt( a.innerHTML ) - parseInt( b.innerHTML ) );
+ * });
+ *
+ * @name sort
+ * @type jQuery
+ * @param Function fn Sort function to act on jQuery element collection.
+ * @param Boolean destruct If true this function will act distructively on a cached jQuery object.
+ * @cat Plugins/Array
+ */
+jQuery.fn.sort = function(fn,destruct) {
+	return this[destruct?'setArray':'pushStack'](this.get().sort(fn));
+};
+
+/**
+ * Randomize the order of the elements in the current jQuery element collection.
+ * Note: Does not affect the order of elements in the DOM.
+ *
+ * @example $("li").randomize();
+ *
+ * @name randomize
+ * @type jQuery
+ * @param Boolean destruct If true, this function will act distructively on a cached jQuery object.
+ * @cat Plugins/Array
+ */
+jQuery.fn.randomize = function(destruct){
+	return this.sort( function(){return(Math.round(Math.random())-0.5)}, destruct );
+};
+
+/**
+ * Take a slice out of the current jQuery element collection
+ *
+ * @example $li = $("li").slice(2, 6).hide();
+ * @result $li.length == 4
+ *
+ * @name slice
+ * @type jQuery
+ * @param Int Start of slice
+ * @param Int End of slice
+ * @param Boolean destruct If true, this function will act distructively on a cached jQuery object
+ * @cat Plugins/Array
+ */
+// Check for jQuery 1.2 and backup default slice function
+if(jQuery.fn.slice) jQuery.fn._slice = jQuery.fn.slice;
+jQuery.fn.slice = function(start,end,destruct) {
+	return this[destruct?'setArray':'pushStack']( Array.prototype.slice.call( this, start, end ) );
+};
+
+/**
+ * Remove the last element in the jQuery element collection and disregard
+ * Note: This does not return the popped element, which is unlike the native Array.pop
+ * Javascript function. It returns a jQuery object to maintain chainability. See
+ * returnObject parameter for a workaround.
+ *
+ * @example $('li').pop().length == $('li').length - 1
+ *
+ * @name shift
+ * @type jQuery
+ * @param Boolean destruct If true, this function will act distructively on a cached jQuery object
+ * @param Object returnObject If supplied the object will be extended with a "pop" property containing the popped element
+ * @cat Plugins/Array
+ */
+jQuery.fn.pop = function(destruct,returnObject){
+	if(returnObject)jQuery.extend(returnObject,{pop:this[this.length-1]});
+	return this.slice( 0, -1, destruct );
+};
+
+/**
+ * Remove the first element in the jQuery element collection and disregard
+ * Note: This does not return the shifted element, which is unlike the native Array.shift
+ * Javascript function. It returns a jQuery object to maintain chainability. See
+ * returnObject parameter for a workaround.
+ *
+ * @example $('li').shift()[0] == 2nd <li> found
+ *
+ * @name shift
+ * @type jQuery
+ * @param Boolean destruct If true, this function will act distructively on a cached jQuery object
+ * @param Object returnObject If supplied the object will be extended with a "shift" property containing the shifted element
+ * @cat Plugins/Array
+ */
+jQuery.fn.shift = function(destruct,returnObject){
+	if(returnObject)jQuery.extend(returnObject,{shift:this[0]});
+	return this.slice( 1, this.length, destruct );
+};
+
+/**
+ * Move the first jQuery element to the last
+ *
+ * @example $('li').rotate()[0] == 2nd <li> found
+ *
+ * @name rotate
+ * @type jQuery
+ * @param Boolean destruct If true, this function will act distructively on a cached jQuery object
+ * @cat Plugins/Array
+ */
+jQuery.fn.rotate = function(destruct){
+	var a = this.get();
+	a.push(a.shift());
+	return this[destruct?'setArray':'pushStack']( a );
+};
+
+/**
+ * Move the last jQuery element to the first
+ *
+ * @example $('li').rrotate()[0] == last <li> found
+ *
+ * @name rrotate
+ * @type jQuery
+ * @param Boolean destruct If true, this function will act distructively on a cached jQuery object
+ * @cat Plugins/Array
+ */
+jQuery.fn.rrotate = function(destruct){
+  var a = this.get();
+  a.unshift(a.pop());
+  return this[destruct?'setArray':'pushStack']( a );
+};