1 | /** |
---|
2 | * Protify jQuery Plugin |
---|
3 | * version 0.3 |
---|
4 | * |
---|
5 | * Copyright (c) 2009 Josh Powell |
---|
6 | * Licensed under the MIT license. |
---|
7 | * |
---|
8 | * * Date: 2009-02-04 11:45:50 (Wed, 04 Feb 2009) |
---|
9 | * |
---|
10 | */ |
---|
11 | (function ( $ ) |
---|
12 | { |
---|
13 | var $break = { }; |
---|
14 | var arrayFunc = { |
---|
15 | _each: function( iterator ) |
---|
16 | { |
---|
17 | for( var i = 0, length = this.length; i < length; i++ ) |
---|
18 | { |
---|
19 | iterator( this[i] ); |
---|
20 | } |
---|
21 | }, |
---|
22 | |
---|
23 | all: function( iterator, context ) |
---|
24 | { |
---|
25 | iterator = iterator || function( x ) |
---|
26 | { |
---|
27 | return x; |
---|
28 | }; |
---|
29 | var result = true; |
---|
30 | this.each( function( value, index ) |
---|
31 | { |
---|
32 | result = result && !!iterator.call( context, value, index ); |
---|
33 | if( !result ) |
---|
34 | { |
---|
35 | throw $break; |
---|
36 | } |
---|
37 | } ); |
---|
38 | return result; |
---|
39 | }, |
---|
40 | |
---|
41 | any: function( iterator, context ) |
---|
42 | { |
---|
43 | iterator = iterator || function( x ) |
---|
44 | { |
---|
45 | return x; |
---|
46 | }; |
---|
47 | var result = false; |
---|
48 | this.each( function( value, index ) |
---|
49 | { |
---|
50 | if( result = !!iterator.call( context, value, index ) ) |
---|
51 | { |
---|
52 | throw $break; |
---|
53 | } |
---|
54 | } ); |
---|
55 | return result; |
---|
56 | }, |
---|
57 | |
---|
58 | clear: function() |
---|
59 | { |
---|
60 | this.length = 0; |
---|
61 | return this; |
---|
62 | }, |
---|
63 | |
---|
64 | clone: function() |
---|
65 | { |
---|
66 | return $.protify( [].concat( this ) ); |
---|
67 | }, |
---|
68 | |
---|
69 | collect: function( iterator, context ) |
---|
70 | { |
---|
71 | iterator = iterator || function( x ) |
---|
72 | { |
---|
73 | return x; |
---|
74 | }; |
---|
75 | var results = $.protify( [] ); |
---|
76 | this.each( function( value, index ) |
---|
77 | { |
---|
78 | results.push( iterator.call( context, value, index ) ); |
---|
79 | } ); |
---|
80 | return results; |
---|
81 | }, |
---|
82 | |
---|
83 | detect: function( iterator, context ) |
---|
84 | { |
---|
85 | var result; |
---|
86 | this.each( function( value, index ) |
---|
87 | { |
---|
88 | if( iterator.call( context, value, index ) ) |
---|
89 | { |
---|
90 | result = value; |
---|
91 | throw $break; |
---|
92 | } |
---|
93 | } ); |
---|
94 | return result; |
---|
95 | }, |
---|
96 | |
---|
97 | compact: function() |
---|
98 | { |
---|
99 | return $.protify( this.select( function( value ) |
---|
100 | { |
---|
101 | return value !== null; |
---|
102 | } ) ); |
---|
103 | }, |
---|
104 | |
---|
105 | each: function( iterator, context ) |
---|
106 | { |
---|
107 | context = context || this; |
---|
108 | var index = 0; |
---|
109 | try |
---|
110 | { |
---|
111 | this._each( function( value ) |
---|
112 | { |
---|
113 | iterator.call( context, value, index++ ); |
---|
114 | } ); |
---|
115 | } |
---|
116 | catch ( e ) |
---|
117 | { |
---|
118 | if( e != $break ) |
---|
119 | { |
---|
120 | throw e; |
---|
121 | } |
---|
122 | } |
---|
123 | return this; |
---|
124 | }, |
---|
125 | |
---|
126 | eachSlice: function( number, iterator, context ) |
---|
127 | { |
---|
128 | var index = -number, slices = [], array = this.toArray(); |
---|
129 | if( number < 1 ) |
---|
130 | { |
---|
131 | return array; |
---|
132 | } |
---|
133 | while( (index += number) < array.length ) |
---|
134 | { |
---|
135 | slices.push( array.slice( index, index + number ) ); |
---|
136 | } |
---|
137 | return $.protify( $.protify( slices ).collect( iterator, context ) ); |
---|
138 | }, |
---|
139 | |
---|
140 | extended : function() |
---|
141 | { |
---|
142 | return true; |
---|
143 | }, |
---|
144 | |
---|
145 | findAll: function( iterator, context ) |
---|
146 | { |
---|
147 | var results = $.protify( [] ); |
---|
148 | this.each( function( value, index ) |
---|
149 | { |
---|
150 | if( iterator.call( context, value, index ) ) |
---|
151 | { |
---|
152 | results.push( value ); |
---|
153 | } |
---|
154 | } ); |
---|
155 | return results; |
---|
156 | }, |
---|
157 | |
---|
158 | flatten: function() |
---|
159 | { |
---|
160 | return this.inject( [], function( array, value ) |
---|
161 | { |
---|
162 | $.protify( value ); |
---|
163 | return $.protify( array.concat( $.isArray( value ) ? |
---|
164 | value.flatten() : [value] ) ); |
---|
165 | } ); |
---|
166 | }, |
---|
167 | |
---|
168 | first: function() |
---|
169 | { |
---|
170 | return this[0]; |
---|
171 | }, |
---|
172 | |
---|
173 | grep: function( filter, iterator, context ) |
---|
174 | { |
---|
175 | iterator = iterator || function( x ) |
---|
176 | { |
---|
177 | return x; |
---|
178 | }; |
---|
179 | var results = $.protify( [] ); |
---|
180 | if( typeof filter === 'string' ) |
---|
181 | { |
---|
182 | filter = new RegExp( filter ); |
---|
183 | } |
---|
184 | |
---|
185 | this.each( function( value, index ) |
---|
186 | { |
---|
187 | if( filter.test( value ) ) |
---|
188 | { |
---|
189 | results.push( iterator.call( context, value, index ) ); |
---|
190 | } |
---|
191 | } ); |
---|
192 | return results; |
---|
193 | }, |
---|
194 | |
---|
195 | include: function( object ) |
---|
196 | { |
---|
197 | if( $.isFunction( this.indexOf ) ) |
---|
198 | { |
---|
199 | if( this.indexOf( object ) != -1 ) |
---|
200 | { |
---|
201 | return true; |
---|
202 | } |
---|
203 | } |
---|
204 | |
---|
205 | var found = false; |
---|
206 | this.each( function( value ) |
---|
207 | { |
---|
208 | if( value == object ) |
---|
209 | { |
---|
210 | found = true; |
---|
211 | throw $break; |
---|
212 | } |
---|
213 | } ); |
---|
214 | return found; |
---|
215 | }, |
---|
216 | |
---|
217 | indexOf: function( item, i ) |
---|
218 | { |
---|
219 | i || (i = 0); |
---|
220 | var length = this.length; |
---|
221 | if( i < 0 ) i = length + i; |
---|
222 | for( ; i < length; i++ ) |
---|
223 | if( this[i] === item ) return i; |
---|
224 | return -1; |
---|
225 | }, |
---|
226 | |
---|
227 | inGroupsOf: function( number, fillWith ) |
---|
228 | { |
---|
229 | fillWith = fillWith ? null : fillWith; |
---|
230 | return this.eachSlice( number, function( slice ) |
---|
231 | { |
---|
232 | while( slice.length < number ) |
---|
233 | { |
---|
234 | slice.push( fillWith ); |
---|
235 | } |
---|
236 | return slice; |
---|
237 | } ); |
---|
238 | }, |
---|
239 | |
---|
240 | inject: function( memo, iterator, context ) |
---|
241 | { |
---|
242 | this.each( function( value, index ) |
---|
243 | { |
---|
244 | memo = iterator.call( context, memo, value, index ); |
---|
245 | } ); |
---|
246 | return memo; |
---|
247 | }, |
---|
248 | |
---|
249 | inspect: function() |
---|
250 | { |
---|
251 | return '[' + this.map( $.inspect ).join( ', ' ) + ']'; |
---|
252 | }, |
---|
253 | |
---|
254 | intersect: function( array ) |
---|
255 | { |
---|
256 | $.protify( array ); |
---|
257 | return this.uniq().findAll( function( item ) |
---|
258 | { |
---|
259 | return array.detect( function( value ) |
---|
260 | { |
---|
261 | return item === value; |
---|
262 | } ); |
---|
263 | } ); |
---|
264 | }, |
---|
265 | |
---|
266 | invoke: function( method ) |
---|
267 | { |
---|
268 | var args = $.makeArray( arguments ).slice( 1 ); |
---|
269 | return this.map( function( value ) |
---|
270 | { |
---|
271 | return value[method].apply( value, args ); |
---|
272 | } ); |
---|
273 | }, |
---|
274 | |
---|
275 | last: function() |
---|
276 | { |
---|
277 | return this[this.length - 1]; |
---|
278 | }, |
---|
279 | |
---|
280 | lastIndexOf : function( item, i ) |
---|
281 | { |
---|
282 | i = isNaN( i ) ? this.length : (i < 0 ? this.length + i : i) + 1; |
---|
283 | var n = $.protify( this.slice( 0, i ).reverse() ).indexOf( item ); |
---|
284 | return (n < 0) ? n : i - n - 1; |
---|
285 | }, |
---|
286 | max: function( iterator, context ) |
---|
287 | { |
---|
288 | iterator = iterator || function( x ) |
---|
289 | { |
---|
290 | return x; |
---|
291 | }; |
---|
292 | var result; |
---|
293 | this.each( function( value, index ) |
---|
294 | { |
---|
295 | value = iterator.call( context, value, index ); |
---|
296 | if( result == null || value >= result ) |
---|
297 | { |
---|
298 | result = value; |
---|
299 | } |
---|
300 | } ); |
---|
301 | return result; |
---|
302 | }, |
---|
303 | |
---|
304 | min: function( iterator, context ) |
---|
305 | { |
---|
306 | iterator = iterator || function( x ) |
---|
307 | { |
---|
308 | return x; |
---|
309 | }; |
---|
310 | var result; |
---|
311 | this.each( function( value, index ) |
---|
312 | { |
---|
313 | value = iterator.call( context, value, index ); |
---|
314 | if( result == null || value < result ) |
---|
315 | { |
---|
316 | result = value; |
---|
317 | } |
---|
318 | } ); |
---|
319 | return result; |
---|
320 | }, |
---|
321 | |
---|
322 | partition: function( iterator, context ) |
---|
323 | { |
---|
324 | iterator = iterator || function( x ) |
---|
325 | { |
---|
326 | return x; |
---|
327 | }; |
---|
328 | var trues = [], falses = []; |
---|
329 | this.each( function( value, index ) |
---|
330 | { |
---|
331 | (iterator.call( context, value, index ) ? trues : falses).push( value ); |
---|
332 | } ); |
---|
333 | return [trues, falses]; |
---|
334 | }, |
---|
335 | |
---|
336 | pluck: function( property ) |
---|
337 | { |
---|
338 | var results = $.protify( [] ); |
---|
339 | this.each( function( value ) |
---|
340 | { |
---|
341 | results.push( value[property] ); |
---|
342 | } ); |
---|
343 | return results; |
---|
344 | }, |
---|
345 | |
---|
346 | purge: function () |
---|
347 | { |
---|
348 | return [].concat( this ); |
---|
349 | }, |
---|
350 | |
---|
351 | reduce: function() |
---|
352 | { |
---|
353 | return this.length > 1 ? this : this[0]; |
---|
354 | }, |
---|
355 | |
---|
356 | reject: function( iterator, context ) |
---|
357 | { |
---|
358 | var results = $.protify( [] ); |
---|
359 | this.each( function( value, index ) |
---|
360 | { |
---|
361 | if( !iterator.call( context, value, index ) ) |
---|
362 | { |
---|
363 | results.push( value ); |
---|
364 | } |
---|
365 | } ); |
---|
366 | return results; |
---|
367 | }, |
---|
368 | |
---|
369 | size: function() |
---|
370 | { |
---|
371 | return this.length; |
---|
372 | }, |
---|
373 | |
---|
374 | sortBy: function( iterator, context ) |
---|
375 | { |
---|
376 | return this.map( |
---|
377 | function( value, index ) |
---|
378 | { |
---|
379 | return { |
---|
380 | value: value, |
---|
381 | criteria: iterator.call( context, value, index ) |
---|
382 | }; |
---|
383 | } ).sort( |
---|
384 | function( left, right ) |
---|
385 | { |
---|
386 | var a = left.criteria, b = right.criteria; |
---|
387 | return a < b ? -1 : a > b ? 1 : 0; |
---|
388 | } ).pluck( 'value' ); |
---|
389 | }, |
---|
390 | |
---|
391 | toArray: function() |
---|
392 | { |
---|
393 | return $.protify( this.map() ); |
---|
394 | }, |
---|
395 | |
---|
396 | // toJSON: function() { |
---|
397 | // var results = []; |
---|
398 | // this.each(function(object) { |
---|
399 | // var value = Object.toJSON(object); |
---|
400 | // if (!Object.isUndefined(value)) results.push(value); |
---|
401 | // }); |
---|
402 | // return '[' + results.join(', ') + ']'; |
---|
403 | //}, |
---|
404 | |
---|
405 | uniq: function( sorted ) |
---|
406 | { |
---|
407 | return $.protify( this.inject( [], function( array, value, index ) |
---|
408 | { |
---|
409 | $.protify( array, true ); |
---|
410 | if( 0 === index || (sorted ? array.last() != value : !array.include( value )) ) |
---|
411 | { |
---|
412 | array.push( value ); |
---|
413 | } |
---|
414 | return array; |
---|
415 | } ) ); |
---|
416 | }, |
---|
417 | |
---|
418 | without: function() |
---|
419 | { |
---|
420 | var values = $.protify( $.makeArray( arguments ) ); |
---|
421 | return $.protify( this.select( function( value ) |
---|
422 | { |
---|
423 | return !values.include( value ); |
---|
424 | } ) ); |
---|
425 | }, |
---|
426 | |
---|
427 | zip: function() |
---|
428 | { |
---|
429 | var iterator = function( x ) |
---|
430 | { |
---|
431 | return x; |
---|
432 | }, args = $.protify( $.makeArray( arguments ) ); |
---|
433 | if( $.isFunction( args.last() ) ) |
---|
434 | { |
---|
435 | iterator = args.pop(); |
---|
436 | } |
---|
437 | |
---|
438 | var collections = $.protify( [this].concat( args ) ).map(); |
---|
439 | return this.map( function( value, index ) |
---|
440 | { |
---|
441 | return iterator( collections.pluck( index ) ); |
---|
442 | } ); |
---|
443 | } |
---|
444 | }; |
---|
445 | |
---|
446 | $.extend( arrayFunc, { |
---|
447 | map: arrayFunc.collect, |
---|
448 | find: arrayFunc.detect, |
---|
449 | select: arrayFunc.findAll, |
---|
450 | filter: arrayFunc.findAll, |
---|
451 | member: arrayFunc.include, |
---|
452 | entries: arrayFunc.toArray, |
---|
453 | every: arrayFunc.all, |
---|
454 | some: arrayFunc.any |
---|
455 | } ); |
---|
456 | |
---|
457 | $.protify = function( target, permanent ) |
---|
458 | { |
---|
459 | if( permanent ) |
---|
460 | { |
---|
461 | $.extend( target, arrayFunc ); |
---|
462 | return target; |
---|
463 | } |
---|
464 | target = $.extend( target.slice(), arrayFunc ); |
---|
465 | return target; |
---|
466 | }; |
---|
467 | |
---|
468 | })( jQuery ); |
---|
469 | |
---|
470 | |
---|
471 | function $A( array ) |
---|
472 | { |
---|
473 | return $.protify( array ) |
---|
474 | } |
---|