summaryrefslogtreecommitdiff
path: root/includes/js/dojox/lang/functional/listcomp.js
diff options
context:
space:
mode:
authormensonge <mensonge@b3834d28-1941-0410-a4f8-b48e95affb8f>2008-11-13 09:49:11 +0000
committermensonge <mensonge@b3834d28-1941-0410-a4f8-b48e95affb8f>2008-11-13 09:49:11 +0000
commite44a7e37b6c7b5961adaffc62b9042b8d442938e (patch)
tree95b67c356e93163467db2451f2b8cce84ed5d582 /includes/js/dojox/lang/functional/listcomp.js
parenta62b9742ee5e28bcec6872d88f50f25b820914f6 (diff)
downloadsemanticscuttle-e44a7e37b6c7b5961adaffc62b9042b8d442938e.tar.gz
semanticscuttle-e44a7e37b6c7b5961adaffc62b9042b8d442938e.tar.bz2
New feature: basic Ajax suggestion for tags and implementation of Dojo toolkit
git-svn-id: https://semanticscuttle.svn.sourceforge.net/svnroot/semanticscuttle/trunk@151 b3834d28-1941-0410-a4f8-b48e95affb8f
Diffstat (limited to 'includes/js/dojox/lang/functional/listcomp.js')
-rw-r--r--includes/js/dojox/lang/functional/listcomp.js54
1 files changed, 54 insertions, 0 deletions
diff --git a/includes/js/dojox/lang/functional/listcomp.js b/includes/js/dojox/lang/functional/listcomp.js
new file mode 100644
index 0000000..adb4883
--- /dev/null
+++ b/includes/js/dojox/lang/functional/listcomp.js
@@ -0,0 +1,54 @@
+if(!dojo._hasResource["dojox.lang.functional.listcomp"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
+dojo._hasResource["dojox.lang.functional.listcomp"] = true;
+dojo.provide("dojox.lang.functional.listcomp");
+
+// This module adds high-level functions and related constructs:
+// - list comprehensions similar to JavaScript 1.7
+
+// Notes:
+// - listcomp() produces functions, which after the compilation step are
+// as fast as regular JS functions (at least theoretically).
+
+(function(){
+ var g_re = /\bfor\b|\bif\b/gm;
+
+ var listcomp = function(/*String*/ s){
+ var frag = s.split(g_re), act = s.match(g_re),
+ head = ["var r = [];"], tail = [];
+ for(var i = 0; i < act.length;){
+ var a = act[i], f = frag[++i];
+ if(a == "for" && !/^\s*\(\s*(;|var)/.test(f)){
+ f = f.replace(/^\s*\(/, "(var ");
+ }
+ head.push(a, f, "{");
+ tail.push("}");
+ }
+ return head.join("") + "r.push(" + frag[0] + ");" + tail.join("") + "return r;"; // String
+ };
+
+ dojo.mixin(dojox.lang.functional, {
+ buildListcomp: function(/*String*/ s){
+ // summary: builds a function from a text snippet, which represents a valid
+ // JS 1.7 list comprehension, returns a string, which represents the function.
+ // description: This method returns a textual representation of a function
+ // built from the list comprehension text snippet (conformant to JS 1.7).
+ // It is meant to be evaled in the proper context, so local variable can be
+ // pulled from the environment.
+ return "function(){" + listcomp(s) + "}"; // String
+ },
+ compileListcomp: function(/*String*/ s){
+ // summary: builds a function from a text snippet, which represents a valid
+ // JS 1.7 list comprehension, returns a function object.
+ // description: This method returns a function built from the list
+ // comprehension text snippet (conformant to JS 1.7). It is meant to be
+ // reused several times.
+ return new Function([], listcomp(s)); // Function
+ },
+ listcomp: function(/*String*/ s){
+ // summary: executes the list comprehension building an array.
+ return (new Function([], listcomp(s)))(); // Array
+ }
+ });
+})();
+
+}