root / trunk / web / dojo / dojox / lang / functional / array.js @ 11
History | View | Annotate | Download (2.5 KB)
1 | 9 | andrej.cim | /*
|
---|---|---|---|
2 | Copyright (c) 2004-2010, The Dojo Foundation All Rights Reserved.
|
||
3 | Available via Academic Free License >= 2.1 OR the modified BSD license.
|
||
4 | see: http://dojotoolkit.org/license for details
|
||
5 | */
|
||
6 | |||
7 | |||
8 | if(!dojo._hasResource["dojox.lang.functional.array"]){ |
||
9 | dojo._hasResource["dojox.lang.functional.array"]=true; |
||
10 | dojo.provide("dojox.lang.functional.array");
|
||
11 | dojo.require("dojox.lang.functional.lambda");
|
||
12 | (function(){
|
||
13 | var d=dojo,df=dojox.lang.functional,_1={};
|
||
14 | d.mixin(df,{filter:function(a,f,o){ |
||
15 | if(typeof a=="string"){ |
||
16 | a=a.split("");
|
||
17 | } |
||
18 | o=o||d.global; |
||
19 | f=df.lambda(f); |
||
20 | var t=[],v,i,n;
|
||
21 | if(d.isArray(a)){
|
||
22 | for(i=0,n=a.length;i<n;++i){ |
||
23 | v=a[i]; |
||
24 | if(f.call(o,v,i,a)){
|
||
25 | t.push(v); |
||
26 | } |
||
27 | } |
||
28 | }else{
|
||
29 | if(typeof a.hasNext=="function"&&typeof a.next=="function"){ |
||
30 | for(i=0;a.hasNext();){ |
||
31 | v=a.next(); |
||
32 | if(f.call(o,v,i++,a)){
|
||
33 | t.push(v); |
||
34 | } |
||
35 | } |
||
36 | }else{
|
||
37 | for(i in a){ |
||
38 | if(!(i in _1)){ |
||
39 | v=a[i]; |
||
40 | if(f.call(o,v,i,a)){
|
||
41 | t.push(v); |
||
42 | } |
||
43 | } |
||
44 | } |
||
45 | } |
||
46 | } |
||
47 | return t;
|
||
48 | },forEach:function(a,f,o){ |
||
49 | if(typeof a=="string"){ |
||
50 | a=a.split("");
|
||
51 | } |
||
52 | o=o||d.global; |
||
53 | f=df.lambda(f); |
||
54 | var i,n;
|
||
55 | if(d.isArray(a)){
|
||
56 | for(i=0,n=a.length;i<n;f.call(o,a[i],i,a),++i){ |
||
57 | } |
||
58 | }else{
|
||
59 | if(typeof a.hasNext=="function"&&typeof a.next=="function"){ |
||
60 | for(i=0;a.hasNext();f.call(o,a.next(),i++,a)){ |
||
61 | } |
||
62 | }else{
|
||
63 | for(i in a){ |
||
64 | if(!(i in _1)){ |
||
65 | f.call(o,a[i],i,a); |
||
66 | } |
||
67 | } |
||
68 | } |
||
69 | } |
||
70 | return o;
|
||
71 | },map:function(a,f,o){ |
||
72 | if(typeof a=="string"){ |
||
73 | a=a.split("");
|
||
74 | } |
||
75 | o=o||d.global; |
||
76 | f=df.lambda(f); |
||
77 | var t,n,i;
|
||
78 | if(d.isArray(a)){
|
||
79 | t=new Array(n=a.length);
|
||
80 | for(i=0;i<n;t[i]=f.call(o,a[i],i,a),++i){ |
||
81 | } |
||
82 | }else{
|
||
83 | if(typeof a.hasNext=="function"&&typeof a.next=="function"){ |
||
84 | t=[]; |
||
85 | for(i=0;a.hasNext();t.push(f.call(o,a.next(),i++,a))){ |
||
86 | } |
||
87 | }else{
|
||
88 | t=[]; |
||
89 | for(i in a){ |
||
90 | if(!(i in _1)){ |
||
91 | t.push(f.call(o,a[i],i,a)); |
||
92 | } |
||
93 | } |
||
94 | } |
||
95 | } |
||
96 | return t;
|
||
97 | },every:function(a,f,o){ |
||
98 | if(typeof a=="string"){ |
||
99 | a=a.split("");
|
||
100 | } |
||
101 | o=o||d.global; |
||
102 | f=df.lambda(f); |
||
103 | var i,n;
|
||
104 | if(d.isArray(a)){
|
||
105 | for(i=0,n=a.length;i<n;++i){ |
||
106 | if(!f.call(o,a[i],i,a)){
|
||
107 | return false; |
||
108 | } |
||
109 | } |
||
110 | }else{
|
||
111 | if(typeof a.hasNext=="function"&&typeof a.next=="function"){ |
||
112 | for(i=0;a.hasNext();){ |
||
113 | if(!f.call(o,a.next(),i++,a)){
|
||
114 | return false; |
||
115 | } |
||
116 | } |
||
117 | }else{
|
||
118 | for(i in a){ |
||
119 | if(!(i in _1)){ |
||
120 | if(!f.call(o,a[i],i,a)){
|
||
121 | return false; |
||
122 | } |
||
123 | } |
||
124 | } |
||
125 | } |
||
126 | } |
||
127 | return true; |
||
128 | },some:function(a,f,o){ |
||
129 | if(typeof a=="string"){ |
||
130 | a=a.split("");
|
||
131 | } |
||
132 | o=o||d.global; |
||
133 | f=df.lambda(f); |
||
134 | var i,n;
|
||
135 | if(d.isArray(a)){
|
||
136 | for(i=0,n=a.length;i<n;++i){ |
||
137 | if(f.call(o,a[i],i,a)){
|
||
138 | return true; |
||
139 | } |
||
140 | } |
||
141 | }else{
|
||
142 | if(typeof a.hasNext=="function"&&typeof a.next=="function"){ |
||
143 | for(i=0;a.hasNext();){ |
||
144 | if(f.call(o,a.next(),i++,a)){
|
||
145 | return true; |
||
146 | } |
||
147 | } |
||
148 | }else{
|
||
149 | for(i in a){ |
||
150 | if(!(i in _1)){ |
||
151 | if(f.call(o,a[i],i,a)){
|
||
152 | return true; |
||
153 | } |
||
154 | } |
||
155 | } |
||
156 | } |
||
157 | } |
||
158 | return false; |
||
159 | }}); |
||
160 | })(); |
||
161 | } |