util.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // wrapped by build app
  2. define("dojox/lang/functional/util", ["dijit","dojo","dojox","dojo/require!dojox/lang/functional/lambda"], function(dijit,dojo,dojox){
  3. dojo.provide("dojox.lang.functional.util");
  4. dojo.require("dojox.lang.functional.lambda");
  5. // This module provides helpers:
  6. // - inlining string lambda functions.
  7. (function(){
  8. var df = dojox.lang.functional;
  9. dojo.mixin(df, {
  10. inlineLambda: function(/*String*/ lambda, /*String|Array*/ init, /*Function?*/ add2dict){
  11. // summary:
  12. // Creates the inlined version of a string lambda.
  13. // lambda:
  14. // The String variable representing the lambda function.
  15. // init:
  16. // Conveys how to initialize parameters. If it is a String, then the apply() method
  17. // would be emulated treating "init" as a list of input parameters.
  18. // It it is an Array, then the call() method is emulated treating array members
  19. // as input parameters.
  20. // add2dict:
  21. // The optional function, which is used to record names of lambda parameters.
  22. // If supplied, this function is called with a name of every parameter.
  23. var s = df.rawLambda(lambda);
  24. if(add2dict){
  25. df.forEach(s.args, add2dict);
  26. }
  27. var ap = typeof init == "string", // apply or call?
  28. n = ap ? s.args.length : Math.min(s.args.length, init.length),
  29. a = new Array(4 * n + 4), i, j = 1;
  30. for(i = 0; i < n; ++i){
  31. a[j++] = s.args[i];
  32. a[j++] = "=";
  33. a[j++] = ap ? init + "[" + i + "]": init[i];
  34. a[j++] = ",";
  35. }
  36. a[0] = "(";
  37. a[j++] = "(";
  38. a[j++] = s.body;
  39. a[j] = "))";
  40. return a.join(""); // String
  41. }
  42. });
  43. })();
  44. });