object.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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.object"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.lang.functional.object"] = true;
  8. dojo.provide("dojox.lang.functional.object");
  9. dojo.require("dojox.lang.functional.lambda");
  10. // This module adds high-level functions and related constructs:
  11. // - object/dictionary helpers
  12. // Defined methods:
  13. // - take any valid lambda argument as the functional argument
  14. // - skip all attributes that are present in the empty object
  15. // (IE and/or 3rd-party libraries).
  16. (function(){
  17. var d = dojo, df = dojox.lang.functional, empty = {};
  18. d.mixin(df, {
  19. // object helpers
  20. keys: function(/*Object*/ obj){
  21. // summary: returns an array of all keys in the object
  22. var t = [];
  23. for(var i in obj){
  24. if(!(i in empty)){
  25. t.push(i);
  26. }
  27. }
  28. return t; // Array
  29. },
  30. values: function(/*Object*/ obj){
  31. // summary: returns an array of all values in the object
  32. var t = [];
  33. for(var i in obj){
  34. if(!(i in empty)){
  35. t.push(obj[i]);
  36. }
  37. }
  38. return t; // Array
  39. },
  40. filterIn: function(/*Object*/ obj, /*Function|String|Array*/ f, /*Object?*/ o){
  41. // summary: creates new object with all attributes that pass the test
  42. // implemented by the provided function.
  43. o = o || d.global; f = df.lambda(f);
  44. var t = {}, v, i;
  45. for(i in obj){
  46. if(!(i in empty)){
  47. v = obj[i];
  48. if(f.call(o, v, i, obj)){ t[i] = v; }
  49. }
  50. }
  51. return t; // Object
  52. },
  53. forIn: function(/*Object*/ obj, /*Function|String|Array*/ f, /*Object?*/ o){
  54. // summary: iterates over all object attributes.
  55. o = o || d.global; f = df.lambda(f);
  56. for(var i in obj){
  57. if(!(i in empty)){
  58. f.call(o, obj[i], i, obj);
  59. }
  60. }
  61. return o; // Object
  62. },
  63. mapIn: function(/*Object*/ obj, /*Function|String|Array*/ f, /*Object?*/ o){
  64. // summary: creates new object with the results of calling
  65. // a provided function on every attribute in this object.
  66. o = o || d.global; f = df.lambda(f);
  67. var t = {}, i;
  68. for(i in obj){
  69. if(!(i in empty)){
  70. t[i] = f.call(o, obj[i], i, obj);
  71. }
  72. }
  73. return t; // Object
  74. }
  75. });
  76. })();
  77. }