root / trunk / web / dojo / dojox / lang / functional / listcomp.js
History | View | Annotate | Download (937 Bytes)
1 |
/*
|
---|---|
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.listcomp"]){ |
9 |
dojo._hasResource["dojox.lang.functional.listcomp"]=true; |
10 |
dojo.provide("dojox.lang.functional.listcomp");
|
11 |
(function(){
|
12 |
var _1=/\bfor\b|\bif\b/gm; |
13 |
var _2=function(s){ |
14 |
var _3=s.split(_1),_4=s.match(_1),_5=["var r = [];"],_6=[],i=0,l=_4.length; |
15 |
while(i<l){
|
16 |
var a=_4[i],f=_3[++i];
|
17 |
if(a=="for"&&!/^\s*\(\s*(;|var)/.test(f)){ |
18 |
f=f.replace(/^\s*\(/,"(var "); |
19 |
} |
20 |
_5.push(a,f,"{");
|
21 |
_6.push("}");
|
22 |
} |
23 |
return _5.join("")+"r.push("+_3[0]+");"+_6.join("")+"return r;"; |
24 |
}; |
25 |
dojo.mixin(dojox.lang.functional,{buildListcomp:function(s){ |
26 |
return "function(){"+_2(s)+"}"; |
27 |
},compileListcomp:function(s){ |
28 |
return new Function([],_2(s)); |
29 |
},listcomp:function(s){ |
30 |
return (new Function([],_2(s)))(); |
31 |
}}); |
32 |
})(); |
33 |
} |