util.js 1.8 KB

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