~kosova/+junk/tuxfamily-twiki

« back to all changes in this revision

Viewing changes to foswiki/pub/System/JavascriptFiles/foswikiArray.js

  • Committer: James Michael DuPont
  • Date: 2009-07-18 19:58:49 UTC
  • Revision ID: jamesmikedupont@gmail.com-20090718195849-vgbmaht2ys791uo2
added foswiki

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
Array utility functions.
 
3
*/
 
4
 
 
5
var foswiki; if (foswiki == undefined) foswiki = {};
 
6
foswiki.Array = {
 
7
 
 
8
        /**
 
9
        Removes an object from an Array.
 
10
        @param inArray : (required) Array to remove object from
 
11
        @param inObject : (required) Object to remove from Array
 
12
        */
 
13
        remove:function(inArray, inObject) {
 
14
                if (!inArray || !inObject) return null;
 
15
                for (i=0; i<inArray.length; i++) {
 
16
                        if (inObject == inArray[i]) {
 
17
                                inArray.splice(i, 1);
 
18
                        }
 
19
                }
 
20
        },
 
21
        
 
22
        /**
 
23
        Creates an Array from a list of function arguments.
 
24
        @param inArguments : function arguments
 
25
        @param inStartIndex : (optional) the starting index in the list of function arguments
 
26
        @return A new Array with elements from the passed function arguments.
 
27
        @use
 
28
        The following code creates an Array of all arguments passed after inName:
 
29
        <pre>
 
30
        function releaseProps(inName) {
 
31
                var properties = foswiki.Array.convertArgumentsToArray(arguments, 1);
 
32
                if (!properties) return;
 
33
                _releaseProps(inName, properties);
 
34
        }
 
35
        </pre>
 
36
        */
 
37
        convertArgumentsToArray:function(inArguments, inStartIndex) {
 
38
                if (inArguments == undefined) return null;
 
39
                var ilen = inArguments.length;
 
40
                if (ilen == 0) return null;
 
41
                var start = 0;
 
42
                if (inStartIndex) {
 
43
                        if (isNaN(inStartIndex)) return null;
 
44
                        if (inStartIndex > ilen-1) return null;
 
45
                        start = inStartIndex;
 
46
                }               
 
47
                var list = [];
 
48
                for (var i = start; i < ilen; i++) {
 
49
                        list.push(inArguments[i]);
 
50
                }
 
51
                return list;
 
52
        },
 
53
        
 
54
        
 
55
        /**
 
56
        Determine the index of the (first) occurrence of an object in an array.
 
57
        @param inArray : (Array) array to search
 
58
        @param el : (Object) the object to find
 
59
        @return The index number; -1 if the object is not found; null if no valid Array has been passed.
 
60
        */
 
61
        indexOf:function(inArray, el) {
 
62
                if (!inArray || inArray.length == undefined) return null;
 
63
                var i, ilen = inArray.length;
 
64
                for (i=0; i<ilen; ++i) {
 
65
                        if (inArray[i] == el) return i;
 
66
                }
 
67
                return -1;
 
68
        }
 
69
};