object.js 2.0 KB

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