rearrange.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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.oo.rearrange"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.lang.oo.rearrange"] = true;
  8. dojo.provide("dojox.lang.oo.rearrange");
  9. (function(){
  10. var extraNames = dojo._extraNames, extraLen = extraNames.length,
  11. opts = Object.prototype.toString, empty = {};
  12. dojox.lang.oo.rearrange = function(bag, map){
  13. // summary:
  14. // Process properties in place by removing and renaming them.
  15. // description:
  16. // Properties of an object are to be renamed or removed specified
  17. // by "map" argument. Only own properties of "map" are processed.
  18. // example:
  19. // | oo.rearrange(bag, {
  20. // | abc: "def", // rename "abc" attribute to "def"
  21. // | ghi: null // remove/hide "ghi" attribute
  22. // | });
  23. // bag: Object:
  24. // the object to be processed
  25. // map: Object:
  26. // the dictionary for renaming (false value indicates removal of the named property)
  27. // returns: Object:
  28. // the original object
  29. var name, newName, prop, i, t;
  30. for(name in map){
  31. newName = map[name];
  32. if(!newName || opts.call(newName) == "[object String]"){
  33. prop = bag[name];
  34. if(!(name in empty) || empty[name] !== prop){
  35. if(!(delete bag[name])){
  36. // can't delete => hide it
  37. bag[name] = undefined;
  38. }
  39. if(newName){
  40. bag[newName] = prop;
  41. }
  42. }
  43. }
  44. }
  45. if(extraLen){
  46. for(i = 0; i < extraLen; ++i){
  47. name = extraNames[i];
  48. // repeating the body above
  49. newName = map[name];
  50. if(!newName || opts.call(newName) == "[object String]"){
  51. prop = bag[name];
  52. if(!(name in empty) || empty[name] !== prop){
  53. if(!(delete bag[name])){
  54. // can't delete => hide it
  55. bag[name] = undefined;
  56. }
  57. if(newName){
  58. bag[newName] = prop;
  59. }
  60. }
  61. }
  62. }
  63. }
  64. return bag; // Object
  65. };
  66. })();
  67. }