Docs: Update remaining HTTP URLs to HTTPS
[jquery.git] / src / core / access.js
blob86bac126c529683e2691f641a0b98eba696d8417
1 import { jQuery } from "../core.js";
2 import { toType } from "../core/toType.js";
4 // Multifunctional method to get and set values of a collection
5 // The value/s can optionally be executed if it's a function
6 export function access( elems, fn, key, value, chainable, emptyGet, raw ) {
7         var i = 0,
8                 len = elems.length,
9                 bulk = key == null;
11         // Sets many values
12         if ( toType( key ) === "object" ) {
13                 chainable = true;
14                 for ( i in key ) {
15                         access( elems, fn, i, key[ i ], true, emptyGet, raw );
16                 }
18         // Sets one value
19         } else if ( value !== undefined ) {
20                 chainable = true;
22                 if ( typeof value !== "function" ) {
23                         raw = true;
24                 }
26                 if ( bulk ) {
28                         // Bulk operations run against the entire set
29                         if ( raw ) {
30                                 fn.call( elems, value );
31                                 fn = null;
33                         // ...except when executing function values
34                         } else {
35                                 bulk = fn;
36                                 fn = function( elem, _key, value ) {
37                                         return bulk.call( jQuery( elem ), value );
38                                 };
39                         }
40                 }
42                 if ( fn ) {
43                         for ( ; i < len; i++ ) {
44                                 fn(
45                                         elems[ i ], key, raw ?
46                                                 value :
47                                                 value.call( elems[ i ], i, fn( elems[ i ], key ) )
48                                 );
49                         }
50                 }
51         }
53         if ( chainable ) {
54                 return elems;
55         }
57         // Gets
58         if ( bulk ) {
59                 return fn.call( elems );
60         }
62         return len ? fn( elems[ 0 ], key ) : emptyGet;